1.2305051745.1 • Published 12 months ago

@bentoproject/selector v1.2305051745.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
12 months ago

Bento Selector

The Bento Selector is a control that presents a list of options and lets the user choose one or many options; the contents of the options aren't just limited to text.

Usage

Web Component

You must include each Bento component's required CSS library before adding custom styles in order to guarantee proper loading. Or use the lightweight pre-uprgrade styles available inline. See Layout and Style

Example: Import via npm

Install via npm:

npm install @bentoproject/selector
import {defineElement as defineBentoSelector} from '@bentoproject/selector';
defineBentoSelector();

Example: Include via <script>

<head>
  <script src="https://cdn.ampproject.org/bento.js"></script>
  <!-- These styles prevent Cumulative Layout Shift on the unupgraded custom element -->
  <style>
    bento-selector {
      display: block;
    }
  </style>
  <script
    async
    src="https://cdn.ampproject.org/v0/bento-selector-1.0.js"
  ></script>
</head>

<bento-selector class="sample-selector">
  <ul>
    <li option="1">Option 1</li>
    <li option="2">Option 2</li>
    <li option="3">Option 3</li>
    <li option="4">Option 4</li>
  </ul>
</bento-selector>

Usage notes

  • A bento-selector can contain any arbitrary HTML elements or AMP components (e.g., bento-carousel, etc.).
  • A bento-selector cannot contain any nested bento-selector controls.
  • Selectable options can be set by adding the option attribute to the element and assigning a value to the attribute (e.g., <li option="value"></li>).
  • Disabled options can be set by adding the disabled attribute to the element (e.g., <li option="d" disabled></li>).
  • Preselected options can be set by adding the selected attribute to the element (e.g., <li option="b" selected></li>).
  • To allow for multiple selections, add the multiple attribute to the bento-selector element. By default, the bento-selector allows for one selection at a time.
  • To disable the entire bento-selector, add the disabled attribute to the bento-selector element.
  • When an bento-selector contains a name attribute and the bento-selector is inside a form tag, if a submit event occurs on the form, the bento-selectorbehaves like a radio-button/checkbox group and submits the selected values (the ones assigned to the option) against the name of the bento-selector.

Layout and Style

Each Bento component has a small CSS library you must include to guarantee proper loading without content shifts. Because of order-based specificity, you must manually ensure that stylesheets are included before any custom styles.

You may also make the light-weight pre-upgrade styles available inline:

<style>
  bento-selector {
    display: block;
  }
</style>

Attributes on <bento-selector>

disabled, form, multiple, name

The attributes above behave the same way as they do on a standard HTML <select> element.

keyboard-select-mode

The keyboard-select-mode attribute dictates the keyboard navigation behavior for options inside <bento-selector>.

Attributes on option elements

option (required)

Indicates that the option is selectable. If a value is specified, the contents of the value is submitted with the form.

disabled, selected

The attributes above behave the same way as they do on a standard HTML <option> element.

Actions

The bento-selector API allows you to perform the following actions:

selectBy(delta: number) Closes the selector.

api.selectBy(1); // Select next option in DOM sequence.
api.selectBy(-2); // Select the option that is two previous in DOM sequence.

toggle(optionValue: string, opt_select: boolean|undefined) Toggles the option with the given optionValue to be selected or deselected based on opt_select. If opt_select is not present, then the option will be selected if currently not selected, and deselected if currently selected.

api.toggle('a'); // Toggle the item with the attribute `option="a"`.
api.toggle('1', true); // Select the item with the attribute `option="1"`.

Events

The bento-selector API allows you to register and respond to the following events:

select

This event is triggered when the user selects an option. Multi-selectors and single-selectors fire this when selecting or unselecting options. Tapping disabled options does not trigger the select event.

selector.addEventListener('select', (e) => console.log(e.data.targetOption));

Preact/React Component

The examples below demonstrate use of the <BentoSelector> as a functional component usable with the Preact or React libraries.

Example: Import via npm

Install via npm:

npm install @bentoproject/selector
import React from 'react';
import {
  BentoSelector,
  BentoSelectorOption,
} from '@bentoproject/selector/react';
import '@bentoproject/selector/styles.css';

function App() {
  return (
    <BentoSelector>
      <BentoSelectorOption option="1">Option 1</BentoSelectorOption>
      <BentoSelectorOption option="2">Option 2</BentoSelectorOption>
      <BentoSelectorOption option="3">Option 3</BentoSelectorOption>
      <BentoSelectorOption option="4">Option 4</BentoSelectorOption>
    </BentoSelector>
  );
}

Layout and Style

Container type

The BentoSelector component can be styled with standard CSS.

The width and height of the BentoSelector may both be set in order to adjust the default size of the component. To ensure the component renders how you want it to, be sure to apply a size to the component. These can be applied inline:

<BentoSelector style={{width: 100, height: 400}}>
  <BentoSelectorOption option="1">Option 1</BentoSelectorOption>
</BentoSelector>

Or via className:

<BentoSelector className="custom-styles">
  <BentoSelectorOption option="1">Option 1</BentoSelectorOption>
</BentoSelector>
.custom-styles {
  width: 100px;
  height: 400px;
}

Props for <BentoSelector>

disabled, form, multiple, name

The attributes above behave the same way as they do on a standard HTML <select> element.

keyboardSelectMode

The keyboardSelectMode attribute dictates the keyboard navigation behavior for options inside <BentoSelector>.

Props for <BentoSelectorOption>

option

Indicates that the option is selectable. If a value is specified, the contents of the value is submitted with the form.

disabled, selected

The attributes above behave the same way as they do on a standard HTML <option> element.

Interactivity and API usage

Bento components are highly interactive through their API. The BentoSelector component API is accessible by passing a ref:

import React, {createRef} from 'react';
const ref = createRef();

function App() {
  return (
    <BentoSelector ref={ref}>
      <BentoSelectorOption option="1">Option 1</BentoSelectorOption>
      <BentoSelectorOption option="2">Option 2</BentoSelectorOption>
      <BentoSelectorOption option="3">Option 3</BentoSelectorOption>
      <BentoSelectorOption option="4">Option 4</BentoSelectorOption>
    </BentoSelector>
  );
}

Actions

The BentoSelector API allows you to perform the following actions:

selectBy(delta: number) Closes the selector.

ref.current.selectBy(1); // Select next option in DOM sequence.
ref.current.selectBy(-2); // Select the option that is two previous in DOM sequence.

toggle(optionValue: string, opt_select: boolean|undefined) Toggles the option with the given optionValue to be selected or deselected based on opt_select. If opt_select is not present, then the option will be selected if currently not selected, and deselected if currently selected.

ref.current.toggle('1'); // Toggle the item with the attribute `option="1"`.
ref.current.toggle('2', true); // Select the item with the attribute `option="2"`.

Events

BentoSelector API allows you to register and respond to the following events:

onChange

This event is triggered when a selector option is selected or deselected. The onChange prop gives you two key options:

  • option which returns the value of the option prop of the BentoSelectorOption which was selected or deselected.
  • value which returns an array of which BentoSelectorOptions are currently selected in the order they were selected.
<BentoSelector
  as="ul"
  multiple
  onChange={({option, value}) => console.log(option, value)}
>
  <BentoSelectorOption as="li" option="1">
    Option 1
  </BentoSelectorOption>
  <BentoSelectorOption as="li" option="2">
    Option 2
  </BentoSelectorOption>
</BentoSelector>
1.2305051745.1

12 months ago

1.2305051745.0

1 year ago

1.2305022024.0

1 year ago

1.2305232041.0

12 months ago

1.2305152039.0

12 months ago

1.2305241828.0

12 months ago

1.2305182038.0

12 months ago

1.2305221508.0

12 months ago

1.2304212144.0

1 year ago

1.2304262219.0

1 year ago

1.2304122229.0

1 year ago

1.2304241924.0

1 year ago

1.2304132133.0

1 year ago

1.2302171719.0

1 year ago

1.2303231800.0

1 year ago

1.2303151621.0

1 year ago

1.2304062309.0

1 year ago

1.2302271541.0

1 year ago

1.2303151529.0

1 year ago

1.2304040531.0

1 year ago

1.2302031721.0

1 year ago

1.2301301810.0

1 year ago

1.2301041800.0

1 year ago

1.2301112346.0

1 year ago

1.2302021743.0

1 year ago

1.2301031703.0

1 year ago

1.2301181928.0

1 year ago

1.2301032153.0

1 year ago

1.2301261900.0

1 year ago

1.2301100345.0

1 year ago

1.2302012254.0

1 year ago

1.2212151632.2

1 year ago

1.2211042305.0

2 years ago

1.2211111611.0

1 year ago

1.2211060024.0

2 years ago

1.2210191347.0

2 years ago

1.2211302304.0

1 year ago

1.2211302304.2

1 year ago

1.2211302304.1

1 year ago

1.2211151402.0

1 year ago

1.2210251605.0

2 years ago

1.2210272257.0

2 years ago

1.2211182146.0

1 year ago

1.2211250451.0

1 year ago

1.2210172057.0

2 years ago

1.2212092023.0

1 year ago

1.2212151632.1

1 year ago

1.2210211855.0

2 years ago

1.2212151632.0

1 year ago

1.2212071834.0

1 year ago

1.2210142102.0

2 years ago

1.2210041838.0

2 years ago

1.2210112333.0

2 years ago

1.2210052034.0

2 years ago

1.2210010655.0

2 years ago

1.2210071758.0

2 years ago

1.2208242209.0

2 years ago

1.2208242209.1

2 years ago

1.2209142312.0

2 years ago

1.2209072154.0

2 years ago

1.2209131909.0

2 years ago

1.2208172101.0

2 years ago

1.2207201724.0

2 years ago

1.2205270638.4

2 years ago

1.2206071918.0

2 years ago

1.2206071918.1

2 years ago

1.2205120110.1

2 years ago

1.2205270638.0

2 years ago

1.2206131450.0

2 years ago

1.2207071723.0

2 years ago

1.2208121708.0

2 years ago

1.2208021808.0

2 years ago

1.2206291614.0

2 years ago

1.2207181727.0

2 years ago

1.2208042017.0

2 years ago

1.2205232225.0

2 years ago

1.2207261725.0

2 years ago

1.2208112015.0

2 years ago

1.2206082301.0

2 years ago

1.2208081650.0

2 years ago

1.2208092047.0

2 years ago

1.2206221455.0

2 years ago

1.2207221643.0

2 years ago

1.2208051912.0

2 years ago

1.2208051912.1

2 years ago

1.2206072228.0

2 years ago

1.2207281718.2

2 years ago

1.2207281718.1

2 years ago

1.2207281718.0

2 years ago

1.2206101637.0

2 years ago

1.2205251942.0

2 years ago

1.2206162023.0

2 years ago

1.2204281949.0

2 years ago

1.2205120110.0

2 years ago

1.2205051832.0

2 years ago

1.2205101904.0

2 years ago

1.2205041533.0

2 years ago

1.2205181800.0

2 years ago

1.2204252045.0

2 years ago

1.2204292129.0

2 years ago

1.2204272205.0

2 years ago

1.2205090810.0

2 years ago

1.2205161914.0

2 years ago

1.2205191749.0

2 years ago

1.2205031420.0

2 years ago

1.2203101844.0

2 years ago

1.2204182206.0

2 years ago

1.2203151539.0

2 years ago

1.2203281422.0

2 years ago

1.2204122000.0

2 years ago

1.2203221937.0

2 years ago

1.2204221712.0

2 years ago

1.2203231914.0

2 years ago

1.2204160405.0

2 years ago

1.2203172113.0

2 years ago

1.2204132005.0

2 years ago

1.2204142048.0

2 years ago

1.2204211736.0

2 years ago

1.2203041950.0

2 years ago

1.2204121632.0

2 years ago

1.2204191429.0

2 years ago

1.2202230359.1

2 years ago

1.2202241911.0

2 years ago

1.2202252101.0

2 years ago

1.2201101902.0

2 years ago

1.2201042109.0

2 years ago

1.2112231523.2

2 years ago

1.2112231523.1

2 years ago

1.2112231523.0

2 years ago

1.2202162256.0

2 years ago

1.2202012258.0

2 years ago

1.2202230359.0

2 years ago

1.2111242025.1

2 years ago

1.2112062322.2

2 years ago

1.2112091956.0

2 years ago

1.2112062322.1

2 years ago

1.2112062322.0

2 years ago

1.2202042210.0

2 years ago

1.2202042210.1

2 years ago

1.2112201724.0

2 years ago

1.2112162055.0

2 years ago

1.2112221739.0

2 years ago

1.2201061922.0

2 years ago

1.2112132331.0

2 years ago

1.2201122239.0

2 years ago

1.2202142035.0

2 years ago

1.2202152253.0

2 years ago

1.2202112230.0

2 years ago

1.2202082004.0

2 years ago

1.2201120015.0

2 years ago

1.2112082232.0

2 years ago

1.2201212122.3

2 years ago

1.2202022130.0

2 years ago

1.2202031816.0

2 years ago

1.2112212349.0

2 years ago

1.2201052144.0

2 years ago

1.2202180255.0

2 years ago

1.2112080230.1

2 years ago

1.2201262038.1

2 years ago

1.2112080230.0

2 years ago

1.2201141909.0

2 years ago

1.2201141909.1

2 years ago

1.2201141909.3

2 years ago

1.2201141909.4

2 years ago

1.2112160003.0

2 years ago

1.2201071715.0

2 years ago

1.2202072236.0

2 years ago

1.2112170012.0

2 years ago

1.2202092050.0

2 years ago

1.2201131702.0

2 years ago

1.2112102136.0

2 years ago

1.2202232232.0

2 years ago

1.2111191951.0

2 years ago

1.2111292120.0

2 years ago

1.2111180040.0

2 years ago

1.2111230636.0

2 years ago

1.2111230244.0

2 years ago

1.2111152338.2

2 years ago

1.2112010058.0

2 years ago

1.2111060251.3

2 years ago

1.2111242025.0

2 years ago

1.2111182332.0

2 years ago

1.2112032204.0

2 years ago

1.2112021656.0

2 years ago

1.2112012300.0

2 years ago

1.2111060251.9

2 years ago

1.2111022043.0

3 years ago

1.2110212130.2

3 years ago

1.2110152252.3

3 years ago

1.2111011823.0

3 years ago

1.2110290545.0

3 years ago

1.2110281959.0

3 years ago

1.2110252343.0

3 years ago

1.2110261518.0

3 years ago