Cycle Toggle
<cycle-toggle>
Mature
JS
Click to cycle through a variety of options
Features
- Uses
ElementInternalsto work like a built-in form element - Accessible (?)
- Tiny (3K uncompressed and unminified!)
Examples
Basic, no selected option:
<label for="mood">Mood:</label>
<cycle-toggle id="mood">
<span>π</span>
<span>π</span>
<span>π</span>
<span>π</span>
<span>π</span>
</cycle-toggle>
Pre-selected option:
<label for="mood2">Mood:</label>
<cycle-toggle id="mood2">
<span>π</span>
<span>π</span>
<span>π</span>
<span aria-selected="true">π</span>
<span>π</span>
</cycle-toggle>
With values (any child element works):
<label for="mood3">Mood:</label>
<cycle-toggle id="mood3">
<data value="sad">π</data>
<data value="neutral">π</data>
<data value="happy" aria-selected="true">π</data>
<data value="elated">π</data>
</cycle-toggle>
With styles:
<cycle-toggle>
<data value="" style="opacity: .4">ππΌ</data>
<data value="1">ππΌ</data>
</cycle-toggle>
Readonly:
<cycle-toggle id="readonly_toggle" readonly>
<span>π</span>
<span>π</span>
<span>π</span>
<span aria-selected="true">π</span>
<span>π</span>
</cycle-toggle>
<button onclick="readonly_toggle.readonly = !readonly_toggle.readonly">Toggle readonly</button>
Set element.value:
<cycle-toggle id="toggle_rate">
<data value="1">ππΌ</data>
<data value="-1">ππΌ</data>
</cycle-toggle>
<button onclick="toggle_rate.value = 1">Select ππΌ</button>
<button onclick="toggle_rate.value = -1">Select ππΌ</button>
Dynamic aria-selected:
<cycle-toggle id="dynamic_selected">
<span>π</span>
<span>π</span>
<span>π</span>
<span>π</span>
<span>π</span>
</cycle-toggle>
<button onclick="dynamic_selected.children[3].setAttribute('aria-selected', 'true')">Select π</button>
Installation
Just include the componentβs JS file and youβre good:
<script src="https://nudeui.com/elements/cycle-toggle/cycle-toggle.js" type="module"></script>
In case you want to link to local files: CSS is fetched automatically, and assumed to be in the same directory as the JS file.