svelte-ant-design 0.5.2 → 0.7.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 CHANGED
@@ -18,7 +18,15 @@ Ant Design.
18
18
 
19
19
  ## Status
20
20
 
21
- Version `0.5.2`. Available components: `Button`, `Checkbox` (with `CheckboxGroup`), `Input` (with `InputTextArea`, `InputPassword`, `InputSearch` and `InputOTP`), `Select` (with `SelectOption` and `SelectOptGroup`), `Switch`. More components are planned.
21
+ Version `0.7.0`. Available components: `Button`, `Checkbox` (with `CheckboxGroup`), `Input` (with `InputTextArea`, `InputPassword`, `InputSearch` and `InputOTP`), `Radio` (with `RadioButton` and `RadioGroup`), `Select` (with `SelectOption` and `SelectOptGroup`), `Switch`, `Tooltip`. More components are planned.
22
+
23
+ ## Browser support
24
+
25
+ Chrome 89, Firefox 85 and Safari 15.4, and newer versions. The tests of every change run in the latest Chromium, Firefox
26
+ and WebKit. The lowest versions run in a smoke test before a release (`npm run test:floor`: Chromium 90, the nearest
27
+ build to Chrome 89 that is available, Firefox 85 and WebKit 15.4). Not tested: a real Safari on macOS or iOS, and touch
28
+ devices. A newer feature that an older browser lacks falls back to the plain look: the high-contrast block (before
29
+ Firefox 89 and in Safari), `color-scheme` (before Firefox 96) and `overscroll-behavior` (before Safari 16).
22
30
 
23
31
  ## Installation
24
32
 
@@ -52,13 +60,17 @@ Vite in `tests/packaging`; other bundlers are not covered.
52
60
  See the [Button](https://prostreyd.github.io/ant-design-svelte/components/button/),
53
61
  [Checkbox](https://prostreyd.github.io/ant-design-svelte/components/checkbox/),
54
62
  [Input](https://prostreyd.github.io/ant-design-svelte/components/input/),
55
- [Select](https://prostreyd.github.io/ant-design-svelte/components/select/) and
56
- [Switch](https://prostreyd.github.io/ant-design-svelte/components/switch/) pages of the documentation site for
63
+ [Radio](https://prostreyd.github.io/ant-design-svelte/components/radio/),
64
+ [Select](https://prostreyd.github.io/ant-design-svelte/components/select/),
65
+ [Switch](https://prostreyd.github.io/ant-design-svelte/components/switch/) and
66
+ [Tooltip](https://prostreyd.github.io/ant-design-svelte/components/tooltip/) pages of the documentation site for
57
67
  examples and the API tables, or [`src/lib/Button/README.md`](./src/lib/Button/README.md),
58
68
  [`src/lib/Checkbox/README.md`](./src/lib/Checkbox/README.md),
59
69
  [`src/lib/Input/README.md`](./src/lib/Input/README.md),
60
- [`src/lib/Select/README.md`](./src/lib/Select/README.md) and
61
- [`src/lib/Switch/README.md`](./src/lib/Switch/README.md) in the repository.
70
+ [`src/lib/Radio/README.md`](./src/lib/Radio/README.md),
71
+ [`src/lib/Select/README.md`](./src/lib/Select/README.md),
72
+ [`src/lib/Switch/README.md`](./src/lib/Switch/README.md) and
73
+ [`src/lib/Tooltip/README.md`](./src/lib/Tooltip/README.md) in the repository.
62
74
 
63
75
  ### Colors and contrast
64
76
 
@@ -0,0 +1,181 @@
1
+ # Radio
2
+
3
+ A radio picks one option out of several that are all visible, with Ant Design-inspired styles and a Svelte 5 API.
4
+ `Radio` renders a native `<input type="radio">` inside a `<label>`, `RadioButton` draws the same choice as a button, and
5
+ `RadioGroup` owns the selected value, the shared `name` and the layout.
6
+
7
+ ## Usage
8
+
9
+ Import the design tokens once in the application entry point:
10
+
11
+ ```ts
12
+ import 'svelte-ant-design/tokens.css';
13
+ ```
14
+
15
+ A group renders one radio per entry of `options` and holds the selected value:
16
+
17
+ ```svelte
18
+ <script lang="ts">
19
+ import { RadioGroup } from 'svelte-ant-design';
20
+
21
+ let fruit = $state<string | undefined>('Apple');
22
+ </script>
23
+
24
+ <RadioGroup bind:value={fruit} options={['Apple', 'Pear', 'Orange']} aria-label="Fruit" />
25
+ ```
26
+
27
+ Strings, numbers and booleans are both the value and the label. Objects add a label that is text or a snippet, `disabled`,
28
+ `title`, `id`, `required`, `class`, `style` and an `onchange`:
29
+
30
+ ```svelte
31
+ {#snippet cherry()}<strong>Cherry</strong>{/snippet}
32
+
33
+ <RadioGroup
34
+ bind:value={extra}
35
+ aria-label="Extras"
36
+ options={[{ label: cherry, value: 'cherry' }, { label: 'Plum', value: 'plum', disabled: true }]}
37
+ />
38
+ ```
39
+
40
+ For your own layout put `Radio` elements in the group instead of `options`; each one needs a `value`:
41
+
42
+ ```svelte
43
+ <RadioGroup bind:value={day} aria-label="Day">
44
+ <div class="grid">
45
+ <Radio value="mon">Monday</Radio>
46
+ <Radio value="tue">Tuesday</Radio>
47
+ </div>
48
+ </RadioGroup>
49
+ ```
50
+
51
+ `optionType="button"` renders the options as buttons, and `RadioButton` elements can be placed in a group yourself;
52
+ `buttonStyle="solid"` fills the checked button and `size` sets its height:
53
+
54
+ ```svelte
55
+ <RadioGroup bind:value={city} optionType="button" buttonStyle="solid" size="large" options={['Hangzhou', 'Shanghai']} aria-label="City" />
56
+ ```
57
+
58
+ When the parent has to decide whether a choice goes through (for example after a request), use a function binding
59
+ (Svelte 5.9 or newer); a refused choice puts the previous radio back:
60
+
61
+ ```svelte
62
+ <RadioGroup
63
+ bind:value={
64
+ () => plan,
65
+ (next) => {
66
+ if (accepted) plan = next;
67
+ }
68
+ }
69
+ options={['free', 'team']}
70
+ aria-label="Plan"
71
+ />
72
+ ```
73
+
74
+ ## Radio and RadioButton props
75
+
76
+ `RadioButton` takes the same props as `Radio`; it has no `icon` part.
77
+
78
+ | Prop | Type | Default | Description |
79
+ |------|------|---------|-------------|
80
+ | `checked` | `boolean` (`$bindable`) | `false` | Whether the radio is selected. Use `bind:checked`; a plain value is the initial state. Inside a `RadioGroup` the state comes from the group's `value` and this prop is ignored. |
81
+ | `disabled` | `boolean` | `false` | Native `disabled` on the input. A group's `disabled` applies as well. |
82
+ | `value` | `string \| number \| boolean` | — | The identity inside a `RadioGroup` and the native `value` of the input, written as text (`false` becomes `"false"`). `false` and `0` are values like any other. Required inside a group. |
83
+ | `name` | `string` | — | Native `name` of the input. Inside a group the group's name wins. Standalone radios that share a name are not kept in step: use a group. |
84
+ | `children` | `Snippet` | — | The label. It is the accessible name. |
85
+ | `onchange` | `(checked: boolean, event: Event) => void` | — | Called once when the user selects the radio, after the state changed, with `true` (a user cannot unselect a radio) and the native `change` event. Not called when the parent changes `checked`, nor while disabled. |
86
+ | `classNames` | `Partial<Record<'root' \| 'icon' \| 'label', string>>` | — | Extra classes for the parts (`root` and `label` for a `RadioButton`). |
87
+ | `styles` | `Partial<Record<'root' \| 'icon' \| 'label', Record<string, string \| number>>>` | — | Inline styles for the parts. Names are kebab-case or custom properties; numbers are written as they are. |
88
+ | `class` | `string` | — | Additional CSS class appended to the root classes. |
89
+ | `style` | `string` | — | Inline style of the root; `styles.root` is written after it. |
90
+ | `title` | `string` | — | Native `title` of the root label, so the tooltip covers the mark and the text. |
91
+ | Native attributes | `HTMLInputAttributes` | — | Forwarded to the `<input>` (`id`, `required`, `autofocus`, `tabindex`, `aria-*`, `data-*`, `onfocus`, `onblur`, `onclick`, other handlers). `type`, `checked`, `value`, `disabled`, `name` and `onchange` belong to the component. |
92
+
93
+ The parts: `root` is the `<label>`, `icon` is the mark (the dot is drawn inside it), `label` holds the text and exists only
94
+ with `children`. Inline `styles` win over the component's styles. Classes from `classNames` and `class` have the same
95
+ specificity as the component's own rules, so use `styles` (or a more specific selector) when you need to override a
96
+ property the component already sets.
97
+
98
+ ## RadioGroup props
99
+
100
+ | Prop | Type | Default | Description |
101
+ |------|------|---------|-------------|
102
+ | `value` | `string \| number \| boolean \| null` (`$bindable`) | — | The selected value; `undefined` and `null` mean nothing is selected, and setting it back to `undefined` clears the selection. Use `bind:value`; a plain value is the initial selection. A value that matches no radio checks nothing and is kept. |
103
+ | `options` | `ReadonlyArray<string \| number \| boolean \| RadioOption>` | `[]` | One radio per entry. A string, a number or a boolean is both the value and the label. An object is `{ label, value, disabled?, title?, id?, required?, class?, style?, onchange? }` with `label` as text or a snippet. Values must be unique. When it has entries, `children` is ignored. |
104
+ | `optionType` | `'default' \| 'button'` | `'default'` | What `options` render: plain radios or buttons. Children keep the type their author chose. |
105
+ | `buttonStyle` | `'outline' \| 'solid'` | `'outline'` | How a checked button is drawn, for every `RadioButton` inside. |
106
+ | `size` | `'large' \| 'medium' \| 'middle' \| 'small' \| 'default'` | `'medium'` | The height of the buttons (24, 32 and 40 px); plain radios ignore it. `middle` and `default` are aliases of `medium`. |
107
+ | `orientation` | `'horizontal' \| 'vertical'` | — | The direction of the items; horizontal unless `vertical` is set. When it is set it wins over `vertical`. |
108
+ | `vertical` | `boolean` | `false` | A shorthand for `orientation="vertical"`. |
109
+ | `block` | `boolean` | `false` | The group fills the width of its parent and its items share it equally. |
110
+ | `disabled` | `boolean` | `false` | Disables every radio of the group; the own `disabled` of an option disables that one alone. |
111
+ | `name` | `string` | — | Native `name` of every input; it wins over the own name of a radio. Without one the group makes a unique name of its own and is never submitted with a form. |
112
+ | `children` | `Snippet` | — | Your own markup with `Radio` and `RadioButton` elements anywhere inside. Each one needs a `value`; outside production a missing one throws a `TypeError`. |
113
+ | `onchange` | `(value: string \| number \| boolean, event: Event) => void` | — | Called once per user selection with the new value and the `change` event of the selected input. The `onchange` of the radio or option runs first. Not called when the parent changes `value`. |
114
+ | `classNames` | `Partial<Record<'root' \| 'item' \| 'itemIcon' \| 'itemLabel', string>>` | — | `root` is the group; `item`, `itemIcon` and `itemLabel` are appended to the `root`, `icon` and `label` parts of every radio inside (`itemIcon` does nothing for buttons). |
115
+ | `styles` | `Partial<Record<'root' \| 'item' \| 'itemIcon' \| 'itemLabel', Record<string, string \| number>>>` | — | The same for inline styles; the own `styles` of a radio are written after the group's, so they win. |
116
+ | `class`, `style` | `string` | — | Root element (`styles.root` is written after `style`). |
117
+ | Native attributes | `HTMLAttributes<HTMLDivElement>` | — | Forwarded to the `<div>` (`id`, `aria-label`, `aria-labelledby`, `aria-describedby`, `aria-required`, ...). `role="radiogroup"` belongs to the component. |
118
+
119
+ ## Methods
120
+
121
+ `Radio` and `RadioButton` have `focus(options?: FocusOptions)` and `blur()`, available through `bind:this`. The group has no
122
+ methods; focus a radio inside it.
123
+
124
+ ## Keyboard
125
+
126
+ The inputs of a group share one name (your `name`, or one the group makes), so the browser itself gives the group the
127
+ keyboard behavior of a radio group: one Tab stop, on the selected radio (on the first when none is selected, though some
128
+ browsers use the last one when you tab backwards), arrow keys that move the focus and select, and Space to select the
129
+ focused radio. At the ends of the group Chromium and Firefox wrap around and WebKit stops. Two groups never trade focus.
130
+
131
+ ## Forms
132
+
133
+ The input is native, so `name`, `value`, `required` and `form` work and only the selected radio is submitted. A group
134
+ without a `name` is not submitted: its inputs have no form owner, so a surrounding `<form>` does not send a made-up
135
+ key. A native form reset does not change the state of the components (the state is `checked` or the group's `value`): the
136
+ view is put back to the state after the reset, so reset the state of the parent in the form's `onreset`.
137
+
138
+ ## Colors
139
+
140
+ In the light theme the default tokens are the values of the Ant Design token documentation and of measurements of its
141
+ public demo: border `#d9d9d9` (`#1677ff` on hover), selected fill `#1677ff` (`#4096ff` on hover) with a 6 px white dot,
142
+ disabled fill `rgba(0, 0, 0, 0.04)`; a button has a `#d9d9d9` border, the checked outline button `#1677ff` text and border,
143
+ the solid one a `#1677ff` fill. They do **not** keep the contrast of the accessible set: the unchecked border is 1.41:1
144
+ against a white page and the hover fill 2.99:1 (3:1 is needed for a part of a control). For a contrast guarantee import the
145
+ optional file after the tokens:
146
+
147
+ ```ts
148
+ import 'svelte-ant-design/tokens.css';
149
+ import 'svelte-ant-design/tokens-accessible.css';
150
+ ```
151
+
152
+ It darkens the unchecked border (`#737373`) and the selected hover fill (`#0958d9`), and the buttons follow the darker
153
+ `primary` tokens. The dark theme needs no extra file.
154
+
155
+ ## Accessibility
156
+
157
+ - A native `<input type="radio">` in a `<label>`: the role and the checked state come from the browser, a click on the text works, and the keyboard behavior is the one of the [WAI-ARIA radio group pattern](https://www.w3.org/WAI/ARIA/apg/patterns/radio/) (see Keyboard).
158
+ - The group is `role="radiogroup"`. **Name it** with `aria-label` or `aria-labelledby` (or wrap it in a `<fieldset>` with a `<legend>`), and **name every radio**: its text (`children`), `aria-label`, `aria-labelledby` or an external `<label for>` that points at its `id`.
159
+ - A visible `:focus-visible` outline from the design tokens around the mark or the whole button, at least 3:1 against the page.
160
+ - The border, the dot, the button border and the focus ring keep at least 3:1 contrast, and the text of a button 4.5:1, in every state: in the dark theme, and in the light theme when `tokens-accessible.css` is imported (see Colors).
161
+ - In forced-colors modes such as Windows High Contrast a selected radio keeps its dot, a selected button is drawn in the selection colors, and disabled controls use the disabled color.
162
+ - Under `prefers-reduced-motion: reduce` the transitions are disabled.
163
+ - Positions and corners use logical properties, so the radios and the button groups mirror in right-to-left layouts. Selecting a radio never changes the size of the control or moves its neighbours.
164
+ - **Not checked yet:** NVDA, JAWS, VoiceOver and a real touch device. A public report against Ant Design's own radio says VoiceOver may stop twice on a label that wraps the control and its text; this component has the same shape. Orca (with Firefox) is checked by an automated test (`screen-reader/radio.spec.ts`).
165
+
166
+ ## Scope decisions and differences from Ant Design
167
+
168
+ - `onchange` receives `(checked, event)` with the native `change` event, and `checked` is always `true`; React passes an object with `target` and Vue the event. The group's `onchange` receives `(value, event)`; React passes an event object, Angular the value.
169
+ - `defaultChecked` and `defaultValue` (React) are not implemented: `bind:checked` and `bind:value` cover them. The function form of `classNames` / `styles` (React) is not implemented: compute the object in `$derived`. The ripple (`wave`) is not implemented.
170
+ - `value` is `string | number | boolean`; React, Vue and Angular allow any type.
171
+ - `Radio.Group` and `Radio.Button` are separate components, `RadioGroup` and `RadioButton`; `optionType` only asks `options` for buttons.
172
+ - An unnamed group makes a unique name and is never submitted; in React the random name is submitted with the form.
173
+ - Standalone radios that share a `name` are not kept in step (Angular emits `false` to the radio that was deselected); use a group.
174
+ - `size` also accepts `middle` and `default`; `orientation` and `vertical` are both there, and `orientation` wins.
175
+ - The global configuration of `classNames` and `styles` comes with the `ConfigProvider` component.
176
+ - See [`specs/radio.md`](../../../specs/radio.md) for the full comparison and the reasoning.
177
+
178
+ ## Live examples
179
+
180
+ Interactive examples of every option, with their code and the API tables, are on the
181
+ [documentation site](https://prostreyd.github.io/ant-design-svelte/components/radio/).
@@ -0,0 +1,349 @@
1
+ <script lang="ts">
2
+ import { onDestroy, type Snippet } from 'svelte';
3
+ import type { HTMLInputAttributes } from 'svelte/elements';
4
+ import type { RadioClassNames, RadioStyles, RadioValue } from './types.js';
5
+ import { getRadioGroup } from './context.js';
6
+ import { isNone } from './options.js';
7
+ import { isProduction } from '../internal/env.js';
8
+ import { classList, styleText } from '../internal/semantic.js';
9
+
10
+ type RadioProps = Omit<
11
+ HTMLInputAttributes,
12
+ | 'children'
13
+ | 'class'
14
+ | 'style'
15
+ | 'type'
16
+ | 'checked'
17
+ | 'value'
18
+ | 'disabled'
19
+ | 'name'
20
+ | 'title'
21
+ | 'onchange'
22
+ > & {
23
+ /**
24
+ * Whether the radio is selected. Use `bind:checked` for two-way binding. Inside a `RadioGroup`
25
+ * the state comes from the group's `value` and this prop is ignored.
26
+ */
27
+ checked?: boolean;
28
+ /** Whether the radio is disabled. A group's `disabled` applies as well. */
29
+ disabled?: boolean;
30
+ /** Identity inside a `RadioGroup` and the native `value` of the input (written as text). */
31
+ value?: RadioValue;
32
+ /** Native `name` of the input. Inside a group the group's name wins. */
33
+ name?: string;
34
+ /** Label content; it is the accessible name of the radio. */
35
+ children?: Snippet;
36
+ /**
37
+ * Called once when the user selects the radio, after the state changed, with `true` and the native
38
+ * `change` event. A radio cannot be unchecked by the user, so the first argument is always `true`.
39
+ */
40
+ onchange?: (checked: boolean, event: Event) => void;
41
+ /** Extra classes for the `root`, `icon` and `label` parts. */
42
+ classNames?: RadioClassNames;
43
+ /** Inline styles for the `root`, `icon` and `label` parts. */
44
+ styles?: RadioStyles;
45
+ /** Additional CSS class appended to the root classes. */
46
+ class?: string;
47
+ /** Inline style of the root; `styles.root` is applied after it. */
48
+ style?: string;
49
+ /** Native `title` of the root label, so the tooltip covers the mark and the text. */
50
+ title?: string;
51
+ };
52
+
53
+ let {
54
+ checked = $bindable(false),
55
+ disabled = false,
56
+ value = undefined,
57
+ name = undefined,
58
+ children,
59
+ onchange,
60
+ classNames,
61
+ styles,
62
+ class: customClass,
63
+ style: customStyle,
64
+ title = undefined,
65
+ ...rest
66
+ }: RadioProps = $props();
67
+
68
+ const group = getRadioGroup();
69
+
70
+ // A radio takes part in its group only with a value: the group tells the items apart by it.
71
+ // Without one it keeps its own state (and throws while developing, so the mistake is found early).
72
+ const member = $derived.by(() => {
73
+ if (!group) return false;
74
+ if (!isNone(value)) return true;
75
+ if (!isProduction()) throw new TypeError('Radio inside a RadioGroup needs a value.');
76
+ return false;
77
+ });
78
+
79
+ let input: HTMLInputElement | undefined = $state();
80
+
81
+ const isChecked = $derived(member ? group!.isChecked(value as RadioValue) : checked);
82
+ const isDisabled = $derived(disabled || Boolean(group?.disabled));
83
+ const inputName = $derived(member ? group!.name : name);
84
+ const inputForm = $derived(member ? group!.form : undefined);
85
+
86
+ /** Writes the state to the input: the view is always the state. */
87
+ function sync(): void {
88
+ if (input) input.checked = isChecked;
89
+ }
90
+
91
+ // The group calls this after every selection (see the context).
92
+ if (group) onDestroy(group.register(sync));
93
+
94
+ const rootClass = $derived(
95
+ classList(
96
+ 'ads-radio',
97
+ isChecked && 'ads-radio-checked',
98
+ isDisabled && 'ads-radio-disabled',
99
+ member && 'ads-radio-grouped',
100
+ member && group!.orientation === 'vertical' && 'ads-radio-vertical',
101
+ member && group!.block && 'ads-radio-block',
102
+ customClass,
103
+ classNames?.root,
104
+ group?.classNames?.item
105
+ )
106
+ );
107
+ const iconClass = $derived(classList('ads-radio-icon', classNames?.icon, group?.classNames?.itemIcon));
108
+ const labelClass = $derived(classList('ads-radio-label', classNames?.label, group?.classNames?.itemLabel));
109
+
110
+ const rootStyle = $derived(styleText(group?.styles?.item, customStyle, styles?.root));
111
+ const iconStyle = $derived(styleText(group?.styles?.itemIcon, styles?.icon));
112
+ const labelStyle = $derived(styleText(group?.styles?.itemLabel, styles?.label));
113
+
114
+ // A native form reset puts the input back to its default checkedness behind the state's back. The
115
+ // state is the truth (the parent resets it in `onreset`), so the view is restored to it once the
116
+ // reset has run.
117
+ $effect(() => {
118
+ const form = input?.form;
119
+ if (!form) return;
120
+ const restore = () => {
121
+ setTimeout(sync);
122
+ };
123
+ form.addEventListener('reset', restore);
124
+ return () => form.removeEventListener('reset', restore);
125
+ });
126
+
127
+ // A radio sends `change` only when it became checked.
128
+ function handleChange(event: Event): void {
129
+ if (member) {
130
+ onchange?.(true, event);
131
+ group!.select(value as RadioValue, event);
132
+ } else {
133
+ checked = true;
134
+ onchange?.(true, event);
135
+ // The input owns its state. Writing the prop back reverts a selection the parent refused
136
+ // (function binding).
137
+ sync();
138
+ }
139
+ }
140
+
141
+ /** Moves focus to the radio. */
142
+ export function focus(options?: FocusOptions): void {
143
+ input?.focus(options);
144
+ }
145
+
146
+ /** Removes focus from the radio. */
147
+ export function blur(): void {
148
+ input?.blur();
149
+ }
150
+ </script>
151
+
152
+ <!--
153
+ A native input inside a label: the role, the checked state, Space, the arrow keys, the Tab order, the
154
+ click on the text and form participation come from the platform. The input is laid over the mark,
155
+ invisible, so the focus ring and the hit area are those of a real control.
156
+ -->
157
+ <label class={rootClass} style={rootStyle} {title}>
158
+ <span class="ads-radio-control">
159
+ <input
160
+ {...rest}
161
+ bind:this={input}
162
+ type="radio"
163
+ class="ads-radio-input"
164
+ checked={isChecked}
165
+ disabled={isDisabled}
166
+ name={inputName}
167
+ form={inputForm ?? rest.form}
168
+ value={isNone(value) ? 'on' : String(value)}
169
+ onchange={handleChange}
170
+ />
171
+ <span class={iconClass} style={iconStyle} aria-hidden="true"></span>
172
+ </span>
173
+ {#if children}
174
+ <span class={labelClass} style={labelStyle}>{@render children()}</span>
175
+ {/if}
176
+ </label>
177
+
178
+ <style>
179
+ /*
180
+ * The states only set the private `--_*` variables; the rules that paint the mark read them, so
181
+ * checked, hover and disabled combine without fighting over specificity. The colors come from the
182
+ * `--ads-radio-*` tokens. Positions use logical properties, so the radio mirrors in right-to-left layouts.
183
+ */
184
+ .ads-radio {
185
+ --_size: var(--ads-radio-size);
186
+ --_border: var(--ads-radio-border);
187
+ --_bg: var(--ads-radio-bg);
188
+ --_dot: var(--ads-radio-dot-color);
189
+
190
+ position: relative;
191
+ display: inline-flex;
192
+ align-items: flex-start;
193
+ box-sizing: border-box;
194
+ margin: 0;
195
+ color: var(--ads-color-text);
196
+ font-family: var(--ads-font-family);
197
+ font-size: var(--ads-radio-font-size);
198
+ line-height: var(--ads-radio-line-height);
199
+ cursor: pointer;
200
+ }
201
+
202
+ .ads-radio-control {
203
+ position: relative;
204
+ flex: none;
205
+ width: var(--_size);
206
+ height: var(--_size);
207
+ /* Centered in the first line of the text, so a multi-line label keeps the mark at the top. */
208
+ margin-block: calc((var(--ads-radio-line-height) * var(--ads-radio-font-size) - var(--_size)) / 2);
209
+ }
210
+
211
+ .ads-radio-input {
212
+ position: absolute;
213
+ inset: 0;
214
+ z-index: 1;
215
+ box-sizing: border-box;
216
+ width: 100%;
217
+ height: 100%;
218
+ margin: 0;
219
+ padding: 0;
220
+ opacity: 0;
221
+ cursor: inherit;
222
+ }
223
+
224
+ .ads-radio-icon {
225
+ position: absolute;
226
+ inset: 0;
227
+ box-sizing: border-box;
228
+ border: var(--ads-radio-border-width) solid var(--_border);
229
+ border-radius: 50%;
230
+ background-color: var(--_bg);
231
+ transition:
232
+ border-color var(--ads-motion-duration) ease,
233
+ background-color var(--ads-motion-duration) ease;
234
+ }
235
+
236
+ /* The dot is drawn inside the mark and only scaled, so selecting never changes the size of anything. */
237
+ .ads-radio-icon::after {
238
+ content: '';
239
+ position: absolute;
240
+ top: 50%;
241
+ left: 50%;
242
+ box-sizing: border-box;
243
+ width: var(--ads-radio-dot-size);
244
+ height: var(--ads-radio-dot-size);
245
+ border-radius: 50%;
246
+ background-color: var(--_dot);
247
+ transform: translate(-50%, -50%) scale(0);
248
+ opacity: 0;
249
+ transition:
250
+ opacity var(--ads-motion-duration) ease,
251
+ transform var(--ads-motion-duration-slow) var(--ads-motion-ease-in-out-circ);
252
+ }
253
+
254
+ .ads-radio-checked .ads-radio-icon {
255
+ --_border: var(--ads-radio-checked-bg);
256
+ --_bg: var(--ads-radio-checked-bg);
257
+ }
258
+
259
+ .ads-radio-checked .ads-radio-icon::after {
260
+ transform: translate(-50%, -50%) scale(1);
261
+ opacity: 1;
262
+ }
263
+
264
+ /* Hover anywhere over the label, mark or text. */
265
+ .ads-radio:not(.ads-radio-disabled):hover .ads-radio-icon {
266
+ --_border: var(--ads-radio-border-hover);
267
+ }
268
+
269
+ .ads-radio-checked:not(.ads-radio-disabled):hover .ads-radio-icon {
270
+ --_border: var(--ads-radio-checked-bg-hover);
271
+ --_bg: var(--ads-radio-checked-bg-hover);
272
+ }
273
+
274
+ .ads-radio-input:focus-visible + .ads-radio-icon {
275
+ outline: var(--ads-focus-outline-width) solid var(--ads-color-focus-outline);
276
+ outline-offset: var(--ads-focus-outline-offset);
277
+ }
278
+
279
+ .ads-radio-label {
280
+ padding-inline: var(--ads-radio-label-padding);
281
+ }
282
+
283
+ /* In a group: the gap after every item but the last, in the direction of the group. */
284
+ .ads-radio-grouped:not(:last-child) {
285
+ margin-inline-end: var(--ads-radio-group-gap);
286
+ }
287
+
288
+ .ads-radio-vertical:not(:last-child) {
289
+ margin-inline-end: 0;
290
+ margin-block-end: var(--ads-radio-group-gap);
291
+ }
292
+
293
+ /* `block`: the items share the width of the group equally, the content centered in each. */
294
+ .ads-radio-block:not(.ads-radio-vertical) {
295
+ flex: 1 1 0%;
296
+ justify-content: center;
297
+ }
298
+
299
+ /* Last, so the disabled colors win over the checked ones. */
300
+ .ads-radio-disabled {
301
+ color: var(--ads-color-text-disabled);
302
+ cursor: not-allowed;
303
+ }
304
+
305
+ .ads-radio-disabled .ads-radio-icon {
306
+ --_border: var(--ads-radio-disabled-border);
307
+ --_bg: var(--ads-radio-disabled-bg);
308
+ --_dot: var(--ads-radio-disabled-dot-color);
309
+ }
310
+
311
+ /*
312
+ * Forced colors (Windows High Contrast): the system replaces the author's backgrounds, and the dot is a
313
+ * background, so it would vanish and a checked radio would look unchecked. The ring and the dot take system
314
+ * colors: unchecked and checked differ by the dot (and the ring color), disabled uses GrayText.
315
+ */
316
+ @media (forced-colors: active) {
317
+ .ads-radio-icon {
318
+ border-color: ButtonText;
319
+ }
320
+
321
+ .ads-radio-checked .ads-radio-icon {
322
+ border-color: Highlight;
323
+ }
324
+
325
+ .ads-radio-icon::after {
326
+ background-color: Highlight;
327
+ }
328
+
329
+ .ads-radio-disabled {
330
+ color: GrayText;
331
+ }
332
+
333
+ .ads-radio-disabled .ads-radio-icon,
334
+ .ads-radio-disabled.ads-radio-checked .ads-radio-icon {
335
+ border-color: GrayText;
336
+ }
337
+
338
+ .ads-radio-disabled .ads-radio-icon::after {
339
+ background-color: GrayText;
340
+ }
341
+ }
342
+
343
+ @media (prefers-reduced-motion: reduce) {
344
+ .ads-radio-icon,
345
+ .ads-radio-icon::after {
346
+ transition: none;
347
+ }
348
+ }
349
+ </style>
@@ -0,0 +1,39 @@
1
+ import { type Snippet } from 'svelte';
2
+ import type { HTMLInputAttributes } from 'svelte/elements';
3
+ import type { RadioClassNames, RadioStyles, RadioValue } from './types.js';
4
+ type RadioProps = Omit<HTMLInputAttributes, 'children' | 'class' | 'style' | 'type' | 'checked' | 'value' | 'disabled' | 'name' | 'title' | 'onchange'> & {
5
+ /**
6
+ * Whether the radio is selected. Use `bind:checked` for two-way binding. Inside a `RadioGroup`
7
+ * the state comes from the group's `value` and this prop is ignored.
8
+ */
9
+ checked?: boolean;
10
+ /** Whether the radio is disabled. A group's `disabled` applies as well. */
11
+ disabled?: boolean;
12
+ /** Identity inside a `RadioGroup` and the native `value` of the input (written as text). */
13
+ value?: RadioValue;
14
+ /** Native `name` of the input. Inside a group the group's name wins. */
15
+ name?: string;
16
+ /** Label content; it is the accessible name of the radio. */
17
+ children?: Snippet;
18
+ /**
19
+ * Called once when the user selects the radio, after the state changed, with `true` and the native
20
+ * `change` event. A radio cannot be unchecked by the user, so the first argument is always `true`.
21
+ */
22
+ onchange?: (checked: boolean, event: Event) => void;
23
+ /** Extra classes for the `root`, `icon` and `label` parts. */
24
+ classNames?: RadioClassNames;
25
+ /** Inline styles for the `root`, `icon` and `label` parts. */
26
+ styles?: RadioStyles;
27
+ /** Additional CSS class appended to the root classes. */
28
+ class?: string;
29
+ /** Inline style of the root; `styles.root` is applied after it. */
30
+ style?: string;
31
+ /** Native `title` of the root label, so the tooltip covers the mark and the text. */
32
+ title?: string;
33
+ };
34
+ declare const Radio: import("svelte").Component<RadioProps, {
35
+ focus: (options?: FocusOptions) => void;
36
+ blur: () => void;
37
+ }, "checked">;
38
+ type Radio = ReturnType<typeof Radio>;
39
+ export default Radio;