A simple and light-weight custom select generator, built using Vanilla JS and no dependencies
npm install selectiseTransforms 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 and option elements to corresponding elements in the Selectise markup
- select 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 as data-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)
```
npm install selectise
js
import Selectise from 'selectise'const selectise = new Selectise(nativeSelect, options)
`$3
Requires
_selectise-base.scss to work properly. You should also use _selectise-theme.scss if you're not setting your own styles.
`scss
@import '~selectise/src/selectise-base';
@import '~selectise/src/selectise-theme';
`Usage - ES5 and CSS
`html
`Parameters
Selectise takes two arguments:
$3
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$3
If you'd like to configure Selectise, you can pass an options object as the second parameter.
`js
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.
}
`Public methods
$3
Is dropdown menu open - returns true/false$3
Closes the dropdown menu$3
Opens the dropdown menu$3
Toggles the dropdown menu$3
Returns the content of the currently selected option$3
Returns the value of the currently selected option$3
Returns the index of the currently selected option$3
Selects an option based on its index$3
Removes all event listeners of the Selectise instanceGenerated markup
For the following native select element
`html
`The following markup will be generated:
`html
Option 1
`CSS classes used
$3
Added to the top level custom select element, which contains the trigger and options elements$3
Added to the trigger element, which holds the selected value and toggles the dropdown$3
Added to the element containing the options$3
Added to each custom option element$3
Will be added to the Selectise element when the dropdown is open (and removed when it is closed)
Dependencies
None
Contributing
Feel free to submit issues and pull requests
Development
* 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`:This project is licensed under the MIT License - see the LICENSE file for details