Better Select Demo

For more information, please visit the GitHub repository.

See the "vanilla" (non-Bootstrap-styled) demo.

See the Bootstrap-styled demo.

Better Select

Better Select is a minimal custom select, with the option to fallback to the native select on mobile devices. It offers:

The plugin doesn't require jQuery, but it also offers a jQuery adapter if needed.

Demo

See the demo page for a live demo.

Instalation

Installing through npm

If you use npm/yarn, first install the better-select module:

npm install --save @smartimpact-it/better-select

or

yarn add @smartimpact-it/better-select

Usage as web component

The simplest method is to use the web component.

<better-select>
  <select name="select" id="select1" class="form-control">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
    <option value="5">Option 5</option>
  </select>
</better-select>

In JS, you need to register the web component:

import { registerWebComponent } from '@smartimpact-it/better-select';
registerWebComponent();

or simple use the "autoRegister":

import '@smartimpact-it/better-select/autoRegisterWebComponent';

You can also include the triple-slash directive for the typescript definitions, if you use typescript.

/// <reference types="@smartimpact-it/better-select" />
import '@smartimpact-it/better-select/autoRegisterWebComponent';

Attributes for the better-select web component

The available options are:

OptionDescriptionDefault value
no-skip-emptydon't skip options with empty valuefalse
placeholdertext to display when no option is selectednull
fixed-placeholderif active, the placeholder will always be displayed, even when options are selectedfalse
no-native-on-mobiledisplay the original select "dropdown" when on mobilefalse
mobile-breakpointwindow width (px) under which to be considered "mobile"1024
wrapper-classthe class added to the wrapper element'better-select'
trigger-classthe class added to the trigger element'better-select__trigger'
dropdown-classthe class added to the dropdown element'better-select__dropdown'
z-indexthe z-index to be set on the custom select wrapperdecrementing from 100
scroll-selection-into-viewif true, the dropdown will scroll to the selected option when openedtrue

Example:

<better-select wrapper-class="better-select2" z-index="30" no-native-on-mobile>
  <select name="select" id="select1" class="form-control">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
    <option value="5">Option 5</option>
  </select>
</better-select>

Usage without the web component

import BetterSelect from '@smartimpact-it/better-select';

// Using vanilla JS
new BetterSelect(element);

// or pass some options
new BetterSelect(element, {...});

// or using jQuery
import { registerForJquery } from '@smartimpact-it/better-select';
registerForjQuery();
$('select.my-select').betterSelect({...});

// or use the autoRegister
import '@smartimpact-it/better-select/autoRegisterForJquery';
$('select.my-select').betterSelect({...});

Settings for the BetterSelect class and for the jQuery method

The available options for the BetterSelect class are:

OptionDescriptionDefault value
skipEmptydon't display options with empty valuetrue
placeholdertext to display when no option is selectednull
fixedPlaceholderif true, the placeholder will always be displayed, even when options are selectedfalse
nativeOnMobiledisplay the original select "dropdown" when on mobiletrue
mobileBreakpointwindow width (px) under which to be considered "mobile"1024
wrapperClassthe class added to the wrapper element'better-select'
triggerClassthe class added to the trigger element'better-select__trigger'
dropdownClassthe class added to the dropdown element'better-select__dropdown'
zIndexthe z-index to be set on the custom select wrapperdecrementing from 100
scrollSelectionIntoViewif true, the dropdown will scroll to the selected option when openedtrue

Events

There are multiple events dispatched by the better select elements:

Methods on the BetterSelect class

The BetterSelect class exposes the following methods and getters/setters:

Methods on the BetterSelect Web Component

The BetterSelect web component exposes the following methods and getters/setters:

Styling

The package offers an integration with Bootstrap 5, but it can be used with any other CSS framework or custom styles.

Alternatively, we offer a minimal styling for the custom select, which can be imported like this:

/* stylelint-disable-next-line scss/at-import-partial-extension */
@import '@smartimpact-it/better-select/src/scss/main.scss';

Integration with Bootstrap

To setup the styling of the custom select, the package provides an "adapter" for Bootstrap 5, which uses the styles from the default select (for the closed state) and the dropdown (for the open state). This provides a good integration with the Bootstrap variables, so that you don't need to style everything again.

To use this:

// Initialize bootstrap and its variables
@import 'bootstrap/scss/bootstrap';

// Initialize the styles for better-select, based on bootstrap.
/* stylelint-disable-next-line scss/at-import-partial-extension */
@import '@smartimpact-it/better-select/src/scss/bootstrap.scss';

Acknowledgements

The library is a continuation of the Better Select library started by Bogdan Barbu.