@dawn-studio/astryx-svelte 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +36 -0
- package/README.md +152 -2
- package/dist/themes/dawn.css +805 -0
- package/dist/themes/neutral.css +757 -0
- package/package.json +57 -4
- package/src/Button.svelte +168 -0
- package/src/Card.svelte +48 -0
- package/src/Dialog.svelte +177 -0
- package/src/Switch.svelte +146 -0
- package/src/Tabs.svelte +168 -0
- package/src/TextInput.svelte +208 -0
- package/src/Theme.svelte +40 -0
- package/src/index.ts +8 -0
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
|
+
"description": "Astryx for Svelte: Dawn Studio's Svelte 5 implementation of Meta's Astryx design system — the upstream semantic token/theme CSS consumed directly, components re-implemented in Svelte against Astryx's published specs.",
|
|
3
|
+
"devDependencies": {
|
|
4
|
+
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|
|
5
|
+
"@testing-library/svelte": "^5.2.0",
|
|
6
|
+
"jsdom": "^25.0.0",
|
|
7
|
+
"svelte": "^5.20.0",
|
|
8
|
+
"typescript": "^5.6.0",
|
|
9
|
+
"vite": "^6.0.0",
|
|
10
|
+
"vitest": "^2.1.0",
|
|
11
|
+
"@dawn-studio/astryx-theme": "0.1.0"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"default": "./src/index.ts",
|
|
16
|
+
"svelte": "./src/index.ts",
|
|
17
|
+
"types": "./src/index.ts"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json",
|
|
20
|
+
"./themes/dawn.css": "./dist/themes/dawn.css",
|
|
21
|
+
"./themes/neutral.css": "./dist/themes/neutral.css"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"src",
|
|
25
|
+
"dist/themes",
|
|
26
|
+
"README.md",
|
|
27
|
+
"NOTICE"
|
|
28
|
+
],
|
|
29
|
+
"keywords": [
|
|
30
|
+
"astryx",
|
|
31
|
+
"svelte",
|
|
32
|
+
"svelte5",
|
|
33
|
+
"design-system",
|
|
34
|
+
"design-tokens",
|
|
35
|
+
"dawn-studio"
|
|
36
|
+
],
|
|
37
|
+
"license": "Apache-2.0",
|
|
2
38
|
"name": "@dawn-studio/astryx-svelte",
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"svelte": "^5.0.0"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "https://github.com/hectorchanht/dawn-astryx-svelte.git"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "node scripts/build-themes.mjs",
|
|
51
|
+
"demo": "vite --config demo/vite.config.ts",
|
|
52
|
+
"demo:build": "vite build --config demo/vite.config.ts",
|
|
53
|
+
"prepublishOnly": "npm run build && npm test",
|
|
54
|
+
"test": "vitest run",
|
|
55
|
+
"test:watch": "vitest"
|
|
56
|
+
},
|
|
57
|
+
"type": "module",
|
|
58
|
+
"version": "0.1.1"
|
|
59
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import type { HTMLButtonAttributes } from 'svelte/elements';
|
|
4
|
+
|
|
5
|
+
// Astryx Button (spec: Button.doc.mjs in @astryxdesign/core).
|
|
6
|
+
// variant/size mirror Astryx; the string `label` prop becomes the Svelte
|
|
7
|
+
// children snippet. isLoading follows the spec: the button stays focusable
|
|
8
|
+
// (aria-busy + aria-disabled, NOT native disabled) and ignores activation.
|
|
9
|
+
let {
|
|
10
|
+
variant = 'secondary',
|
|
11
|
+
size = 'md',
|
|
12
|
+
isLoading = false,
|
|
13
|
+
disabled = false,
|
|
14
|
+
type = 'button',
|
|
15
|
+
onclick,
|
|
16
|
+
class: className = '',
|
|
17
|
+
children,
|
|
18
|
+
...rest
|
|
19
|
+
}: {
|
|
20
|
+
variant?: 'primary' | 'secondary' | 'ghost' | 'destructive';
|
|
21
|
+
size?: 'sm' | 'md' | 'lg';
|
|
22
|
+
isLoading?: boolean;
|
|
23
|
+
disabled?: boolean;
|
|
24
|
+
type?: 'button' | 'submit' | 'reset';
|
|
25
|
+
onclick?: (e: MouseEvent) => void;
|
|
26
|
+
class?: string;
|
|
27
|
+
children?: Snippet;
|
|
28
|
+
} & Omit<HTMLButtonAttributes, 'type' | 'onclick'> = $props();
|
|
29
|
+
|
|
30
|
+
function handleClick(e: MouseEvent) {
|
|
31
|
+
if (isLoading) {
|
|
32
|
+
e.preventDefault();
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
onclick?.(e);
|
|
36
|
+
}
|
|
37
|
+
</script>
|
|
38
|
+
|
|
39
|
+
<button
|
|
40
|
+
{type}
|
|
41
|
+
class="ax-button ax-button--{variant} ax-button--{size} {className}"
|
|
42
|
+
disabled={disabled || undefined}
|
|
43
|
+
aria-disabled={isLoading || undefined}
|
|
44
|
+
aria-busy={isLoading || undefined}
|
|
45
|
+
onclick={handleClick}
|
|
46
|
+
{...rest}
|
|
47
|
+
>
|
|
48
|
+
{#if isLoading}
|
|
49
|
+
<span class="ax-button__spinner" aria-hidden="true"></span>
|
|
50
|
+
<span class="ax-visually-hidden" role="status">Loading</span>
|
|
51
|
+
{/if}
|
|
52
|
+
<span class="ax-button__label">{@render children?.()}</span>
|
|
53
|
+
</button>
|
|
54
|
+
|
|
55
|
+
<style>
|
|
56
|
+
/* NO STRETCH: heights are fixed per size; the label clamps inside a
|
|
57
|
+
min-width:0 track and can never resize the button. */
|
|
58
|
+
.ax-button {
|
|
59
|
+
position: relative;
|
|
60
|
+
display: inline-flex;
|
|
61
|
+
align-items: center;
|
|
62
|
+
justify-content: center;
|
|
63
|
+
gap: 0.5rem;
|
|
64
|
+
max-width: 100%;
|
|
65
|
+
min-width: 0;
|
|
66
|
+
border: 1px solid transparent;
|
|
67
|
+
border-radius: var(--radius-element, 0.5rem);
|
|
68
|
+
font-family: var(--font-family-body);
|
|
69
|
+
font-size: var(--text-label-size, 0.875rem);
|
|
70
|
+
font-weight: var(--text-label-weight, 500);
|
|
71
|
+
line-height: 1;
|
|
72
|
+
white-space: nowrap;
|
|
73
|
+
cursor: pointer;
|
|
74
|
+
user-select: none;
|
|
75
|
+
-webkit-tap-highlight-color: transparent;
|
|
76
|
+
transition:
|
|
77
|
+
background-color var(--duration-fast, 120ms) ease,
|
|
78
|
+
border-color var(--duration-fast, 120ms) ease;
|
|
79
|
+
}
|
|
80
|
+
.ax-button--sm {
|
|
81
|
+
height: 2.25rem;
|
|
82
|
+
padding-inline: 0.75rem;
|
|
83
|
+
font-size: var(--text-supporting-size, 0.8125rem);
|
|
84
|
+
}
|
|
85
|
+
.ax-button--md {
|
|
86
|
+
height: 2.75rem;
|
|
87
|
+
padding-inline: 1rem;
|
|
88
|
+
}
|
|
89
|
+
.ax-button--lg {
|
|
90
|
+
height: 3.25rem;
|
|
91
|
+
padding-inline: 1.25rem;
|
|
92
|
+
font-size: var(--text-large-size, 1.0625rem);
|
|
93
|
+
}
|
|
94
|
+
.ax-button__label {
|
|
95
|
+
min-width: 0;
|
|
96
|
+
overflow: hidden;
|
|
97
|
+
text-overflow: ellipsis;
|
|
98
|
+
white-space: nowrap;
|
|
99
|
+
}
|
|
100
|
+
.ax-button--primary {
|
|
101
|
+
background: var(--color-accent);
|
|
102
|
+
color: var(--color-on-accent);
|
|
103
|
+
}
|
|
104
|
+
.ax-button--secondary {
|
|
105
|
+
background: var(--color-background-surface);
|
|
106
|
+
border-color: var(--color-border);
|
|
107
|
+
color: var(--color-text-primary);
|
|
108
|
+
}
|
|
109
|
+
.ax-button--ghost {
|
|
110
|
+
background: transparent;
|
|
111
|
+
color: var(--color-text-primary);
|
|
112
|
+
}
|
|
113
|
+
.ax-button--destructive {
|
|
114
|
+
background: var(--color-error);
|
|
115
|
+
color: var(--color-on-error);
|
|
116
|
+
}
|
|
117
|
+
.ax-button:not(:disabled):hover {
|
|
118
|
+
box-shadow: inset 0 0 0 999px var(--color-overlay-hover);
|
|
119
|
+
}
|
|
120
|
+
.ax-button:not(:disabled):active {
|
|
121
|
+
box-shadow: inset 0 0 0 999px var(--color-overlay-pressed);
|
|
122
|
+
}
|
|
123
|
+
.ax-button:focus-visible {
|
|
124
|
+
outline: 2px solid var(--color-accent);
|
|
125
|
+
outline-offset: 2px;
|
|
126
|
+
}
|
|
127
|
+
.ax-button--destructive:focus-visible {
|
|
128
|
+
outline-color: var(--color-error);
|
|
129
|
+
}
|
|
130
|
+
.ax-button:disabled {
|
|
131
|
+
cursor: not-allowed;
|
|
132
|
+
color: var(--color-text-disabled);
|
|
133
|
+
background: var(--color-background-muted);
|
|
134
|
+
border-color: transparent;
|
|
135
|
+
box-shadow: none;
|
|
136
|
+
}
|
|
137
|
+
.ax-button[aria-disabled='true'] {
|
|
138
|
+
cursor: progress;
|
|
139
|
+
}
|
|
140
|
+
.ax-button__spinner {
|
|
141
|
+
flex: none;
|
|
142
|
+
width: 1em;
|
|
143
|
+
height: 1em;
|
|
144
|
+
border-radius: var(--radius-full, 999px);
|
|
145
|
+
border: 2px solid currentColor;
|
|
146
|
+
border-top-color: transparent;
|
|
147
|
+
opacity: 0.85;
|
|
148
|
+
animation: ax-spin 0.7s linear infinite;
|
|
149
|
+
}
|
|
150
|
+
@keyframes ax-spin {
|
|
151
|
+
to {
|
|
152
|
+
transform: rotate(360deg);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
@media (prefers-reduced-motion: reduce) {
|
|
156
|
+
.ax-button__spinner {
|
|
157
|
+
animation-duration: 1.6s;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
.ax-visually-hidden {
|
|
161
|
+
position: absolute;
|
|
162
|
+
width: 1px;
|
|
163
|
+
height: 1px;
|
|
164
|
+
overflow: hidden;
|
|
165
|
+
clip-path: inset(50%);
|
|
166
|
+
white-space: nowrap;
|
|
167
|
+
}
|
|
168
|
+
</style>
|
package/src/Card.svelte
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
|
|
4
|
+
// Astryx Card (spec: Card.doc.mjs in @astryxdesign/core).
|
|
5
|
+
// padding uses the Astryx spacing scale (1 unit = 0.25rem):
|
|
6
|
+
// 0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10.
|
|
7
|
+
let {
|
|
8
|
+
padding = 4,
|
|
9
|
+
elevation = 'low',
|
|
10
|
+
class: className = '',
|
|
11
|
+
children,
|
|
12
|
+
}: {
|
|
13
|
+
padding?: number;
|
|
14
|
+
elevation?: 'none' | 'low' | 'med';
|
|
15
|
+
class?: string;
|
|
16
|
+
children?: Snippet;
|
|
17
|
+
} = $props();
|
|
18
|
+
</script>
|
|
19
|
+
|
|
20
|
+
<div
|
|
21
|
+
class="ax-card ax-card--{elevation} {className}"
|
|
22
|
+
style="padding: {padding * 0.25}rem"
|
|
23
|
+
>
|
|
24
|
+
{@render children?.()}
|
|
25
|
+
</div>
|
|
26
|
+
|
|
27
|
+
<style>
|
|
28
|
+
/* Solid surface only (NO GLASS); geometry comes from tokens, content
|
|
29
|
+
flows inside and never changes the card's own box (NO STRETCH). */
|
|
30
|
+
.ax-card {
|
|
31
|
+
min-width: 0;
|
|
32
|
+
max-width: 100%;
|
|
33
|
+
background: var(--color-background-card);
|
|
34
|
+
border: 1px solid var(--color-border);
|
|
35
|
+
border-radius: var(--radius-container, 0.75rem);
|
|
36
|
+
color: var(--color-text-primary);
|
|
37
|
+
font-family: var(--font-family-body);
|
|
38
|
+
}
|
|
39
|
+
.ax-card--low {
|
|
40
|
+
box-shadow: var(--shadow-low);
|
|
41
|
+
}
|
|
42
|
+
.ax-card--med {
|
|
43
|
+
box-shadow: var(--shadow-med);
|
|
44
|
+
}
|
|
45
|
+
.ax-card--none {
|
|
46
|
+
box-shadow: none;
|
|
47
|
+
}
|
|
48
|
+
</style>
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
|
|
4
|
+
// Astryx Dialog (spec: Dialog.doc.mjs in @astryxdesign/core), built on the
|
|
5
|
+
// native <dialog> element per Modern Web Guidance: showModal() gives focus
|
|
6
|
+
// containment, Esc handling, ::backdrop and focus return for free.
|
|
7
|
+
// `open` mirrors Astryx's isOpen/onOpenChange pair as a bindable prop.
|
|
8
|
+
let {
|
|
9
|
+
open = $bindable(false),
|
|
10
|
+
title,
|
|
11
|
+
onOpenChange,
|
|
12
|
+
class: className = '',
|
|
13
|
+
children,
|
|
14
|
+
footer,
|
|
15
|
+
}: {
|
|
16
|
+
open?: boolean;
|
|
17
|
+
title?: string;
|
|
18
|
+
onOpenChange?: (open: boolean) => void;
|
|
19
|
+
class?: string;
|
|
20
|
+
children?: Snippet;
|
|
21
|
+
footer?: Snippet;
|
|
22
|
+
} = $props();
|
|
23
|
+
|
|
24
|
+
let dialogEl: HTMLDialogElement | undefined = $state();
|
|
25
|
+
|
|
26
|
+
function setOpen(next: boolean) {
|
|
27
|
+
if (open === next) return;
|
|
28
|
+
open = next;
|
|
29
|
+
onOpenChange?.(next);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
$effect(() => {
|
|
33
|
+
const dlg = dialogEl;
|
|
34
|
+
if (!dlg) return;
|
|
35
|
+
if (open && !dlg.open) {
|
|
36
|
+
if (typeof dlg.showModal === 'function') dlg.showModal();
|
|
37
|
+
else dlg.setAttribute('open', '');
|
|
38
|
+
} else if (!open && dlg.open) {
|
|
39
|
+
dlg.close();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
function handleClose() {
|
|
44
|
+
setOpen(false);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Backdrop click: clicks landing on the <dialog> element itself (outside
|
|
48
|
+
// the padded content box) close the dialog.
|
|
49
|
+
function handleClick(e: MouseEvent) {
|
|
50
|
+
const dlg = dialogEl;
|
|
51
|
+
if (!dlg || e.target !== dlg) return;
|
|
52
|
+
const rect = dlg.getBoundingClientRect();
|
|
53
|
+
const inside =
|
|
54
|
+
e.clientX >= rect.left && e.clientX <= rect.right && e.clientY >= rect.top && e.clientY <= rect.bottom;
|
|
55
|
+
if (!inside) setOpen(false);
|
|
56
|
+
}
|
|
57
|
+
</script>
|
|
58
|
+
|
|
59
|
+
<dialog
|
|
60
|
+
bind:this={dialogEl}
|
|
61
|
+
class="ax-dialog {className}"
|
|
62
|
+
aria-label={title}
|
|
63
|
+
onclose={handleClose}
|
|
64
|
+
onclick={handleClick}
|
|
65
|
+
>
|
|
66
|
+
<div class="ax-dialog__panel">
|
|
67
|
+
{#if title}
|
|
68
|
+
<div class="ax-dialog__header">
|
|
69
|
+
<h2 class="ax-dialog__title">{title}</h2>
|
|
70
|
+
<button type="button" class="ax-dialog__close" aria-label="Close dialog" onclick={() => setOpen(false)}>
|
|
71
|
+
×
|
|
72
|
+
</button>
|
|
73
|
+
</div>
|
|
74
|
+
{/if}
|
|
75
|
+
<div class="ax-dialog__body">
|
|
76
|
+
{@render children?.()}
|
|
77
|
+
</div>
|
|
78
|
+
{#if footer}
|
|
79
|
+
<div class="ax-dialog__footer">
|
|
80
|
+
{@render footer()}
|
|
81
|
+
</div>
|
|
82
|
+
{/if}
|
|
83
|
+
</div>
|
|
84
|
+
</dialog>
|
|
85
|
+
|
|
86
|
+
<style>
|
|
87
|
+
/* NO STRETCH: the dialog box is capped by a fixed max-width and the
|
|
88
|
+
viewport; content scrolls inside the body, never widening the dialog. */
|
|
89
|
+
.ax-dialog {
|
|
90
|
+
border: 1px solid var(--color-border);
|
|
91
|
+
background: var(--color-background-popover);
|
|
92
|
+
color: var(--color-text-primary);
|
|
93
|
+
border-radius: var(--radius-container, 0.75rem);
|
|
94
|
+
box-shadow: var(--shadow-high);
|
|
95
|
+
padding: 0;
|
|
96
|
+
width: min(32rem, calc(100vw - 2rem));
|
|
97
|
+
max-width: calc(100vw - 2rem);
|
|
98
|
+
max-height: calc(100dvh - 2rem);
|
|
99
|
+
font-family: var(--font-family-body);
|
|
100
|
+
}
|
|
101
|
+
.ax-dialog::backdrop {
|
|
102
|
+
background: var(--color-overlay);
|
|
103
|
+
}
|
|
104
|
+
.ax-dialog[open] {
|
|
105
|
+
animation: ax-dialog-in var(--duration-medium, 200ms) ease;
|
|
106
|
+
}
|
|
107
|
+
@starting-style {
|
|
108
|
+
.ax-dialog[open] {
|
|
109
|
+
opacity: 0;
|
|
110
|
+
transform: translateY(8px) scale(0.98);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
@keyframes ax-dialog-in {
|
|
114
|
+
from {
|
|
115
|
+
opacity: 0;
|
|
116
|
+
transform: translateY(8px) scale(0.98);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
@media (prefers-reduced-motion: reduce) {
|
|
120
|
+
.ax-dialog[open] {
|
|
121
|
+
animation: none;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
.ax-dialog__panel {
|
|
125
|
+
display: flex;
|
|
126
|
+
flex-direction: column;
|
|
127
|
+
max-height: inherit;
|
|
128
|
+
min-width: 0;
|
|
129
|
+
}
|
|
130
|
+
.ax-dialog__header {
|
|
131
|
+
display: flex;
|
|
132
|
+
align-items: center;
|
|
133
|
+
justify-content: space-between;
|
|
134
|
+
gap: 1rem;
|
|
135
|
+
padding: 1.25rem 1.5rem 0;
|
|
136
|
+
}
|
|
137
|
+
.ax-dialog__title {
|
|
138
|
+
margin: 0;
|
|
139
|
+
min-width: 0;
|
|
140
|
+
overflow: hidden;
|
|
141
|
+
text-overflow: ellipsis;
|
|
142
|
+
white-space: nowrap;
|
|
143
|
+
font-size: var(--text-heading-3-size, 1.0625rem);
|
|
144
|
+
font-weight: var(--text-heading-3-weight, 700);
|
|
145
|
+
line-height: var(--text-heading-3-leading, 1.4118);
|
|
146
|
+
}
|
|
147
|
+
.ax-dialog__close {
|
|
148
|
+
flex: none;
|
|
149
|
+
width: 2.25rem;
|
|
150
|
+
height: 2.25rem;
|
|
151
|
+
border: none;
|
|
152
|
+
border-radius: var(--radius-full, 999px);
|
|
153
|
+
background: transparent;
|
|
154
|
+
color: var(--color-icon-secondary);
|
|
155
|
+
font-size: 1.25rem;
|
|
156
|
+
line-height: 1;
|
|
157
|
+
cursor: pointer;
|
|
158
|
+
}
|
|
159
|
+
.ax-dialog__close:hover {
|
|
160
|
+
background: var(--color-overlay-hover);
|
|
161
|
+
}
|
|
162
|
+
.ax-dialog__close:focus-visible {
|
|
163
|
+
outline: 2px solid var(--color-accent);
|
|
164
|
+
outline-offset: 2px;
|
|
165
|
+
}
|
|
166
|
+
.ax-dialog__body {
|
|
167
|
+
padding: 1rem 1.5rem 1.5rem;
|
|
168
|
+
overflow-y: auto;
|
|
169
|
+
min-width: 0;
|
|
170
|
+
}
|
|
171
|
+
.ax-dialog__footer {
|
|
172
|
+
display: flex;
|
|
173
|
+
justify-content: flex-end;
|
|
174
|
+
gap: 0.75rem;
|
|
175
|
+
padding: 0 1.5rem 1.5rem;
|
|
176
|
+
}
|
|
177
|
+
</style>
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Astryx Switch (spec: Switch.doc.mjs in @astryxdesign/core): a real
|
|
3
|
+
// checkbox input carrying role="switch", with the label/description
|
|
4
|
+
// arrangement from the spec. Track geometry is fixed (NO STRETCH).
|
|
5
|
+
let {
|
|
6
|
+
label,
|
|
7
|
+
checked = $bindable(false),
|
|
8
|
+
isDisabled = false,
|
|
9
|
+
isLabelHidden = false,
|
|
10
|
+
description,
|
|
11
|
+
name,
|
|
12
|
+
onchange,
|
|
13
|
+
class: className = '',
|
|
14
|
+
}: {
|
|
15
|
+
label: string;
|
|
16
|
+
checked?: boolean;
|
|
17
|
+
isDisabled?: boolean;
|
|
18
|
+
isLabelHidden?: boolean;
|
|
19
|
+
description?: string;
|
|
20
|
+
name?: string;
|
|
21
|
+
onchange?: (checked: boolean) => void;
|
|
22
|
+
class?: string;
|
|
23
|
+
} = $props();
|
|
24
|
+
|
|
25
|
+
const uid = $props.id();
|
|
26
|
+
const descId = `${uid}-desc`;
|
|
27
|
+
|
|
28
|
+
function handleChange(e: Event) {
|
|
29
|
+
checked = (e.currentTarget as HTMLInputElement).checked;
|
|
30
|
+
onchange?.(checked);
|
|
31
|
+
}
|
|
32
|
+
</script>
|
|
33
|
+
|
|
34
|
+
<div class="ax-switch {className}">
|
|
35
|
+
<label class="ax-switch__row">
|
|
36
|
+
<span class="ax-switch__control">
|
|
37
|
+
<input
|
|
38
|
+
type="checkbox"
|
|
39
|
+
role="switch"
|
|
40
|
+
class="ax-switch__input"
|
|
41
|
+
{name}
|
|
42
|
+
{checked}
|
|
43
|
+
disabled={isDisabled}
|
|
44
|
+
aria-checked={checked}
|
|
45
|
+
aria-describedby={description ? descId : undefined}
|
|
46
|
+
onchange={handleChange}
|
|
47
|
+
/>
|
|
48
|
+
<span class="ax-switch__track" aria-hidden="true">
|
|
49
|
+
<span class="ax-switch__thumb"></span>
|
|
50
|
+
</span>
|
|
51
|
+
</span>
|
|
52
|
+
<span class="ax-switch__label" class:ax-visually-hidden={isLabelHidden}>{label}</span>
|
|
53
|
+
</label>
|
|
54
|
+
{#if description}
|
|
55
|
+
<p class="ax-switch__description" id={descId}>{description}</p>
|
|
56
|
+
{/if}
|
|
57
|
+
</div>
|
|
58
|
+
|
|
59
|
+
<style>
|
|
60
|
+
.ax-switch {
|
|
61
|
+
font-family: var(--font-family-body);
|
|
62
|
+
min-width: 0;
|
|
63
|
+
}
|
|
64
|
+
.ax-switch__row {
|
|
65
|
+
display: inline-flex;
|
|
66
|
+
align-items: center;
|
|
67
|
+
gap: 0.75rem;
|
|
68
|
+
min-width: 0;
|
|
69
|
+
max-width: 100%;
|
|
70
|
+
cursor: pointer;
|
|
71
|
+
}
|
|
72
|
+
.ax-switch__control {
|
|
73
|
+
position: relative;
|
|
74
|
+
flex: none;
|
|
75
|
+
width: 2.75rem;
|
|
76
|
+
height: 1.5rem;
|
|
77
|
+
}
|
|
78
|
+
.ax-switch__input {
|
|
79
|
+
position: absolute;
|
|
80
|
+
inset: 0;
|
|
81
|
+
width: 100%;
|
|
82
|
+
height: 100%;
|
|
83
|
+
margin: 0;
|
|
84
|
+
opacity: 0;
|
|
85
|
+
cursor: pointer;
|
|
86
|
+
}
|
|
87
|
+
.ax-switch__input:disabled {
|
|
88
|
+
cursor: not-allowed;
|
|
89
|
+
}
|
|
90
|
+
/* Fixed 44x24 track; the thumb travels a fixed 20px. Content can never
|
|
91
|
+
resize the control (NO STRETCH). */
|
|
92
|
+
.ax-switch__track {
|
|
93
|
+
position: absolute;
|
|
94
|
+
inset: 0;
|
|
95
|
+
border-radius: var(--radius-full, 999px);
|
|
96
|
+
background: var(--color-border-emphasized);
|
|
97
|
+
transition: background-color var(--duration-fast, 120ms) ease;
|
|
98
|
+
pointer-events: none;
|
|
99
|
+
}
|
|
100
|
+
.ax-switch__thumb {
|
|
101
|
+
position: absolute;
|
|
102
|
+
top: 0.125rem;
|
|
103
|
+
inset-inline-start: 0.125rem;
|
|
104
|
+
width: 1.25rem;
|
|
105
|
+
height: 1.25rem;
|
|
106
|
+
border-radius: var(--radius-full, 999px);
|
|
107
|
+
background: var(--color-on-dark, #fff);
|
|
108
|
+
box-shadow: var(--shadow-low);
|
|
109
|
+
transition: translate var(--duration-fast, 120ms) ease;
|
|
110
|
+
}
|
|
111
|
+
.ax-switch__input:checked + .ax-switch__track {
|
|
112
|
+
background: var(--color-accent);
|
|
113
|
+
}
|
|
114
|
+
.ax-switch__input:checked + .ax-switch__track .ax-switch__thumb {
|
|
115
|
+
translate: 1.25rem 0;
|
|
116
|
+
}
|
|
117
|
+
.ax-switch__input:focus-visible + .ax-switch__track {
|
|
118
|
+
outline: 2px solid var(--color-accent);
|
|
119
|
+
outline-offset: 2px;
|
|
120
|
+
}
|
|
121
|
+
.ax-switch__input:disabled + .ax-switch__track {
|
|
122
|
+
opacity: 0.5;
|
|
123
|
+
}
|
|
124
|
+
.ax-switch__label {
|
|
125
|
+
min-width: 0;
|
|
126
|
+
overflow: hidden;
|
|
127
|
+
text-overflow: ellipsis;
|
|
128
|
+
white-space: nowrap;
|
|
129
|
+
font-size: var(--text-label-size, 0.875rem);
|
|
130
|
+
font-weight: var(--text-label-weight, 500);
|
|
131
|
+
color: var(--color-text-primary);
|
|
132
|
+
}
|
|
133
|
+
.ax-switch__description {
|
|
134
|
+
margin: 0.25rem 0 0 3.5rem;
|
|
135
|
+
font-size: var(--text-supporting-size, 0.8125rem);
|
|
136
|
+
color: var(--color-text-secondary);
|
|
137
|
+
}
|
|
138
|
+
.ax-visually-hidden {
|
|
139
|
+
position: absolute;
|
|
140
|
+
width: 1px;
|
|
141
|
+
height: 1px;
|
|
142
|
+
overflow: hidden;
|
|
143
|
+
clip-path: inset(50%);
|
|
144
|
+
white-space: nowrap;
|
|
145
|
+
}
|
|
146
|
+
</style>
|