-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathButton.svelte
219 lines (206 loc) · 5.65 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
213
214
215
216
217
218
219
<svelte:options runes />
<MyComponent
{tag}
bind:this={element}
use={[
[
Ripple,
{
ripple,
unbounded: false,
color,
disabled: !!restProps.disabled,
addClass,
removeClass,
addStyle,
},
],
...use,
]}
class={classMap({
[className]: true,
'mdc-button': true,
'mdc-button--raised': variant === 'raised',
'mdc-button--unelevated': variant === 'unelevated',
'mdc-button--outlined': variant === 'outlined',
'smui-button--color-secondary': color === 'secondary',
'mdc-button--touch': touch,
'mdc-card__action': context === 'card:action',
'mdc-card__action--button': context === 'card:action',
'mdc-dialog__button': context === 'dialog:action',
'mdc-top-app-bar__navigation-icon': context === 'top-app-bar:navigation',
'mdc-top-app-bar__action-item': context === 'top-app-bar:action',
'mdc-snackbar__action': context === 'snackbar:actions',
'mdc-banner__secondary-action': context === 'banner' && secondary,
'mdc-banner__primary-action': context === 'banner' && !secondary,
'mdc-tooltip__action': context === 'tooltip:rich-actions',
...internalClasses,
})}
style={Object.entries(internalStyles)
.map(([name, value]) => `${name}: ${value};`)
.concat([style])
.join(' ')}
{...actionProp}
{...defaultProp}
{...secondaryProp}
{href}
{...restProps}
onclick={(e: MouseEvent) => {
handleClick();
restProps.onclick?.(e);
}}
><div class="mdc-button__ripple"></div>
{@render children?.()}{#if touch}<div
class="mdc-button__touch"
></div>{/if}</MyComponent
>
<script
lang="ts"
generics="Href extends string | undefined = undefined, TagName extends SmuiEveryElement = Href extends string ? 'a' : 'button'"
>
import type { Snippet } from 'svelte';
import { setContext, getContext } from 'svelte';
import type { ActionArray } from '@smui/common/internal';
import { classMap, dispatch } from '@smui/common/internal';
import Ripple from '@smui/ripple';
import type {
SmuiComponent,
SmuiElementMap,
SmuiEveryElement,
SmuiAttrs,
} from '@smui/common';
import { SmuiElement } from '@smui/common';
type OwnProps = {
/**
* An array of Action or [Action, ActionProps] to be applied to the element.
*/
use?: ActionArray;
/**
* A space separated list of CSS classes.
*/
class?: string;
/**
* A list of CSS styles.
*/
style?: string;
/**
* Whether to show a ripple animation.
*/
ripple?: boolean;
/**
* The color of the button.
*/
color?: 'primary' | 'secondary';
/**
* The styling variant of the button.
*/
variant?: 'text' | 'raised' | 'unelevated' | 'outlined';
/**
* Whether to use touch styling
*/
touch?: boolean;
/**
* If provided, the button will act as a link.
*/
href?: Href;
/**
* The action the button represents.
*/
action?: string;
/**
* Whether the button is the default action for the dialog.
*/
defaultAction?: boolean;
/**
* Whether the button is the secondary button for the banner.
*/
secondary?: boolean;
/**
* The component to use to render the element.
*/
component?: SmuiComponent<SmuiElementMap[TagName]>;
/**
* The tag name of the element to create.
*/
tag?: TagName;
children?: Snippet;
};
let {
use = [],
class: className = '',
style = '',
ripple = true,
color = 'primary',
variant = 'text',
touch = false,
href,
action = 'close',
defaultAction = false,
secondary = false,
component: MyComponent = SmuiElement,
tag = (href == null ? 'button' : 'a') as TagName,
children,
...restProps
}: OwnProps & SmuiAttrs<TagName, keyof OwnProps> = $props();
let element: ReturnType<SmuiComponent<SmuiElementMap[TagName]>>;
let internalClasses: { [k: string]: boolean } = $state({});
let internalStyles: { [k: string]: string } = $state({});
let context = getContext<string | undefined>('SMUI:button:context');
const actionProp = $derived(
context === 'dialog:action' && action != null
? { 'data-mdc-dialog-action': action }
: { action },
);
const defaultProp = $derived(
context === 'dialog:action' && defaultAction
? { 'data-mdc-dialog-button-default': '' }
: {},
);
const secondaryProp = $derived(context === 'banner' ? {} : { secondary });
let previousDisabled = restProps.disabled;
$effect(() => {
if (previousDisabled !== restProps.disabled) {
if (element) {
const el = getElement();
if ('blur' in el) {
el.blur();
}
}
previousDisabled = restProps.disabled;
}
});
setContext('SMUI:label:context', 'button');
setContext('SMUI:icon:context', 'button');
function addClass(className: string) {
if (!internalClasses[className]) {
internalClasses[className] = true;
}
}
function removeClass(className: string) {
if (!(className in internalClasses) || internalClasses[className]) {
internalClasses[className] = false;
}
}
function addStyle(name: string, value: string) {
if (internalStyles[name] != value) {
if (value === '' || value == null) {
delete internalStyles[name];
} else {
internalStyles[name] = value;
}
}
}
function handleClick() {
if (context === 'banner') {
dispatch(
getElement(),
secondary
? 'SMUIBannerButtonSecondaryActionClick'
: 'SMUIBannerButtonPrimaryActionClick',
);
}
}
export function getElement() {
return element.getElement();
}
</script>