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.
@@ -0,0 +1,382 @@
1
+ <script lang="ts">
2
+ import { onDestroy, type Snippet } from 'svelte';
3
+ import type { HTMLInputAttributes } from 'svelte/elements';
4
+ import type { RadioButtonClassNames, RadioButtonStyles, 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 RadioButtonProps = 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 button 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 button 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 button. */
35
+ children?: Snippet;
36
+ /**
37
+ * Called once when the user selects the button, 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` and `label` parts. */
42
+ classNames?: RadioButtonClassNames;
43
+ /** Inline styles for the `root` and `label` parts. */
44
+ styles?: RadioButtonStyles;
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. */
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
+ }: RadioButtonProps = $props();
67
+
68
+ const group = getRadioGroup();
69
+
70
+ // As for `Radio`: a button takes part in its group only with a value.
71
+ const member = $derived.by(() => {
72
+ if (!group) return false;
73
+ if (!isNone(value)) return true;
74
+ if (!isProduction()) throw new TypeError('RadioButton inside a RadioGroup needs a value.');
75
+ return false;
76
+ });
77
+
78
+ let input: HTMLInputElement | undefined = $state();
79
+
80
+ const isChecked = $derived(member ? group!.isChecked(value as RadioValue) : checked);
81
+ const isDisabled = $derived(disabled || Boolean(group?.disabled));
82
+ const inputName = $derived(member ? group!.name : name);
83
+ const inputForm = $derived(member ? group!.form : undefined);
84
+ const size = $derived(member ? group!.size : 'medium');
85
+ const solid = $derived(member && group!.buttonStyle === 'solid');
86
+
87
+ /** Writes the state to the input: the view is always the state. */
88
+ function sync(): void {
89
+ if (input) input.checked = isChecked;
90
+ }
91
+
92
+ if (group) onDestroy(group.register(sync));
93
+
94
+ const rootClass = $derived(
95
+ classList(
96
+ 'ads-radio-button',
97
+ solid && 'ads-radio-button-solid',
98
+ size !== 'medium' && `ads-radio-button-${size}`,
99
+ isChecked && 'ads-radio-button-checked',
100
+ isDisabled && 'ads-radio-button-disabled',
101
+ member && 'ads-radio-button-grouped',
102
+ member && group!.orientation === 'vertical' && 'ads-radio-button-vertical',
103
+ member && group!.block && 'ads-radio-button-block',
104
+ customClass,
105
+ classNames?.root,
106
+ group?.classNames?.item
107
+ )
108
+ );
109
+ const labelClass = $derived(
110
+ classList('ads-radio-button-label', classNames?.label, group?.classNames?.itemLabel)
111
+ );
112
+
113
+ const rootStyle = $derived(styleText(group?.styles?.item, customStyle, styles?.root));
114
+ const labelStyle = $derived(styleText(group?.styles?.itemLabel, styles?.label));
115
+
116
+ // A native form reset puts the input back to its default checkedness behind the state's back; the
117
+ // state is the truth, so the view is restored to it once the reset has run.
118
+ $effect(() => {
119
+ const form = input?.form;
120
+ if (!form) return;
121
+ const restore = () => {
122
+ setTimeout(sync);
123
+ };
124
+ form.addEventListener('reset', restore);
125
+ return () => form.removeEventListener('reset', restore);
126
+ });
127
+
128
+ // A radio sends `change` only when it became checked.
129
+ function handleChange(event: Event): void {
130
+ if (member) {
131
+ onchange?.(true, event);
132
+ group!.select(value as RadioValue, event);
133
+ } else {
134
+ checked = true;
135
+ onchange?.(true, event);
136
+ sync();
137
+ }
138
+ }
139
+
140
+ /** Moves focus to the button. */
141
+ export function focus(options?: FocusOptions): void {
142
+ input?.focus(options);
143
+ }
144
+
145
+ /** Removes focus from the button. */
146
+ export function blur(): void {
147
+ input?.blur();
148
+ }
149
+ </script>
150
+
151
+ <!--
152
+ A radio drawn as a button: the same native input in a label, so the role, Space, the arrow keys, the Tab
153
+ order and forms come from the platform. The input covers the whole button, invisible except for its focus
154
+ ring, which it draws itself (no `:has()`, so the ring needs no newer engine).
155
+ -->
156
+ <label class={rootClass} style={rootStyle} {title}>
157
+ <input
158
+ {...rest}
159
+ bind:this={input}
160
+ type="radio"
161
+ class="ads-radio-button-input"
162
+ checked={isChecked}
163
+ disabled={isDisabled}
164
+ name={inputName}
165
+ form={inputForm ?? rest.form}
166
+ value={isNone(value) ? 'on' : String(value)}
167
+ onchange={handleChange}
168
+ />
169
+ {#if children}
170
+ <span class={labelClass} style={labelStyle}>{@render children()}</span>
171
+ {/if}
172
+ </label>
173
+
174
+ <style>
175
+ /*
176
+ * The states only set the private `--_*` variables; the rules that paint the button read them. A
177
+ * button keeps the same 1px border in every state and only changes colors, so nothing moves when
178
+ * it is selected. Positions and corners use logical properties, so a group mirrors in right-to-left layouts.
179
+ */
180
+ .ads-radio-button {
181
+ --_height: var(--ads-control-height);
182
+ --_radius: var(--ads-radius-md);
183
+ --_pad: var(--ads-radio-button-padding-inline);
184
+ --_font: var(--ads-font-size);
185
+ --_bg: var(--ads-radio-button-bg);
186
+ --_border: var(--ads-radio-button-border);
187
+ --_color: var(--ads-radio-button-color);
188
+
189
+ position: relative;
190
+ display: inline-flex;
191
+ align-items: center;
192
+ justify-content: center;
193
+ box-sizing: border-box;
194
+ min-height: var(--_height);
195
+ margin: 0;
196
+ padding-block: 0;
197
+ padding-inline: var(--_pad);
198
+ border: var(--ads-border-width) solid var(--_border);
199
+ border-radius: var(--_radius);
200
+ background-color: var(--_bg);
201
+ color: var(--_color);
202
+ font-family: var(--ads-font-family);
203
+ font-size: var(--_font);
204
+ line-height: var(--ads-radio-line-height);
205
+ text-align: center;
206
+ cursor: pointer;
207
+ transition:
208
+ color var(--ads-motion-duration) ease,
209
+ background-color var(--ads-motion-duration) ease,
210
+ border-color var(--ads-motion-duration) ease;
211
+ }
212
+
213
+ .ads-radio-button-large {
214
+ --_height: var(--ads-control-height-lg);
215
+ --_radius: var(--ads-radius-lg);
216
+ --_font: var(--ads-font-size-lg);
217
+ }
218
+
219
+ .ads-radio-button-small {
220
+ --_height: var(--ads-control-height-sm);
221
+ --_radius: var(--ads-radius-sm);
222
+ --_pad: var(--ads-radio-button-padding-inline-sm);
223
+ --_font: var(--ads-font-size-sm);
224
+ }
225
+
226
+ /*
227
+ * The input covers the border box and draws nothing but its focus ring: no native look, no background,
228
+ * and the corners of the button, so the ring follows them in the engines that round outlines.
229
+ */
230
+ .ads-radio-button-input {
231
+ position: absolute;
232
+ inset: calc(-1 * var(--ads-border-width));
233
+ z-index: 1;
234
+ box-sizing: border-box;
235
+ width: calc(100% + 2 * var(--ads-border-width));
236
+ height: calc(100% + 2 * var(--ads-border-width));
237
+ margin: 0;
238
+ padding: 0;
239
+ border: 0;
240
+ border-radius: inherit;
241
+ background: transparent;
242
+ -webkit-appearance: none;
243
+ appearance: none;
244
+ cursor: inherit;
245
+ }
246
+
247
+ .ads-radio-button-input:focus-visible {
248
+ outline: var(--ads-focus-outline-width) solid var(--ads-color-focus-outline);
249
+ outline-offset: var(--ads-focus-outline-offset);
250
+ }
251
+
252
+ /* The checked button is raised, so its border is whole where it meets a neighbour's. */
253
+ .ads-radio-button-checked {
254
+ --_bg: var(--ads-radio-button-checked-bg);
255
+ --_border: var(--ads-color-primary-text);
256
+ --_color: var(--ads-color-primary-text);
257
+ z-index: 1;
258
+ }
259
+
260
+ .ads-radio-button:not(.ads-radio-button-disabled):hover {
261
+ --_color: var(--ads-color-primary-text);
262
+ }
263
+
264
+ .ads-radio-button-checked:not(.ads-radio-button-disabled):hover {
265
+ --_border: var(--ads-color-primary-text-hover);
266
+ --_color: var(--ads-color-primary-text-hover);
267
+ }
268
+
269
+ .ads-radio-button-checked:not(.ads-radio-button-disabled):active {
270
+ --_border: var(--ads-color-primary-text-active);
271
+ --_color: var(--ads-color-primary-text-active);
272
+ }
273
+
274
+ .ads-radio-button-solid.ads-radio-button-checked {
275
+ --_bg: var(--ads-color-primary-solid);
276
+ --_border: var(--ads-color-primary-solid);
277
+ --_color: var(--ads-color-text-inverse);
278
+ }
279
+
280
+ .ads-radio-button-solid.ads-radio-button-checked:not(.ads-radio-button-disabled):hover {
281
+ --_bg: var(--ads-color-primary-solid-hover);
282
+ --_border: var(--ads-color-primary-solid-hover);
283
+ --_color: var(--ads-color-text-inverse);
284
+ }
285
+
286
+ .ads-radio-button-solid.ads-radio-button-checked:not(.ads-radio-button-disabled):active {
287
+ --_bg: var(--ads-color-primary-solid-active);
288
+ --_border: var(--ads-color-primary-solid-active);
289
+ --_color: var(--ads-color-text-inverse);
290
+ }
291
+
292
+ /* In a group the buttons touch and share a border; only the outer corners are round. */
293
+ .ads-radio-button-grouped {
294
+ border-radius: 0;
295
+ }
296
+
297
+ .ads-radio-button-grouped:not(:first-child) {
298
+ margin-inline-start: calc(-1 * var(--ads-border-width));
299
+ }
300
+
301
+ .ads-radio-button-grouped:not(.ads-radio-button-vertical):first-child {
302
+ border-start-start-radius: var(--_radius);
303
+ border-end-start-radius: var(--_radius);
304
+ }
305
+
306
+ .ads-radio-button-grouped:not(.ads-radio-button-vertical):last-child {
307
+ border-start-end-radius: var(--_radius);
308
+ border-end-end-radius: var(--_radius);
309
+ }
310
+
311
+ .ads-radio-button-vertical:not(:first-child) {
312
+ margin-inline-start: 0;
313
+ margin-block-start: calc(-1 * var(--ads-border-width));
314
+ }
315
+
316
+ .ads-radio-button-vertical:first-child {
317
+ border-start-start-radius: var(--_radius);
318
+ border-start-end-radius: var(--_radius);
319
+ }
320
+
321
+ .ads-radio-button-vertical:last-child {
322
+ border-end-start-radius: var(--_radius);
323
+ border-end-end-radius: var(--_radius);
324
+ }
325
+
326
+ /* `block`: the buttons share the width of the group equally. */
327
+ .ads-radio-button-block:not(.ads-radio-button-vertical) {
328
+ flex: 1 1 0%;
329
+ }
330
+
331
+ /* Last, so the disabled colors win over the checked and solid ones. */
332
+ .ads-radio-button-disabled {
333
+ --_bg: var(--ads-radio-button-disabled-bg);
334
+ --_border: var(--ads-radio-button-disabled-border);
335
+ --_color: var(--ads-radio-button-disabled-color);
336
+ cursor: not-allowed;
337
+ }
338
+
339
+ .ads-radio-button-disabled.ads-radio-button-checked {
340
+ --_bg: var(--ads-radio-button-checked-disabled-bg);
341
+ --_color: var(--ads-radio-button-checked-disabled-color);
342
+ }
343
+
344
+ .ads-radio-button-disabled.ads-radio-button-solid.ads-radio-button-checked {
345
+ --_border: var(--ads-radio-button-disabled-border);
346
+ }
347
+
348
+ /*
349
+ * Forced colors (Windows High Contrast): the system replaces the author's backgrounds and the colors of
350
+ * the checked state would all become the same. A checked button is drawn in the system's selection colors,
351
+ * the others in the colors of a button, a disabled one in GrayText.
352
+ */
353
+ @media (forced-colors: active) {
354
+ .ads-radio-button {
355
+ border-color: ButtonText;
356
+ color: ButtonText;
357
+ }
358
+
359
+ .ads-radio-button-checked {
360
+ border-color: Highlight;
361
+ background-color: Highlight;
362
+ color: HighlightText;
363
+ }
364
+
365
+ .ads-radio-button-disabled {
366
+ border-color: GrayText;
367
+ color: GrayText;
368
+ }
369
+
370
+ .ads-radio-button-disabled.ads-radio-button-checked {
371
+ border-color: GrayText;
372
+ background-color: GrayText;
373
+ color: Canvas;
374
+ }
375
+ }
376
+
377
+ @media (prefers-reduced-motion: reduce) {
378
+ .ads-radio-button {
379
+ transition: none;
380
+ }
381
+ }
382
+ </style>
@@ -0,0 +1,39 @@
1
+ import { type Snippet } from 'svelte';
2
+ import type { HTMLInputAttributes } from 'svelte/elements';
3
+ import type { RadioButtonClassNames, RadioButtonStyles, RadioValue } from './types.js';
4
+ type RadioButtonProps = Omit<HTMLInputAttributes, 'children' | 'class' | 'style' | 'type' | 'checked' | 'value' | 'disabled' | 'name' | 'title' | 'onchange'> & {
5
+ /**
6
+ * Whether the button 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 button 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 button. */
17
+ children?: Snippet;
18
+ /**
19
+ * Called once when the user selects the button, 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` and `label` parts. */
24
+ classNames?: RadioButtonClassNames;
25
+ /** Inline styles for the `root` and `label` parts. */
26
+ styles?: RadioButtonStyles;
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. */
32
+ title?: string;
33
+ };
34
+ declare const RadioButton: import("svelte").Component<RadioButtonProps, {
35
+ focus: (options?: FocusOptions) => void;
36
+ blur: () => void;
37
+ }, "checked">;
38
+ type RadioButton = ReturnType<typeof RadioButton>;
39
+ export default RadioButton;
@@ -0,0 +1,238 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte';
3
+ import type { HTMLAttributes } from 'svelte/elements';
4
+ import type {
5
+ RadioButtonStyle,
6
+ RadioGroupClassNames,
7
+ RadioGroupStyles,
8
+ RadioOption,
9
+ RadioOptionType,
10
+ RadioOrientation,
11
+ RadioSize,
12
+ RadioValue
13
+ } from './types.js';
14
+ import Radio from './Radio.svelte';
15
+ import RadioButton from './RadioButton.svelte';
16
+ import { setRadioGroup } from './context.js';
17
+ import {
18
+ RADIO_BUTTON_STYLES,
19
+ RADIO_OPTION_TYPES,
20
+ RADIO_ORIENTATIONS,
21
+ RADIO_SIZES,
22
+ isNone,
23
+ normalizeSize
24
+ } from './options.js';
25
+ import { resolveOption } from '../internal/options.js';
26
+ import { classList, styleText } from '../internal/semantic.js';
27
+
28
+ type RadioGroupProps = Omit<
29
+ HTMLAttributes<HTMLDivElement>,
30
+ 'children' | 'class' | 'style' | 'role' | 'onchange'
31
+ > & {
32
+ /**
33
+ * The selected value; `undefined` and `null` mean nothing is selected. Use `bind:value` for
34
+ * two-way binding.
35
+ */
36
+ value?: RadioValue | null;
37
+ /** Renders one radio per entry. A string, a number or a boolean is both the value and the label. */
38
+ options?: ReadonlyArray<RadioValue | RadioOption>;
39
+ /** What `options` render: plain radios (`default`) or buttons (`button`). */
40
+ optionType?: RadioOptionType;
41
+ /** Custom markup with `Radio` and `RadioButton` elements inside; ignored when `options` has entries. */
42
+ children?: Snippet;
43
+ /** How a checked button is drawn, for every `RadioButton` inside. */
44
+ buttonStyle?: RadioButtonStyle;
45
+ /** The size of the buttons; plain radios ignore it. `middle` and `default` are aliases of `medium`. */
46
+ size?: RadioSize;
47
+ /** The direction of the items. When it is set it wins over `vertical`. */
48
+ orientation?: RadioOrientation;
49
+ /** A shorthand for `orientation="vertical"`. */
50
+ vertical?: boolean;
51
+ /** The group fills the width of its parent and its items share it equally. */
52
+ block?: boolean;
53
+ /** Disables every radio of the group. */
54
+ disabled?: boolean;
55
+ /** Native `name` of every input of the group. Without one the group makes a unique name of its own. */
56
+ name?: string;
57
+ /** Called once per user selection with the new value and the `change` event of the selected input. */
58
+ onchange?: (value: RadioValue, event: Event) => void;
59
+ /** Extra classes for the `root`, `item`, `itemIcon` and `itemLabel` parts. */
60
+ classNames?: RadioGroupClassNames;
61
+ /** Inline styles for the `root`, `item`, `itemIcon` and `itemLabel` parts. */
62
+ styles?: RadioGroupStyles;
63
+ /** Additional CSS class appended to the root classes. */
64
+ class?: string;
65
+ /** Inline style of the root; `styles.root` is applied after it. */
66
+ style?: string;
67
+ };
68
+
69
+ let {
70
+ value = $bindable(undefined),
71
+ options = [],
72
+ optionType = 'default',
73
+ children,
74
+ buttonStyle = 'outline',
75
+ size = 'medium',
76
+ orientation = undefined,
77
+ vertical = false,
78
+ block = false,
79
+ disabled = false,
80
+ name = undefined,
81
+ onchange,
82
+ classNames,
83
+ styles,
84
+ class: customClass,
85
+ style: customStyle,
86
+ ...rest
87
+ }: RadioGroupProps = $props();
88
+
89
+ // Stable between the server and the client, unique per group.
90
+ const uid = $props.id();
91
+
92
+ const resolved = $derived({
93
+ optionType: resolveOption('RadioGroup', 'optionType', optionType, RADIO_OPTION_TYPES, 'default'),
94
+ buttonStyle: resolveOption('RadioGroup', 'buttonStyle', buttonStyle, RADIO_BUTTON_STYLES, 'outline'),
95
+ size: normalizeSize(resolveOption('RadioGroup', 'size', size, RADIO_SIZES, 'medium')),
96
+ // `orientation` wins over `vertical` when both are given, as in React.
97
+ orientation:
98
+ orientation === undefined
99
+ ? vertical
100
+ ? ('vertical' as const)
101
+ : ('horizontal' as const)
102
+ : resolveOption('RadioGroup', 'orientation', orientation, RADIO_ORIENTATIONS, 'horizontal')
103
+ });
104
+
105
+ // A group without a name of its own gets a unique one, so the browser treats its inputs as one native
106
+ // radio group (one Tab stop, arrow keys that stay inside). It has no form owner then, so a surrounding
107
+ // `<form>` does not submit that made-up name.
108
+ const generatedName = `ads-radio-group-${uid}`;
109
+ const inputName = $derived(name ? name : generatedName);
110
+ const detachedForm = $derived(name ? undefined : `${generatedName}-no-form`);
111
+
112
+ const normalized = $derived<RadioOption[]>(
113
+ options.map((option) =>
114
+ typeof option === 'object' ? option : { label: String(option), value: option }
115
+ )
116
+ );
117
+ const hasOptions = $derived(normalized.length > 0);
118
+
119
+ // The functions that write the state of each radio inside back to its input.
120
+ let syncs: Array<() => void> = [];
121
+
122
+ function select(item: RadioValue, event: Event): void {
123
+ value = item;
124
+ onchange?.(item, event);
125
+ // The browser unchecked the previous radio on its own; if the parent refused the change (function
126
+ // binding) or chose another value, the view has to be put back to the state, for every radio.
127
+ for (const sync of syncs) sync();
128
+ }
129
+
130
+ setRadioGroup({
131
+ isChecked: (item) => !isNone(value) && value === item,
132
+ get disabled() {
133
+ return disabled;
134
+ },
135
+ get name() {
136
+ return inputName;
137
+ },
138
+ get form() {
139
+ return detachedForm;
140
+ },
141
+ get buttonStyle() {
142
+ return resolved.buttonStyle;
143
+ },
144
+ get size() {
145
+ return resolved.size;
146
+ },
147
+ get orientation() {
148
+ return resolved.orientation;
149
+ },
150
+ get block() {
151
+ return block;
152
+ },
153
+ get classNames() {
154
+ return classNames;
155
+ },
156
+ get styles() {
157
+ return styles;
158
+ },
159
+ select,
160
+ register(sync) {
161
+ syncs = [...syncs, sync];
162
+ return () => {
163
+ syncs = syncs.filter((other) => other !== sync);
164
+ };
165
+ }
166
+ });
167
+
168
+ const rootClass = $derived(
169
+ classList(
170
+ 'ads-radio-group',
171
+ `ads-radio-group-${resolved.buttonStyle}`,
172
+ resolved.orientation === 'vertical' && 'ads-radio-group-vertical',
173
+ block && 'ads-radio-group-block',
174
+ customClass,
175
+ classNames?.root
176
+ )
177
+ );
178
+ const rootStyle = $derived(styleText(customStyle, styles?.root));
179
+ </script>
180
+
181
+ <div {...rest} role="radiogroup" class={rootClass} style={rootStyle}>
182
+ {#if hasOptions}
183
+ {#each normalized as option (option.value)}
184
+ {#if resolved.optionType === 'button'}
185
+ <RadioButton
186
+ value={option.value}
187
+ disabled={option.disabled}
188
+ title={option.title}
189
+ id={option.id}
190
+ required={option.required}
191
+ class={option.class}
192
+ style={option.style}
193
+ onchange={option.onchange}
194
+ >
195
+ {#if typeof option.label === 'string'}{option.label}{:else}{@render option.label()}{/if}
196
+ </RadioButton>
197
+ {:else}
198
+ <Radio
199
+ value={option.value}
200
+ disabled={option.disabled}
201
+ title={option.title}
202
+ id={option.id}
203
+ required={option.required}
204
+ class={option.class}
205
+ style={option.style}
206
+ onchange={option.onchange}
207
+ >
208
+ {#if typeof option.label === 'string'}{option.label}{:else}{@render option.label()}{/if}
209
+ </Radio>
210
+ {/if}
211
+ {/each}
212
+ {:else}
213
+ {@render children?.()}
214
+ {/if}
215
+ </div>
216
+
217
+ <style>
218
+ .ads-radio-group {
219
+ display: inline-flex;
220
+ flex-wrap: wrap;
221
+ box-sizing: border-box;
222
+ color: var(--ads-color-text);
223
+ font-family: var(--ads-font-family);
224
+ font-size: var(--ads-radio-font-size);
225
+ line-height: var(--ads-radio-line-height);
226
+ }
227
+
228
+ .ads-radio-group-vertical {
229
+ flex-direction: column;
230
+ flex-wrap: nowrap;
231
+ }
232
+
233
+ /* `block`: as wide as the parent; the items share the width (they carry their own `flex`). */
234
+ .ads-radio-group-block {
235
+ display: flex;
236
+ width: 100%;
237
+ }
238
+ </style>
@@ -0,0 +1,43 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type { HTMLAttributes } from 'svelte/elements';
3
+ import type { RadioButtonStyle, RadioGroupClassNames, RadioGroupStyles, RadioOption, RadioOptionType, RadioOrientation, RadioSize, RadioValue } from './types.js';
4
+ type RadioGroupProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class' | 'style' | 'role' | 'onchange'> & {
5
+ /**
6
+ * The selected value; `undefined` and `null` mean nothing is selected. Use `bind:value` for
7
+ * two-way binding.
8
+ */
9
+ value?: RadioValue | null;
10
+ /** Renders one radio per entry. A string, a number or a boolean is both the value and the label. */
11
+ options?: ReadonlyArray<RadioValue | RadioOption>;
12
+ /** What `options` render: plain radios (`default`) or buttons (`button`). */
13
+ optionType?: RadioOptionType;
14
+ /** Custom markup with `Radio` and `RadioButton` elements inside; ignored when `options` has entries. */
15
+ children?: Snippet;
16
+ /** How a checked button is drawn, for every `RadioButton` inside. */
17
+ buttonStyle?: RadioButtonStyle;
18
+ /** The size of the buttons; plain radios ignore it. `middle` and `default` are aliases of `medium`. */
19
+ size?: RadioSize;
20
+ /** The direction of the items. When it is set it wins over `vertical`. */
21
+ orientation?: RadioOrientation;
22
+ /** A shorthand for `orientation="vertical"`. */
23
+ vertical?: boolean;
24
+ /** The group fills the width of its parent and its items share it equally. */
25
+ block?: boolean;
26
+ /** Disables every radio of the group. */
27
+ disabled?: boolean;
28
+ /** Native `name` of every input of the group. Without one the group makes a unique name of its own. */
29
+ name?: string;
30
+ /** Called once per user selection with the new value and the `change` event of the selected input. */
31
+ onchange?: (value: RadioValue, event: Event) => void;
32
+ /** Extra classes for the `root`, `item`, `itemIcon` and `itemLabel` parts. */
33
+ classNames?: RadioGroupClassNames;
34
+ /** Inline styles for the `root`, `item`, `itemIcon` and `itemLabel` parts. */
35
+ styles?: RadioGroupStyles;
36
+ /** Additional CSS class appended to the root classes. */
37
+ class?: string;
38
+ /** Inline style of the root; `styles.root` is applied after it. */
39
+ style?: string;
40
+ };
41
+ declare const RadioGroup: import("svelte").Component<RadioGroupProps, {}, "value">;
42
+ type RadioGroup = ReturnType<typeof RadioGroup>;
43
+ export default RadioGroup;