Use input/select or toggle buttons to control UI elements
MIT License
ground-control
A Web Component for user control of HTML attributes and CSS properties on other elements of the page.
input-control
to get valuesinput
and select
elementstoggle-control
to get valuesswitch-control
foronPress()
and onUnPress()
methods)sessionStorage
or localStorage
output
elements to display the current valuesground-control
base classWith a range input:
<script type="module" src="index.js"></script>
<input-control
data-for=".color-swatch"
data-prop="--hue"
data-session="hue"
>
<label for="hue">Hue</label>
<input id="hue" type="range" min="0" max="360" value="200">
<output for="hue"></output>
</input-control>
Or a group of toggle buttons:
<toggle-control
id="color-scheme"
data-prop="color-scheme"
data-local="color-scheme"
data-off="light dark"
>
<strong>Color Scheme</strong>
<button data-value="light dark" aria-pressed="true">auto</button>
<button>light</button>
<button>dark</button>
</toggle-control>
Or a switch:
<switch-control data-prop="color-scheme" data-on="dark">
<button id="dark-mode" is-switch>dark mode</button>
</switch-control>
You have a few options (choose one):
npm install @terriblemia/ground-control
Make sure you include the <script>
in your project
(choose one, and update the version number as needed):
<!-- Host yourself -->
<script type="module" src="index.js"></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
type="module"
src="https://www.unpkg.com/@terriblemia/[email protected]/index.js"
></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
type="module"
src="https://esm.sh/@terriblemia/[email protected]"
></script>
Or use the built in
WebC components
with Eleventy,
by adding "npm:@terriblemia/ground-control/*.webc"
to the Eleventy WebC Plugin components
registry:
// Only one module.exports per configuration file, please!
module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(eleventyWebcPlugin, {
components: [
// Add as a global WebC component
"npm:@terriblemia/ground-control/*.webc",
],
})
}
This provides input-control
and toggle-control
components.
ground-control
base classinput-control
supports basic input
and select
typestoggle-control
supports solo and grouped togglesid="<control-id>"
[optional]:data-for="<selector>"
[defaults to :root
]:data-prop="<css-property-name>"
[optional]:data-attr="<attribute-name>"
[optional]:data-local="<localStorage-key>"
[optional]:localStorage
data-session="<sessionStorage-key>"
[optional]:sessionStorage
data-event="<event-name>"
[optional]:input-control
at this point,change
(the default) or input
data-off="<value>"
[optional]:select
)switch-control
only…
data-on="<value>"
[optional]:data-id
[optional]:button
to use as a toggle,button
element attributes inside toggle-control
:
data-value
[defaults to the button.innerText
]:aria-pressed
[optional]:toggle-control
can be pressed at a time)output
displays and reset
buttons:
id
of the input
/select
or the toggle-control
…<output for="<control-id>">
:output
as a display for the control<button data-reset="<control-id-list>">
:At OddBird, we enjoy collaborating and contributing as part of an open web community. But those contributions take time and effort. If you're interested in supporting our open-source work, consider becoming a GitHub sponsor, or contributing to our Open Collective.
❤️ Thanks!
With thanks to the following people: