svelte-ant-design 0.1.0-alpha.2 → 0.2.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/README.md CHANGED
@@ -18,7 +18,7 @@ Ant Design.
18
18
 
19
19
  ## Status
20
20
 
21
- Early alpha (`0.1.0-alpha.2`). Available components: `Button`. More components are planned.
21
+ Version `0.2.1`. Available components: `Button`, `Switch`. More components are planned.
22
22
 
23
23
  ## Installation
24
24
 
@@ -49,8 +49,25 @@ 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 page](https://prostreyd.github.io/ant-design-svelte/components/button/) of the documentation
53
- site for examples and the API table, or [`src/lib/Button/README.md`](./src/lib/Button/README.md) in the repository.
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.
56
+
57
+ ### Colors and contrast
58
+
59
+ In the light theme the colors are the values of the Ant Design token documentation, so the components look
60
+ like Ant Design. Those values do not reach WCAG AA for text: white on the primary fill is 4.10:1 and on the
61
+ danger fill 3.27:1 (4.5:1 is needed), and the off track of a switch is 1.84:1 against a white page (3:1 is
62
+ needed). For a contrast guarantee import one more file after the tokens:
63
+
64
+ ```ts
65
+ import 'svelte-ant-design/tokens.css';
66
+ import 'svelte-ant-design/tokens-accessible.css';
67
+ ```
68
+
69
+ It darkens the fills, the link and text colors and the switch tracks; every text pair is then at least 4.5:1.
70
+ The dark theme needs no extra file. The contrast tests run with the accessible file.
54
71
 
55
72
  ### Dark theme
56
73
 
@@ -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('iconPlacement', iconPlacement, BUTTON_ICON_PLACEMENTS, 'start')
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);
@@ -324,17 +314,22 @@
324
314
  border-style: dashed;
325
315
  }
326
316
 
317
+ /* A text button keeps its text color on hover and pressed and only gets a background. */
327
318
  .ads-btn-text {
319
+ --_fg-hover: var(--ads-color-text);
320
+ --_fg-active: var(--ads-color-text);
328
321
  --_bg: transparent;
329
- --_bg-hover: var(--ads-color-bg-text-hover);
330
- --_bg-active: var(--ads-color-bg-text-hover);
322
+ --_bg-hover: var(--ads-button-text-hover-bg);
323
+ --_bg-active: var(--ads-button-text-active-bg);
331
324
  --_border: transparent;
332
325
  --_border-hover: transparent;
333
326
  --_border-active: transparent;
334
327
  }
335
328
 
336
329
  .ads-btn-link {
337
- --_fg: var(--ads-color-primary-text);
330
+ --_fg: var(--ads-color-link);
331
+ --_fg-hover: var(--ads-color-link-hover);
332
+ --_fg-active: var(--ads-color-link-active);
338
333
  --_bg: transparent;
339
334
  --_border: transparent;
340
335
  --_border-hover: transparent;
@@ -348,6 +343,13 @@
348
343
  --_fg: var(--ads-color-danger-text);
349
344
  }
350
345
 
346
+ /* More specific than the text and link rules above, so a danger one keeps its danger states. */
347
+ .ads-btn-danger.ads-btn-text,
348
+ .ads-btn-danger.ads-btn-link {
349
+ --_fg-hover: var(--ads-color-danger-text-hover);
350
+ --_fg-active: var(--ads-color-danger-text-active);
351
+ }
352
+
351
353
  .ads-btn-danger.ads-btn-primary {
352
354
  --_bg: var(--ads-color-danger-solid);
353
355
  --_bg-hover: var(--ads-color-danger-solid-hover);
@@ -82,6 +82,22 @@ the delay expires cancels the pending timer.
82
82
  - States/modifiers: `disabled`, `loading`, `danger`, `block`, `ghost`.
83
83
  - Loading supports immediate and delayed presentation.
84
84
 
85
+ ## Colors
86
+
87
+ In the light theme the default tokens are the values of the Ant Design token documentation, so the button
88
+ looks like Ant Design: primary `#1677ff` (hover `#4096ff`, pressed `#0958d9`), danger `#ff4d4f`, links
89
+ `#1677ff` (hover `#69b1ff`). A text button keeps its text color on hover and gets the background
90
+ `rgba(0, 0, 0, 0.04)`. These colors do **not** reach WCAG AA for text: white on `#1677ff` is 4.10:1 and on the
91
+ danger fill 3.27:1 (4.5:1 is needed). For a contrast guarantee import the optional file after the tokens:
92
+
93
+ ```ts
94
+ import 'svelte-ant-design/tokens.css';
95
+ import 'svelte-ant-design/tokens-accessible.css';
96
+ ```
97
+
98
+ It darkens the `-solid`, `-text` and link tokens (white on the primary fill becomes 5.19:1). The dark theme
99
+ needs no extra file.
100
+
85
101
  ## Accessibility
86
102
 
87
103
  - Uses native `<button>` and `<a>` elements and forwards their supported attributes.
@@ -90,7 +106,7 @@ the delay expires cancels the pending timer.
90
106
  - Disabled/loading anchors are rendered without `href`, with `role="link"`, `aria-disabled="true"`, and `tabindex="-1"`, so they cannot be opened by click, middle click, or the context menu.
91
107
  - Both element types expose `aria-busy="true"` while loading.
92
108
  - The spinner and icon snippet are hidden from assistive technology. An icon-only button must receive an accessible name, for example `aria-label="Close"`.
93
- - A visible `:focus-visible` outline is provided using design tokens. The outline has at least 3:1 contrast against the page, and text in every type, `danger`, and `ghost` combination keeps at least 4.5:1 in the light and dark themes, including hover, focus, and pressed states.
109
+ - A visible `:focus-visible` outline is provided using design tokens. The outline has at least 3:1 contrast against the page. Text in every type, `danger`, and `ghost` combination keeps at least 4.5:1 in the dark theme, and in the light theme when `tokens-accessible.css` is imported (see Colors), including hover, focus, and pressed states.
94
110
  - Under `prefers-reduced-motion: reduce`, color transitions are disabled and the spinner turns more slowly.
95
111
 
96
112
  ## Scope decisions and limitations
@@ -0,0 +1,124 @@
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
+ ## Colors
88
+
89
+ In the light theme the default tokens are the values of the Ant Design token documentation: the on track
90
+ `#1677ff` (hover `#4096ff`), the off track `rgba(0, 0, 0, 0.25)` (hover `rgba(0, 0, 0, 0.45)`), a white handle and
91
+ a loading switch dimmed to `0.65`. The documentation names no separate pressed color, so pressed equals hover.
92
+ These colors do **not** keep the contrast of the accessible set: the off track is 1.84:1 against a white page
93
+ (3:1 is needed) and the white text on the on track 4.10:1 (4.5:1 is needed). For a contrast guarantee import the
94
+ optional file after the tokens:
95
+
96
+ ```ts
97
+ import 'svelte-ant-design/tokens.css';
98
+ import 'svelte-ant-design/tokens-accessible.css';
99
+ ```
100
+
101
+ It restores a darker off track (`#737373`), a darker on track (`#1668dc`) and no dimming while loading. The dark
102
+ theme needs no extra file.
103
+
104
+ ## Accessibility
105
+
106
+ - 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.
107
+ - **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.
108
+ - 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.
109
+ - A visible `:focus-visible` outline from the design tokens, at least 3:1 against the page.
110
+ - The track and the handle keep at least 3:1 contrast, and the on/off text 4.5:1, in every state: in the dark theme, and in the light theme when `tokens-accessible.css` is imported (see Colors). For that the accessible light set has a darker off track and does not dim a loading switch.
111
+ - Under `prefers-reduced-motion: reduce`, transitions are disabled and the spinner turns more slowly.
112
+ - Positions use logical properties, so the switch mirrors in right-to-left layouts.
113
+
114
+ ## Scope decisions and differences from Ant Design
115
+
116
+ - `onclick` receives only the event (the React and Vue versions pass `(checked, event)`); read the new value in `onchange`.
117
+ - `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.
118
+ - The function form of `classNames` / `styles` (React) is not implemented: compute the object in `$derived`.
119
+ - See [`specs/switch.md`](../../../specs/switch.md) for the full comparison and the reasoning.
120
+
121
+ ## Live examples
122
+
123
+ Interactive examples of every option, with their code and the API table, are on the
124
+ [documentation site](https://prostreyd.github.io/ant-design-svelte/components/switch/).
@@ -0,0 +1,317 @@
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
+ opacity: var(--ads-switch-loading-opacity);
292
+ }
293
+
294
+ .ads-switch-disabled {
295
+ cursor: not-allowed;
296
+ opacity: var(--ads-switch-disabled-opacity);
297
+ }
298
+
299
+ @keyframes ads-switch-spin {
300
+ to {
301
+ transform: rotate(360deg);
302
+ }
303
+ }
304
+
305
+ /* Honour the OS "reduce motion" setting: no transitions, a slower spinner. */
306
+ @media (prefers-reduced-motion: reduce) {
307
+ .ads-switch,
308
+ .ads-switch-content,
309
+ .ads-switch-indicator {
310
+ transition: none;
311
+ }
312
+
313
+ .ads-switch-spinner {
314
+ animation-duration: var(--ads-motion-duration-spinner-reduced);
315
+ }
316
+ }
317
+ </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.0-alpha.2";
1
+ export declare const VERSION = "0.2.1";
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
@@ -1,3 +1,4 @@
1
1
  // Public API of the library. Components are re-exported from here.
2
- export const VERSION = '0.1.0-alpha.2';
2
+ export const VERSION = '0.2.1';
3
3
  export { default as Button } from './Button/Button.svelte';
4
+ export { default as Switch } from './Switch/Switch.svelte';
@@ -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
+ }
@@ -0,0 +1,42 @@
1
+ /*
2
+ * Accessible light-theme colors (optional).
3
+ *
4
+ * tokens.css uses the values of the Ant Design token documentation, which do not reach WCAG AA for
5
+ * text: white on the primary fill is 4.10:1 and on the danger fill 3.27:1 (4.5:1 is needed), and the
6
+ * off track of a Switch is 1.84:1 against a white page (3:1 is needed). Import this file AFTER tokens.css
7
+ * to get darker values for the `-solid`, `-text` and link tokens and for the Switch:
8
+ *
9
+ * import 'svelte-ant-design/tokens.css';
10
+ * import 'svelte-ant-design/tokens-accessible.css';
11
+ *
12
+ * Every text pair is at least 4.5:1 and every track and handle at least 3:1, in every state. The dark
13
+ * theme needs no override and is not touched: the selector below does not apply when
14
+ * `data-theme="dark"` is set. The selector is more specific than the `:root` of tokens.css, so the
15
+ * order of the two imports does not matter.
16
+ */
17
+
18
+ :root:not([data-theme='dark']) {
19
+ --ads-color-primary-solid: #1668dc;
20
+ --ads-color-primary-solid-hover: #0958d9;
21
+ --ads-color-primary-solid-active: #003eb3;
22
+ --ads-color-primary-text: #1668dc;
23
+ --ads-color-primary-text-hover: #0958d9;
24
+ --ads-color-primary-text-active: #003eb3;
25
+ --ads-color-danger-solid: #d9363e;
26
+ --ads-color-danger-solid-hover: #cf1322;
27
+ --ads-color-danger-solid-active: #a8071a;
28
+ --ads-color-danger-text: #cf1322;
29
+ --ads-color-danger-text-hover: #a8071a;
30
+ --ads-color-danger-text-active: #820014;
31
+ --ads-color-link: #1668dc;
32
+ --ads-color-link-hover: #0958d9;
33
+ --ads-color-link-active: #003eb3;
34
+
35
+ --ads-switch-track-off: #737373;
36
+ --ads-switch-track-off-hover: #666666;
37
+ --ads-switch-track-off-active: #595959;
38
+ --ads-switch-track-on: #1668dc;
39
+ --ads-switch-track-on-hover: #0958d9;
40
+ --ads-switch-track-on-active: #003eb3;
41
+ --ads-switch-loading-opacity: 1;
42
+ }
package/dist/tokens.css CHANGED
@@ -17,21 +17,27 @@
17
17
  --ads-color-danger-active: #d9363e;
18
18
 
19
19
  /*
20
- * Accessible variants. `-solid` is a fill behind `--ads-color-text-inverse`;
21
- * `-text` is a foreground on the page background. All pairs reach WCAG AA (4.5:1).
20
+ * `-solid` is a fill behind `--ads-color-text-inverse`; `-text` and the link tokens are a foreground
21
+ * on the page background. In the light theme the values are the ones of the Ant Design token
22
+ * documentation, so the library looks like Ant Design. They do not reach WCAG AA (4.5:1) for
23
+ * text: import `tokens-accessible.css` after this file for values that do. The dark theme below
24
+ * keeps AA without it.
22
25
  */
23
- --ads-color-primary-solid: #1668dc;
24
- --ads-color-primary-solid-hover: #0958d9;
25
- --ads-color-primary-solid-active: #003eb3;
26
- --ads-color-primary-text: #1668dc;
27
- --ads-color-primary-text-hover: #0958d9;
28
- --ads-color-primary-text-active: #003eb3;
29
- --ads-color-danger-solid: #d9363e;
30
- --ads-color-danger-solid-hover: #cf1322;
31
- --ads-color-danger-solid-active: #a8071a;
32
- --ads-color-danger-text: #cf1322;
33
- --ads-color-danger-text-hover: #a8071a;
34
- --ads-color-danger-text-active: #820014;
26
+ --ads-color-primary-solid: #1677ff;
27
+ --ads-color-primary-solid-hover: #4096ff;
28
+ --ads-color-primary-solid-active: #0958d9;
29
+ --ads-color-primary-text: #1677ff;
30
+ --ads-color-primary-text-hover: #4096ff;
31
+ --ads-color-primary-text-active: #0958d9;
32
+ --ads-color-danger-solid: #ff4d4f;
33
+ --ads-color-danger-solid-hover: #ff7875;
34
+ --ads-color-danger-solid-active: #d9363e;
35
+ --ads-color-danger-text: #ff4d4f;
36
+ --ads-color-danger-text-hover: #ff7875;
37
+ --ads-color-danger-text-active: #d9363e;
38
+ --ads-color-link: #1677ff;
39
+ --ads-color-link-hover: #69b1ff;
40
+ --ads-color-link-active: #0958d9;
35
41
 
36
42
  --ads-color-text: rgba(0, 0, 0, 0.88);
37
43
  --ads-color-text-secondary: rgba(0, 0, 0, 0.65);
@@ -46,6 +52,8 @@
46
52
  --ads-color-bg-container: #ffffff;
47
53
  --ads-color-bg-container-disabled: rgba(0, 0, 0, 0.04);
48
54
  --ads-color-bg-text-hover: rgba(0, 0, 0, 0.06);
55
+ --ads-button-text-hover-bg: rgba(0, 0, 0, 0.04);
56
+ --ads-button-text-active-bg: rgba(0, 0, 0, 0.15);
49
57
 
50
58
  --ads-border-width: 1px;
51
59
  --ads-focus-outline-width: 2px;
@@ -79,6 +87,42 @@
79
87
  --ads-motion-duration-spinner: 1s;
80
88
  --ads-motion-duration-spinner-reduced: 2.5s;
81
89
  --ads-motion-ease: cubic-bezier(0.645, 0.045, 0.355, 1);
90
+
91
+ /* Switch */
92
+ --ads-switch-height: 22px;
93
+ --ads-switch-height-sm: 16px;
94
+ --ads-switch-min-width: 44px;
95
+ --ads-switch-min-width-sm: 28px;
96
+ --ads-switch-handle-size: 18px;
97
+ --ads-switch-handle-size-sm: 12px;
98
+ --ads-switch-padding: 2px;
99
+ --ads-switch-content-margin-min: 9px;
100
+ --ads-switch-content-margin-max: 24px;
101
+ --ads-switch-content-margin-min-sm: 6px;
102
+ --ads-switch-content-margin-max-sm: 18px;
103
+ --ads-switch-font-size: 12px;
104
+ --ads-switch-font-size-sm: 10px;
105
+ --ads-switch-spinner-border-width: 2px;
106
+ --ads-switch-handle-shadow: 0 2px 4px 0 rgb(0 35 11 / 20%);
107
+ --ads-switch-disabled-opacity: 0.4;
108
+
109
+ /*
110
+ * Switch colors. In the light theme the values are the ones of the Ant Design token documentation
111
+ * (the documentation names no separate pressed color, so pressed equals hover, and a loading switch
112
+ * is dimmed to `opacityLoading`). They do not keep the contrast of the accessible set: the off track
113
+ * is 1.84:1 against a white page and the white text on the on track 4.10:1. `tokens-accessible.css`
114
+ * restores a darker off track, a darker on track and no dimming, which keep 3:1 for the tracks and the
115
+ * handle and 4.5:1 for the text. The dark theme below keeps these ratios without it.
116
+ */
117
+ --ads-switch-track-off: rgba(0, 0, 0, 0.25);
118
+ --ads-switch-track-off-hover: rgba(0, 0, 0, 0.45);
119
+ --ads-switch-track-off-active: rgba(0, 0, 0, 0.45);
120
+ --ads-switch-track-on: #1677ff;
121
+ --ads-switch-track-on-hover: #4096ff;
122
+ --ads-switch-track-on-active: #4096ff;
123
+ --ads-switch-handle-bg: #ffffff;
124
+ --ads-switch-content-color: #ffffff;
125
+ --ads-switch-loading-opacity: 0.65;
82
126
  }
83
127
 
84
128
  :root[data-theme='dark'] {
@@ -105,6 +149,9 @@
105
149
  --ads-color-danger-text: #ff7875;
106
150
  --ads-color-danger-text-hover: #ffa39e;
107
151
  --ads-color-danger-text-active: #ff7875;
152
+ --ads-color-link: #69b1ff;
153
+ --ads-color-link-hover: #91caff;
154
+ --ads-color-link-active: #69b1ff;
108
155
 
109
156
  --ads-color-text: rgb(255 255 255 / 88%);
110
157
  --ads-color-text-secondary: rgb(255 255 255 / 65%);
@@ -119,5 +166,17 @@
119
166
  --ads-color-bg-container: #141414;
120
167
  --ads-color-bg-container-disabled: rgb(255 255 255 / 8%);
121
168
  --ads-color-bg-text-hover: rgb(255 255 255 / 12%);
122
-
169
+ --ads-button-text-hover-bg: rgb(255 255 255 / 12%);
170
+ --ads-button-text-active-bg: rgb(255 255 255 / 12%);
171
+
172
+ /* In the dark theme a lighter track would lose contrast against the white handle, so hover and pressed go darker. */
173
+ --ads-switch-track-off: #8c8c8c;
174
+ --ads-switch-track-off-hover: #858585;
175
+ --ads-switch-track-off-active: #808080;
176
+ --ads-switch-track-on: #3a8aff;
177
+ --ads-switch-track-on-hover: #3283f5;
178
+ --ads-switch-track-on-active: #2f7ff0;
179
+ --ads-switch-handle-bg: #ffffff;
180
+ --ads-switch-content-color: #141414;
181
+ --ads-switch-loading-opacity: 1;
123
182
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-ant-design",
3
- "version": "0.1.0-alpha.2",
3
+ "version": "0.2.1",
4
4
  "description": "An independent UI component library for Svelte 5, inspired by Ant Design.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -44,7 +44,8 @@
44
44
  "svelte": "./dist/index.js",
45
45
  "default": "./dist/index.js"
46
46
  },
47
- "./tokens.css": "./dist/tokens.css"
47
+ "./tokens.css": "./dist/tokens.css",
48
+ "./tokens-accessible.css": "./dist/tokens-accessible.css"
48
49
  },
49
50
  "scripts": {
50
51
  "dev": "npm run dev -w docs",