Transforms a native select
element to a markup structure that allows styling using CSS (as opposed to the native select
and option
elements)
http://www.liran.co.uk/selectise
- Ability to style the
select
element freely using CSS - CSS theme out of the box (using _selectise-theme.scss)
- Keyboard shortcuts:
- Enter to open dropdown, when the trigger is focused
- Arrow up/down to navigate the dropdown's option list, when it is opened
- Esc to close the dropdown, when it is opened
- Copies all attributes from native
select
andoption
elements to corresponding elements in the Selectise markupselect
element:- Copies all attributes
- If
tabindex
exists, it will also be copied to the trigger element and to each option element
option
elements:- Copies all attributes
- If
value
attribute exists, it will be copied asdata-value
- If
selected
attribute exists, this option will be selected, and the attribute will be removed from the custom option element
- Supports screen readers thanks to these factors:
- Copying the tabindex from the
select
element - Supporting keyboard shortcuts
- Focusing the current option when using the keyboard to navigate the options list
- Focusing the trigger once a selection has been made (this both confirms the selection to the accessiblilty user, and allows for an easy re-opening of the options dropdown)
- Copying the tabindex from the
npm install selectise
import Selectise from 'selectise'
const selectise = new Selectise(nativeSelect, options)
Requires _selectise-base.scss
to work properly. You should also use _selectise-theme.scss
if you're not setting your own styles.
@import '~selectise/src/selectise-base';
@import '~selectise/src/selectise-theme';
<head>
<!-- ... -->
<link rel="stylesheet" href="selectise/style.css">
<script src="selectise/index.js"></script>
</head>
<body>
<!-- ... -->
<script>
var selectise = new Selectise.default(nativeSelect, options);
</script>
</body>
Selectise takes two arguments:
This can be either:
- A refrence to the native select element
- A string representing a selector for the native select element (e.g.
'#main .my-select-element'
), in which case Selectise will select and use the first matching element
If you'd like to configure Selectise, you can pass an options object as the second parameter.
const options = {
onSelect, // [Callback function] Will be called when an option has been selected. When called, an Object with the following properties will be passed: `selectionContent`, `selectionValue`, `selectionIndex`.
shouldCloseOnClickBody, // [Boolean, default: false] Whether or not to close the dropdown on click body.
shouldSetOptionContentToTitle // [Boolean, default: false] Whether or not to set the content of each option to its title. This is useful when some of the options are expected to exceed the width of the select dropdown.
shouldReplaceSelectBeAsync // [Boolean, default: false] Whether or to use setTimeout for replacing the native select with the custom select (Selectise). This can help in fixing the issue of Selectise getting focused on insertion to the DOM, which happens when using tabindex.
}
Is dropdown menu open - returns true
/false
Closes the dropdown menu
Opens the dropdown menu
Toggles the dropdown menu
Returns the content of the currently selected option
Returns the value of the currently selected option
Returns the index of the currently selected option
Selects an option based on its index
Removes all event listeners of the Selectise instance
For the following native select element
<select id="example-select" tabindex="1">
<option value="value1" data-some-attr="some-attr-value" class="some-class">Option 1</option>
<option value="value2" title="Some title">Option 2</option>
<option value="value3">Option 3</option>
</select>
The following markup will be generated:
<div class="selectise" id="example-select" tabindex="1">
<div class="selectise-trigger" tabindex="1">Option 1</div>
<div class="selectise-options">
<div data-value="value1" data-some-attr="some-attr-value" class="some-class selectise-option" tabindex="1">Option 1</div>
<div data-value="value2" title="Some title" class="selectise-option" tabindex="1">Option 2</div>
<div data-value="value3" class="selectise-option" tabindex="1">Option 3</div>
</div>
</div>
Added to the top level custom select element, which contains the trigger and options elements
Added to the trigger element, which holds the selected value and toggles the dropdown
Added to the element containing the options
Added to each custom option element
Will be added to the Selectise element when the dropdown is open (and removed when it is closed)
None
Feel free to submit issues and pull requests
- Run the following, which will serve the Selctise example on localhost:8081 and watch for changes.
npm start
- Navigate to http://localhost:8081/example/ to view the output
- Test the library in
src/example
:- index.html
- index.js
- index.scss
- Edit the library itself in
src
:- index.js
- _selectise-base.scss
- _selectise-theme.scss
This project is licensed under the MIT License - see the LICENSE file for details