Skip to content

The UiSwitch component is a flexible alternative to the non existing but widely used "switch" type input.

License

Notifications You must be signed in to change notification settings

Fendui/UiSwitch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

UiSwitch

The UiSwitch component is a flexible alternative to the non existing but widely used "switch" type input.

Features ✨

  • Vue 3
  • Fully accessible
  • Easily customisable with CSS
  • Familiar input element experience
  • Can be controlled or uncontrolled
  • Renders a visually hidden input element

Props ⚙

NameTypeDefaultDescription
modelValueBooleanundefinedUsed internally by v-model
tagStringundefinedThis is the root tag that will be rendered. Mainly for semantics or core web functionality. Preferably a HTMLElement
idStringbuttonThis is useful if you want to control the internal <input/> with another element, mostly a <label>
requiredBooleanundefinedThis is used if the UiSwitch will be used as a required field in a form
readonlyBooleanundefinedThis is adds the readonly attribute the the internal <input/>
disabledBooleanundefinedThis is used to completely disable the UiSwitch
validateFunctionundefinedUse this prop if you wish to validate the current value of an input. The function is expected to return a String of an error message which will be set on the internal <input/> or true if the current value is accepted
initialBooleanundefinedThis is used to set the initial state when no v-model is used
height[String, Number]undefinedThis is used to set the height of the root element. This prop basically sets a the --ui-height CSS variable on the style attribute
width[String, Number]undefinedThis is used to set the width of the root element. This prop basically sets a the --ui-width CSS variable on the style attribute

Structure 🏗

<slot name='prepend'/>
    
 <props.tag data-ui-switch='' class='root'>
    <div class='track'>
      <slot name='track'/>
    </div>
      
    <div class='thumb'>
      <input/>
      <slot name='thumb' />
    </div>
 </props.tag> 
     
<slot name='append'/>   

Slots 🎰

NamePayloadDescription
prepend
{ 
  active: Boolean,
  validation: {
    message: '',
    valid: true
  },
  toggle: Function
}
Use this slot to render something else before the root component, commonly used for prepending a <label/>
trackSame as prependUse this slot to render anything inside the .track element
thumbSame as prependUse this slot to render anything inside the .thumb element
defaultSame as prependUse this slot to render anything inside the root element
appendSame as prependUse this slot to render something else after the root component, commonly used for appending a <label/>

All slots are optional, and can accept multiple elements

Examples 💁‍♀️

Simplest form

<div id='app'>
 <UiSwitch/>
</div>

With v-model

<div id='app'>
 <UiSwitch v-model='switch'/>
</div>

With an external <label>

<div id='app'>
 <label for='switch'>
  Toggle UiSwitch
 </label>
 
 <UiSwitch id='switch' v-model='switch'/>
</div>

With an internal <label> (prepend) and an internal validation message <span> (append)

<div id='app'>
 <UiSwitch id='switch'>
  <template v-slot:prepend='{active}'>
    <label for='switch'>
     Selected: {{active}}
    </label> 
  </template>
  
  <template v-slot:append='{validation, toggle}'>
    <span v-if='validation.message' @click='toggle'>
     Error!
    </span>
  </template>
 </UiSwitch>
</div>

CSS variables

NameDefault valueDescription
--ui-height28pxUse this variable to set the root element's height
--ui-width44pxUse this variable to set the root element's width
--ui-track-height100%Sets the height of the track
--ui-track-radius100%Sets the border-radius of the track
--ui-track-background#999Sets the background of the track
--ui-track-background-checked#007bffSets the background of the track when the component is active
--ui-thumb-size25pxSets the height and width of the .thumb element
--ui-thumb-radius50%Sets the border-radius of the .thumb element
--ui-thumb-offset1.5pxThe gap between the thumb and the edges
--ui-thumb-translatex
calc(var(--ui-width) - 
   var(--ui-thumb-size) - 
   var(--ui-thumb-offset)
)
Computes the active state transformation
--ui-thumb-background#fffThe background of the thumb

About

The UiSwitch component is a flexible alternative to the non existing but widely used "switch" type input.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published