-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathTextButton.svelte
More file actions
208 lines (181 loc) · 5.67 KB
/
TextButton.svelte
File metadata and controls
208 lines (181 loc) · 5.67 KB
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
<script lang="ts">
import { createEventDispatcher } from "svelte";
import MenuList from "/src/components/floating-menus/MenuList.svelte";
import ConditionalWrapper from "/src/components/layout/ConditionalWrapper.svelte";
import IconLabel from "/src/components/widgets/labels/IconLabel.svelte";
import TextLabel from "/src/components/widgets/labels/TextLabel.svelte";
import type { IconName } from "/src/icons";
import type { MenuListEntry, ActionShortcut } from "/wrapper/pkg/graphite_wasm_wrapper";
const dispatch = createEventDispatcher<{ selectedEntryValuePath: string[] }>();
let self: MenuList;
let open = false;
// Note: IconButton should instead be used if only an icon, but no label, is desired.
// However, if multiple TextButton widgets are used in a group with only some having no label, this component is able to accommodate that.
// Content
export let label: string;
export let icon: IconName | undefined = undefined;
export let hoverIcon: IconName | undefined = undefined;
export let disabled = false;
// Children
export let menuListChildren: MenuListEntry[][] | undefined = undefined;
export let menuListChildrenHash: bigint | undefined = undefined;
// Styling
export let emphasized = false;
export let flush = false;
export let narrow = false;
// Sizing
export let minWidth = 0;
// Tooltips
export let tooltipLabel: string | undefined = undefined;
export let tooltipDescription: string | undefined = undefined;
export let tooltipShortcut: ActionShortcut | undefined = undefined;
// Callbacks
// TODO: Replace this with an event binding (and on other components that do this)
export let action: (() => void) | undefined;
$: menuListChildrenExists = (menuListChildren?.length ?? 0) > 0;
// Handles either a button click or, if applicable, the opening of the menu list floating menu
function onClick(e: MouseEvent) {
// If there's no menu to open, trigger the action
if ((menuListChildren?.length ?? 0) === 0) {
// Call the action
if (!disabled) action?.();
// Exit early so we don't continue on and try to open the menu
return;
}
// Focus the target so that keyboard inputs are sent to the dropdown
if (e.target instanceof HTMLElement) e.target.focus();
// Open the menu list floating menu
if (self) self.open = true;
else throw new Error("The menu bar floating menu has no reference to `self`");
}
</script>
<ConditionalWrapper condition={menuListChildrenExists} wrapperClass="text-button-container">
<button
class="text-button"
class:open={self?.open}
class:hover-icon={hoverIcon && !disabled}
class:emphasized
class:disabled
class:narrow
class:flush
style:min-width={minWidth > 0 ? `${minWidth}px` : undefined}
data-tooltip-label={tooltipLabel}
data-tooltip-description={tooltipDescription}
data-tooltip-shortcut={tooltipShortcut?.shortcut ? JSON.stringify(tooltipShortcut.shortcut) : undefined}
data-emphasized={emphasized || undefined}
data-disabled={disabled || undefined}
data-text-button
tabindex={disabled ? -1 : 0}
data-floating-menu-spawner
data-block-hover-transfer={menuListChildrenExists ? undefined : ""}
on:click={onClick}
>
{#if icon}
<IconLabel {icon} />
{#if hoverIcon && !disabled}
<IconLabel icon={hoverIcon} />
{/if}
{/if}
{#if label}
<TextLabel>{label}</TextLabel>
{/if}
</button>
{#if menuListChildrenExists}
<MenuList
on:selectedEntryValuePath={({ detail }) => dispatch("selectedEntryValuePath", detail)}
on:open={({ detail }) => (open = detail)}
{open}
entries={menuListChildren || []}
entriesHash={menuListChildrenHash || 0n}
direction="Bottom"
minWidth={240}
drawIcon={true}
bind:this={self}
/>
{/if}
</ConditionalWrapper>
<style lang="scss" global>
.text-button-container {
display: flex;
position: relative;
}
.text-button {
display: flex;
justify-content: center;
align-items: center;
flex: 0 0 auto;
white-space: nowrap;
height: var(--widget-height);
margin: 0;
padding: 0 8px;
box-sizing: border-box;
border: none;
border-radius: 2px;
background: var(--button-background-color);
color: var(--button-text-color);
--button-background-color: var(--color-4-dimgray);
--button-text-color: var(--color-e-nearwhite);
--widget-height: 24px;
&.narrow.narrow {
--widget-height: 20px;
}
&:hover,
&.open {
--button-background-color: var(--color-6-lowergray);
--button-text-color: var(--color-f-white);
}
&.hover-icon {
&:not(:hover) .icon-label:nth-of-type(2) {
display: none;
}
&:hover .icon-label:nth-of-type(1) {
display: none;
}
}
&.disabled {
--button-background-color: var(--color-4-dimgray);
--button-text-color: var(--color-8-uppergray);
}
&.emphasized {
--button-background-color: var(--color-e-nearwhite);
--button-text-color: var(--color-2-mildblack);
&:hover,
&.open {
--button-background-color: var(--color-f-white);
}
&.disabled {
--button-background-color: var(--color-8-uppergray);
}
}
&.flush {
--button-background-color: none;
--button-text-color: var(--color-e-nearwhite);
&:hover,
&.open {
--button-background-color: var(--color-5-dullgray);
}
&.disabled {
--button-text-color: var(--color-8-uppergray);
--button-background-color: none;
}
}
.icon-label {
fill: var(--button-text-color);
+ .text-label {
margin-left: 8px;
}
}
.text-label {
overflow: hidden;
}
// Custom styling for when multiple TextButton widgets are used next to one another in a row or column
.widget-span.row > & + .text-button,
.layout-row > & + .text-button {
margin-left: 8px;
}
.widget-span.column > & + .text-button,
.layout-column > & + .text-button {
margin-top: 8px;
}
}
</style>