Switch
Theme-aware, responsive switch component.
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
| Prop | Type | Requirement | Default |
|---|---|---|---|
size | string | Optional | "default" |
color | string | Optional | "primary" |
id | string | Optional | "" |
name | string | Optional | "" |
label | string | Optional | "Switch" |
hiddenLabel | boolean | Optional | false |
placeholder | string | Optional | "" |
variant | string | Optional | "normal" |
icon | string | Optional | "" |
iconPosition | string | Optional | "start" |
value | string | Optional | "on" |
checked | boolean | Optional | false |
helperText | string | Optional | "" |
cornerHint | string | Optional | "" |
error | string | Optional | "" |
inline | boolean | Optional | false |
readonly | boolean | Optional | false |
disabled | boolean | Optional | false |
required | boolean | Optional | false |
class | string | Optional | "" |
Slots
This component does not declare a slot.
Events
inputchangefocusblur
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>
}
}