Checkbox.svelte 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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
  15. ? 'opacity-50 cursor-not-allowed'
  16. : ''}"
  17. on:click={() => {
  18. if (disabled) return;
  19. if (_state === 'unchecked') {
  20. _state = 'checked';
  21. dispatch('change', _state);
  22. } else if (_state === 'checked') {
  23. _state = 'unchecked';
  24. if (!indeterminate) {
  25. dispatch('change', _state);
  26. }
  27. } else if (indeterminate) {
  28. _state = 'checked';
  29. dispatch('change', _state);
  30. }
  31. }}
  32. type="button"
  33. {disabled}
  34. >
  35. <div class="top-0 left-0 absolute w-full flex justify-center">
  36. {#if _state === 'checked'}
  37. <svg
  38. class="w-3.5 h-3.5"
  39. aria-hidden="true"
  40. xmlns="http://www.w3.org/2000/svg"
  41. fill="none"
  42. viewBox="0 0 24 24"
  43. >
  44. <path
  45. stroke="currentColor"
  46. stroke-linecap="round"
  47. stroke-linejoin="round"
  48. stroke-width="3"
  49. d="m5 12 4.7 4.5 9.3-9"
  50. />
  51. </svg>
  52. {:else if indeterminate}
  53. <svg
  54. class="w-3 h-3.5 text-gray-800 dark:text-white"
  55. aria-hidden="true"
  56. xmlns="http://www.w3.org/2000/svg"
  57. fill="none"
  58. viewBox="0 0 24 24"
  59. >
  60. <path
  61. stroke="currentColor"
  62. stroke-linecap="round"
  63. stroke-linejoin="round"
  64. stroke-width="3"
  65. d="M5 12h14"
  66. />
  67. </svg>
  68. {/if}
  69. </div>
  70. <!-- {checked} -->
  71. </button>