Web Components Handbook
Staticview components are standard web components. This page covers how web components work in practice and the patterns every Staticview component follows, and once you know them, you can guess most of a component's API before you open its reference.
Loading a Component
Importing a component registers its tag with customElements.define() for the whole page. Import each component once, at your app's entry point:
import "@staticview/ui/dialog";Load components before your framework renders them, because React 19, Preact, and Vue set a property only when the element already has it. On an unregistered element they set a string attribute instead, objects turn into "[object Object]", and functions, false, and null are lost.
For script tags, the CLI, and CDNs, see Vanilla HTML.
Shadow DOM
Since each component renders its internals inside a shadow root, your page styles don't reach in, and the component's styles don't leak out.
Inherited properties like font-family, color, and line-height are the exception, and components pick up your base typography with no extra setup:
body { font-family: Inter, sans-serif; color: #111;}Your selectors and non-inherited properties stay outside, and Styling covers the ways in.
Attributes and Properties
Like on native elements, most attributes have a matching camelCase property, like backdrop-dismiss and backdropDismiss.
<sv-dialog backdrop-dismiss close-button></sv-dialog>dialog.backdropDismiss = true;dialog.opened; // booleanBoolean attributes follow HTML, where a present attribute means true whatever its value.
Properties that hold objects or functions, like valueLabelHandler on the slider, have no attribute, and you set them from JavaScript.
Methods and States
Methods are verbs, like open(), close(), check(), and toggle(), and the state they change is a property and an attribute with the same name, like opened, checked, or pressed.
Methods that animate take a last isInstant argument that skips the animation:
dialog.open(); // Animatesdialog.open(true); // Skips the animationTo know when the animation is done, listen to transitionfinish, which fires when it ends or right away when there is none, and read the state there:
dialog.addEventListener("transitionfinish", () => { console.log(dialog.opened ? "Fully open" : "Fully closed");});Events
Component events are regular DOM events that you listen to with addEventListener or your framework's event binding, and any extra data is on event.detail.
Change Events
<property>change, like valuechange, openedchange, or checkedchange, fires after a property changes, whether the user, your code, or a form reset changed it.
Interaction Events
Events named in the past tense, like clicked, triggerclicked, adjusted, or dismissed, fire only when the user acts, before anything changes, and you can cancel them. When they carry a detail, it holds the value or state that is about to apply.
flyout.addEventListener("triggerclicked", event => { if (event.detail.opened && !isSignedIn) { event.preventDefault(); // Stays closed }});Use an interaction event to block or adjust what the user does, and the change event to react to the result.
Native Form Events
Like native controls, form components fire input and change events only on user interaction, and existing form code and framework bindings that listen for them keep working. To also catch changes made from code, listen to the change event, like valuechange.
Slots
Content with a slot attribute goes to the named slot that matches it, and the rest goes to the default slot.
<sv-dialog aria-label="Settings" close-button> <svg slot="close-icon" viewBox="0 0 24 24"><path d="..." /></svg> <p>This goes to the default slot.</p></sv-dialog>You control the content of a slot, and the component decides where it renders.
Data Attributes
Some components look for data-sv-<component>- markers on your own elements. For example, a button with data-sv-dialog-toggle opens and closes the dialog with that id:
<button data-sv-dialog-toggle="settings">Settings</button>
<sv-dialog id="settings" aria-label="Settings">...</sv-dialog>To style a state that a component marks on your elements, select its data-sv-<state> attribute, like data-sv-active on the option the keyboard is on.
Forms
Form components join their <form> like native inputs. They take name and disabled, take part in browser validation, submit their value with the form, and return to their default on a form reset.
<form> <label for="wifi">Wi-Fi</label> <sv-switch id="wifi" name="wifi" checked></sv-switch> <button type="submit">Save</button></form>form.addEventListener("submit", event => { event.preventDefault(); new FormData(form).get("wifi"); // "on"});Styling
The four ways to style a component below go from broad to narrow. Start with the broadest one that does the job.
Theme
Global tokens like --sv-accent and --sv-bdr-rad-md style every component at once, as Theming explains. Without a theme, components use the browser's system colors.
Component Tokens
Each component reads its own CSS custom properties, named --sv-<component>-<name>. Set them on the element to style one instance, or on an ancestor to style every instance inside it:
sv-slider { --sv-slider-thumb-sz: 1.6em; --sv-slider-fill-clr: hotpink;}The name puts the state first, the part next, and the property last, like active-bg, thumb-sz, or focus-bdr-clr, and common words are always shortened:
| Short | Word |
|---|---|
bg | background |
clr | color |
bdr | border |
rad | radius |
sz | size |
pad | padding |
dur | duration |
ease | easing |
anim | animation |
btn | button |
ln | line |
num | number |
dir | direction |
CSS Parts
::part() styles an element inside the shadow root directly, for what no token covers.
sv-dialog::part(dialog) { box-shadow: 0 8px 32px #0004;}CSS States
:state() matches a component in a given state, like a pseudo-class. State names start with --, and a state that mirrors a property takes its name, like :state(--opened) for opened.
sv-accordion:state(--opened)::part(trigger) { font-weight: 600;}Combine it with :has() to style the rest of the page from a component's state, with no script:
form:has(sv-switch:state(--checked)) .advanced-settings { display: block;}Reading the API Reference
When you are unsure what a component accepts, check its API reference first. It has a section for each topic on this page: Methods, Properties, Attributes, Events, Slots, CSS Properties, CSS Parts, and CSS States.