Checkbox.svelte 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <script lang="ts">
  2. import { createEventDispatcher } from 'svelte';
  3. const dispatch = createEventDispatcher();
  4. export let state = 'unchecked';
  5. export let indeterminate = false;
  6. export let disabled = false;
  7. let _state = 'unchecked';
  8. $: _state = state;
  9. </script>
  10. <button
  11. class=" outline -outline-offset-1 outline-[1.5px] outline-gray-200 dark:outline-gray-600 {state !==
  12. 'unchecked'
  13. ? 'bg-black outline-black '
  14. : 'hover:outline-gray-500 hover:bg-gray-50 dark:hover:bg-gray-800'} text-white transition-all rounded-sm inline-block w-3.5 h-3.5 relative {disabled ? 'opacity-50 cursor-not-allowed' : ''}"
  15. on:click={() => {
  16. if (disabled) return;
  17. if (_state === 'unchecked') {
  18. _state = 'checked';
  19. dispatch('change', _state);
  20. } else if (_state === 'checked') {
  21. _state = 'unchecked';
  22. if (!indeterminate) {
  23. dispatch('change', _state);
  24. }
  25. } else if (indeterminate) {
  26. _state = 'checked';
  27. dispatch('change', _state);
  28. }
  29. }}
  30. type="button"
  31. {disabled}
  32. >
  33. <div class="top-0 left-0 absolute w-full flex justify-center">
  34. {#if _state === 'checked'}
  35. <svg
  36. class="w-3.5 h-3.5"
  37. aria-hidden="true"
  38. xmlns="http://www.w3.org/2000/svg"
  39. fill="none"
  40. viewBox="0 0 24 24"
  41. >
  42. <path
  43. stroke="currentColor"
  44. stroke-linecap="round"
  45. stroke-linejoin="round"
  46. stroke-width="3"
  47. d="m5 12 4.7 4.5 9.3-9"
  48. />
  49. </svg>
  50. {:else if indeterminate}
  51. <svg
  52. class="w-3 h-3.5 text-gray-800 dark:text-white"
  53. aria-hidden="true"
  54. xmlns="http://www.w3.org/2000/svg"
  55. fill="none"
  56. viewBox="0 0 24 24"
  57. >
  58. <path
  59. stroke="currentColor"
  60. stroke-linecap="round"
  61. stroke-linejoin="round"
  62. stroke-width="3"
  63. d="M5 12h14"
  64. />
  65. </svg>
  66. {/if}
  67. </div>
  68. <!-- {checked} -->
  69. </button>