Methane@methane-ui/*
CSS LayoutsComponentsSkillsTheme BuilderGitHub
← All Components

<mth-switch>

@methane-ui/ui~9.0 KB brotli (cold + Lit)~2.6 KB marginal

Form-associated binary toggle switch with role="switch", Space/Enter activation, FACE validation, same-root ARIA, and FieldContext.

When NOT to use: Use a lightweight @methane-ui/css layout class instead of a custom element when static CSS suffices.

Live Interactive Preview

Interactive StageLive Custom Element · <mth-switch> (@methane-ui/ui)
Automatic OKLCH dark mode
Ready · interact with <mth-switch>, or toggle Theme, Direction (LTR/RTL), or props above.

Live Variants & States Showcase

uncheckedEmail notifications
checkedHigh-contrast mode
disabled (off / on)Disabled offDisabled on

Multi-Framework Playground & Snippets

<script type="module" src="@methane-ui/ui/switch"></script>
<mth-switch value="on">Example</mth-switch>

Properties & Attributes

PropertyAttributeTypeDefaultDescription
checkedcheckedbooleanfalseWhether the switch is turned on.
valuevaluestring"on"Form value submitted when checked (defaults to "on").
namenamestring""Form submission field name.
labellabelstring""Accessible label when default slot is omitted.
hinthintstring""Helper text linked via aria-describedby inside shadow root.
errorerrorstring""Explicit custom error message displayed inside shadow root.
disableddisabledbooleanfalseDisables switch interaction.
requiredrequiredbooleanfalseMarks switch required for constraint validation.
readonlyreadonlybooleanfalsePrevents toggling while remaining focusable and submittable.

Events

EventDetail TypeDescription
mth-change{ checked: boolean; value: string }Emitted when the switch toggles.

APG Keyboard Matrix

KeyDirectionWhenExpected Behavior
bothfocused and not disabled or readonlytoggles switch state
Enterbothfocused and not disabled or readonlytoggles switch state

Accessibility Checklist

CategoryRequirementVerified By
ariaAll aria-* IDREFs resolve within the same DOM root (ADR-003).scanSameRootAria
focusdelegatesFocus: true with zero host internals.role (ADR-014).lint-plugin & spec-compiler
contrastRenders legible borders, focus outlines, and selection indicators under forced-colors: active.Playwright forced-colors matrix
keyboardImplements all 2 APG keyboard transitions across LTR/RTL.mth-switch.spec.test.ts
statesExposes ElementInternals CustomStateSet flags: :state(checked), :state(dirty), :state(touched), :state(user-invalid), :state(disabled), :state(required), :state(readonly).Conformance suite
i18nRoutes 3 UI/ARIA strings through @methane-ui/core i18n catalog.Locale context test