svelte-ant-design 0.1.0-alpha.2 → 0.2.0
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/README.md +5 -3
- package/dist/Button/Button.svelte +13 -23
- package/dist/Switch/README.md +107 -0
- package/dist/Switch/Switch.svelte +316 -0
- package/dist/Switch/Switch.svelte.d.ts +43 -0
- package/dist/Switch/types.d.ts +7 -0
- package/dist/Switch/types.js +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -1
- package/dist/internal/options.d.ts +8 -0
- package/dist/internal/options.js +16 -0
- package/dist/internal/semantic.d.ts +14 -0
- package/dist/internal/semantic.js +32 -0
- package/dist/tokens.css +41 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ Ant Design.
|
|
|
18
18
|
|
|
19
19
|
## Status
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
Version `0.2.0`. Available components: `Button`, `Switch`. More components are planned.
|
|
22
22
|
|
|
23
23
|
## Installation
|
|
24
24
|
|
|
@@ -49,8 +49,10 @@ Import components by name. The package is ESM and declares `sideEffects: ["**/*.
|
|
|
49
49
|
bundler that supports tree shaking leaves out the components you do not use. This is checked with
|
|
50
50
|
Vite in `tests/packaging`; other bundlers are not covered.
|
|
51
51
|
|
|
52
|
-
See the [Button
|
|
53
|
-
|
|
52
|
+
See the [Button](https://prostreyd.github.io/ant-design-svelte/components/button/) and
|
|
53
|
+
[Switch](https://prostreyd.github.io/ant-design-svelte/components/switch/) pages of the documentation site for
|
|
54
|
+
examples and the API tables, or [`src/lib/Button/README.md`](./src/lib/Button/README.md) and
|
|
55
|
+
[`src/lib/Switch/README.md`](./src/lib/Switch/README.md) in the repository.
|
|
54
56
|
|
|
55
57
|
### Dark theme
|
|
56
58
|
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
} from './types.js';
|
|
15
15
|
import type { Snippet } from 'svelte';
|
|
16
16
|
import { isProduction } from '../internal/env.js';
|
|
17
|
+
import { resolveOption } from '../internal/options.js';
|
|
17
18
|
|
|
18
19
|
type ButtonProps = Omit<HTMLAttributes<HTMLElement>, 'children' | 'class' | 'onclick'> & {
|
|
19
20
|
/** Visual style of the button. */
|
|
@@ -94,31 +95,20 @@
|
|
|
94
95
|
const BUTTON_HTML_TYPES = ['button', 'submit', 'reset'] as const;
|
|
95
96
|
const BUTTON_ICON_PLACEMENTS = ['start', 'end'] as const;
|
|
96
97
|
|
|
97
|
-
/**
|
|
98
|
-
* Values outside the public contract throw a `RangeError` while developing, so the
|
|
99
|
-
* mistake is caught early. A production build falls back to the documented default
|
|
100
|
-
* instead, so a bad value (for example one coming from a CMS) cannot take down the page.
|
|
101
|
-
*/
|
|
102
|
-
function resolveOption<T extends string>(
|
|
103
|
-
name: string,
|
|
104
|
-
value: unknown,
|
|
105
|
-
allowed: readonly T[],
|
|
106
|
-
fallback: T
|
|
107
|
-
): T {
|
|
108
|
-
if (allowed.includes(value as T)) return value as T;
|
|
109
|
-
if (!isProduction()) {
|
|
110
|
-
throw new RangeError(`Button ${name} must be one of: ${allowed.join(', ')}.`);
|
|
111
|
-
}
|
|
112
|
-
return fallback;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
98
|
// One derived object, so every option is validated on each render, whichever element is used.
|
|
99
|
+
// Values outside the contract throw in development and fall back to the default in production.
|
|
116
100
|
const resolved = $derived({
|
|
117
|
-
type: resolveOption('type', type, BUTTON_TYPES, 'default'),
|
|
118
|
-
size: resolveOption('size', size, BUTTON_SIZES, 'medium'),
|
|
119
|
-
shape: resolveOption('shape', shape, BUTTON_SHAPES, 'default'),
|
|
120
|
-
htmlType: resolveOption('htmlType', htmlType, BUTTON_HTML_TYPES, 'button'),
|
|
121
|
-
iconPlacement: resolveOption(
|
|
101
|
+
type: resolveOption('Button', 'type', type, BUTTON_TYPES, 'default'),
|
|
102
|
+
size: resolveOption('Button', 'size', size, BUTTON_SIZES, 'medium'),
|
|
103
|
+
shape: resolveOption('Button', 'shape', shape, BUTTON_SHAPES, 'default'),
|
|
104
|
+
htmlType: resolveOption('Button', 'htmlType', htmlType, BUTTON_HTML_TYPES, 'button'),
|
|
105
|
+
iconPlacement: resolveOption(
|
|
106
|
+
'Button',
|
|
107
|
+
'iconPlacement',
|
|
108
|
+
iconPlacement,
|
|
109
|
+
BUTTON_ICON_PLACEMENTS,
|
|
110
|
+
'start'
|
|
111
|
+
)
|
|
122
112
|
});
|
|
123
113
|
|
|
124
114
|
let delayedLoading = $state(false);
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# Switch
|
|
2
|
+
|
|
3
|
+
A switch that turns one setting on or off, with Ant Design-inspired styles and a
|
|
4
|
+
Svelte 5 API. It renders a native `<button role="switch">`.
|
|
5
|
+
|
|
6
|
+
## Usage
|
|
7
|
+
|
|
8
|
+
Import the design tokens once in the application entry point:
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import 'svelte-ant-design/tokens.css';
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```svelte
|
|
15
|
+
<script lang="ts">
|
|
16
|
+
import { Switch } from 'svelte-ant-design';
|
|
17
|
+
|
|
18
|
+
let notifications = $state(true);
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<Switch bind:checked={notifications} aria-label="Notifications" />
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Text for the two states is a plain string; use a snippet for icons and other markup:
|
|
25
|
+
|
|
26
|
+
```svelte
|
|
27
|
+
<Switch checkedChildren="On" unCheckedChildren="Off" aria-label="Power" />
|
|
28
|
+
|
|
29
|
+
{#snippet check()}<svg aria-hidden="true">…</svg>{/snippet}
|
|
30
|
+
{#snippet cross()}<svg aria-hidden="true">…</svg>{/snippet}
|
|
31
|
+
|
|
32
|
+
<Switch checkedChildren={check} unCheckedChildren={cross} aria-label="Approved" />
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
When the parent has to decide whether a change goes through (for example after a request),
|
|
36
|
+
use a function binding (Svelte 5.9 or newer):
|
|
37
|
+
|
|
38
|
+
```svelte
|
|
39
|
+
<Switch
|
|
40
|
+
aria-label="Enable the feature"
|
|
41
|
+
bind:checked={
|
|
42
|
+
() => enabled,
|
|
43
|
+
(next) => {
|
|
44
|
+
if (accepted) enabled = next;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Props
|
|
51
|
+
|
|
52
|
+
| Prop | Type | Default | Description |
|
|
53
|
+
|------|------|---------|-------------|
|
|
54
|
+
| `checked` | `boolean` (`$bindable`) | `false` | Whether the switch is on. Use `bind:checked` for two-way binding; a plain value is the initial state. |
|
|
55
|
+
| `disabled` | `boolean` | `false` | Native `disabled`: not focusable, ignores clicks and keys. |
|
|
56
|
+
| `loading` | `boolean` | `false` | Shows a spinner in the handle and ignores activation. The switch stays focusable. |
|
|
57
|
+
| `size` | `'medium' \| 'small' \| 'default'` | `'medium'` | Size. `default` is a compatibility alias of `medium`. |
|
|
58
|
+
| `checkedChildren` | `string \| Snippet` | — | Content shown while on: text, or a snippet for icons and markup. Hidden from assistive technology. Stays mounted while off (see below). |
|
|
59
|
+
| `unCheckedChildren` | `string \| Snippet` | — | Content shown while off: text, or a snippet for icons and markup. Hidden from assistive technology. Stays mounted while on (see below). |
|
|
60
|
+
| `classNames` | `Partial<Record<'root' \| 'content' \| 'indicator', string>>` | — | Extra classes for the parts of the switch. |
|
|
61
|
+
| `styles` | `Partial<Record<'root' \| 'content' \| 'indicator', Record<string, string \| number>>>` | — | Inline styles for the parts. Names are kebab-case or custom properties; numbers are written as they are. |
|
|
62
|
+
| `class` | `string` | — | Additional CSS class appended to the root classes. |
|
|
63
|
+
| `style` | `string` | — | Inline style of the root; `styles.root` is written after it. |
|
|
64
|
+
| Native attributes | `HTMLAttributes<HTMLButtonElement>` | — | Forwarded to the `<button>` (`id`, `title`, `tabindex`, `aria-*`, `data-*`, other handlers). `type`, `role` and `aria-checked` belong to the component. The `loading` state takes priority over a consumer `aria-busy` and `aria-disabled`. |
|
|
65
|
+
|
|
66
|
+
Both contents stay mounted: the one of the other state is `visibility: hidden` in the same grid cell, so the switch always has the width of the longer one and nothing around it moves when it toggles (the shorter content is centered). A string is shown as text, never as HTML; an empty string counts as not given. Because both contents are mounted at once, avoid `id`s, `bind:`s and side effects inside a snippet.
|
|
67
|
+
|
|
68
|
+
Invalid values: `size` is checked. While developing (and in tests) an invalid value throws a
|
|
69
|
+
`RangeError`; in a production build the switch uses `medium` instead.
|
|
70
|
+
|
|
71
|
+
The parts: `root` is the `<button>`, `content` holds the on/off text, `indicator` is the handle
|
|
72
|
+
(it also holds the spinner). Inline `styles` win over the component's styles. Classes from
|
|
73
|
+
`classNames` and `class` have the same specificity as the component's own rules, so use `styles`
|
|
74
|
+
(or a more specific selector) when you need to override a property the component already sets.
|
|
75
|
+
|
|
76
|
+
## Events
|
|
77
|
+
|
|
78
|
+
| Callback | Payload | Description |
|
|
79
|
+
|-----------|---------|-------------|
|
|
80
|
+
| `onchange` | `(checked: boolean, event: MouseEvent)` | Called once per user toggle, after the state flips, with the new value. Not called when the parent changes `checked`, nor while disabled or loading. |
|
|
81
|
+
| `onclick` | `MouseEvent` | Native click callback. Runs before the state flips; `event.preventDefault()` cancels the toggle and `onchange`. Suppressed while disabled or loading. |
|
|
82
|
+
|
|
83
|
+
## Methods
|
|
84
|
+
|
|
85
|
+
`focus(options?: FocusOptions)` and `blur()`, available through `bind:this`.
|
|
86
|
+
|
|
87
|
+
## Accessibility
|
|
88
|
+
|
|
89
|
+
- A native `<button>` with `role="switch"` and `aria-checked`. Enter and Space toggle it. Arrow keys are not part of the pattern and do nothing.
|
|
90
|
+
- **A switch needs an accessible name:** `aria-label`, `aria-labelledby` or a `<label for>` that points at its `id`. The on/off text is `aria-hidden`, because as a name it would only repeat the state.
|
|
91
|
+
- A loading switch is not natively disabled: it keeps keyboard focus when loading starts, exposes `aria-busy="true"` and `aria-disabled="true"`, and ignores clicks, Enter and Space. A disabled switch is natively disabled.
|
|
92
|
+
- A visible `:focus-visible` outline from the design tokens, at least 3:1 against the page.
|
|
93
|
+
- The track and the handle keep at least 3:1 contrast, and the on/off text 4.5:1, in the light and dark themes and in the rest, hover, focus and pressed states. For that reason the off track is darker than in the Ant Design design, and a loading switch is not dimmed.
|
|
94
|
+
- Under `prefers-reduced-motion: reduce`, transitions are disabled and the spinner turns more slowly.
|
|
95
|
+
- Positions use logical properties, so the switch mirrors in right-to-left layouts.
|
|
96
|
+
|
|
97
|
+
## Scope decisions and differences from Ant Design
|
|
98
|
+
|
|
99
|
+
- `onclick` receives only the event (the React and Vue versions pass `(checked, event)`); read the new value in `onchange`.
|
|
100
|
+
- `defaultChecked`, `value` and `defaultValue` (React) and `checkedValue` / `unCheckedValue` (Vue) are not implemented: `bind:checked` covers them. `nzControl` (Angular) is replaced by a function binding. `autofocus` (Vue): call `focus()` after mount.
|
|
101
|
+
- The function form of `classNames` / `styles` (React) is not implemented: compute the object in `$derived`.
|
|
102
|
+
- See [`specs/switch.md`](../../../specs/switch.md) for the full comparison and the reasoning.
|
|
103
|
+
|
|
104
|
+
## Live examples
|
|
105
|
+
|
|
106
|
+
Interactive examples of every option, with their code and the API table, are on the
|
|
107
|
+
[documentation site](https://prostreyd.github.io/ant-design-svelte/components/switch/).
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
4
|
+
import type { SwitchClassNames, SwitchSize, SwitchStyles } from './types.js';
|
|
5
|
+
import { resolveOption } from '../internal/options.js';
|
|
6
|
+
import { classList, styleText } from '../internal/semantic.js';
|
|
7
|
+
|
|
8
|
+
type SwitchProps = Omit<
|
|
9
|
+
HTMLAttributes<HTMLButtonElement>,
|
|
10
|
+
'children' | 'class' | 'style' | 'onclick' | 'onchange' | 'role'
|
|
11
|
+
> & {
|
|
12
|
+
/** Whether the switch is on. Use `bind:checked` for two-way binding. */
|
|
13
|
+
checked?: boolean;
|
|
14
|
+
/** Called once per user toggle, after the state flips, with the new value. */
|
|
15
|
+
onchange?: (checked: boolean, event: MouseEvent) => void;
|
|
16
|
+
/** Native click callback. Runs before the state flips; `preventDefault()` cancels the toggle. */
|
|
17
|
+
onclick?: (event: MouseEvent) => void;
|
|
18
|
+
/** Whether the switch is disabled. */
|
|
19
|
+
disabled?: boolean;
|
|
20
|
+
/** Shows a spinner in the handle and ignores activation. The switch keeps focus. */
|
|
21
|
+
loading?: boolean;
|
|
22
|
+
/** Size. `default` is a compatibility alias of `medium`. */
|
|
23
|
+
size?: SwitchSize;
|
|
24
|
+
/**
|
|
25
|
+
* Content shown while the switch is on: plain text, or a snippet for icons and markup.
|
|
26
|
+
* Hidden from assistive technology. It stays mounted while the switch is off, so the
|
|
27
|
+
* switch keeps the width of the wider content.
|
|
28
|
+
*/
|
|
29
|
+
checkedChildren?: string | Snippet;
|
|
30
|
+
/**
|
|
31
|
+
* Content shown while the switch is off: plain text, or a snippet for icons and markup.
|
|
32
|
+
* Hidden from assistive technology. It stays mounted while the switch is on, so the
|
|
33
|
+
* switch keeps the width of the wider content.
|
|
34
|
+
*/
|
|
35
|
+
unCheckedChildren?: string | Snippet;
|
|
36
|
+
/** Extra classes for the `root`, `content` and `indicator` parts. */
|
|
37
|
+
classNames?: SwitchClassNames;
|
|
38
|
+
/** Inline styles for the `root`, `content` and `indicator` parts. */
|
|
39
|
+
styles?: SwitchStyles;
|
|
40
|
+
/** Additional CSS class appended to the root classes. */
|
|
41
|
+
class?: string;
|
|
42
|
+
/** Inline style of the root; `styles.root` is applied after it. */
|
|
43
|
+
style?: string;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
let {
|
|
47
|
+
checked = $bindable(false),
|
|
48
|
+
onchange,
|
|
49
|
+
onclick,
|
|
50
|
+
disabled = false,
|
|
51
|
+
loading = false,
|
|
52
|
+
size = 'medium',
|
|
53
|
+
checkedChildren,
|
|
54
|
+
unCheckedChildren,
|
|
55
|
+
classNames,
|
|
56
|
+
styles,
|
|
57
|
+
class: customClass,
|
|
58
|
+
style: customStyle,
|
|
59
|
+
'aria-busy': ariaBusy = undefined,
|
|
60
|
+
'aria-disabled': ariaDisabled = undefined,
|
|
61
|
+
...rest
|
|
62
|
+
}: SwitchProps = $props();
|
|
63
|
+
|
|
64
|
+
const SWITCH_SIZES = ['medium', 'small', 'default'] as const;
|
|
65
|
+
|
|
66
|
+
let element: HTMLButtonElement | undefined = $state();
|
|
67
|
+
|
|
68
|
+
// Validated on every render. Values outside the contract throw in development and fall
|
|
69
|
+
// back to `medium` in production. `default` is rendered exactly like `medium`.
|
|
70
|
+
const resolvedSize = $derived(
|
|
71
|
+
resolveOption('Switch', 'size', size, SWITCH_SIZES, 'medium') === 'small' ? 'small' : 'medium'
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
const isOn = $derived(Boolean(checked));
|
|
75
|
+
|
|
76
|
+
// A loading switch stays focusable: a natively disabled element drops focus, which would
|
|
77
|
+
// strand keyboard users the moment an action starts. Activation is blocked in `handleClick`
|
|
78
|
+
// and `aria-disabled` tells assistive technology it is unavailable. Attributes the consumer
|
|
79
|
+
// passes are kept unless the component's own state overrides them.
|
|
80
|
+
const isInactive = $derived(disabled || loading);
|
|
81
|
+
const busyValue = $derived(loading ? true : ariaBusy);
|
|
82
|
+
const inactiveValue = $derived(loading && !disabled ? true : ariaDisabled);
|
|
83
|
+
|
|
84
|
+
const rootClass = $derived(
|
|
85
|
+
classList(
|
|
86
|
+
'ads-switch',
|
|
87
|
+
`ads-switch-${resolvedSize}`,
|
|
88
|
+
isOn && 'ads-switch-checked',
|
|
89
|
+
loading && 'ads-switch-loading',
|
|
90
|
+
disabled && 'ads-switch-disabled',
|
|
91
|
+
customClass,
|
|
92
|
+
classNames?.root
|
|
93
|
+
)
|
|
94
|
+
);
|
|
95
|
+
const contentClass = $derived(classList('ads-switch-content', classNames?.content));
|
|
96
|
+
const indicatorClass = $derived(classList('ads-switch-indicator', classNames?.indicator));
|
|
97
|
+
|
|
98
|
+
const rootStyle = $derived(styleText(customStyle, styles?.root));
|
|
99
|
+
const contentStyle = $derived(styleText(styles?.content));
|
|
100
|
+
const indicatorStyle = $derived(styleText(styles?.indicator));
|
|
101
|
+
|
|
102
|
+
function handleClick(event: MouseEvent): void {
|
|
103
|
+
if (isInactive) return;
|
|
104
|
+
onclick?.(event);
|
|
105
|
+
if (event.defaultPrevented) return;
|
|
106
|
+
const next = !isOn;
|
|
107
|
+
checked = next;
|
|
108
|
+
onchange?.(next, event);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Moves focus to the switch. */
|
|
112
|
+
export function focus(options?: FocusOptions): void {
|
|
113
|
+
element?.focus(options);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Removes focus from the switch. */
|
|
117
|
+
export function blur(): void {
|
|
118
|
+
element?.blur();
|
|
119
|
+
}
|
|
120
|
+
</script>
|
|
121
|
+
|
|
122
|
+
<!-- A string is text (never HTML); anything else is a snippet. -->
|
|
123
|
+
{#snippet content(value: string | Snippet)}
|
|
124
|
+
{#if typeof value === 'string'}{value}{:else}{@render value()}{/if}
|
|
125
|
+
{/snippet}
|
|
126
|
+
|
|
127
|
+
<button
|
|
128
|
+
{...rest}
|
|
129
|
+
bind:this={element}
|
|
130
|
+
type="button"
|
|
131
|
+
role="switch"
|
|
132
|
+
class={rootClass}
|
|
133
|
+
style={rootStyle}
|
|
134
|
+
{disabled}
|
|
135
|
+
aria-checked={isOn}
|
|
136
|
+
aria-disabled={inactiveValue}
|
|
137
|
+
aria-busy={busyValue}
|
|
138
|
+
onclick={handleClick}
|
|
139
|
+
>
|
|
140
|
+
<!--
|
|
141
|
+
Both contents are mounted in one grid cell and the one of the other state is hidden, so the
|
|
142
|
+
cell is as wide as the wider of the two and the switch does not change width when it toggles.
|
|
143
|
+
-->
|
|
144
|
+
<span class={contentClass} style={contentStyle} aria-hidden="true">
|
|
145
|
+
{#if checkedChildren}
|
|
146
|
+
<span class="ads-switch-content-item" class:ads-switch-content-hidden={!isOn}>
|
|
147
|
+
{@render content(checkedChildren)}
|
|
148
|
+
</span>
|
|
149
|
+
{/if}
|
|
150
|
+
{#if unCheckedChildren}
|
|
151
|
+
<span class="ads-switch-content-item" class:ads-switch-content-hidden={isOn}>
|
|
152
|
+
{@render content(unCheckedChildren)}
|
|
153
|
+
</span>
|
|
154
|
+
{/if}
|
|
155
|
+
</span>
|
|
156
|
+
<span class={indicatorClass} style={indicatorStyle} aria-hidden="true">
|
|
157
|
+
{#if loading}<span class="ads-switch-spinner"></span>{/if}
|
|
158
|
+
</span>
|
|
159
|
+
</button>
|
|
160
|
+
|
|
161
|
+
<style>
|
|
162
|
+
/*
|
|
163
|
+
* The size and the state only set the private `--_*` variables below; the rules that
|
|
164
|
+
* paint the track read them, so size, checked, hover and pressed combine without
|
|
165
|
+
* fighting over specificity. The track and handle colors come from the `--ads-switch-*`
|
|
166
|
+
* tokens, which keep WCAG contrast in the light and dark themes. Positions use logical
|
|
167
|
+
* properties, so the switch mirrors in right-to-left layouts.
|
|
168
|
+
*/
|
|
169
|
+
.ads-switch {
|
|
170
|
+
--_height: var(--ads-switch-height);
|
|
171
|
+
--_min-width: var(--ads-switch-min-width);
|
|
172
|
+
--_handle: var(--ads-switch-handle-size);
|
|
173
|
+
--_margin-min: var(--ads-switch-content-margin-min);
|
|
174
|
+
--_margin-max: var(--ads-switch-content-margin-max);
|
|
175
|
+
--_font-size: var(--ads-switch-font-size);
|
|
176
|
+
--_spinner-border: var(--ads-switch-spinner-border-width);
|
|
177
|
+
--_track: var(--ads-switch-track-off);
|
|
178
|
+
--_track-hover: var(--ads-switch-track-off-hover);
|
|
179
|
+
--_track-active: var(--ads-switch-track-off-active);
|
|
180
|
+
|
|
181
|
+
position: relative;
|
|
182
|
+
display: inline-flex;
|
|
183
|
+
align-items: center;
|
|
184
|
+
box-sizing: border-box;
|
|
185
|
+
min-width: var(--_min-width);
|
|
186
|
+
height: var(--_height);
|
|
187
|
+
margin: 0;
|
|
188
|
+
padding: 0;
|
|
189
|
+
border: 0;
|
|
190
|
+
border-radius: var(--_height);
|
|
191
|
+
background-color: var(--_track);
|
|
192
|
+
color: var(--ads-switch-content-color);
|
|
193
|
+
font-family: var(--ads-font-family);
|
|
194
|
+
font-size: var(--_font-size);
|
|
195
|
+
line-height: var(--_height);
|
|
196
|
+
vertical-align: middle;
|
|
197
|
+
cursor: pointer;
|
|
198
|
+
user-select: none;
|
|
199
|
+
touch-action: manipulation;
|
|
200
|
+
transition: background-color var(--ads-motion-duration) var(--ads-motion-ease);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
.ads-switch-small {
|
|
204
|
+
--_height: var(--ads-switch-height-sm);
|
|
205
|
+
--_min-width: var(--ads-switch-min-width-sm);
|
|
206
|
+
--_handle: var(--ads-switch-handle-size-sm);
|
|
207
|
+
--_margin-min: var(--ads-switch-content-margin-min-sm);
|
|
208
|
+
--_margin-max: var(--ads-switch-content-margin-max-sm);
|
|
209
|
+
--_font-size: var(--ads-switch-font-size-sm);
|
|
210
|
+
--_spinner-border: 1.5px;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
.ads-switch-checked {
|
|
214
|
+
--_track: var(--ads-switch-track-on);
|
|
215
|
+
--_track-hover: var(--ads-switch-track-on-hover);
|
|
216
|
+
--_track-active: var(--ads-switch-track-on-active);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
.ads-switch:focus-visible {
|
|
220
|
+
outline: var(--ads-focus-outline-width) solid var(--ads-color-focus-outline);
|
|
221
|
+
outline-offset: var(--ads-focus-outline-offset);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
.ads-switch:not(.ads-switch-disabled):not(.ads-switch-loading):hover {
|
|
225
|
+
background-color: var(--_track-hover);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
.ads-switch:not(.ads-switch-disabled):not(.ads-switch-loading):active {
|
|
229
|
+
background-color: var(--_track-active);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
.ads-switch-content {
|
|
233
|
+
display: grid;
|
|
234
|
+
flex: 1;
|
|
235
|
+
align-content: center;
|
|
236
|
+
box-sizing: border-box;
|
|
237
|
+
height: 100%;
|
|
238
|
+
overflow: hidden;
|
|
239
|
+
border-radius: inherit;
|
|
240
|
+
padding-inline: var(--_margin-max) var(--_margin-min);
|
|
241
|
+
white-space: nowrap;
|
|
242
|
+
transition: padding var(--ads-motion-duration) var(--ads-motion-ease);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
.ads-switch-checked .ads-switch-content {
|
|
246
|
+
padding-inline: var(--_margin-min) var(--_margin-max);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/* Both contents share one cell; the shorter one is centered in the width of the wider one. */
|
|
250
|
+
.ads-switch-content-item {
|
|
251
|
+
grid-area: 1 / 1;
|
|
252
|
+
justify-self: center;
|
|
253
|
+
min-width: 0;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
.ads-switch-content-hidden {
|
|
257
|
+
visibility: hidden;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
.ads-switch-indicator {
|
|
261
|
+
position: absolute;
|
|
262
|
+
top: var(--ads-switch-padding);
|
|
263
|
+
inset-inline-start: var(--ads-switch-padding);
|
|
264
|
+
display: flex;
|
|
265
|
+
align-items: center;
|
|
266
|
+
justify-content: center;
|
|
267
|
+
width: var(--_handle);
|
|
268
|
+
height: var(--_handle);
|
|
269
|
+
border-radius: 50%;
|
|
270
|
+
background-color: var(--ads-switch-handle-bg);
|
|
271
|
+
box-shadow: var(--ads-switch-handle-shadow);
|
|
272
|
+
transition: inset-inline-start var(--ads-motion-duration) var(--ads-motion-ease);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
.ads-switch-checked .ads-switch-indicator {
|
|
276
|
+
inset-inline-start: calc(100% - var(--_handle) - var(--ads-switch-padding));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
.ads-switch-spinner {
|
|
280
|
+
box-sizing: border-box;
|
|
281
|
+
width: calc(var(--_handle) * 0.6);
|
|
282
|
+
height: calc(var(--_handle) * 0.6);
|
|
283
|
+
border: var(--_spinner-border) solid var(--_track);
|
|
284
|
+
border-top-color: transparent;
|
|
285
|
+
border-radius: 50%;
|
|
286
|
+
animation: ads-switch-spin var(--ads-motion-duration-spinner) linear infinite;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
.ads-switch-loading {
|
|
290
|
+
cursor: not-allowed;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
.ads-switch-disabled {
|
|
294
|
+
cursor: not-allowed;
|
|
295
|
+
opacity: var(--ads-switch-disabled-opacity);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
@keyframes ads-switch-spin {
|
|
299
|
+
to {
|
|
300
|
+
transform: rotate(360deg);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/* Honour the OS "reduce motion" setting: no transitions, a slower spinner. */
|
|
305
|
+
@media (prefers-reduced-motion: reduce) {
|
|
306
|
+
.ads-switch,
|
|
307
|
+
.ads-switch-content,
|
|
308
|
+
.ads-switch-indicator {
|
|
309
|
+
transition: none;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
.ads-switch-spinner {
|
|
313
|
+
animation-duration: var(--ads-motion-duration-spinner-reduced);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
</style>
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Snippet } from 'svelte';
|
|
2
|
+
import type { HTMLAttributes } from 'svelte/elements';
|
|
3
|
+
import type { SwitchClassNames, SwitchSize, SwitchStyles } from './types.js';
|
|
4
|
+
type SwitchProps = Omit<HTMLAttributes<HTMLButtonElement>, 'children' | 'class' | 'style' | 'onclick' | 'onchange' | 'role'> & {
|
|
5
|
+
/** Whether the switch is on. Use `bind:checked` for two-way binding. */
|
|
6
|
+
checked?: boolean;
|
|
7
|
+
/** Called once per user toggle, after the state flips, with the new value. */
|
|
8
|
+
onchange?: (checked: boolean, event: MouseEvent) => void;
|
|
9
|
+
/** Native click callback. Runs before the state flips; `preventDefault()` cancels the toggle. */
|
|
10
|
+
onclick?: (event: MouseEvent) => void;
|
|
11
|
+
/** Whether the switch is disabled. */
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
/** Shows a spinner in the handle and ignores activation. The switch keeps focus. */
|
|
14
|
+
loading?: boolean;
|
|
15
|
+
/** Size. `default` is a compatibility alias of `medium`. */
|
|
16
|
+
size?: SwitchSize;
|
|
17
|
+
/**
|
|
18
|
+
* Content shown while the switch is on: plain text, or a snippet for icons and markup.
|
|
19
|
+
* Hidden from assistive technology. It stays mounted while the switch is off, so the
|
|
20
|
+
* switch keeps the width of the wider content.
|
|
21
|
+
*/
|
|
22
|
+
checkedChildren?: string | Snippet;
|
|
23
|
+
/**
|
|
24
|
+
* Content shown while the switch is off: plain text, or a snippet for icons and markup.
|
|
25
|
+
* Hidden from assistive technology. It stays mounted while the switch is on, so the
|
|
26
|
+
* switch keeps the width of the wider content.
|
|
27
|
+
*/
|
|
28
|
+
unCheckedChildren?: string | Snippet;
|
|
29
|
+
/** Extra classes for the `root`, `content` and `indicator` parts. */
|
|
30
|
+
classNames?: SwitchClassNames;
|
|
31
|
+
/** Inline styles for the `root`, `content` and `indicator` parts. */
|
|
32
|
+
styles?: SwitchStyles;
|
|
33
|
+
/** Additional CSS class appended to the root classes. */
|
|
34
|
+
class?: string;
|
|
35
|
+
/** Inline style of the root; `styles.root` is applied after it. */
|
|
36
|
+
style?: string;
|
|
37
|
+
};
|
|
38
|
+
declare const Switch: import("svelte").Component<SwitchProps, {
|
|
39
|
+
focus: (options?: FocusOptions) => void;
|
|
40
|
+
blur: () => void;
|
|
41
|
+
}, "checked">;
|
|
42
|
+
type Switch = ReturnType<typeof Switch>;
|
|
43
|
+
export default Switch;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SemanticStyle } from '../internal/semantic.js';
|
|
2
|
+
/** `default` is a compatibility alias of `medium` (the Vue and Angular name). */
|
|
3
|
+
export type SwitchSize = 'medium' | 'small' | 'default';
|
|
4
|
+
/** The named parts of a Switch that `classNames` and `styles` address. */
|
|
5
|
+
export type SwitchSemanticName = 'root' | 'content' | 'indicator';
|
|
6
|
+
export type SwitchClassNames = Partial<Record<SwitchSemanticName, string>>;
|
|
7
|
+
export type SwitchStyles = Partial<Record<SwitchSemanticName, SemanticStyle>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export declare const VERSION = "0.
|
|
1
|
+
export declare const VERSION = "0.2.0";
|
|
2
2
|
export { default as Button } from './Button/Button.svelte';
|
|
3
|
+
export { default as Switch } from './Switch/Switch.svelte';
|
|
4
|
+
export type { SwitchClassNames, SwitchSemanticName, SwitchSize, SwitchStyles } from './Switch/types.js';
|
|
3
5
|
export type { ButtonHTMLType, ButtonIconPlacement, ButtonLoading, ButtonShape, ButtonSize, ButtonType } from './Button/types.js';
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates an enum-like prop against its contract.
|
|
3
|
+
*
|
|
4
|
+
* Values outside the contract throw a `RangeError` while developing, so the mistake is
|
|
5
|
+
* caught early. A production build falls back to the documented default instead, so a bad
|
|
6
|
+
* value (for example one coming from a CMS) cannot take down the page.
|
|
7
|
+
*/
|
|
8
|
+
export declare function resolveOption<T extends string>(component: string, name: string, value: unknown, allowed: readonly T[], fallback: T): T;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { isProduction } from './env.js';
|
|
2
|
+
/**
|
|
3
|
+
* Validates an enum-like prop against its contract.
|
|
4
|
+
*
|
|
5
|
+
* Values outside the contract throw a `RangeError` while developing, so the mistake is
|
|
6
|
+
* caught early. A production build falls back to the documented default instead, so a bad
|
|
7
|
+
* value (for example one coming from a CMS) cannot take down the page.
|
|
8
|
+
*/
|
|
9
|
+
export function resolveOption(component, name, value, allowed, fallback) {
|
|
10
|
+
if (allowed.includes(value))
|
|
11
|
+
return value;
|
|
12
|
+
if (!isProduction()) {
|
|
13
|
+
throw new RangeError(`${component} ${name} must be one of: ${allowed.join(', ')}.`);
|
|
14
|
+
}
|
|
15
|
+
return fallback;
|
|
16
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for the `classNames` / `styles` props, which address the named parts of a
|
|
3
|
+
* component ("semantic DOM"). Kept free of Svelte so they are easy to test and to share.
|
|
4
|
+
*/
|
|
5
|
+
/** Inline styles of one part: CSS property names in kebab-case (or custom properties) to values. */
|
|
6
|
+
export type SemanticStyle = Record<string, string | number>;
|
|
7
|
+
/** Joins the class names that are set, in order. */
|
|
8
|
+
export declare function classList(...parts: ReadonlyArray<string | false | null | undefined>): string;
|
|
9
|
+
/**
|
|
10
|
+
* Builds the text of a `style` attribute from strings and style objects, in order, so a
|
|
11
|
+
* later one wins. Numbers are written as they are (no automatic `px`), property names are
|
|
12
|
+
* not converted. Returns `undefined` when nothing is left, so Svelte drops the attribute.
|
|
13
|
+
*/
|
|
14
|
+
export declare function styleText(...styles: ReadonlyArray<string | SemanticStyle | false | null | undefined>): string | undefined;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for the `classNames` / `styles` props, which address the named parts of a
|
|
3
|
+
* component ("semantic DOM"). Kept free of Svelte so they are easy to test and to share.
|
|
4
|
+
*/
|
|
5
|
+
/** Joins the class names that are set, in order. */
|
|
6
|
+
export function classList(...parts) {
|
|
7
|
+
return parts.filter(Boolean).join(' ');
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Builds the text of a `style` attribute from strings and style objects, in order, so a
|
|
11
|
+
* later one wins. Numbers are written as they are (no automatic `px`), property names are
|
|
12
|
+
* not converted. Returns `undefined` when nothing is left, so Svelte drops the attribute.
|
|
13
|
+
*/
|
|
14
|
+
export function styleText(...styles) {
|
|
15
|
+
const declarations = [];
|
|
16
|
+
for (const style of styles) {
|
|
17
|
+
if (!style)
|
|
18
|
+
continue;
|
|
19
|
+
if (typeof style === 'string') {
|
|
20
|
+
const text = style.trim().replace(/;+$/, '');
|
|
21
|
+
if (text)
|
|
22
|
+
declarations.push(text);
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
for (const [property, value] of Object.entries(style)) {
|
|
26
|
+
if (value === undefined || value === null || value === '')
|
|
27
|
+
continue;
|
|
28
|
+
declarations.push(`${property}: ${value}`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return declarations.length > 0 ? declarations.join('; ') : undefined;
|
|
32
|
+
}
|
package/dist/tokens.css
CHANGED
|
@@ -79,6 +79,38 @@
|
|
|
79
79
|
--ads-motion-duration-spinner: 1s;
|
|
80
80
|
--ads-motion-duration-spinner-reduced: 2.5s;
|
|
81
81
|
--ads-motion-ease: cubic-bezier(0.645, 0.045, 0.355, 1);
|
|
82
|
+
|
|
83
|
+
/* Switch */
|
|
84
|
+
--ads-switch-height: 22px;
|
|
85
|
+
--ads-switch-height-sm: 16px;
|
|
86
|
+
--ads-switch-min-width: 44px;
|
|
87
|
+
--ads-switch-min-width-sm: 28px;
|
|
88
|
+
--ads-switch-handle-size: 18px;
|
|
89
|
+
--ads-switch-handle-size-sm: 12px;
|
|
90
|
+
--ads-switch-padding: 2px;
|
|
91
|
+
--ads-switch-content-margin-min: 9px;
|
|
92
|
+
--ads-switch-content-margin-max: 24px;
|
|
93
|
+
--ads-switch-content-margin-min-sm: 6px;
|
|
94
|
+
--ads-switch-content-margin-max-sm: 18px;
|
|
95
|
+
--ads-switch-font-size: 12px;
|
|
96
|
+
--ads-switch-font-size-sm: 10px;
|
|
97
|
+
--ads-switch-spinner-border-width: 2px;
|
|
98
|
+
--ads-switch-handle-shadow: 0 2px 4px 0 rgb(0 35 11 / 20%);
|
|
99
|
+
--ads-switch-disabled-opacity: 0.4;
|
|
100
|
+
|
|
101
|
+
/*
|
|
102
|
+
* Switch colors. Both track states keep 3:1 against the page background (WCAG 1.4.11),
|
|
103
|
+
* and the handle and the on/off text keep their contrast against the track (3:1 and 4.5:1)
|
|
104
|
+
* in every state, so the unchecked track is darker than the default Ant Design gray.
|
|
105
|
+
*/
|
|
106
|
+
--ads-switch-track-off: #737373;
|
|
107
|
+
--ads-switch-track-off-hover: #666666;
|
|
108
|
+
--ads-switch-track-off-active: #595959;
|
|
109
|
+
--ads-switch-track-on: #1668dc;
|
|
110
|
+
--ads-switch-track-on-hover: #0958d9;
|
|
111
|
+
--ads-switch-track-on-active: #003eb3;
|
|
112
|
+
--ads-switch-handle-bg: #ffffff;
|
|
113
|
+
--ads-switch-content-color: #ffffff;
|
|
82
114
|
}
|
|
83
115
|
|
|
84
116
|
:root[data-theme='dark'] {
|
|
@@ -120,4 +152,13 @@
|
|
|
120
152
|
--ads-color-bg-container-disabled: rgb(255 255 255 / 8%);
|
|
121
153
|
--ads-color-bg-text-hover: rgb(255 255 255 / 12%);
|
|
122
154
|
|
|
155
|
+
/* In the dark theme a lighter track would lose contrast against the white handle, so hover and pressed go darker. */
|
|
156
|
+
--ads-switch-track-off: #8c8c8c;
|
|
157
|
+
--ads-switch-track-off-hover: #858585;
|
|
158
|
+
--ads-switch-track-off-active: #808080;
|
|
159
|
+
--ads-switch-track-on: #3a8aff;
|
|
160
|
+
--ads-switch-track-on-hover: #3283f5;
|
|
161
|
+
--ads-switch-track-on-active: #2f7ff0;
|
|
162
|
+
--ads-switch-handle-bg: #ffffff;
|
|
163
|
+
--ads-switch-content-color: #141414;
|
|
123
164
|
}
|