This content originally appeared on CodyHouse and was authored by CodyHouse
Download + Demo
How To
The Advanced Custom Select component is used to override the native select appearance and replace it with a custom control and a custom dropdown.
Dropdown structure #
The dropdown structure can be customized by modifying the content of the .js-adv-select-popover element.
<select> with <optgroup> elements #
The basic dropdown structure (.js-adv-select-popover content) is:
<div class="adv-select-popover__optgroup" role="group" describedby="optgroup-label">
<div class="adv-select-popover__label" id="optgroup-label">{optgroup-label}</div>
<div class="adv-select-popover__option" role="option">{option-label}</div>
</div>
where:
.adv-select-popover__labelis the label of the<optgroup> element;{optgroup-label}will be replaced by the<optgroup> labelattribute;.adv-select-popover__optionis the option element;{option-label}will be replaced by the<option>label.
The template used for the option elements can be customized based on your project needs. For example, if you want to include an image next to the option text, modify the template as follows:
<div class="adv-select-popover__optgroup" role="group" describedby="optgroup-label">
<div class="adv-select-popover__label" id="optgroup-label">{optgroup-label}</div>
<div class="adv-select-popover__option" role="option">
<img src="{img-src}" alt="{img-alt}">
<span>{option-label}</span>
</div>
</div>
To each <option> in the native <select> element, make sure to add a data-img-src and data-img-alt to pass the image src and alt attribute respectively:
<select>
<optgroup label="Group 1">
<option value="1" data-img-src="image-1.png" data-img-alt="Image 1 Description">Option 1</option>
<option value="2" data-img-src="image-2.png" data-img-alt="Image 2 Description">Option 2</option>
<option value="2" data-img-src="image-3.png" data-img-alt="Image 3 Description">Option 3</option>
</optgroup>
</select>
<select> with <option> elements only #
The basic dropdown structure (.js-adv-select-popover content) is:
<div class="adv-select-popover__option" role="option">{option-label}</div>
You can customize this template (see Select with <optgroup> elements section).
Control Button #
The .js-adv-select__control button is the dropdown control element. Its aria-controls attribute needs to be equal to the id of .js-adv-select-popover element.
When an option in the dropdown is selected, the content of its child .js-adv-select__value element is updated with the selected option content.
This content originally appeared on CodyHouse and was authored by CodyHouse
CodyHouse | Sciencx (2021-04-02T08:16:08+00:00) Component – Advanced Custom Select. Retrieved from https://www.scien.cx/2021/04/02/component-advanced-custom-select/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.