page Switchcomponent { seo { title = "Switch component" description = "Theme-aware, responsive switch component." } view {
W WRNexusJS
forms component

Switch

Theme-aware, responsive switch component.

@wrnexus/ui 0.5.020 props0 slots4 events

Usage

Components are auto-discovered. Use the component directly in any .wrn view:

<Switch
  size="default"
  color="primary"
  id="id"
  name="name"
  label="Switch"
/>

Legacy mount name: data-component="Switch".

Props and attributes

PropTypeRequirementDefault
sizestringOptional"default"
colorstringOptional"primary"
idstringOptional""
namestringOptional""
labelstringOptional"Switch"
hiddenLabelbooleanOptionalfalse
placeholderstringOptional""
variantstringOptional"normal"
iconstringOptional""
iconPositionstringOptional"start"
valuestringOptional"on"
checkedbooleanOptionalfalse
helperTextstringOptional""
cornerHintstringOptional""
errorstringOptional""
inlinebooleanOptionalfalse
readonlybooleanOptionalfalse
disabledbooleanOptionalfalse
requiredbooleanOptionalfalse
classstringOptional""

Slots

This component does not declare a slot.

Events

  • input
  • change
  • focus
  • blur

Source contract

Installed source: components/Switch.wrn

component Switch {
  props {
    @event input = function
    @event change = function
    @event focus = function
    @event blur = function
    size = "default"
    color = "primary"
    id = ""
    name = ""
    label = "Switch"
    hiddenLabel = false
    placeholder = ""
    variant = "normal"
    icon = ""
    iconPosition = "start"
    value = "on"
    checked = false
    helperText = ""
    cornerHint = ""
    error = ""
    inline = false
    readonly = false
    disabled = false
    required = false
    class = ""
  }
  functions {
    function emitField(nameEvent, sourceEvent) { sourceEvent.stopPropagation(); $emit(nameEvent, { checked: sourceEvent.currentTarget.checked, value: value, name: name, sourceEvent: sourceEvent }) }
    function preventReadonly(sourceEvent) { if (readonly) sourceEvent.preventDefault() }
  }
  view {
        <div {...attrs} class="wire-next wire-next--color-{color} wire-next--size-{size} wire-next--choice-field {class}" data-inline="{inline}" data-invalid="{error ? 'true' : 'false'}">
        <div class="wire-next__field-heading">{#if cornerHint}<span class="{hiddenLabel ? 'wire-next__sr-only' : ''}">{label}</span>
        <span class="wire-next__field-hint">{cornerHint}</span>{/if}</div>
        <label class="wire-next__choice wire-next--switch" for="{id || name}" data-variant="{variant}" data-icon-position="{iconPosition}">
        <input id="{id || name}" type="checkbox" role="switch" name="{name}" value="{value}" checked="{checked}" disabled="{disabled}" required="{required}" readonly="{readonly}" aria-checked="{checked ? 'true' : 'false'}" aria-invalid="{error ? 'true' : 'false'}" @click="preventReadonly(event)" @input="emitField('input', event)" @change="emitField('change', event)" @focus="emitField('focus', event)" @blur="emitField('blur', event)" />{#if icon}<span class="{icon}" aria-hidden="true">
        </span>{/if}<span class="{hiddenLabel || cornerHint ? 'wire-next__sr-only' : ''}">{label}</span>
        </label>
      {#if helperText}<small class="wire-next__field-help">{helperText}</small>{/if}<small class="wire-next__field-error" data-error="{name}">{error}</small>
        </div>
    }
}
} }