{"version":3,"file":"index.mjs","sources":["../src/Switch.svelte"],"sourcesContent":["<!--\n  INPUT SWITCH COMPONENT\n\n  This component is intended to be an alternative to checkbox inputs in forms\n  when there's a need to toggle something with an on and off state. From a UX\n  perspective, it should be clearly understood that something is being turned on\n  or off.\n\n  An example of this is a switch to turn on or off sound in an interactive web\n  app. On the other hand, you should use a checkbox when users are accustomed to\n  seeing a checkbox such as an input to accept terms and conditions in a\n  transaction flow. That said, most of the time this is a great option.\n\n  USAGE:\n    Simply import this component into your vue file and use it in your template\n    as if it was a normal input; bind it to a vue model. Example:\n\n      <Switch bind:value=\"switch\"/>\n\n    The default switch is the same size as buttons. If this is too big, you can\n    add a \"mini\" property for a compact version. Example:\n\n      <Switch bind:value=\"switch\" mini/>\n\n    It's possible to change the text from \"ON\"/\"OFF\" by passing the props textOn\n    and textOff. If you change this you may need to modify the width too, which\n    can be done via the --switch-width CSS variable. Example:\n\n      <Switch bind:value=\"switch\" textOn=\"YES\" textOff=\"NO\"/>\n\n  TIPS:\n    - It's a good idea to add a title attribute so desktop users can understand\n      the elements purpose more clearly.\n    - If following accessibility guidelines it's recommended to label the\n      purpose of the switch by adding an `aria-label` or `aria-labelledby`\n      attribute.\n    - If used in a form this component won't send any data by default. If you\n      wish to use it as a form input you'll need to bind the v-model to a\n      hidden input.\n    - The `disabled` attribute is not valid on <div> so we use a class.\n\n  @format\n-->\n\n<!--\n  TODO: Ability to drag the switch using mouse/touch in a high performance way.\n-->\n\n<script>\n  import { afterUpdate, createEventDispatcher } from 'svelte';\n\n  export let textOn = 'ON';\n  export let textOff = 'OFF';\n  export let mini = false;\n  export let disabled = false;\n  export let value;\n\n  const dispatch = createEventDispatcher();\n\n  let previousValue = value;\n\n  function toggle() {\n    if (!disabled) {\n      value = !value;\n    }\n  }\n\n  function handleKeyDown(event) {\n    // Choose key with graceful fallback for old browsers\n    /* eslint-disable @typescript-eslint/no-magic-numbers */\n    switch (event.key || event.keyCode) {\n      case 'Enter':\n      case 13: // Enter\n      case ' ':\n      case 'Spacebar':\n      case 32:\n        event.preventDefault(); // Don't submit form (enter) or scroll page (space)\n        toggle();\n        break;\n      default:\n        // No matching key\n    }\n    /* eslint-enable @typescript-eslint/no-magic-numbers */\n  }\n\n  afterUpdate(() => {\n    if (value !== previousValue) {\n      previousValue = value;\n      dispatch('change', value);\n    }\n  });\n</script>\n\n<style type=\"text/postcss\">\n  @import '%%/import.css';\n  @import './_switch.css';\n\n  /* This purposely looks like .button and uses many .button variables */\n  .switch {\n    position: relative;\n    display: inline-flex;\n    width: $switch-width;\n    padding: $switch-padding;\n    color: $switch-off-text-color;\n    text-align: center;\n    vertical-align: middle;\n    background-color: $switch-off-background-color;\n    border-radius: $switch-radius;\n    box-shadow: $switch-inner-shadow, $switch-shadow;\n    backface-visibility: hidden; /* Promote to own layer to avoid repaints */\n    cursor: pointer;\n    /* stylelint-disable-next-line plugin/no-low-performance-animation-properties */\n    transition: background-color $switch-animate-out-speed ease-out;\n    transition-delay: 0s;\n    user-select: none;\n\n    @if $switch-optimize {\n      will-change: background-color;\n    }\n\n    &.switch-mini {\n      width: $switch-mini-width;\n      padding: $switch-mini-padding;\n    }\n\n    &:hover {\n      /* FIXME: Add hover style */\n    }\n\n    &:focus {\n      /* FIXME: Add focus style */\n    }\n  }\n\n  .switch-checked {\n    background-color: $switch-on-background-color;\n    transition-delay: $switch-animate-in-speed;\n    transition-duration: $switch-animate-in-speed;\n  }\n\n  .switch-on,\n  .switch-off {\n    flex-basis: calc($switch-width / 2);\n  }\n\n  .switch-on {\n    color: $switch-on-text-color;\n\n    .switch-disabled > & {\n      color: $switch-disabled-text-color;\n    }\n  }\n\n  .switch-slider {\n    position: absolute;\n    top: 0;\n    bottom: 0;\n    width: 50%;\n    background: $switch-slider-background-color;\n    border: $switch-slider-border;\n    border-radius: $button-radius;\n    box-shadow: $switch-slider-inner-shadow;\n    transition: transform $switch-animate-in-speed ease-out;\n\n    @if $switch-optimize {\n      will-change: transform;\n    }\n\n    .switch-checked > & {\n      transform: translateX(100%);\n    }\n\n    .switch-mini > & {\n      box-shadow: $switch-mini-slider-inner-shadow;\n    }\n\n    .switch:hover > &,\n    .switch:focus > & {\n      background: $switch-hover-slider-background-color;\n    }\n\n    /* stylelint-disable-next-line no-descending-specificity */\n    .switch-disabled > & {\n      box-shadow: none; /* inner shadow */\n      transition: none;\n\n      @if $switch-optimize {\n        will-change: auto;\n      }\n    }\n\n    /* stylelint-disable-next-line no-descending-specificity */\n    .switch-disabled > &,\n    .switch-disabled:hover > &,\n    .switch-disabled:focus > & {\n      background: $switch-disabled-slider-background-color;\n    }\n  }\n\n  .switch-disabled {\n    color: $button-disabled-text-color;\n    background-color: $button-background-color;\n    border: $switch-disabled-border;\n    box-shadow: none; /* inner shadow */\n    cursor: not-allowed;\n    transition: none;\n\n    @if $switch-optimize {\n      will-change: auto;\n    }\n\n    /* stylelint-disable a11y/no-outline-none */\n    &:hover,\n    &:focus {\n      outline: none;\n    }\n    /* stylelint-enable */\n  }\n</style>\n\n<svelte:options immutable=\"{true}\" />\n\n<div\n  class=\"switch {mini ? 'switch-mini' : ''} {disabled ? 'switch-disabled' : ''} {value ? 'switch-checked' : ''}\"\n  role=\"switch\"\n  tabindex=\"{disabled ? -1 : 0}\"\n  aria-checked=\"{!!value}\"\n  on:click=\"{toggle}\"\n  on:keydown=\"{handleKeyDown}\"\n>\n  <div class=\"switch-slider\"></div>\n  <div class=\"switch-on\">{textOn}</div>\n  <div class=\"switch-off\">{textOff}</div>\n</div>\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA0G0B,GAAM;;;yBACL,GAAO;;;;gEATjB,GAAI,MAAG,aAAa,GAAG,EAAE,wBAAG,GAAQ,MAAG,iBAAiB,GAAG,EAAE,qBAAG,GAAK,MAAG,gBAAgB,GAAG,EAAE;;6DAEjG,GAAQ,OAAI,CAAC,GAAG,CAAC;oEACX,GAAK;;;;;;;;;;;;;;qCACX,GAAM;8CACJ,GAAa;;;;qDAGF,GAAM;uDACL,GAAO;;8GATjB,GAAI,MAAG,aAAa,GAAG,EAAE,wBAAG,GAAQ,MAAG,iBAAiB,GAAG,EAAE,qBAAG,GAAK,MAAG,gBAAgB,GAAG,EAAE;;;;8FAEjG,GAAQ,OAAI,CAAC,GAAG,CAAC;;;;iGACX,GAAK;;;;;;;;;;;;;;OAlGX,MAAM,GAAG,IAAI;OACb,OAAO,GAAG,KAAK;OACf,IAAI,GAAG,KAAK;OACZ,QAAQ,GAAG,KAAK;OAChB,KAAK;OAEV,QAAQ,GAAG,qBAAqB;KAElC,aAAa,GAAG,KAAK;;UAEhB,MAAM;OACR,QAAQ;mBACX,KAAK,IAAI,KAAK;;;;UAIT,aAAa,CAAC,KAAK;;;UAGlB,KAAK,CAAC,GAAG,IAAI,KAAK,CAAC,OAAO;QAC3B,OAAO;QACP,EAAE;QACF,GAAG;QACH,UAAU;QACV,EAAE;IACL,KAAK,CAAC,cAAc;IACpB,MAAM;;;;;;CAQZ,WAAW;MACL,KAAK,KAAK,aAAa;GACzB,aAAa,GAAG,KAAK;GACrB,QAAQ,CAAC,QAAQ,EAAE,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}