@linxin666/dsh-client-ui-task-board 0.4.9 → 0.4.11

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,15 +1,23 @@
1
1
  // Generated by scripts/sync-shared.mjs from shared/client/settings/PluginSettingsCard.tsx. Do not edit this copy; edit the shared source and run "node scripts/sync-shared.mjs".
2
2
  /**
3
3
  * Family-shared chrome for plugin settings cards: a disclosure header naming
4
- * the plugin and what its settings govern, the controls inside, and the save
5
- * that writes them. Renders nothing while the namespace is unavailable — a
4
+ * the plugin and what its settings govern, the controls inside, and the line
5
+ * that reports a refused write. A pick is written the moment it is made and a
6
+ * typed value after a pause, so the card has no save button and nothing waits
7
+ * on the reader. Renders nothing while the namespace is unavailable — a
6
8
  * deployment that does not compose the owning plugin should show no trace of
7
9
  * it. Inlined into each consumer's client bundle; mirrors the official
8
10
  * ui-plugin-config PluginCard in a self-contained slice.
11
+ *
12
+ * Every field inside the body is one settings row: the label and its hint on
13
+ * the left, the control on the right, a hairline between two rows; a narrow
14
+ * card stacks the control under the text (settings-card.module.css).
9
15
  */
10
16
 
11
- import { useCallback, useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent, type ReactNode } from 'react'
17
+ import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent, type ReactNode } from 'react'
12
18
  import type { CardShell } from './settings-form.ts'
19
+ import { RangeBubble } from './RangeBubble.tsx'
20
+ import { SegmentGroup } from './SegmentGroup.tsx'
13
21
  import css from './settings-card.module.css'
14
22
 
15
23
  /** Copy keys the card chrome itself reads; every consumer locale carries this shared vocabulary. */
@@ -17,14 +25,17 @@ export const CARD_COPY_KEYS = [
17
25
  'settings.collapse',
18
26
  'settings.expand',
19
27
  'settings.notExposed',
20
- 'settings.unsaved',
21
28
  'settings.readOnly',
22
29
  'settings.saveFailed',
23
- 'settings.discard',
24
- 'settings.save',
25
- 'settings.saving',
26
30
  ] as const
27
31
 
32
+ /**
33
+ * What a text field inside a card reaches without its plugin wiring it: write
34
+ * the pending draft now (Enter, leaving the field) or put it back (Escape).
35
+ * Picks need neither; they are written the moment they are made.
36
+ */
37
+ const CardApply = createContext<{ commit(): void, revert(): void } | null>(null)
38
+
28
39
  /** Copy key the card chrome itself reads. */
29
40
  export type CardCopyKey = (typeof CARD_COPY_KEYS)[number]
30
41
 
@@ -56,11 +67,11 @@ export interface PluginSettingsCardProps<TKey extends string = string> {
56
67
  * seat.
57
68
  */
58
69
  icon?: ReactNode
59
- /** The card's form state: availability, writability, and what a save would do. */
70
+ /** The card's form state: availability, writability, and how the last write went. */
60
71
  state: CardShell
61
- /** Write every staged edit. */
72
+ /** Write every pending draft now: a text field's Enter or blur. */
62
73
  onSave: () => void
63
- /** Drop every staged edit. */
74
+ /** Drop every pending draft: a text field's Escape. */
64
75
  onDiscard: () => void
65
76
  /**
66
77
  * Id applied to the card's title element, so a header control (an immediate
@@ -87,15 +98,15 @@ export interface PluginSettingsCardProps<TKey extends string = string> {
87
98
  */
88
99
  alwaysOpen?: boolean
89
100
  /**
90
- * Hide the save/discard footer: cards whose body applies its own changes
91
- * immediately (an embedded external settings section) have no staged
92
- * edits, so the footer would sit there permanently disabled.
101
+ * Hide the line under the body that reports a refused write: a card whose
102
+ * body applies its own changes (an embedded external settings section)
103
+ * reports its own failures.
93
104
  */
94
105
  hideFooter?: boolean
95
106
  /**
96
- * Optional control rendered at the header's trailing edge, before the pending
97
- * badge and the chevron. A card whose setting is one immediate flip (it
98
- * applies outside the staged form) puts its ToggleSwitch here, so the control
107
+ * Optional control rendered at the header's trailing edge, before the
108
+ * chevron. A card whose setting is one immediate flip (it
109
+ * applies outside the card's form) puts its ToggleSwitch here, so the control
99
110
  * is reachable without opening the card and matches a plugin card's shape.
100
111
  */
101
112
  headerAction?: ReactNode
@@ -162,7 +173,6 @@ export function PluginSettingsCard<TKey extends string = string>(props: PluginSe
162
173
  const title = props.t(props.titleKey)
163
174
  const description = props.t(props.descriptionKey)
164
175
  const descriptionTitle = props.descriptionTitleKey === undefined ? description : props.t(props.descriptionTitleKey)
165
- const blocked = !state.dirty || state.invalid || state.saving
166
176
  const baseClass = expanded ? `${css.cardOpen} ${css.card}` : css.card
167
177
  const cardClass = props.density === 'nested' ? `${baseClass} ${css.cardNested}` : baseClass
168
178
  /** The body region both legs share; a hidden phase renders nothing at all. */
@@ -184,7 +194,6 @@ export function PluginSettingsCard<TKey extends string = string>(props: PluginSe
184
194
  <span className={css.description} title={descriptionTitle}>{props.descriptionNode ?? description}</span>
185
195
  </span>
186
196
  {props.headerAction !== undefined ? <span className={css.headerAction}>{props.headerAction}</span> : null}
187
- {state.dirty ? <span className={css.pending} title={props.t('settings.unsaved')}>{props.t('settings.unsaved')}</span> : null}
188
197
  </div>
189
198
  )
190
199
  : (
@@ -201,7 +210,6 @@ export function PluginSettingsCard<TKey extends string = string>(props: PluginSe
201
210
  <span className={css.description} title={descriptionTitle}>{props.descriptionNode ?? description}</span>
202
211
  </span>
203
212
  {props.headerAction !== undefined ? <span className={css.headerAction}>{props.headerAction}</span> : null}
204
- {state.dirty ? <span className={css.pending} title={props.t('settings.unsaved')}>{props.t('settings.unsaved')}</span> : null}
205
213
  <svg
206
214
  width="14"
207
215
  height="14"
@@ -240,36 +248,16 @@ export function PluginSettingsCard<TKey extends string = string>(props: PluginSe
240
248
  ? bodyRegion(
241
249
  <>
242
250
  {!state.writable ? <p className={css.readOnly} role="status">{props.t('settings.readOnly')}</p> : null}
243
- {props.children}
244
- {props.hideFooter === true
245
- ? null
246
- : (
247
- <div className={css.footer}>
248
- {state.failed
249
- ? (
250
- <p className={css.failed} role="status">
251
- {props.t('settings.saveFailed')}{state.failedReason ? ' - ' + state.failedReason : ''}
252
- </p>
253
- )
254
- : null}
255
- <button
256
- type="button"
257
- className={css.discard}
258
- disabled={!state.dirty || state.saving}
259
- onClick={props.onDiscard}
260
- >
261
- {props.t('settings.discard')}
262
- </button>
263
- <button
264
- type="button"
265
- className={css.save}
266
- disabled={blocked}
267
- onClick={props.onSave}
268
- >
269
- {props.t(!state.saving ? 'settings.save' : 'settings.saving')}
270
- </button>
271
- </div>
272
- )}
251
+ <CardApply.Provider value={{ commit: props.onSave, revert: props.onDiscard }}>
252
+ {props.children}
253
+ </CardApply.Provider>
254
+ {props.hideFooter !== true && state.failed
255
+ ? (
256
+ <p className={css.failed} role="status">
257
+ {props.t('settings.saveFailed')}{state.failedReason ? ' - ' + state.failedReason : ''}
258
+ </p>
259
+ )
260
+ : null}
273
261
  </>,
274
262
  )
275
263
  : null}
@@ -293,7 +281,7 @@ export interface FieldProps {
293
281
  hintTitle?: string
294
282
  /** Draft text this control renders. */
295
283
  text: string
296
- /** True when saving would leave a user-layer entry for this field. */
284
+ /** True when the field holds a user-layer entry over the inherited value. */
297
285
  overridden: boolean
298
286
  /** True when the draft is not a value this field accepts. */
299
287
  invalid: boolean
@@ -305,19 +293,52 @@ export interface FieldProps {
305
293
  invalidLabel: string
306
294
  /** Disables every control (read-only document, or an unavailable namespace). */
307
295
  disabled: boolean
308
- /** Stage draft text. */
296
+ /** Take new draft text; the card writes it as the field's entry rule says. */
309
297
  onEdit: (text: string) => void
310
- /** Stage a clear so the field re-inherits the composition layer. */
298
+ /** Clear the field at once so it re-inherits the composition layer. */
311
299
  onReset: () => void
312
300
  }
313
301
 
314
- /** A staged value field. `numeric` only hints the keypad: which drafts a field accepts is decided by its spec. */
302
+ /**
303
+ * A typed value field. Its draft is written after a pause in typing, at once on
304
+ * Enter or when focus leaves, and Escape puts the stored value back. `numeric`
305
+ * only hints the keypad: which drafts a field accepts is decided by its spec.
306
+ * A field given a `range` also offers the coarse slider bubble: dragging fills
307
+ * the draft live and releasing writes it.
308
+ */
315
309
  export function ValueField(props: FieldProps & {
316
310
  /** Hints a numeric keypad without narrowing what the control accepts. */
317
311
  numeric?: boolean
318
312
  /** Placeholder shown while the draft is empty. */
319
313
  placeholder?: string
314
+ /** Bounds of a numeric field, for the coarse slider; the spec still judges every draft. */
315
+ range?: { min: number, max: number, step: number }
320
316
  }) {
317
+ const apply = useContext(CardApply)
318
+ const onKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
319
+ if (apply === null || (event.key !== 'Enter' && event.key !== 'Escape')) return
320
+ event.preventDefault()
321
+ if (event.key === 'Enter') apply.commit()
322
+ else apply.revert()
323
+ event.currentTarget.blur()
324
+ }
325
+ const input = (
326
+ <input
327
+ id={props.id}
328
+ className={props.invalid ? css.inputInvalid : css.input}
329
+ type="text"
330
+ {...props.numeric === true ? { inputMode: 'numeric' as const } : {}}
331
+ {...props.invalid ? { 'aria-invalid': true } : {}}
332
+ value={props.text}
333
+ placeholder={props.placeholder ?? ''}
334
+ disabled={props.disabled}
335
+ onChange={(event) => { props.onEdit(event.target.value) }}
336
+ onKeyDown={onKeyDown}
337
+ onBlur={() => { apply?.commit() }}
338
+ />
339
+ )
340
+ const { range } = props
341
+ const typed = Number(props.text)
321
342
  return (
322
343
  <div className={css.field}>
323
344
  <div className={css.head}>
@@ -338,17 +359,25 @@ export function ValueField(props: FieldProps & {
338
359
  )
339
360
  : null}
340
361
  </div>
341
- <input
342
- id={props.id}
343
- className={props.invalid ? css.inputInvalid : css.input}
344
- type="text"
345
- {...props.numeric === true ? { inputMode: 'numeric' as const } : {}}
346
- {...props.invalid ? { 'aria-invalid': true } : {}}
347
- value={props.text}
348
- placeholder={props.placeholder ?? ''}
349
- disabled={props.disabled}
350
- onChange={(event) => { props.onEdit(event.target.value) }}
351
- />
362
+ {range === undefined
363
+ ? input
364
+ : (
365
+ <RangeBubble
366
+ min={range.min}
367
+ max={range.max}
368
+ step={range.step}
369
+ value={props.text.trim() === '' || Number.isNaN(typed) ? range.min : typed}
370
+ label={`${props.label} ${range.min}–${range.max}`}
371
+ disabled={props.disabled}
372
+ onInput={(value) => { props.onEdit(String(value)) }}
373
+ onCommit={(value) => {
374
+ props.onEdit(String(value))
375
+ apply?.commit()
376
+ }}
377
+ >
378
+ {input}
379
+ </RangeBubble>
380
+ )}
352
381
  <p className={props.invalid ? css.invalid : css.hint} title={props.hintTitle}>
353
382
  {props.invalid ? props.invalidLabel : props.hint}
354
383
  </p>
@@ -376,8 +405,8 @@ const SELECT_CLOSE_MS = 100
376
405
  * renders the legacy native `<select>` untouched, so element-level skin
377
406
  * selectors keep working; under the default appearance it renders a
378
407
  * self-drawn `role="listbox"` popup whose open/close is transition-animated.
379
- * Staged cards reach it through BooleanField/ChoiceField; immediate-apply
380
- * editors (the side-card prefs) bind it directly through onEdit.
408
+ * Cards reach it through BooleanField/ChoiceField; editors outside a card
409
+ * (the side-card prefs) bind it directly through onEdit.
381
410
  * 双模式下拉框:皮肤激活时用原生 select,默认外观用自绘动画弹层。
382
411
  */
383
412
  export function SelectField(props: {
@@ -560,7 +589,56 @@ export function SelectField(props: {
560
589
  )
561
590
  }
562
591
 
563
- /** A staged boolean field: 继承 / 开 / 关. */
592
+ /**
593
+ * A visible segmented group for a small set of mutually exclusive values.
594
+ * Every option stays on screen, so inherit/on/off and short enum fields take
595
+ * one click without opening a popup. The pressed option carries the group's
596
+ * sliding highlight and `aria-pressed`; the group takes the field label as its
597
+ * accessible name.
598
+ */
599
+ export function SegmentField(props: {
600
+ id: string
601
+ label: string
602
+ options: ReadonlyArray<SelectOption>
603
+ value: string
604
+ disabled: boolean
605
+ invalid: boolean
606
+ onEdit: (text: string) => void
607
+ }) {
608
+ return (
609
+ <SegmentGroup
610
+ id={props.id}
611
+ role="group"
612
+ aria-label={props.label}
613
+ aria-invalid={props.invalid || undefined}
614
+ >
615
+ {props.options.map((option, index) => {
616
+ const active = option.value === props.value
617
+ return (
618
+ <button
619
+ key={option.value}
620
+ type="button"
621
+ className={css.segment}
622
+ aria-pressed={active}
623
+ disabled={props.disabled}
624
+ data-active={active ? '' : undefined}
625
+ data-segment-index={index}
626
+ onClick={() => {
627
+ if (!active) props.onEdit(option.value)
628
+ }}
629
+ >
630
+ {option.label}
631
+ </button>
632
+ )
633
+ })}
634
+ </SegmentGroup>
635
+ )
636
+ }
637
+
638
+ /** How a small single-choice field presents its options. */
639
+ export type FieldPresentation = 'select' | 'segment'
640
+
641
+ /** A boolean field: 继承 / 开 / 关. */
564
642
  export function BooleanField(props: FieldProps & {
565
643
  /** Copy for the inherit option. */
566
644
  inheritLabel: string
@@ -568,11 +646,16 @@ export function BooleanField(props: FieldProps & {
568
646
  onLabel: string
569
647
  /** Copy for the off option. */
570
648
  offLabel: string
649
+ /** Defaults to the visible segments: the three answers always fit on one row. */
650
+ presentation?: FieldPresentation
571
651
  }) {
652
+ const segmented = props.presentation !== 'select'
572
653
  return (
573
654
  <div className={css.field}>
574
655
  <div className={css.head}>
575
- <label className={css.label} htmlFor={props.id}>{props.label}</label>
656
+ {segmented
657
+ ? <span className={css.label}>{props.label}</span>
658
+ : <label className={css.label} htmlFor={props.id}>{props.label}</label>}
576
659
  {props.overridden
577
660
  ? (
578
661
  <span className={css.badges}>
@@ -589,34 +672,61 @@ export function BooleanField(props: FieldProps & {
589
672
  )
590
673
  : null}
591
674
  </div>
592
- <SelectField
593
- id={props.id}
594
- options={[
595
- { value: '', label: props.inheritLabel },
596
- { value: 'true', label: props.onLabel },
597
- { value: 'false', label: props.offLabel },
598
- ]}
599
- value={props.text}
600
- disabled={props.disabled}
601
- invalid={props.invalid}
602
- onEdit={props.onEdit}
603
- />
675
+ {segmented
676
+ ? (
677
+ <SegmentField
678
+ id={props.id}
679
+ label={props.label}
680
+ options={[
681
+ { value: '', label: props.inheritLabel },
682
+ { value: 'true', label: props.onLabel },
683
+ { value: 'false', label: props.offLabel },
684
+ ]}
685
+ value={props.text}
686
+ disabled={props.disabled}
687
+ invalid={props.invalid}
688
+ onEdit={props.onEdit}
689
+ />
690
+ )
691
+ : (
692
+ <SelectField
693
+ id={props.id}
694
+ options={[
695
+ { value: '', label: props.inheritLabel },
696
+ { value: 'true', label: props.onLabel },
697
+ { value: 'false', label: props.offLabel },
698
+ ]}
699
+ value={props.text}
700
+ disabled={props.disabled}
701
+ invalid={props.invalid}
702
+ onEdit={props.onEdit}
703
+ />
704
+ )}
604
705
  <p className={css.hint} title={props.hintTitle}>{props.hint}</p>
605
706
  </div>
606
707
  )
607
708
  }
608
709
 
609
- /** A staged enumerated field rendered as a select. */
710
+ /** An enumerated field, selecting or segmenting its small choice set. */
610
711
  export function ChoiceField(props: FieldProps & {
611
712
  /** Copy for the inherit option (draft text is the empty string). */
612
713
  inheritLabel: string
613
714
  /** Choices rendered in order; `value` is the draft/stored text. */
614
715
  choices: ReadonlyArray<{ value: string; label: string }>
716
+ /**
717
+ * Visible segmented options for a short fixed set; defaults to the compact
718
+ * select, which also suits a list whose length follows the deployment.
719
+ */
720
+ presentation?: FieldPresentation
615
721
  }) {
722
+ const segmented = props.presentation === 'segment'
723
+ const options = [{ value: '', label: props.inheritLabel }, ...props.choices]
616
724
  return (
617
725
  <div className={css.field}>
618
726
  <div className={css.head}>
619
- <label className={css.label} htmlFor={props.id}>{props.label}</label>
727
+ {segmented
728
+ ? <span className={css.label}>{props.label}</span>
729
+ : <label className={css.label} htmlFor={props.id}>{props.label}</label>}
620
730
  {props.overridden
621
731
  ? (
622
732
  <span className={css.badges}>
@@ -633,14 +743,28 @@ export function ChoiceField(props: FieldProps & {
633
743
  )
634
744
  : null}
635
745
  </div>
636
- <SelectField
637
- id={props.id}
638
- options={[{ value: '', label: props.inheritLabel }, ...props.choices]}
639
- value={props.text}
640
- disabled={props.disabled}
641
- invalid={props.invalid}
642
- onEdit={props.onEdit}
643
- />
746
+ {segmented
747
+ ? (
748
+ <SegmentField
749
+ id={props.id}
750
+ label={props.label}
751
+ options={options}
752
+ value={props.text}
753
+ disabled={props.disabled}
754
+ invalid={props.invalid}
755
+ onEdit={props.onEdit}
756
+ />
757
+ )
758
+ : (
759
+ <SelectField
760
+ id={props.id}
761
+ options={options}
762
+ value={props.text}
763
+ disabled={props.disabled}
764
+ invalid={props.invalid}
765
+ onEdit={props.onEdit}
766
+ />
767
+ )}
644
768
  <p className={props.invalid ? css.invalid : css.hint} title={props.hintTitle}>
645
769
  {props.invalid ? props.invalidLabel : props.hint}
646
770
  </p>
@@ -649,11 +773,15 @@ export function ChoiceField(props: FieldProps & {
649
773
  }
650
774
 
651
775
  /**
652
- * An immediate boolean toggle drawn as a switch track, for a setting that
653
- * applies on flip rather than through a staged save. It is a real button with
776
+ * A boolean toggle drawn as a switch track, for a setting that is only on or
777
+ * off and applies on flip. It is a real button with
654
778
  * `role="switch"` and `aria-checked`, so keyboard and assistive activation
655
779
  * arrive as a click and the state is announced instead of inferred from a
656
780
  * checkbox tick.
781
+ *
782
+ * The thumb bounces across only once the reader has pressed this switch: the
783
+ * press writes the mark that arms the bounce, so a page opening, a TAB
784
+ * switching back or a value arriving from elsewhere draws every thumb at rest.
657
785
  */
658
786
  export function ToggleSwitch(props: {
659
787
  /** Id of the element naming the switch (the card's title). */
@@ -667,6 +795,7 @@ export function ToggleSwitch(props: {
667
795
  /** Called with the requested next state. */
668
796
  onChange: (next: boolean) => void
669
797
  }) {
798
+ const [moved, setMoved] = useState(false)
670
799
  return (
671
800
  <button
672
801
  type="button"
@@ -676,7 +805,11 @@ export function ToggleSwitch(props: {
676
805
  aria-labelledby={props.labelledBy}
677
806
  aria-label={props.labelledBy === undefined ? props.ariaLabel : undefined}
678
807
  disabled={props.disabled}
679
- onClick={() => { props.onChange(!props.checked) }}
808
+ data-moved={moved ? '' : undefined}
809
+ onClick={() => {
810
+ setMoved(true)
811
+ props.onChange(!props.checked)
812
+ }}
680
813
  >
681
814
  <span className={css.toggleTrack}><span className={css.toggleThumb} /></span>
682
815
  </button>
@@ -0,0 +1,164 @@
1
+ // Generated by scripts/sync-shared.mjs from shared/client/settings/RangeBubble.tsx. Do not edit this copy; edit the shared source and run "node scripts/sync-shared.mjs".
2
+ /**
3
+ * Coarse adjustment for a bounded number: resting the pointer on the control
4
+ * (or moving focus into it) raises a small bubble holding a slider across the
5
+ * field's whole range, so a large change is one drag instead of many presses
6
+ * or a retyped value. The control itself stays the precise entry.
7
+ *
8
+ * The bubble belongs to the anchor's subtree, so moving from the control onto
9
+ * the bubble never counts as leaving. It opens after a short rest and closes a
10
+ * moment after the pointer and focus have both gone, so crossing the control
11
+ * on the way elsewhere does not flash it. While a drag is in progress the
12
+ * bubble stays put even if the pointer strays off it.
13
+ *
14
+ * Dragging reports each value through `onInput`, so the field shows it live;
15
+ * releasing the thumb, or a key press on it, reports `onCommit` once. The
16
+ * bubble opens on the side of the control that has room inside the nearest
17
+ * scrolling ancestor, so a row near the top of a settings page does not hang
18
+ * its bubble under the page edge.
19
+ */
20
+
21
+ import { useEffect, useRef, useState, type ChangeEvent, type CSSProperties, type KeyboardEvent, type ReactNode } from 'react'
22
+ import css from './settings-card.module.css'
23
+
24
+ /** Rest on the control before the bubble opens. */
25
+ const OPEN_DELAY_MS = 220
26
+ /** Grace after pointer and focus have left before the bubble closes. */
27
+ const CLOSE_DELAY_MS = 200
28
+ /** Room the bubble needs between the control and the scroller's edge. */
29
+ const BUBBLE_ROOM_PX = 64
30
+
31
+ /** Props of {@link RangeBubble}. */
32
+ export interface RangeBubbleProps {
33
+ /** Lowest value the slider reaches. */
34
+ min: number
35
+ /** Highest value the slider reaches. */
36
+ max: number
37
+ /** Slider granularity. */
38
+ step: number
39
+ /** Current value; a value outside the range parks the thumb at the nearer end. */
40
+ value: number
41
+ /** Accessible name of the slider. */
42
+ label: string
43
+ /** Disables the bubble along with the control. */
44
+ disabled?: boolean
45
+ /** Each value while the thumb moves. */
46
+ onInput: (value: number) => void
47
+ /** The value the thumb was released on, once per drag or key press. */
48
+ onCommit: (value: number) => void
49
+ /** The precise control the bubble hangs off. */
50
+ children: ReactNode
51
+ }
52
+
53
+ /** The nearest ancestor that scrolls vertically, or the viewport's root. */
54
+ function scroller(node: HTMLElement): HTMLElement {
55
+ for (let parent = node.parentElement; parent !== null; parent = parent.parentElement) {
56
+ const overflow = getComputedStyle(parent).overflowY
57
+ if (overflow === 'auto' || overflow === 'scroll') return parent
58
+ }
59
+ return node.ownerDocument.documentElement
60
+ }
61
+
62
+ /**
63
+ * Render the control with its coarse-adjustment bubble.
64
+ * @param props - the range, the value and the two report callbacks.
65
+ * @returns the anchor holding the control and the bubble.
66
+ */
67
+ export function RangeBubble(props: RangeBubbleProps): ReactNode {
68
+ const { min, max, step, disabled = false } = props
69
+ const [open, setOpen] = useState(false)
70
+ const [side, setSide] = useState<'above' | 'below'>('above')
71
+ const [live, setLive] = useState<number | null>(null)
72
+ const anchorRef = useRef<HTMLDivElement | null>(null)
73
+ const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
74
+ const hovered = useRef(false)
75
+ const focused = useRef(false)
76
+ const dragging = useRef(false)
77
+
78
+ useEffect(() => () => { clearTimeout(timer.current) }, [])
79
+ useEffect(() => {
80
+ if (disabled) {
81
+ clearTimeout(timer.current)
82
+ setOpen(false)
83
+ }
84
+ }, [disabled])
85
+
86
+ const show = (): void => {
87
+ const anchor = anchorRef.current
88
+ if (anchor !== null) {
89
+ const room = anchor.getBoundingClientRect().top - scroller(anchor).getBoundingClientRect().top
90
+ setSide(room >= BUBBLE_ROOM_PX ? 'above' : 'below')
91
+ }
92
+ setOpen(true)
93
+ }
94
+ const schedule = (): void => {
95
+ clearTimeout(timer.current)
96
+ if (disabled) return
97
+ if (hovered.current || focused.current || dragging.current) {
98
+ if (!open) timer.current = setTimeout(show, focused.current ? 0 : OPEN_DELAY_MS)
99
+ return
100
+ }
101
+ timer.current = setTimeout(() => { setOpen(false) }, CLOSE_DELAY_MS)
102
+ }
103
+
104
+ const commit = (value: number): void => {
105
+ dragging.current = false
106
+ setLive(null)
107
+ props.onCommit(value)
108
+ schedule()
109
+ }
110
+ const onChange = (event: ChangeEvent<HTMLInputElement>): void => {
111
+ const value = Number(event.target.value)
112
+ setLive(value)
113
+ props.onInput(value)
114
+ }
115
+ const onKeyUp = (event: KeyboardEvent<HTMLInputElement>): void => {
116
+ if (live !== null) commit(live)
117
+ if (event.key === 'Escape') {
118
+ clearTimeout(timer.current)
119
+ setOpen(false)
120
+ }
121
+ }
122
+ const shown = live ?? Math.min(max, Math.max(min, props.value))
123
+
124
+ return (
125
+ <div
126
+ ref={anchorRef}
127
+ className={css.rangeAnchor}
128
+ onPointerEnter={() => { hovered.current = true; schedule() }}
129
+ onPointerLeave={() => { hovered.current = false; schedule() }}
130
+ onFocus={() => { focused.current = true; schedule() }}
131
+ onBlur={(event) => {
132
+ if (event.currentTarget.contains(event.relatedTarget as Node | null)) return
133
+ focused.current = false
134
+ if (live !== null) commit(live)
135
+ else schedule()
136
+ }}
137
+ >
138
+ {props.children}
139
+ <div className={css.rangeBubble} data-open={open ? '' : undefined} data-side={side}>
140
+ <input
141
+ type="range"
142
+ className={css.rangeSlider}
143
+ min={min}
144
+ max={max}
145
+ step={step}
146
+ value={shown}
147
+ aria-label={props.label}
148
+ tabIndex={open ? 0 : -1}
149
+ disabled={disabled}
150
+ style={{ '--range-fill': `${((shown - min) / (max - min)) * 100}%` } as CSSProperties}
151
+ onPointerDown={() => { dragging.current = true }}
152
+ onPointerUp={(event) => { commit(Number(event.currentTarget.value)) }}
153
+ onPointerCancel={(event) => { commit(Number(event.currentTarget.value)) }}
154
+ onChange={onChange}
155
+ onKeyUp={onKeyUp}
156
+ />
157
+ <span className={css.rangeEnds} aria-hidden="true">
158
+ <span>{min}</span>
159
+ <span>{max}</span>
160
+ </span>
161
+ </div>
162
+ </div>
163
+ )
164
+ }