How to Implement a Toggle Switch Inside a Multi-Select Option?
When building user interfaces, add toggle switches inside multi-select options that show whether the option is selected or not. can enhance the user experience without using actual <input type=”checkbox”> behind the toggle switches instead we are using <DIV>. This guide explains how to implement a toggle switch that activates based on the option selected. It uses HTML, CSS, or SCSS.
Key Features
- Custom Toggle Switch: Designed using SCSS that allows smooth animations and transitions.
- Scalable Solution: This can be easily implemented into designs or dropdowns.
Challenge – Dynamic toggling inside options.
The biggest issue was creating a toggle switch that works interactively within a multi-select box. The switch required to change position and color based on whether the option was selected or not.
Solution – CSS transitions
We used a combination of HTML, and SCSS.
HTML: Organize the toggle switch within each multiple-select option.
SCSS: Style the toggle switch for the best look and feel and for better transition.
HTML Implementation
Here’s the HTML structure for the toggle switch
1 2 3 4 5 | <div class="multiselect-option"> <div class="option-switch"> <div class="switch-handle"></div> </div> </div> |
SCSS Styling
The SCSS code defines the toggle switch’s appearance and transitions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | .multiselect-option { .option-switch { width: 36px; height: 20px; background-color: #d9dbdf; border-radius: 12px; position: relative; transition: background-color 0.2s ease-in-out; .switch-handle { width: 16px; height: 16px; background-color: white; border-radius: 50%; position: absolute; left: 2px; top: 2px; transition: transform 0.2s ease-in-out; } } //This is a toggle class, Assume it has already been added to the option. &.is-selected { .option-switch { background-color: #1da8f7; .switch-handle { transform: translateX(16px); } } } } |
Key Functionality
- Class Toggling: The .is-selected class dynamically toggles based on user clicks options. (for the toggle class we can additionally write a script if needed)
- Smooth Transitions: SCSS transitions ensure seamless changes in appearance.
By integrating a toggle switch inside a multi-select option, you can enhance the user experience of your application. This solution is lightweight, scalable, and can be easily customized to suit your design needs.
For further assistance or custom implementations, feel free to reach out!
Recent help desk articles
Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.