-
Notifications
You must be signed in to change notification settings - Fork 103
/
Dropdown.svelte
292 lines (275 loc) · 9.26 KB
/
Dropdown.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<script lang="typescript">
import { FontWeight, Icon, Text, TextPropTypes, TextType, Error } from 'shared/components'
import { clickOutside } from 'shared/lib/actions'
import { onMount } from 'svelte'
import { DropdownChoice, isNumberLetterOrPunctuation } from '@core/utils'
export let value: string
export let label: string = ''
export let placeholder: string = ''
export let disabled = false
export let valueKey = 'label'
export let sortItems = false
export let items: DropdownChoice[] = []
export let small = false
export let contentWidth = false
export let error = ''
export let classes = ''
export let autofocus = false
export let showBorderWhenClosed = true
export let isFocused = false
export let enableTyping = false
// Text Props
export let type = TextType.p
export let fontSize = '12'
export let lineHeight = '120'
export let fontWeight: FontWeight = FontWeight.normal
let textProps: TextPropTypes
$: textProps = { type, fontSize, lineHeight, fontWeight }
$: placeholderColor = value ? '' : 'gray-500'
export let onSelect: (..._: DropdownChoice[]) => void
let dropdown = false
let navContainer
let divContainer: HTMLElement
let focusedItem: HTMLElement
let search = ''
items = sortItems ? items.sort((a, b) => (a.label > b.label ? 1 : -1)) : items
let navWidth: string
function handleClickOutside(): void {
dropdown = false
}
function toggleDropDown(): void {
dropdown = !dropdown
isFocused = !isFocused
if (dropdown) {
let elem = document.getElementById(value)
if (!elem) {
elem = navContainer.firstChild
}
if (elem) {
navContainer.scrollTop = elem.offsetTop
elem.focus()
}
} else {
divContainer.focus()
focusedItem = undefined
search = ''
}
}
function focusItem(itemId: string): void {
focusedItem = document.getElementById(itemId)
}
function handleKey(event: KeyboardEvent): void {
if (!enableTyping) {
return
}
if (!dropdown) {
// Note that space uses code not key, this is intentional
if (event.key === 'Enter' || event.key === 'ArrowDown' || event.code === 'Space') {
toggleDropDown()
event.preventDefault()
}
} else {
if (event.key === 'Escape') {
toggleDropDown()
event.preventDefault()
} else if (event.key === 'ArrowDown') {
if (focusedItem) {
const children = [...navContainer.children]
const idx = children.indexOf(focusedItem)
if (idx < children.length - 1) {
children[idx + 1].focus()
event.preventDefault()
}
}
} else if (event.key === 'ArrowUp') {
if (focusedItem) {
const children = [...navContainer.children]
const idx = children.indexOf(focusedItem)
if (idx > 0) {
children[idx - 1].focus()
event.preventDefault()
}
}
} else if (isNumberLetterOrPunctuation(event.key)) {
const children = [...navContainer.children]
const itemsValues = items.map((item) => item.label.toLowerCase())
search += event.key
const idx = itemsValues.findIndex((item) => item.includes(search.toLowerCase()))
if (idx >= 0) {
children[idx].focus()
event.preventDefault()
}
} else if (event.key === 'Backspace') {
search = search.slice(0, -1)
}
}
}
onMount(() => {
if (contentWidth) {
navWidth = `width: ${navContainer.clientWidth + 8}px`
}
if (autofocus) {
divContainer.focus()
}
})
</script>
<dropdown-input
class="relative {contentWidth ? '' : 'w-full'} {classes}"
on:click={(e) => {
e.stopPropagation()
toggleDropDown()
}}
use:clickOutside
on:clickOutside={handleClickOutside}
on:keydown={handleKey}
class:active={dropdown}
class:small
class:floating-active={value && label}
class:disabled
class:hasBorder={showBorderWhenClosed || dropdown}
style={navWidth}
>
<div
class="selection relative flex items-center w-full whitespace-nowrap cursor-pointer
bg-white dark:bg-gray-800 {dropdown
? 'border-blue-500'
: showBorderWhenClosed
? 'focus:border-blue-500 border-gray-300 dark:border-gray-700 hover:border-gray-500 dark:hover:border-gray-700'
: 'border-transparent'}"
tabindex="0"
bind:this={divContainer}
>
<div class="w-full text-12 leading-140 text-gray-800 dark:text-white">
<Text color="{placeholderColor}," darkColor={placeholderColor} {...textProps} classes="overflow-hidden">
{search || value || placeholder || ''}
</Text>
</div>
<Icon
icon={small ? 'small-chevron-down' : 'chevron-down'}
width={small ? 16 : 24}
height={small ? 16 : 24}
classes="absolute text-gray-500 fill-current"
/>
{#if label}
<floating-label class:floating-active={value && label}>{label}</floating-label>
{/if}
</div>
{#if error}
<Error {error} />
{/if}
<nav
class:active={dropdown}
class="absolute w-full overflow-hidden pointer-events-none opacity-0 z-10 text-left
bg-white dark:bg-gray-800
border border-solid border-blue-500 border-t-gray-500 dark:border-t-gray-700"
>
<div class="inner overflow-y-auto" bind:this={navContainer}>
{#each items as item}
<button
class="relative flex items-center p-4 w-full whitespace-nowrap
{item[valueKey] === value && 'bg-gray-100 dark:bg-gray-700 dark:bg-opacity-20'}
hover:bg-gray-100 dark:hover:bg-gray-700 dark:hover:bg-opacity-20
focus:bg-gray-200 dark:focus:bg-gray-600 dark:focus:bg-opacity-20"
id={item[valueKey]}
on:click={() => onSelect(item)}
on:focus={() => focusItem(item[valueKey])}
tabindex={dropdown ? 0 : -1}
class:active={item[valueKey] === value}><Text {...textProps}>{item[valueKey]}</Text></button
>
{/each}
</div>
</nav>
</dropdown-input>
<style type="text/scss">
dropdown-input {
@apply block;
.selection {
min-height: 50px;
border-radius: 0.625rem; // TODO: add to tailwind
@apply border-solid;
@apply border;
@apply py-4;
@apply pl-3;
@apply pr-10;
}
&:not(.small) {
:global(svg) {
@apply right-3;
@apply top-3;
}
}
&.active {
.selection {
border-bottom-color: transparent;
border-radius: 0.625rem 0.625rem 0 0; // TODO: add to tailwind
}
}
&.disabled {
@apply pointer-events-none;
@apply opacity-50;
}
nav {
.inner {
max-height: 235px;
}
top: 50px;
border-radius: 0 0 0.625rem 0.625rem; // TODO: add to tailwind
@apply left-0;
&.active {
@apply opacity-100;
@apply pointer-events-auto;
}
}
&.small {
.selection {
min-height: 36px;
@apply py-2.5;
@apply pl-3;
@apply pr-8;
@apply rounded-lg;
}
nav {
top: 36px;
@apply rounded-lg;
@apply rounded-tl-none;
@apply rounded-tr-none;
}
:global(svg) {
@apply right-2;
}
&.active {
.selection {
@apply rounded-bl-none;
@apply rounded-br-none;
}
}
}
&.floating-active {
.selection {
@apply pt-6;
@apply pb-2;
}
}
floating-label {
transform: translateY(3px);
transition: all 0.2s ease-out;
@apply block;
@apply text-gray-500;
@apply text-11;
@apply leading-120;
@apply overflow-hidden;
@apply opacity-0;
@apply pointer-events-none;
@apply absolute;
@apply left-3;
@apply select-none;
@apply whitespace-nowrap;
@apply w-full;
top: 8px;
&.floating-active {
@apply opacity-100;
transform: none;
}
}
}
</style>