b12-archive / bare-select

The lean alternative to <select>.
https://studio-b12.github.io/bare-select
MIT License
7 stars 0 forks source link

Coveralls – test coverage
Travis – build status
David – status of dependencies
Code style: airbnb

<bare-select>

The lean alternative to <select>.

Using a bundler?

If you’re not using webpack or the like, skip to the next section.

$ npm install bare-select

You’ll most likely be fine with the default settings. Just execute this to register the <bare-select>:

require('bare-select')();

Using <script> tags?

Download the latest version from https://registry.npmjs.org/bare-select/-/bare-select-0.1.0.tgz. Unpack the file package/dist/bare-select.drop-in.min.js. Then add this to your <head>:

<script src="https://github.com/b12-archive/bare-select/raw/master/path/to/bare-select.drop-in.min.js"></script>

You’re all set!

Prepare your markup

<bare-select>             <!-- • The custom element.                         -->
  <label></label>         <!-- • A `<label>` for the switch. A click on this -->
                          <!--   element will unfold the dropdown.           -->
                          <!-- • The caption of the select box. It’s the     -->
                          <!--   same element as the label unless you        -->
                          <!--   configure it otherwise.                     -->
  <input type=checkbox>   <!-- • The switch. Controls the visibility of the  -->
                          <!--   dropdown.                                   -->
  <ul>                    <!-- • The dropdown. Folds and unfolds.            -->
    <li>                  <!-- • An option. You can have zero or more.       -->
      <input type=radio>  <!-- • The option’s radio. Its `value` is the      -->
                          <!--   value of the option. When it’s checked, the -->
                          <!--   option is selected.                         -->
      <label></label>     <!-- • The content of the option. What the user    -->
    </li>                 <!--   sees.                                       -->
  </ul>
</bare-select>

Looks complicated? How about an example:

<bare-select unfolded>
  <label>Pick a number</label>
  <input type="checkbox" />
  <ul>
    <li><input type="radio" value="1" /><label>
      One
    </label></li>
    <li><input type="radio" value="2" /><label>
      Two
    </label></li>
    <li><input type="radio" value="3" /><label>
      Three
    </label></li>
  </ul>
</bare-select>

That’s it!

To get a feeling of how it works, try taking the unfolded attribute off your <bare-select>. Try setting value="2" on it.

Configuring

 

bareSelect([{[view], [model], [plugins], [logger]}])
  → HTMLBareSelectElement

Register the element.

This function should only be called once.

Importing: var bareSelect = require('bare-select')

Parameters:

Return value:

 

model()
  → customElementModel

A model based on a custom element.

The state of the model is stored as attributes on a custom element. Changing an attribute will update the model, and patching the model’s state will update the attribute.

Importing: var model = require('bare-select/model')

Parameters:

None.

Return value:

 

view([{[selectors]}])
  → pureView

A pure HTML+CSS view.

Have a look at <../Readme.md> to see an example of the markup.

Importing: var view = require('bare-select/view')

Parameters:

Return value:

 

keyboardNavigation()
  → keyboardNavigationPlugin

Great keyboard navigation.

Importing: var keyboardNavigation = require('bare-select/plugins/keyboardNavigation')

Parameters:

None.

Return value:

 

mouseNavigation()
  → mouseNavigationPlugin

Great mouse navigation.

Importing: var mouseNavigation = require('bare-select/plugins/mouseNavigation')

Parameters:

None.

Return value:

 

unfolded()
  → unfoldedPlugin

Adds support for the attribute unfolded. Adding the attribute to the <bare-select> will unfold the select – and removing the attribute will fold it.

Importing: var unfolded = require('bare-select/plugins/unfolded')

Parameters:

None.

Return value:

 

updateCaption()
  → updateCaptionPlugin

Updates content displayed in the caption to match the selected option.

Importing: var updateCaption = require('bare-select/plugins/updateCaption')

Parameters:

None.

Return value:

 

value()
  → valuePlugin

Adds support for the attribute value. Changing the selection will update the attribute value within the <bare-select>. Changing the attribute will update the selection.

Importing: var value = require('bare-select/plugins/value')

Parameters:

None.

Return value:

Hacking

Our event-driven design means that every feature is a plugin. Plugins are the glue between the view and model.

You can easily add, remove, fork and modify plugins to suit them to your needs. You can even replace the view and the model with your own custom implementations.

Work in progress…

License

MIT © Studio B12 GmbH