-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dropdown.svelte
156 lines (131 loc) · 3.77 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
<!--
Copyright © 2022 The Radicle Design System Contributors
This file is part of radicle-design-system, distributed under the GPLv3
with Radicle Linking Exception. For full terms see the included
LICENSE file.
-->
<script lang="ts">
import Option from "./Dropdown/Option.svelte";
import Overlay from "./Overlay.svelte";
import ChevronUpDownIcon from "./icons/ChevronUpDown.svelte";
type OptionT = { value: string; title: string };
export let options: OptionT[];
export let placeholder = "";
export let style: string | undefined = undefined;
export let optionStyle: string | undefined = undefined;
export let menuStyle: string | undefined = undefined;
export let valid: boolean = true;
export let validationMessage = "";
export let validationPending: boolean = false;
let expanded = false;
// bind to this prop from the outside
export let value = "";
export let disabled: boolean = false;
const toggleMenu = () => {
if (disabled) {
return;
}
expanded = !expanded;
};
const hideMenu = () => {
expanded = false;
};
const optionSelectedHandler = (event: CustomEvent<{ value: string }>) => {
value = event.detail.value;
toggleMenu();
};
const disabledColor = () => {
return disabled
? "var(--color-foreground-level-4)"
: "var(--color-foreground-level-6)";
};
$: optionByValue = options.find(option => option.value === value);
</script>
<style>
.dropdown {
position: relative;
cursor: pointer;
}
.button {
height: 40px;
background-color: var(--color-background);
border: 1px solid var(--color-foreground-level-3);
border-radius: 0.5rem;
display: flex;
align-items: center;
user-select: none;
display: flex;
justify-content: space-between;
overflow: hidden; /* hack to make inner option corners rounded */
}
.button:hover {
background-color: var(--color-foreground-level-1);
color: var(--color-foreground);
}
.button.disabled {
cursor: not-allowed;
box-shadow: 0px 0px 0px 0px;
background-color: var(--color-background);
}
.button.disabled:hover {
transform: none;
}
.menu {
position: absolute;
top: 0px;
left: 0px;
box-shadow: var(--elevation-medium);
border: 1px solid var(--color-foreground-level-3);
border-radius: 0.5rem;
user-select: none;
background-color: var(--color-background);
overflow: hidden; /* hack to make inner option corners rounded */
z-index: 1;
margin-bottom: 24px;
}
.validation-row {
display: flex;
align-items: center;
margin-top: 12px;
margin-left: 12px;
}
.button.invalid {
box-shadow: 0 0 0 1px var(--color-negative);
border: 1px solid var(--color-negative);
}
</style>
<Overlay {expanded} on:hide={hideMenu}>
<div class="dropdown" {style}>
<div
class="button button-transition"
class:invalid={!valid}
class:disabled
on:click={toggleMenu}>
{#if value && optionByValue}
<Option {...optionByValue} {disabled} />
{:else}
<p style={`margin: 0 42px 0 12px; color: ${disabledColor()}`}>
{placeholder}
</p>
{/if}
<ChevronUpDownIcon
style={`position: absolute; top: 8px; right: 8px; fill: ${disabledColor()};`} />
</div>
<div style={menuStyle} class="menu" hidden={!expanded}>
{#each options as option}
<Option
style={optionStyle}
{...option}
on:selected={optionSelectedHandler}
selected={value === option.value} />
{/each}
</div>
{#if !validationPending && !valid && validationMessage}
<div class="validation-row">
<p style="color: var(--color-negative); text-align: left;">
{validationMessage}
</p>
</div>
{/if}
</div>
</Overlay>