-
Notifications
You must be signed in to change notification settings - Fork 255
/
Button.svelte
212 lines (193 loc) · 5.42 KB
/
Button.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
<script>
/**
* @extends {"./ButtonSkeleton.svelte"} ButtonSkeletonProps
* @restProps {button | a | div}
* @slot {{ props: { role: "button"; type?: string; tabindex: any; disabled: boolean; href?: string; class: string; [key: string]: any; } }}
*/
/**
* Specify the kind of button
* @type {"primary" | "secondary" | "tertiary" | "ghost" | "danger" | "danger-tertiary" | "danger-ghost"}
*/
export let kind = "primary";
/**
* Specify the size of button
* @type {"default" | "field" | "small" | "lg" | "xl"}
*/
export let size = "default";
/** Set to `true` to use Carbon's expressive typesetting */
export let expressive = false;
/**
* Set to `true` to enable the selected state for an icon-only, ghost button
*/
export let isSelected = false;
/**
* Specify the icon to render
* Alternatively, use the named slot "icon" (e.g., `<Icon slot="icon" size="{20}" />`)
*
* @type {typeof import("svelte").SvelteComponent<any>}
*/
export let icon = undefined;
/**
* Specify the ARIA label for the button icon
* @type {string}
*/
export let iconDescription = undefined;
/**
* Set the alignment of the tooltip relative to the icon.
* Only applies to icon-only buttons
* @type {"start" | "center" | "end"}
*/
export let tooltipAlignment = "center";
/**
* Set the position of the tooltip relative to the icon
* @type {"top" | "right" | "bottom" | "left"}
*/
export let tooltipPosition = "bottom";
/**
* Set to `true` to render a custom HTML element
* Props are destructured as `props` in the default slot (e.g., <Button let:props><div {...props}>...</div></Button>)
*/
export let as = false;
/** Set to `true` to display the skeleton state */
export let skeleton = false;
/** Set to `true` to disable the button */
export let disabled = false;
/**
* Set the `href` to use an anchor link
* @type {string}
*/
export let href = undefined;
/** Specify the tabindex */
export let tabindex = "0";
/** Specify the `type` attribute for the button element */
export let type = "button";
/** Obtain a reference to the HTML element */
export let ref = null;
import { getContext } from "svelte";
import ButtonSkeleton from "./ButtonSkeleton.svelte";
const ctx = getContext("ComposedModal");
$: if (ctx && ref) {
ctx.declareRef(ref);
}
$: hasIconOnly = (icon || $$slots.icon) && !$$slots.default;
$: iconProps = {
"aria-hidden": "true",
class: "bx--btn__icon",
"aria-label": iconDescription,
};
$: buttonProps = {
type: href && !disabled ? undefined : type,
tabindex,
disabled: disabled === true ? true : undefined,
href,
"aria-pressed":
hasIconOnly && kind === "ghost" && !href ? isSelected : undefined,
...$$restProps,
class: [
"bx--btn",
expressive && "bx--btn--expressive",
((size === "small" && !expressive) ||
(size === "sm" && !expressive) ||
(size === "small" && !expressive)) &&
"bx--btn--sm",
(size === "field" && !expressive) ||
(size === "md" && !expressive && "bx--btn--md"),
size === "field" && "bx--btn--field",
size === "small" && "bx--btn--sm",
size === "lg" && "bx--btn--lg",
size === "xl" && "bx--btn--xl",
kind && `bx--btn--${kind}`,
disabled && "bx--btn--disabled",
hasIconOnly && "bx--btn--icon-only",
hasIconOnly && "bx--tooltip__trigger",
hasIconOnly && "bx--tooltip--a11y",
hasIconOnly &&
tooltipPosition &&
`bx--btn--icon-only--${tooltipPosition}`,
hasIconOnly &&
tooltipAlignment &&
`bx--tooltip--align-${tooltipAlignment}`,
hasIconOnly && isSelected && kind === "ghost" && "bx--btn--selected",
$$restProps.class,
]
.filter(Boolean)
.join(" "),
};
</script>
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
{#if skeleton}
<ButtonSkeleton
href="{href}"
size="{size}"
{...$$restProps}
style="{hasIconOnly && 'width: 3rem;'}"
on:click
on:focus
on:blur
on:mouseover
on:mouseenter
on:mouseleave
/>
{:else if as}
<slot props="{buttonProps}" />
{:else if href && !disabled}
<!-- svelte-ignore a11y-missing-attribute -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<a
bind:this="{ref}"
{...buttonProps}
on:click
on:focus
on:blur
on:mouseover
on:mouseenter
on:mouseleave
>
{#if hasIconOnly}
<span class:bx--assistive-text="{true}">{iconDescription}</span>
{/if}
<slot />
{#if $$slots.icon}
<slot
name="icon"
style="{hasIconOnly ? 'margin-left: 0' : undefined}"
{...iconProps}
/>
{:else if icon}
<svelte:component
this="{icon}"
style="{hasIconOnly ? 'margin-left: 0' : undefined}"
{...iconProps}
/>
{/if}
</a>
{:else}
<button
bind:this="{ref}"
{...buttonProps}
on:click
on:focus
on:blur
on:mouseover
on:mouseenter
on:mouseleave
>
{#if hasIconOnly}
<span class:bx--assistive-text="{true}">{iconDescription}</span>
{/if}
<slot />
{#if $$slots.icon}
<slot
name="icon"
style="{hasIconOnly ? 'margin-left: 0' : undefined}"
{...iconProps}
/>
{:else if icon}
<svelte:component
this="{icon}"
style="{hasIconOnly ? 'margin-left: 0' : undefined}"
{...iconProps}
/>
{/if}
</button>
{/if}