UI State Examples
The examples below assume that you have imported the component and set up the theme. Since sv-ui-state evaluates its expressions with sv-expression-parser, import that one too.
Under each example, a panel lists its stores with their live values, and a value lights up when it changes. The panel belongs to these docs, not to the example code.
Basic
Declare a named value and show and change it from the markup. <sv-store> inside <sv-ui-state> holds it, bind-text reads it, and bind-event sets it by calling the store with an argument.
<sv-ui-state> <sv-store type="number" name="$count" value="0"></sv-store>
<div class="basic-counter"> <button type="button" class="btn btn-surface" bind-event="click:$count($count + 1)"> Add one </button> <strong bind-text="$count"></strong> </div></sv-ui-state>
<style> .basic-counter { display: flex; gap: 1em; align-items: center; justify-content: center; }</style>Store Types
Store a number, boolean, or JSON object instead of text. type="number" makes $quantity + 1 add instead of join, and type="json" lets $product.price read a field.
<sv-ui-state> <sv-store type="json" name="$product" value='{"name":"Desk lamp","price":24}'></sv-store> <sv-store type="number" name="$quantity" value="1"></sv-store> <sv-store type="boolean" name="$giftWrap" value="false"></sv-store>
<div class="cart-line"> <button type="button" class="btn btn-surface" bind-event="click:$quantity($quantity + 1)"> Add a lamp </button>
<button type="button" class="btn btn-surface" bind-event="click:$giftWrap(!$giftWrap)" bind-text="$giftWrap ? 'Remove gift wrap' : 'Add gift wrap, $3'" ></button>
<strong bind-text="$quantity + ' × ' + $product.name + ': $' + ($product.price * $quantity + ($giftWrap ? 3 : 0))" ></strong> </div></sv-ui-state>
<style> .cart-line { display: grid; gap: 0.75em; justify-items: center; }</style>Text and Attributes
Show computed text and set attributes from an expression. bind-text takes an expression, bind-attr writes an attribute, and bind-attr-toggle adds or removes a boolean attribute.
<sv-ui-state> <sv-store type="number" name="$step" value="1"></sv-store>
<div class="steps"> <p class="steps-label" bind-text="'Step ' + $step + ' of 3'"></p>
<progress class="steps-bar" bind-attr="value:$step" max="3"></progress>
<div class="steps-actions"> <button type="button" class="btn btn-surface" bind-attr-toggle="disabled:$step === 1" bind-event="click:$step($step - 1)" > Back </button> <button type="button" class="btn btn-surface" bind-attr-toggle="disabled:$step === 3" bind-event="click:$step($step + 1)" > Next </button> </div> </div></sv-ui-state>
<style> .steps { display: grid; gap: 0.75em; justify-items: center;
.steps-label { margin: 0; font-size: 0.9em; color: var(--sv-gray-3); }
.steps-bar { inline-size: 14rem; accent-color: var(--sv-accent); }
.steps-actions { display: flex; gap: 0.4em;
button { font-size: 0.85em;
&:disabled { color: var(--sv-gray-4); cursor: not-allowed; } } } }</style>Form Inputs
Keep an input and a store in sync in both directions. bind-prop writes the store into the input value, and bind-event writes what the user types back into the store.
<sv-ui-state> <sv-store name="$name" value="Alice"></sv-store>
<div class="inputs-demo"> <input type="text" aria-label="Name" autocomplete="name" bind-event="input:$name(EVENT.target.value)" bind-prop="value:$name" name="name" /> <button type="button" class="btn btn-surface" bind-event="click:$name('Alice')">Reset</button> <p bind-text="'Hello, ' + $name"></p> </div></sv-ui-state>
<style> .inputs-demo { display: flex; flex-wrap: wrap; gap: 0.75em; align-items: center; justify-content: center;
input { padding: 0.4em 0.6em; font: inherit; color: inherit; background-color: transparent; border: 1px solid var(--sv-gray-5); border-radius: var(--sv-bdr-rad-sm); }
p { flex-basis: 100%; margin: 0; text-align: center; } }</style>Classes
Change the CSS classes of an element from a store. bind-class uses the value as the class name, and bind-class-toggle adds or removes a fixed class from a condition.
<sv-ui-state> <sv-store name="$status" value="operational"></sv-store>
<div class="status-demo"> <span class="status-badge" bind-class="$status" bind-text="$status"></span>
<div class="status-picker"> <button type="button" bind-class-toggle="is-active:$status === 'operational'" bind-event="click:$status('operational')" > Operational </button> <button type="button" bind-class-toggle="is-active:$status === 'degraded'" bind-event="click:$status('degraded')" > Degraded </button> <button type="button" bind-class-toggle="is-active:$status === 'down'" bind-event="click:$status('down')" > Down </button> </div> </div></sv-ui-state>
<style> .status-demo { display: grid; gap: 0.75em; justify-items: center;
.status-badge { padding: 0.25em 0.9em; font-size: 0.85em; background-color: var(--sv-gray-6); border-radius: 999px;
&.operational { color: var(--sv-success); }
&.degraded { color: var(--sv-warning); }
&.down { color: var(--sv-error); } }
.status-picker { display: flex; flex-wrap: wrap; gap: 0.4em; justify-content: center;
button { padding: 0.3em 0.9em; font: inherit; font-size: 0.85em; color: var(--sv-gray-3); cursor: pointer; background-color: var(--sv-gray-6); border: var(--sv-bdr-sz-sm) solid transparent; border-radius: var(--sv-bdr-rad-sm);
&.is-active { color: var(--sv-gray-0); border-color: var(--sv-accent); } } } }</style>Styles
Drive a continuous style value from a store, like the width of a resizable sidebar. bind-style sets the --sidebar-width custom property that the grid columns read.
<sv-ui-state> <sv-store type="number" name="$sidebarWidth" value="30"></sv-store>
<div class="layout-demo"> <div class="layout-preview" bind-style="--sidebar-width:$sidebarWidth + '%'"> <div class="layout-sidebar">Sidebar</div> <div class="layout-main">Content</div> </div>
<label class="layout-control"> Sidebar width <input type="range" bind-event="input.passive:$sidebarWidth(+EVENT.target.value)" bind-prop="value:$sidebarWidth" max="60" min="15" /> <span bind-text="$sidebarWidth + '%'"></span> </label> </div></sv-ui-state>
<style> .layout-demo { display: grid; gap: 1em; inline-size: min(24rem, 100%); margin-inline: auto;
.layout-preview { display: grid; grid-template-columns: var(--sidebar-width, 30%) minmax(0, 1fr); gap: 0.4em; block-size: 8em; font-size: 0.85em;
> div { display: grid; place-items: center; min-inline-size: 0; overflow: hidden; color: var(--sv-gray-3); background-color: var(--sv-gray-6); border-radius: var(--sv-bdr-rad-sm); } }
.layout-control { display: flex; gap: 0.6em; align-items: center; font-size: 0.85em; color: var(--sv-gray-3);
input { flex: 1; accent-color: var(--sv-accent); }
span { min-inline-size: 3ch; font-variant-numeric: tabular-nums; } } }</style>Show and Hide
Show or hide an element from a condition and keep it in the DOM. bind-show sets display: none while the expression is falsy.
- Plan
- Team
- Seats
- 8
- Renews
- March 1
<sv-ui-state> <sv-store type="boolean" name="$details" value="false"></sv-store>
<div class="show-demo"> <button type="button" class="btn btn-surface show-toggle" bind-attr="aria-expanded:$details" bind-event="click:$details(!$details)" bind-text="$details ? 'Hide details' : 'Show details'" ></button>
<dl class="show-panel" bind-show="$details"> <dt>Plan</dt> <dd>Team</dd> <dt>Seats</dt> <dd>8</dd> <dt>Renews</dt> <dd>March 1</dd> </dl> </div></sv-ui-state>
<style> .show-demo { display: grid; gap: 0.75em; justify-items: center;
.show-toggle { font-size: 0.85em; }
.show-panel { display: grid; grid-template-columns: auto auto; gap: 0.3em 1.5em; min-inline-size: 12rem; padding: 0.8em 1.2em; margin: 0; font-size: 0.85em; background-color: var(--sv-gray-6); border-radius: var(--sv-bdr-rad-md);
dt { color: var(--sv-gray-3); }
dd { margin: 0; text-align: end; } } }</style>Events
Read the native event, and stop its default action. EVENT is the event object, like EVENT.key on keydown, and the prevent modifier in submit.prevent keeps the form from reloading the page.
<sv-ui-state> <sv-store name="$lastKey" value="none"></sv-store> <sv-store type="number" name="$sends" value="0"></sv-store>
<form class="events-demo" bind-event="submit.prevent:$sends($sends + 1)"> <input type="text" aria-label="Message" bind-event="keydown:$lastKey(EVENT.key)" name="message" placeholder="Type, then press Enter" /> <p bind-text="'Last key: ' + $lastKey"></p> <p bind-text="'Sends without a reload: ' + $sends"></p> </form></sv-ui-state>
<style> .events-demo { display: grid; gap: 0.5em; justify-items: center;
input { padding: 0.4em 0.6em; font: inherit; color: inherit; background-color: transparent; border: 1px solid var(--sv-gray-5); border-radius: var(--sv-bdr-rad-sm); }
p { margin: 0; font-size: 0.85em; color: var(--sv-gray-3); } }</style>Derived Stores
Compute a value from other stores instead of setting it by hand. A store with derive recomputes its expression whenever a store it reads changes.
<sv-ui-state> <sv-store type="number" name="$seats" value="1"></sv-store> <sv-store derive="$seats * 12" name="$total"></sv-store>
<div class="derive-demo"> <button type="button" class="btn btn-surface" bind-event="click:$seats($seats + 1)"> Add a seat </button> <strong bind-text="$seats + ' × $12 = $' + $total"></strong> </div></sv-ui-state>
<style> .derive-demo { display: flex; gap: 1em; align-items: center; justify-content: center; }</style>Persistent Stores
Keep a store value across page reloads, like a dismissed banner. persistent on the store saves its value to localStorage, and an id on sv-ui-state keeps the storage key stable.
<sv-ui-state id="ui-state-banner"> <sv-store type="boolean" name="$dismissed" persistent value="false"></sv-store>
<div class="persist-demo"> <p class="persist-banner" bind-show="!$dismissed"> Version 2 is out. <button type="button" class="btn btn-surface" bind-event="click:$dismissed(true)"> Dismiss </button> </p>
<button type="button" class="btn btn-surface" bind-event="click:$dismissed(false)" bind-show="$dismissed" > Show the banner again </button> </div></sv-ui-state>
<style> .persist-demo { display: grid; justify-items: center;
.persist-banner { display: flex; gap: 1em; align-items: center; padding: 0.5em 0.5em 0.5em 1em; margin: 0; background-color: var(--sv-gray-6); border-radius: var(--sv-bdr-rad-md); } }</style>Scopes
Share stores page-wide, or keep them local to one component. global shares the stores of an sv-ui-state with the whole page, and each local sv-ui-state keeps its own stores, even under the same name.
Sunny, light wind from the north.
Cloudy, snow in the evening.
<!-- One unit for the whole page --><sv-ui-state global> <sv-store name="$tempUnit" value="C"></sv-store></sv-ui-state>
<div class="weather"> <button type="button" class="btn btn-surface" bind-event="click:$tempUnit($tempUnit === 'C' ? 'F' : 'C')" bind-text="'Show °' + ($tempUnit === 'C' ? 'F' : 'C')" ></button>
<!-- Each card has its own $celsius and $open --> <sv-ui-state> <sv-store type="number" name="$celsius" value="18"></sv-store> <sv-store type="boolean" name="$open" value="false"></sv-store>
<div class="weather-card"> <div class="weather-row"> <strong>Lisbon</strong> <span bind-text="$tempUnit === 'C' ? $celsius + '°C' : Math.round($celsius * 1.8 + 32) + '°F'" ></span> <button type="button" class="btn btn-surface" bind-event="click:$open(!$open)" bind-text="$open ? 'Less' : 'More'" ></button> </div> <p bind-show="$open">Sunny, light wind from the north.</p> </div> </sv-ui-state>
<sv-ui-state> <sv-store type="number" name="$celsius" value="4"></sv-store> <sv-store type="boolean" name="$open" value="false"></sv-store>
<div class="weather-card"> <div class="weather-row"> <strong>Oslo</strong> <span bind-text="$tempUnit === 'C' ? $celsius + '°C' : Math.round($celsius * 1.8 + 32) + '°F'" ></span> <button type="button" class="btn btn-surface" bind-event="click:$open(!$open)" bind-text="$open ? 'Less' : 'More'" ></button> </div> <p bind-show="$open">Cloudy, snow in the evening.</p> </div> </sv-ui-state></div>
<style> .weather { display: grid; gap: 0.75em; justify-items: start; inline-size: min(22rem, 100%); margin-inline: auto;
.weather-card { display: grid; gap: 0.5em; justify-self: stretch; padding: 0.6em 0.9em; background-color: var(--sv-gray-6); border-radius: var(--sv-bdr-rad-md);
p { margin: 0; font-size: 0.85em; color: var(--sv-gray-3); } }
.weather-row { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 1em; align-items: center;
span { font-variant-numeric: tabular-nums; } }
.btn { font-size: 0.85em; } }</style>