@rohal12/spindle 0.64.21 → 0.64.23

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.
@@ -1,19 +1,18 @@
1
- import { useEffect, useState } from 'preact/hooks';
1
+ import { useEffect, useId, useState } from 'preact/hooks';
2
2
  import { settings, subscribeSettings, type SettingDef } from '../../settings';
3
3
  import { defineMacro } from '../../define-macro';
4
4
 
5
- function SettingControl({ name, def }: { name: string; def: SettingDef }) {
6
- const [value, setValue] = useState(() => settings.get(name));
7
-
8
- // Follow changes made elsewhere: Story.settings.set(), another view
9
- useEffect(() => {
10
- const sync = () => setValue(settings.get(name));
11
- sync();
12
- return subscribeSettings(sync);
13
- }, [name]);
14
-
15
- const update = (newValue: unknown) => settings.set(name, newValue);
16
-
5
+ function SettingRow({
6
+ def,
7
+ value,
8
+ describedBy,
9
+ update,
10
+ }: {
11
+ def: SettingDef;
12
+ value: unknown;
13
+ describedBy: string | undefined;
14
+ update: (value: unknown) => void;
15
+ }) {
17
16
  switch (def.type) {
18
17
  case 'toggle':
19
18
  return (
@@ -22,6 +21,7 @@ function SettingControl({ name, def }: { name: string; def: SettingDef }) {
22
21
  <input
23
22
  type="checkbox"
24
23
  checked={!!value}
24
+ aria-describedby={describedBy}
25
25
  onChange={(e) => update((e.target as HTMLInputElement).checked)}
26
26
  />
27
27
  </label>
@@ -33,6 +33,7 @@ function SettingControl({ name, def }: { name: string; def: SettingDef }) {
33
33
  <span>{def.config.label}</span>
34
34
  <select
35
35
  value={String(value)}
36
+ aria-describedby={describedBy}
36
37
  onChange={(e) => update((e.target as HTMLSelectElement).value)}
37
38
  >
38
39
  {def.config.options.map((opt) => (
@@ -59,6 +60,7 @@ function SettingControl({ name, def }: { name: string; def: SettingDef }) {
59
60
  max={def.config.max}
60
61
  step={def.config.step}
61
62
  value={Number(value)}
63
+ aria-describedby={describedBy}
62
64
  onInput={(e) =>
63
65
  update(parseFloat((e.target as HTMLInputElement).value))
64
66
  }
@@ -68,6 +70,46 @@ function SettingControl({ name, def }: { name: string; def: SettingDef }) {
68
70
  }
69
71
  }
70
72
 
73
+ function SettingControl({ name, def }: { name: string; def: SettingDef }) {
74
+ const [value, setValue] = useState(() => settings.get(name));
75
+ const descriptionId = useId();
76
+ // Plain text, never markup. No description, no element.
77
+ const text = def.config.description;
78
+ const description =
79
+ typeof text === 'string' && text.trim() !== '' ? text : null;
80
+
81
+ // Follow changes made elsewhere: Story.settings.set(), another view
82
+ useEffect(() => {
83
+ const sync = () => setValue(settings.get(name));
84
+ sync();
85
+ return subscribeSettings(sync);
86
+ }, [name]);
87
+
88
+ const update = (newValue: unknown) => settings.set(name, newValue);
89
+
90
+ const row = (
91
+ <SettingRow
92
+ def={def}
93
+ value={value}
94
+ describedBy={description === null ? undefined : descriptionId}
95
+ update={update}
96
+ />
97
+ );
98
+ if (description === null) return row;
99
+
100
+ return (
101
+ <div class="settings-item">
102
+ {row}
103
+ <p
104
+ id={descriptionId}
105
+ class="settings-description"
106
+ >
107
+ {description}
108
+ </p>
109
+ </div>
110
+ );
111
+ }
112
+
71
113
  function SettingsControlsContent({
72
114
  className,
73
115
  id,
package/src/settings.ts CHANGED
@@ -2,19 +2,23 @@ import { emit } from './event-emitter';
2
2
  import { useStoryStore } from './store';
3
3
  import { createNamespace, hasOwn, ownValue } from './utils/namespace';
4
4
 
5
- export interface ToggleConfig {
5
+ /** What every setting type shares. */
6
+ interface SettingConfigBase {
6
7
  label: string;
8
+ /** Plain text shown with the setting in `{settings-controls}`. */
9
+ description?: string;
10
+ }
11
+
12
+ export interface ToggleConfig extends SettingConfigBase {
7
13
  default: boolean;
8
14
  }
9
15
 
10
- export interface ListConfig {
11
- label: string;
16
+ export interface ListConfig extends SettingConfigBase {
12
17
  options: string[];
13
18
  default: string;
14
19
  }
15
20
 
16
- export interface RangeConfig {
17
- label: string;
21
+ export interface RangeConfig extends SettingConfigBase {
18
22
  min: number;
19
23
  max: number;
20
24
  step: number;
package/src/styles.css CHANGED
@@ -317,6 +317,18 @@ tw-storydata {
317
317
  gap: 1em;
318
318
  }
319
319
 
320
+ .settings-item {
321
+ display: flex;
322
+ flex-direction: column;
323
+ gap: 0.25em;
324
+ }
325
+
326
+ .settings-description {
327
+ margin: 0;
328
+ font-size: 0.85em;
329
+ color: #9e9e9e;
330
+ }
331
+
320
332
  .settings-row select,
321
333
  .settings-row input[type='range'] {
322
334
  accent-color: #64b5f6;
package/types/index.d.ts CHANGED
@@ -42,12 +42,21 @@ export interface SavePayload {
42
42
  prng?: { seed: string; pull: number } | null;
43
43
  }
44
44
 
45
+ /** What every setting type shares. */
46
+ interface SettingConfigBase {
47
+ label: string;
48
+ /**
49
+ * Plain text shown with the setting in `{settings-controls}`, under its
50
+ * control. Not markup.
51
+ */
52
+ description?: string;
53
+ }
54
+
45
55
  /**
46
56
  * Configuration for a toggle (boolean) setting.
47
57
  * @see {@link ../../src/settings.ts} for the implementation.
48
58
  */
49
- export interface ToggleConfig {
50
- label: string;
59
+ export interface ToggleConfig extends SettingConfigBase {
51
60
  default: boolean;
52
61
  }
53
62
 
@@ -55,8 +64,7 @@ export interface ToggleConfig {
55
64
  * Configuration for a list (dropdown) setting.
56
65
  * @see {@link ../../src/settings.ts} for the implementation.
57
66
  */
58
- export interface ListConfig {
59
- label: string;
67
+ export interface ListConfig extends SettingConfigBase {
60
68
  options: string[];
61
69
  default: string;
62
70
  }
@@ -65,8 +73,7 @@ export interface ListConfig {
65
73
  * Configuration for a range (slider) setting.
66
74
  * @see {@link ../../src/settings.ts} for the implementation.
67
75
  */
68
- export interface RangeConfig {
69
- label: string;
76
+ export interface RangeConfig extends SettingConfigBase {
70
77
  min: number;
71
78
  max: number;
72
79
  step: number;