-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
DropdownOptions.svelte
138 lines (125 loc) · 3.39 KB
/
DropdownOptions.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<script lang="ts">
import { fly } from "svelte/transition";
import { createEventDispatcher } from "svelte";
export let value: string | Array<string> | undefined = undefined;
export let filtered: Array<string>;
export let showOptions: boolean = false;
export let activeOption: string | null;
export let disabled: boolean = false;
let distance_from_top: number;
let distance_from_bottom: number;
let input_height: number;
let input_width: number;
let refElement: HTMLDivElement;
let listElement: HTMLUListElement;
let top: string | null, bottom: string | null, max_height: number;
let innerHeight: number;
const calculate_window_distance = () => {
const { top: ref_top, bottom: ref_bottom } =
refElement.getBoundingClientRect();
distance_from_top = ref_top;
distance_from_bottom = innerHeight - ref_bottom;
};
let scroll_timeout: NodeJS.Timeout | null = null;
const scroll_listener = () => {
if (!showOptions) return;
if (scroll_timeout !== null) {
clearTimeout(scroll_timeout);
}
scroll_timeout = setTimeout(() => {
calculate_window_distance();
scroll_timeout = null;
}, 10);
};
$: {
if (showOptions && refElement) {
if (listElement && typeof value === "string") {
let el = document.querySelector(
`li[data-value="${value}"]`
) as HTMLLIElement;
if (el) {
listElement.scrollTo(0, el.offsetTop);
}
}
calculate_window_distance();
const rect = refElement.parentElement?.getBoundingClientRect();
input_height = rect?.height || 0;
input_width = rect?.width || 0;
}
if (distance_from_bottom > distance_from_top) {
top = `${distance_from_top}px`;
max_height = distance_from_bottom;
bottom = null;
} else {
bottom = `${distance_from_bottom + input_height}px`;
max_height = distance_from_top - input_height;
top = null;
}
}
const dispatch = createEventDispatcher();
$: _value = Array.isArray(value) ? value : [value];
</script>
<svelte:window on:scroll={scroll_listener} bind:innerHeight />
<div class="reference" bind:this={refElement} />
{#if showOptions && !disabled}
<ul
class="options"
aria-expanded={showOptions}
transition:fly={{ duration: 200, y: 5 }}
on:mousedown|preventDefault={(e) => dispatch("change", e)}
style:top
style:bottom
style:max-height={`calc(${max_height}px - var(--window-padding))`}
style:width={input_width + "px"}
bind:this={listElement}
>
{#each filtered as choice}
<li
class="item"
role="button"
class:selected={_value.includes(choice)}
class:active={activeOption === choice}
class:bg-gray-100={activeOption === choice}
class:dark:bg-gray-600={activeOption === choice}
data-value={choice}
aria-label={choice}
>
<span class:hide={!_value.includes(choice)} class="inner-item">
✓
</span>
{choice}
</li>
{/each}
</ul>
{/if}
<style>
.options {
--window-padding: var(--size-8);
position: fixed;
z-index: var(--layer-top);
margin-left: 0;
box-shadow: var(--shadow-drop-lg);
border-radius: var(--container-radius);
background: var(--background-fill-primary);
min-width: fit-content;
max-width: inherit;
overflow: auto;
color: var(--body-text-color);
list-style: none;
}
.item {
display: flex;
cursor: pointer;
padding: var(--size-2);
}
.item:hover,
.active {
background: var(--background-fill-secondary);
}
.inner-item {
padding-right: var(--size-1);
}
.hide {
visibility: hidden;
}
</style>