amp-selector
Introduction
amp-selector lets the user choose from a list of options. The contents of the options isn't limited to just text.
Setup
Import the amp-selector component.
<script async custom-element="amp-selector" src="https://cdn.ampproject.org/v0/amp-selector-0.1.js"></script>
This sample also demonstrates how to combine amp-selector with forms ...
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
... and amp-bind.
<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
Single select
amp-selector defaults to single selection. The option is required for all selectable elements, for example, the divider in the following sample cannot be selected.
<amp-selector class="sample-selector" layout="container">
<amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60" option="1"></amp-img>
<amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60" option="2"></amp-img>
<div class="divider inline-block mx1"></div>
<amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60" option="3"></amp-img>
<amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60" option="4"></amp-img>
</amp-selector>
Multi select
Add the attribute multiple to enable multi selection.
<amp-selector layout="container" class="sample-selector" multiple>
<amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60" option="1"></amp-img>
<amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60" option="2"></amp-img>
<amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60" option="3"></amp-img>
<amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60" option="4"></amp-img>
</amp-selector>
Disable and pre-select attributes
The following attributes are supported for select elements:
disabled: disable an element.selected: pre-select an element.
<amp-selector layout="container" class="sample-selector" multiple>
<amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60" option="1"></amp-img>
<amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60" option="2" selected></amp-img>
<amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60" option="3"></amp-img>
<amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60" option="4" disabled></amp-img>
</amp-selector>
Disabled amp-selector
Add the attribute disabled to disable the whole selector.
<amp-selector layout="container" class="sample-selector" disabled>
<amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60"></amp-img>
<amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60"></amp-img>
<amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60"></amp-img>
<amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60"></amp-img>
</amp-selector>
Keyboard select modes
Add the attribute keyboard-select-mode to opt into a pre-defined user interaction pattern for amp-selector. By default the value is 'NONE' and displays the following behavior.
<amp-selector layout="container" keyboard-select-mode="none" class="sample-selector" disabled>
<amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60"></amp-img>
<amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60"></amp-img>
<amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60"></amp-img>
<amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60"></amp-img>
</amp-selector>
Keyboard select mode: Focus
Add the attribute keyboard-select-mode="focus" so the Tab key gives focus to amp-selector. The user navigates between items with the arrow keys. Must press space or enter to change the selection.
<amp-selector layout="container" keyboard-select-mode="focus" class="sample-selector">
<amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60"></amp-img>
<amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60"></amp-img>
<amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60"></amp-img>
<amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60"></amp-img>
</amp-selector>
Keyboard select mode: Select
Add the attribute keyboard-select-mode="focus" so the Tab key gives focus to amp-selector. The selection changes as the user navigates options with arrow keys.
<amp-selector layout="container" keyboard-select-mode="select" class="sample-selector">
<amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60"></amp-img>
<amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60"></amp-img>
<amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60"></amp-img>
<amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4" width="90" height="60"></amp-img>
</amp-selector>
Actions
The clear action may be used to clear all selections of an amp-selector.
<amp-selector id="actionsSample" layout="container" class="sample-selector" multiple>
<amp-img src="/static/samples/img/landscape_sea_300x199.jpg" alt="Image 1" width="90" height="60" option="1"></amp-img>
<amp-img src="/static/samples/img/landscape_desert_300x200.jpg" alt="Image 2" width="90" height="60" option="2"></amp-img>
<amp-img src="/static/samples/img/landscape_ship_300x200.jpg" alt="Image 3" width="90" height="60" option="3" selected></amp-img>
<amp-img src="/static/samples/img/landscape_village_300x200.jpg" alt="Image 4"