|
| 1 | +<script lang="ts"> |
| 2 | + import { flyAndScale } from '$lib/utils/transitions'; |
| 3 | + import { tick } from 'svelte'; |
| 4 | +
|
| 5 | + /** Currently selected value */ |
| 6 | + export let value = ''; |
| 7 | +
|
| 8 | + /** Items array: { value: string, label: string }[] */ |
| 9 | + export let items = []; |
| 10 | +
|
| 11 | + /** Placeholder text when no value is selected */ |
| 12 | + export let placeholder = ''; |
| 13 | +
|
| 14 | + /** Callback when value changes */ |
| 15 | + export let onChange: (value: string) => void = () => {}; |
| 16 | +
|
| 17 | + /** CSS classes for the trigger button */ |
| 18 | + export let triggerClass = ''; |
| 19 | +
|
| 20 | + /** CSS classes for the label inside the trigger */ |
| 21 | + export let labelClass = ''; |
| 22 | +
|
| 23 | + /** CSS classes for the dropdown content container */ |
| 24 | + export let contentClass = 'rounded-2xl min-w-[170px] p-1 border border-gray-100 dark:border-gray-800 bg-white dark:bg-gray-850 dark:text-white shadow-lg'; |
| 25 | +
|
| 26 | + /** CSS classes for each item button */ |
| 27 | + export let itemClass = 'flex w-full gap-2 items-center px-3 py-1.5 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl'; |
| 28 | +
|
| 29 | + /** Alignment of the dropdown: 'start' | 'end' */ |
| 30 | + export let align = 'start'; |
| 31 | +
|
| 32 | + /** Callback when dropdown closes */ |
| 33 | + export let onClose: () => void = () => {}; |
| 34 | +
|
| 35 | + export let open = false; |
| 36 | +
|
| 37 | + let triggerEl; |
| 38 | + let contentEl; |
| 39 | +
|
| 40 | + $: selectedLabel = items.find((i) => i.value === value)?.label ?? placeholder; |
| 41 | +
|
| 42 | + /** Svelte action: moves the node to document.body (portal) */ |
| 43 | + function portal(node) { |
| 44 | + document.body.appendChild(node); |
| 45 | + return { |
| 46 | + destroy() { |
| 47 | + if (node.parentNode) { |
| 48 | + node.parentNode.removeChild(node); |
| 49 | + } |
| 50 | + } |
| 51 | + }; |
| 52 | + } |
| 53 | +
|
| 54 | + function positionContent() { |
| 55 | + if (!triggerEl || !contentEl) return; |
| 56 | + const rect = triggerEl.getBoundingClientRect(); |
| 57 | +
|
| 58 | + contentEl.style.position = 'fixed'; |
| 59 | + contentEl.style.zIndex = '9999'; |
| 60 | + contentEl.style.top = `${rect.bottom + 4}px`; |
| 61 | + contentEl.style.minWidth = `${rect.width}px`; |
| 62 | +
|
| 63 | + if (align === 'end') { |
| 64 | + contentEl.style.right = `${window.innerWidth - rect.right}px`; |
| 65 | + contentEl.style.left = 'auto'; |
| 66 | + } else { |
| 67 | + contentEl.style.left = `${rect.left}px`; |
| 68 | + contentEl.style.right = 'auto'; |
| 69 | + } |
| 70 | + } |
| 71 | +
|
| 72 | + async function toggleOpen() { |
| 73 | + open = !open; |
| 74 | + if (open) { |
| 75 | + await tick(); |
| 76 | + positionContent(); |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | + function handleWindowClick(event) { |
| 81 | + if (!open) return; |
| 82 | + if (triggerEl?.contains(event.target)) return; |
| 83 | + if (contentEl?.contains(event.target)) return; |
| 84 | + open = false; |
| 85 | + onClose(); |
| 86 | + } |
| 87 | +
|
| 88 | + function handleKeydown(event) { |
| 89 | + if (event.key === 'Escape' && open) { |
| 90 | + open = false; |
| 91 | + onClose(); |
| 92 | + } |
| 93 | + } |
| 94 | +
|
| 95 | + export function selectItem(item) { |
| 96 | + value = item.value; |
| 97 | + open = false; |
| 98 | + onChange(value); |
| 99 | + } |
| 100 | +</script> |
| 101 | + |
| 102 | +<svelte:window on:click={handleWindowClick} on:keydown={handleKeydown} on:scroll|capture={positionContent} on:resize={positionContent} /> |
| 103 | + |
| 104 | +<button |
| 105 | + bind:this={triggerEl} |
| 106 | + class={triggerClass} |
| 107 | + aria-label={placeholder} |
| 108 | + type="button" |
| 109 | + on:click={toggleOpen} |
| 110 | +> |
| 111 | + <slot name="trigger" {selectedLabel} {open}> |
| 112 | + <span class={labelClass}> |
| 113 | + {selectedLabel} |
| 114 | + </span> |
| 115 | + </slot> |
| 116 | +</button> |
| 117 | + |
| 118 | +{#if open} |
| 119 | + <div |
| 120 | + use:portal |
| 121 | + bind:this={contentEl} |
| 122 | + class={contentClass} |
| 123 | + transition:flyAndScale |
| 124 | + > |
| 125 | + <slot {open} {selectItem}> |
| 126 | + {#each items as item} |
| 127 | + <button |
| 128 | + class={itemClass} |
| 129 | + type="button" |
| 130 | + on:click={() => selectItem(item)} |
| 131 | + > |
| 132 | + <slot name="item" {item} selected={value === item.value}> |
| 133 | + {item.label} |
| 134 | + </slot> |
| 135 | + </button> |
| 136 | + {/each} |
| 137 | + </slot> |
| 138 | + </div> |
| 139 | +{/if} |
0 commit comments