-
Notifications
You must be signed in to change notification settings - Fork 171
/
VButton.vue
484 lines (439 loc) · 13.1 KB
/
VButton.vue
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<template>
<Component
:is="as"
:type="typeRef"
class="group/button flex appearance-none items-center justify-center rounded-sm no-underline"
:class="[
$style.button,
$style[variant],
$style[`size-${size}`],
{
[$style[`${variant}-pressed`]]: isActive,
[$style[`connection-${connections}`]]: isConnected,
[$style[`icon-start-${size}`]]: hasIconStart,
[$style[`icon-end-${size}`]]: hasIconEnd,
'gap-x-2': hasIconEnd || hasIconStart,
// Custom tailwind classes don't work with CSS modules in Vue.
'focus-slim-filled': isFilled,
'focus-slim-tx': isBordered || isTransparent,
'description-bold': isNewVariant,
'border border-tx ring-offset-1 focus:outline-none focus-visible:ring focus-visible:ring-pink':
!isPlainDangerous && !isNewVariant,
},
]"
:aria-pressed="pressed"
:aria-disabled="ariaDisabledRef"
:disabled="disabledAttributeRef"
v-bind="$attrs"
v-on="$listeners"
>
<!--
@slot The content of the button
-->
<slot />
</Component>
</template>
<script lang="ts">
import { defineComponent, ref, watch, toRefs, computed, PropType } from "vue"
import { warn } from "~/utils/console"
import type { ProperlyExtractPropTypes } from "~/types/prop-extraction"
import {
ButtonConnections,
buttonForms,
ButtonSize,
ButtonType,
ButtonVariant,
} from "~/types/button"
import type { ButtonForm } from "~/types/button"
import VLink from "~/components/VLink.vue"
export type ButtonProps = ProperlyExtractPropTypes<
NonNullable<typeof VButton["props"]>
>
/**
* A button component that behaves just like a regular HTML `button` element
* aside from pre-applied styles based on the passed in variant.
*
* All props available for the basic `button` component are available here as
* well, including an `as` prop which allows for component polymorphism. The
* most common use case for this prop is to turn the `VButton` component into
* an `anchor` element, so that you can render a link instead of a `button`.
*
* The accessibility helpers on this component are critical and are completely
* adapted from Reakit's Button, Clickable, and Tabbable component implementations.
*/
const VButton = defineComponent({
name: "VButton",
components: { VLink },
props: {
/**
* Passed to `component :is` to allow the button to *appear* as a button but
* work like another element (like an `anchor`). May only be either `button` or `a`.
*
* We do not support other elements because their use cases are marginal, and they
* add complexity that we can avoid otherwise.
*
* We also don't allow any old Vue component because Vue does not have ref-forwarding,
* so we wouldn't be able to detect the type of the DOM node that is ultimately rendered
* by any Vue component passed.
*
* @default 'button'
*/
as: {
type: String as PropType<ButtonForm>,
default: "button",
validate: (val: ButtonForm) => buttonForms.includes(val),
},
/**
* The variant of the button.
*
* Plain removes all styles except the focus ring.
* Plain--avoid removes _all_ styles including the focus ring.
*
* @default 'primary'
*/
variant: {
type: String as PropType<ButtonVariant>,
default: "primary",
},
/**
* Allows for programmatically setting the pressed state of a button,
* i.e., in the case of a button opening a menu.
*
* @default false
*/
pressed: {
type: Boolean,
default: false,
},
/**
* The size of the button. `disabled` removes all internal padding allowing
* the consumer of the component to determine the padding.
*
* @default 'medium'
*/
size: {
type: String as PropType<ButtonSize>,
default: "medium-old",
},
/**
* Whether the button is disabled. Used alone this will only
* visually effect the button but will not "truly" disable the
* button unless the `focusable` prop is also set to `false`.
*
* @default false
*/
disabled: {
type: Boolean,
default: false,
},
/**
* Whether the button is focusable when disabled. Should be `false`
* in almost all cases except when a button needs to be focusable
* while still being disabled (in the case of a form submit button
* that is disabled due to an incomplete form for example).
*
* @default false
*/
focusableWhenDisabled: {
type: Boolean,
default: false,
},
/**
* The HTML `type` attribute for the button. Ignored if `as` is
* passed as anything other than `"button"`.
*
* @default 'button'
*/
type: {
type: String as PropType<ButtonType>,
default: "button",
},
/**
* Whether the button is connected to another control and needs to have no rounded
* borders at that edge.
* `all` means that the button is not rounded.
*
* @default 'none'
*/
connections: {
type: String as PropType<ButtonConnections>,
default: "none",
},
/**
* Whether the button has an icon at the inline start of the button.
*
* @default false
*/
hasIconStart: {
type: Boolean,
default: false,
},
/**
* Whether the button has an icon at the inline end of the button.
*
* @default false
*/
hasIconEnd: {
type: Boolean,
default: false,
},
},
setup(props, { attrs }) {
const propsRef = toRefs(props)
const disabledAttributeRef = ref<boolean | undefined>(
propsRef.disabled.value
)
const ariaDisabledRef = ref<boolean>()
const trulyDisabledRef = ref<boolean>()
const typeRef = ref<ButtonType | undefined>(propsRef.type.value)
const supportsDisabledAttributeRef = ref(true)
const isConnected = computed(() => props.connections !== "none")
const isActive = computed(() => {
return (
propsRef.pressed.value ||
attrs["aria-pressed"] ||
attrs["aria-expanded"]
)
})
const isPlainDangerous = computed(() => {
return propsRef.variant.value === "plain--avoid"
})
const isFilled = computed(() => {
return props.variant.startsWith("filled-")
})
const isBordered = computed(() => {
return props.variant.startsWith("bordered-")
})
const isTransparent = computed(() => {
return props.variant.startsWith("transparent-")
})
watch(
[propsRef.disabled, propsRef.focusableWhenDisabled],
([disabled, focusableWhenDisabled]) => {
trulyDisabledRef.value = disabled && !focusableWhenDisabled
// If disabled and focusable then use `aria-disabled` instead of the `disabled` attribute to allow
// the button to still be tabbed into by screen reader users
if (disabled && focusableWhenDisabled) {
ariaDisabledRef.value = true
} else {
ariaDisabledRef.value = undefined
}
},
{ immediate: true }
)
watch(
propsRef.as,
(as) => {
if (["VLink"].includes(as)) {
typeRef.value = undefined
supportsDisabledAttributeRef.value = false
} else if (["a", "NuxtLink"].includes(as)) {
warn(
`Please use \`VLink\` with an \`href\` prop instead of ${as} for the button component`
)
}
},
{ immediate: true }
)
watch(
[trulyDisabledRef, supportsDisabledAttributeRef],
([trulyDisabled, supportsDisabled]) => {
disabledAttributeRef.value =
trulyDisabled && supportsDisabled ? true : undefined
},
{ immediate: true }
)
// TODO: remove after the Core UI improvements are done
const isNewVariant = computed(() => {
return (
props.variant.startsWith("filled-") ||
props.variant.startsWith("bordered-") ||
props.variant.startsWith("transparent-")
)
})
return {
disabledAttributeRef,
ariaDisabledRef,
typeRef,
isActive,
isConnected,
isPlainDangerous,
isNewVariant,
isFilled,
isBordered,
isTransparent,
}
},
})
export default VButton
</script>
<style module>
.button[disabled="disabled"],
.button[aria-disabled="true"] {
@apply cursor-not-allowed;
}
.size-small-old {
@apply py-1 px-2;
}
.size-medium-old {
@apply py-2 px-4;
}
.size-large-old {
@apply py-6 px-8;
}
.size-small {
@apply h-8 py-0 px-3;
}
.icon-start-small {
@apply ps-1;
}
.icon-end-small {
@apply pe-1;
}
.size-medium {
@apply h-10 py-0 px-4;
}
.icon-start-medium {
@apply ps-3;
}
.icon-end-medium {
@apply pe-3;
}
.size-large {
@apply h-12 py-0 px-6;
}
.icon-start-large {
@apply ps-5;
}
.icon-end-large {
@apply pe-5;
}
a.button {
@apply no-underline hover:no-underline;
}
.filled-pink {
@apply bg-pink text-white hover:bg-dark-pink hover:text-white;
}
.filled-dark {
@apply bg-dark-charcoal text-white hover:bg-dark-charcoal-80 hover:text-white;
}
.filled-gray {
@apply bg-dark-charcoal-10 text-dark-charcoal hover:bg-dark-charcoal hover:text-white;
}
.filled-white {
@apply bg-white text-dark-charcoal hover:bg-dark-charcoal hover:text-white;
}
.bordered-white {
@apply border border-white bg-white text-dark-charcoal hover:border-dark-charcoal-20;
}
.bordered-gray {
@apply border border-dark-charcoal-20 bg-white text-dark-charcoal hover:border-dark-charcoal;
}
.transparent-gray {
@apply bg-tx text-dark-charcoal hover:bg-dark-charcoal-10;
}
.transparent-dark {
@apply bg-tx text-dark-charcoal hover:bg-dark-charcoal hover:text-white;
}
.primary {
@apply border-tx bg-pink text-white hover:border-tx hover:bg-dark-pink hover:text-white;
}
.primary-pressed {
@apply bg-dark-pink;
}
.secondary {
@apply border-tx bg-tx hover:bg-dark-charcoal hover:text-white focus-visible:ring focus-visible:ring-pink;
}
.secondary-pressed {
@apply border-tx bg-dark-charcoal text-white hover:border-tx hover:bg-dark-charcoal-90;
}
.secondary[disabled="disabled"],
.secondary[aria-disabled="true"] {
@apply border-tx bg-tx text-dark-charcoal-40;
}
.secondary-filled {
@apply border-tx bg-dark-charcoal text-white hover:bg-dark-charcoal-80 hover:text-white focus-visible:ring focus-visible:ring-pink disabled:bg-dark-charcoal-10 disabled:text-dark-charcoal-40;
}
.secondary-bordered {
@apply border-dark-charcoal bg-tx hover:bg-dark-charcoal hover:text-white focus-visible:border-tx disabled:bg-dark-charcoal-10 disabled:text-dark-charcoal-40;
}
.secondary-bordered-pressed {
@apply bg-dark-charcoal text-white hover:border-tx hover:bg-dark-charcoal-90 focus-visible:bg-dark-charcoal-90;
}
.secondary-filled[disabled="disabled"],
.secondary-bordered[disabled="disabled"],
.secondary-filled[aria-disabled="true"],
.secondary-bordered[aria-disabled="true"] {
@apply border-tx bg-dark-charcoal-10 text-dark-charcoal-40;
}
.text {
@apply border-tx bg-tx px-0 text-sm font-semibold text-pink hover:underline focus-visible:ring focus-visible:ring-pink;
}
.text[disabled="disabled"],
.text[aria-disabled="true"] {
@apply border-tx bg-tx text-dark-charcoal-40;
}
.menu {
@apply border-tx bg-white text-dark-charcoal ring-offset-0;
}
.menu-pressed {
@apply border-tx bg-dark-charcoal text-white;
}
.action-menu {
@apply border-tx bg-white text-dark-charcoal hover:border-dark-charcoal-20 group-focus-within:hover:border-tx;
}
.action-menu-pressed {
@apply border-tx bg-dark-charcoal text-white hover:border-tx hover:bg-dark-charcoal-90;
}
/**
Similar to `action-menu`, but always has a border, not only on hover.
https://www.figma.com/file/GIIQ4sDbaToCfFQyKMvzr8/Openverse-Design-Library?node-id=1684%3A3678
*/
.action-menu-bordered {
@apply border-dark-charcoal-20 bg-white text-dark-charcoal focus-visible:border-tx;
}
.action-menu-bordered-pressed {
@apply border-dark-charcoal bg-dark-charcoal text-white hover:bg-dark-charcoal-90;
}
.action-menu-muted {
@apply border-tx bg-dark-charcoal-10 text-dark-charcoal hover:border-dark-charcoal-20;
}
.action-menu-muted-pressed {
@apply border-tx bg-dark-charcoal text-white hover:border-tx hover:bg-dark-charcoal-90;
}
.action-menu[disabled="disabled"],
.action-menu[aria-disabled="true"],
.action-menu-muted[disabled="disabled"],
.action-menu-muted[aria-disabled="true"],
.action-menu-bordered[disabled="disabled"],
.action-menu-bordered[aria-disabled="true"] {
@apply border-dark-charcoal-10 bg-dark-charcoal-10 text-dark-charcoal-40;
}
.action-menu-secondary {
@apply border border-tx bg-white text-dark-charcoal hover:border-dark-charcoal-20;
}
.action-menu-secondary-pressed {
@apply border-tx bg-dark-charcoal text-white;
}
.full {
@apply w-full bg-dark-charcoal-06 font-semibold hover:bg-dark-charcoal-40 hover:text-white;
}
.full-pressed {
@apply w-full bg-dark-charcoal-06 font-semibold text-dark-charcoal;
}
.dropdown-label {
@apply border border-dark-charcoal-20 text-dark-charcoal focus-slim-tx hover:border-tx hover:bg-dark-charcoal hover:text-white;
}
.dropdown-label-pressed {
@apply border-tx bg-dark-charcoal text-white focus-bold-filled active:hover:border-white;
}
.connection-start {
@apply rounded-s-none;
}
.connection-end {
@apply rounded-e-none;
}
.connection-all {
@apply rounded-none;
}
</style>