@human-kit/ui 1.0.0-beta.9 → 1.0.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.
Files changed (98) hide show
  1. package/README.md +27 -60
  2. package/dist/FOCUS_STATE_CONTRACT.md +1 -0
  3. package/dist/color-picker/README.md +78 -0
  4. package/dist/color-picker/area/color-picker-area.svelte +195 -0
  5. package/dist/color-picker/area/color-picker-area.svelte.d.ts +4 -0
  6. package/dist/color-picker/area-thumb/color-picker-area-thumb.svelte +231 -0
  7. package/dist/color-picker/area-thumb/color-picker-area-thumb.svelte.d.ts +4 -0
  8. package/dist/color-picker/channel-field/color-picker-channel-field.svelte +95 -0
  9. package/dist/color-picker/channel-field/color-picker-channel-field.svelte.d.ts +4 -0
  10. package/dist/color-picker/eye-dropper/color-picker-eye-dropper.svelte +79 -0
  11. package/dist/color-picker/eye-dropper/color-picker-eye-dropper.svelte.d.ts +4 -0
  12. package/dist/color-picker/hex-field/color-picker-hex-field.svelte +105 -0
  13. package/dist/color-picker/hex-field/color-picker-hex-field.svelte.d.ts +4 -0
  14. package/dist/color-picker/index.d.ts +16 -0
  15. package/dist/color-picker/index.js +15 -0
  16. package/dist/color-picker/index.parts.d.ts +12 -0
  17. package/dist/color-picker/index.parts.js +12 -0
  18. package/dist/color-picker/label/color-picker-label.svelte +34 -0
  19. package/dist/color-picker/label/color-picker-label.svelte.d.ts +4 -0
  20. package/dist/color-picker/preview/color-picker-preview.svelte +43 -0
  21. package/dist/color-picker/preview/color-picker-preview.svelte.d.ts +4 -0
  22. package/dist/color-picker/root/color-picker-root.svelte +365 -0
  23. package/dist/color-picker/root/color-picker-root.svelte.d.ts +4 -0
  24. package/dist/color-picker/root/color.d.ts +68 -0
  25. package/dist/color-picker/root/color.js +266 -0
  26. package/dist/color-picker/root/context.d.ts +127 -0
  27. package/dist/color-picker/root/context.js +44 -0
  28. package/dist/color-picker/slider/color-picker-slider.svelte +188 -0
  29. package/dist/color-picker/slider/color-picker-slider.svelte.d.ts +4 -0
  30. package/dist/color-picker/slider-thumb/color-picker-slider-thumb.svelte +208 -0
  31. package/dist/color-picker/slider-thumb/color-picker-slider-thumb.svelte.d.ts +4 -0
  32. package/dist/color-picker/swatch/color-picker-swatch.svelte +149 -0
  33. package/dist/color-picker/swatch/color-picker-swatch.svelte.d.ts +4 -0
  34. package/dist/color-picker/swatch-list/color-picker-swatch-list.svelte +133 -0
  35. package/dist/color-picker/swatch-list/color-picker-swatch-list.svelte.d.ts +4 -0
  36. package/dist/color-picker/types.d.ts +176 -0
  37. package/dist/color-picker/types.js +1 -0
  38. package/dist/index.d.ts +8 -0
  39. package/dist/index.js +8 -0
  40. package/dist/internal/localized-strings.d.ts +152 -0
  41. package/dist/internal/localized-strings.js +152 -0
  42. package/dist/pin-input/README.md +55 -0
  43. package/dist/pin-input/cell/pin-input-cell.svelte +210 -0
  44. package/dist/pin-input/cell/pin-input-cell.svelte.d.ts +4 -0
  45. package/dist/pin-input/index.d.ts +6 -0
  46. package/dist/pin-input/index.js +5 -0
  47. package/dist/pin-input/index.parts.d.ts +3 -0
  48. package/dist/pin-input/index.parts.js +3 -0
  49. package/dist/pin-input/label/pin-input-label.svelte +43 -0
  50. package/dist/pin-input/label/pin-input-label.svelte.d.ts +4 -0
  51. package/dist/pin-input/root/context.d.ts +70 -0
  52. package/dist/pin-input/root/context.js +15 -0
  53. package/dist/pin-input/root/pin-input-root.svelte +337 -0
  54. package/dist/pin-input/root/pin-input-root.svelte.d.ts +4 -0
  55. package/dist/pin-input/root/pin-input-utils.d.ts +18 -0
  56. package/dist/pin-input/root/pin-input-utils.js +44 -0
  57. package/dist/pin-input/types.d.ts +84 -0
  58. package/dist/pin-input/types.js +1 -0
  59. package/dist/rating/README.md +58 -0
  60. package/dist/rating/index.d.ts +7 -0
  61. package/dist/rating/index.js +6 -0
  62. package/dist/rating/index.parts.d.ts +4 -0
  63. package/dist/rating/index.parts.js +4 -0
  64. package/dist/rating/item/rating-item.svelte +177 -0
  65. package/dist/rating/item/rating-item.svelte.d.ts +4 -0
  66. package/dist/rating/label/rating-label.svelte +42 -0
  67. package/dist/rating/label/rating-label.svelte.d.ts +4 -0
  68. package/dist/rating/output/rating-output.svelte +38 -0
  69. package/dist/rating/output/rating-output.svelte.d.ts +4 -0
  70. package/dist/rating/root/context.d.ts +87 -0
  71. package/dist/rating/root/context.js +15 -0
  72. package/dist/rating/root/rating-root.svelte +521 -0
  73. package/dist/rating/root/rating-root.svelte.d.ts +4 -0
  74. package/dist/rating/root/rating-utils.d.ts +28 -0
  75. package/dist/rating/root/rating-utils.js +53 -0
  76. package/dist/rating/types.d.ts +124 -0
  77. package/dist/rating/types.js +1 -0
  78. package/dist/search-field/README.md +29 -0
  79. package/dist/search-field/clear/README.md +5 -0
  80. package/dist/search-field/clear/search-field-clear.svelte +99 -0
  81. package/dist/search-field/clear/search-field-clear.svelte.d.ts +10 -0
  82. package/dist/search-field/index.d.ts +15 -0
  83. package/dist/search-field/index.js +6 -0
  84. package/dist/search-field/index.parts.d.ts +4 -0
  85. package/dist/search-field/index.parts.js +4 -0
  86. package/dist/search-field/input/README.md +5 -0
  87. package/dist/search-field/input/search-field-input.svelte +158 -0
  88. package/dist/search-field/input/search-field-input.svelte.d.ts +14 -0
  89. package/dist/search-field/label/README.md +3 -0
  90. package/dist/search-field/label/search-field-label.svelte +27 -0
  91. package/dist/search-field/label/search-field-label.svelte.d.ts +7 -0
  92. package/dist/search-field/root/README.md +5 -0
  93. package/dist/search-field/root/context.d.ts +28 -0
  94. package/dist/search-field/root/context.js +15 -0
  95. package/dist/search-field/root/search-field-root.svelte +230 -0
  96. package/dist/search-field/root/search-field-root.svelte.d.ts +33 -0
  97. package/dist/tooltip/root/tooltip-root.svelte +16 -11
  98. package/package.json +21 -1
@@ -144,6 +144,14 @@ declare const LOCALIZED_STRINGS: {
144
144
  readonly de: "Spalte";
145
145
  readonly it: "Colonna";
146
146
  };
147
+ readonly 'searchField.clear': {
148
+ readonly en: "Clear search";
149
+ readonly es: "Borrar búsqueda";
150
+ readonly pt: "Limpar pesquisa";
151
+ readonly fr: "Effacer la recherche";
152
+ readonly de: "Suche löschen";
153
+ readonly it: "Cancella ricerca";
154
+ };
147
155
  readonly 'numberField.increment': {
148
156
  readonly en: "Increment";
149
157
  readonly es: "Incrementar";
@@ -384,6 +392,150 @@ declare const LOCALIZED_STRINGS: {
384
392
  readonly de: "{count} Benachrichtigungen";
385
393
  readonly it: "{count} notifiche";
386
394
  };
395
+ readonly 'rating.valueText': {
396
+ readonly en: "{value} of {count}";
397
+ readonly es: "{value} de {count}";
398
+ readonly pt: "{value} de {count}";
399
+ readonly fr: "{value} sur {count}";
400
+ readonly de: "{value} von {count}";
401
+ readonly it: "{value} su {count}";
402
+ };
403
+ readonly 'rating.empty': {
404
+ readonly en: "No rating";
405
+ readonly es: "Sin calificación";
406
+ readonly pt: "Sem classificação";
407
+ readonly fr: "Aucune note";
408
+ readonly de: "Keine Bewertung";
409
+ readonly it: "Nessuna valutazione";
410
+ };
411
+ readonly 'rating.label': {
412
+ readonly en: "Rating";
413
+ readonly es: "Calificación";
414
+ readonly pt: "Classificação";
415
+ readonly fr: "Note";
416
+ readonly de: "Bewertung";
417
+ readonly it: "Valutazione";
418
+ };
419
+ readonly 'pinInput.digitLabel': {
420
+ readonly en: "Digit {index} of {count}";
421
+ readonly es: "Dígito {index} de {count}";
422
+ readonly pt: "Dígito {index} de {count}";
423
+ readonly fr: "Chiffre {index} sur {count}";
424
+ readonly de: "Ziffer {index} von {count}";
425
+ readonly it: "Cifra {index} di {count}";
426
+ };
427
+ readonly 'pinInput.characterLabel': {
428
+ readonly en: "Character {index} of {count}";
429
+ readonly es: "Carácter {index} de {count}";
430
+ readonly pt: "Caractere {index} de {count}";
431
+ readonly fr: "Caractère {index} sur {count}";
432
+ readonly de: "Zeichen {index} von {count}";
433
+ readonly it: "Carattere {index} di {count}";
434
+ };
435
+ readonly 'pinInput.label': {
436
+ readonly en: "Verification code";
437
+ readonly es: "Código de verificación";
438
+ readonly pt: "Código de verificação";
439
+ readonly fr: "Code de vérification";
440
+ readonly de: "Bestätigungscode";
441
+ readonly it: "Codice di verifica";
442
+ };
443
+ readonly 'colorPicker.label': {
444
+ readonly en: "Color";
445
+ readonly es: "Color";
446
+ readonly pt: "Cor";
447
+ readonly fr: "Couleur";
448
+ readonly de: "Farbe";
449
+ readonly it: "Colore";
450
+ };
451
+ readonly 'colorPicker.hue': {
452
+ readonly en: "Hue";
453
+ readonly es: "Matiz";
454
+ readonly pt: "Matiz";
455
+ readonly fr: "Teinte";
456
+ readonly de: "Farbton";
457
+ readonly it: "Tonalità";
458
+ };
459
+ readonly 'colorPicker.saturation': {
460
+ readonly en: "Saturation";
461
+ readonly es: "Saturación";
462
+ readonly pt: "Saturação";
463
+ readonly fr: "Saturation";
464
+ readonly de: "Sättigung";
465
+ readonly it: "Saturazione";
466
+ };
467
+ readonly 'colorPicker.brightness': {
468
+ readonly en: "Brightness";
469
+ readonly es: "Brillo";
470
+ readonly pt: "Brilho";
471
+ readonly fr: "Luminosité";
472
+ readonly de: "Helligkeit";
473
+ readonly it: "Luminosità";
474
+ };
475
+ readonly 'colorPicker.lightness': {
476
+ readonly en: "Lightness";
477
+ readonly es: "Luminosidad";
478
+ readonly pt: "Luminosidade";
479
+ readonly fr: "Clarté";
480
+ readonly de: "Helligkeit";
481
+ readonly it: "Chiarezza";
482
+ };
483
+ readonly 'colorPicker.alpha': {
484
+ readonly en: "Alpha";
485
+ readonly es: "Alfa";
486
+ readonly pt: "Alfa";
487
+ readonly fr: "Alpha";
488
+ readonly de: "Alpha";
489
+ readonly it: "Alfa";
490
+ };
491
+ readonly 'colorPicker.red': {
492
+ readonly en: "Red";
493
+ readonly es: "Rojo";
494
+ readonly pt: "Vermelho";
495
+ readonly fr: "Rouge";
496
+ readonly de: "Rot";
497
+ readonly it: "Rosso";
498
+ };
499
+ readonly 'colorPicker.green': {
500
+ readonly en: "Green";
501
+ readonly es: "Verde";
502
+ readonly pt: "Verde";
503
+ readonly fr: "Vert";
504
+ readonly de: "Grün";
505
+ readonly it: "Verde";
506
+ };
507
+ readonly 'colorPicker.blue': {
508
+ readonly en: "Blue";
509
+ readonly es: "Azul";
510
+ readonly pt: "Azul";
511
+ readonly fr: "Bleu";
512
+ readonly de: "Blau";
513
+ readonly it: "Blu";
514
+ };
515
+ readonly 'colorPicker.hex': {
516
+ readonly en: "Hex";
517
+ readonly es: "Hex";
518
+ readonly pt: "Hex";
519
+ readonly fr: "Hex";
520
+ readonly de: "Hex";
521
+ readonly it: "Hex";
522
+ };
523
+ readonly 'colorPicker.eyeDropper': {
524
+ readonly en: "Pick a color from the screen";
525
+ readonly es: "Tomar un color de la pantalla";
526
+ readonly pt: "Escolher uma cor da tela";
527
+ readonly fr: "Prendre une couleur sur l’écran";
528
+ readonly de: "Eine Farbe vom Bildschirm wählen";
529
+ readonly it: "Scegliere un colore dallo schermo";
530
+ };
531
+ readonly 'colorPicker.swatches': {
532
+ readonly en: "Color swatches";
533
+ readonly es: "Muestras de color";
534
+ readonly pt: "Amostras de cor";
535
+ readonly fr: "Échantillons de couleur";
536
+ readonly de: "Farbfelder";
537
+ readonly it: "Campioni di colore";
538
+ };
387
539
  };
388
540
  export type LocalizedStringKey = keyof typeof LOCALIZED_STRINGS;
389
541
  export declare function resolveLocalizedString(locale: string | undefined, key: LocalizedStringKey, params?: Record<string, string | number>): string;
@@ -144,6 +144,14 @@ const LOCALIZED_STRINGS = {
144
144
  de: 'Spalte',
145
145
  it: 'Colonna'
146
146
  },
147
+ 'searchField.clear': {
148
+ en: 'Clear search',
149
+ es: 'Borrar búsqueda',
150
+ pt: 'Limpar pesquisa',
151
+ fr: 'Effacer la recherche',
152
+ de: 'Suche löschen',
153
+ it: 'Cancella ricerca'
154
+ },
147
155
  'numberField.increment': {
148
156
  en: 'Increment',
149
157
  es: 'Incrementar',
@@ -383,6 +391,150 @@ const LOCALIZED_STRINGS = {
383
391
  fr: '{count} notifications',
384
392
  de: '{count} Benachrichtigungen',
385
393
  it: '{count} notifiche'
394
+ },
395
+ 'rating.valueText': {
396
+ en: '{value} of {count}',
397
+ es: '{value} de {count}',
398
+ pt: '{value} de {count}',
399
+ fr: '{value} sur {count}',
400
+ de: '{value} von {count}',
401
+ it: '{value} su {count}'
402
+ },
403
+ 'rating.empty': {
404
+ en: 'No rating',
405
+ es: 'Sin calificación',
406
+ pt: 'Sem classificação',
407
+ fr: 'Aucune note',
408
+ de: 'Keine Bewertung',
409
+ it: 'Nessuna valutazione'
410
+ },
411
+ 'rating.label': {
412
+ en: 'Rating',
413
+ es: 'Calificación',
414
+ pt: 'Classificação',
415
+ fr: 'Note',
416
+ de: 'Bewertung',
417
+ it: 'Valutazione'
418
+ },
419
+ 'pinInput.digitLabel': {
420
+ en: 'Digit {index} of {count}',
421
+ es: 'Dígito {index} de {count}',
422
+ pt: 'Dígito {index} de {count}',
423
+ fr: 'Chiffre {index} sur {count}',
424
+ de: 'Ziffer {index} von {count}',
425
+ it: 'Cifra {index} di {count}'
426
+ },
427
+ 'pinInput.characterLabel': {
428
+ en: 'Character {index} of {count}',
429
+ es: 'Carácter {index} de {count}',
430
+ pt: 'Caractere {index} de {count}',
431
+ fr: 'Caractère {index} sur {count}',
432
+ de: 'Zeichen {index} von {count}',
433
+ it: 'Carattere {index} di {count}'
434
+ },
435
+ 'pinInput.label': {
436
+ en: 'Verification code',
437
+ es: 'Código de verificación',
438
+ pt: 'Código de verificação',
439
+ fr: 'Code de vérification',
440
+ de: 'Bestätigungscode',
441
+ it: 'Codice di verifica'
442
+ },
443
+ 'colorPicker.label': {
444
+ en: 'Color',
445
+ es: 'Color',
446
+ pt: 'Cor',
447
+ fr: 'Couleur',
448
+ de: 'Farbe',
449
+ it: 'Colore'
450
+ },
451
+ 'colorPicker.hue': {
452
+ en: 'Hue',
453
+ es: 'Matiz',
454
+ pt: 'Matiz',
455
+ fr: 'Teinte',
456
+ de: 'Farbton',
457
+ it: 'Tonalità'
458
+ },
459
+ 'colorPicker.saturation': {
460
+ en: 'Saturation',
461
+ es: 'Saturación',
462
+ pt: 'Saturação',
463
+ fr: 'Saturation',
464
+ de: 'Sättigung',
465
+ it: 'Saturazione'
466
+ },
467
+ 'colorPicker.brightness': {
468
+ en: 'Brightness',
469
+ es: 'Brillo',
470
+ pt: 'Brilho',
471
+ fr: 'Luminosité',
472
+ de: 'Helligkeit',
473
+ it: 'Luminosità'
474
+ },
475
+ 'colorPicker.lightness': {
476
+ en: 'Lightness',
477
+ es: 'Luminosidad',
478
+ pt: 'Luminosidade',
479
+ fr: 'Clarté',
480
+ de: 'Helligkeit',
481
+ it: 'Chiarezza'
482
+ },
483
+ 'colorPicker.alpha': {
484
+ en: 'Alpha',
485
+ es: 'Alfa',
486
+ pt: 'Alfa',
487
+ fr: 'Alpha',
488
+ de: 'Alpha',
489
+ it: 'Alfa'
490
+ },
491
+ 'colorPicker.red': {
492
+ en: 'Red',
493
+ es: 'Rojo',
494
+ pt: 'Vermelho',
495
+ fr: 'Rouge',
496
+ de: 'Rot',
497
+ it: 'Rosso'
498
+ },
499
+ 'colorPicker.green': {
500
+ en: 'Green',
501
+ es: 'Verde',
502
+ pt: 'Verde',
503
+ fr: 'Vert',
504
+ de: 'Grün',
505
+ it: 'Verde'
506
+ },
507
+ 'colorPicker.blue': {
508
+ en: 'Blue',
509
+ es: 'Azul',
510
+ pt: 'Azul',
511
+ fr: 'Bleu',
512
+ de: 'Blau',
513
+ it: 'Blu'
514
+ },
515
+ 'colorPicker.hex': {
516
+ en: 'Hex',
517
+ es: 'Hex',
518
+ pt: 'Hex',
519
+ fr: 'Hex',
520
+ de: 'Hex',
521
+ it: 'Hex'
522
+ },
523
+ 'colorPicker.eyeDropper': {
524
+ en: 'Pick a color from the screen',
525
+ es: 'Tomar un color de la pantalla',
526
+ pt: 'Escolher uma cor da tela',
527
+ fr: 'Prendre une couleur sur l’écran',
528
+ de: 'Eine Farbe vom Bildschirm wählen',
529
+ it: 'Scegliere un colore dallo schermo'
530
+ },
531
+ 'colorPicker.swatches': {
532
+ en: 'Color swatches',
533
+ es: 'Muestras de color',
534
+ pt: 'Amostras de cor',
535
+ fr: 'Échantillons de couleur',
536
+ de: 'Farbfelder',
537
+ it: 'Campioni di colore'
386
538
  }
387
539
  };
388
540
  function resolveLanguage(locale) {
@@ -0,0 +1,55 @@
1
+ # PinInput
2
+
3
+ ## Description
4
+
5
+ `PinInput` is a form field for a short code: a PIN, or the verification code of a message. Each character has its own cell, and each cell is a real input. The telephone shows the correct keyboard, and a code from a message fills each cell with one touch.
6
+
7
+ ## Anatomy
8
+
9
+ - `PinInput.Root`
10
+ - `PinInput.Label`
11
+ - `PinInput.Cell`
12
+
13
+ ```svelte
14
+ <PinInput.Root name="code" length={6} otp>
15
+ <PinInput.Label>Verification code</PinInput.Label>
16
+ {#each { length: 6 } as _, index (index)}
17
+ <PinInput.Cell />
18
+ {/each}
19
+ </PinInput.Root>
20
+ ```
21
+
22
+ ## Usage guidelines
23
+
24
+ - Use `bind:value` for the state, or `value` with `onChange` and `controlledValue` to hold it yourself.
25
+ - Give one `PinInput.Cell` for each character of `length`.
26
+ - Use `onComplete` for the work that follows the last character, for example the send of the form.
27
+ - Use `otp` for a code that arrives in a message, and `mask` for a PIN the shoulder of a stranger must not read.
28
+ - `type` decides which characters each cell accepts: `numeric`, `alphanumeric` or `alphabetic`. Give `pattern` for a test of your own.
29
+ - The value has no holes. The characters fill the cells from the first one. A press on a cell past the first empty one goes to that one. A character that goes away takes the ones after it one cell to the left.
30
+
31
+ ## API reference
32
+
33
+ - `PinInput.Root`
34
+ - `value?: string`
35
+ - `defaultValue?: string`
36
+ - `controlledValue?: boolean`
37
+ - `onChange?: (value, details) => void`
38
+ - `onComplete?: (value) => void`
39
+ - `length?: number`
40
+ - `type?: 'numeric' | 'alphanumeric' | 'alphabetic'`, `pattern?: RegExp`
41
+ - `otp?: boolean`, `mask?: boolean`, `placeholder?: string`
42
+ - `blurOnComplete?: boolean`
43
+ - `disabled?: boolean`, `readonly?: boolean`, `required?: boolean`, `invalid?: boolean`
44
+ - `name?: string`, `form?: string`
45
+ - `PinInput.Cell`
46
+ - `index?: number`
47
+ - `aria-label?: string`
48
+
49
+ ## Accessibility
50
+
51
+ - The root is a `role="group"` named by `PinInput.Label`, `aria-labelledby` or `aria-label`. With `otp` the group takes the name "Verification code" in the locale.
52
+ - Each cell is an `<input>` with its own name, for example "Digit 2 of 6", and with `inputmode`, `autocomplete` and `aria-invalid`.
53
+ - The keyboard: a character moves the focus to the next cell. `Backspace` clears the cell, or the one before it. `Delete` clears the cell, the arrows move between the cells, and `Home` and `End` go to the ends. The horizontal arrows follow the text direction.
54
+ - A paste goes across the cells from the cell it starts in, and the characters the type refuses are dropped.
55
+ - The whole value in a form comes from a hidden input. A `<form>` reset takes the first value back.
@@ -0,0 +1,210 @@
1
+ <script lang="ts">
2
+ import { untrack } from 'svelte';
3
+ import { isRtl } from '../../internal/rtl';
4
+ import { watchFocusVisible } from '../../primitives/focus-visible.svelte';
5
+ import {
6
+ shouldShowFocusVisible,
7
+ trackInteractionModality
8
+ } from '../../primitives/input-modality';
9
+ import { usePinInputContext } from '../root/context';
10
+ import type { PinInputCellProps } from '../types';
11
+
12
+ /**
13
+ * PinInput.Cell — one character of the value.
14
+ *
15
+ * The cell is a real `<input>`: the telephone shows the correct keyboard, the password
16
+ * manager sees a field, and a code from a message fills each cell with one touch. A code
17
+ * that arrives in one cell, from a message or from the clipboard, goes across the cells from
18
+ * this one.
19
+ */
20
+ let {
21
+ index: indexProp,
22
+ 'aria-label': ariaLabel,
23
+ class: className = '',
24
+ element = $bindable<HTMLInputElement | null>(null),
25
+ oninput,
26
+ onkeydown,
27
+ onpaste,
28
+ onfocus,
29
+ onblur,
30
+ onpointerdown,
31
+ ...restProps
32
+ }: PinInputCellProps = $props();
33
+
34
+ const ctx = usePinInputContext('PinInput.Cell');
35
+
36
+ let inputRef: HTMLInputElement | null = $state(null);
37
+
38
+ const registration = ctx.registerCell({
39
+ index: untrack(() => indexProp),
40
+ inputRef: () => inputRef
41
+ });
42
+ const index = registration.index;
43
+
44
+ $effect(() => {
45
+ element = inputRef;
46
+ return () => {
47
+ element = null;
48
+ };
49
+ });
50
+
51
+ $effect(() => registration.unregister);
52
+
53
+ const character = $derived(ctx.getCellCharacter(index));
54
+ const filled = $derived(character !== '');
55
+ const focused = $derived(ctx.focusedIndex === index);
56
+ const focusVisible = $derived(focused && ctx.isFocusVisible);
57
+ /** The cell that takes the next character: the first empty one. */
58
+ const active = $derived(focused || (ctx.focusedIndex === null && ctx.value.length === index));
59
+
60
+ // The input holds one character, and the context holds the value. A key that the value
61
+ // refuses leaves the character of the DOM ahead of it, thus the two are made equal again
62
+ // after each change.
63
+ $effect(() => {
64
+ const node = inputRef;
65
+ if (!node) return;
66
+ if (node.value !== character) node.value = character;
67
+ });
68
+
69
+ watchFocusVisible({
70
+ isFocused: () => focused,
71
+ element: () => inputRef,
72
+ set: (visible) => ctx.setFocusVisible(visible)
73
+ });
74
+
75
+ function handleInput(event: Event & { currentTarget: EventTarget & HTMLInputElement }) {
76
+ oninput?.(event as never);
77
+ const node = event.currentTarget;
78
+ const raw = node.value;
79
+ // A browser that fills a code from a message writes it whole into one cell. What the user
80
+ // typed is what the cell has past the character it had.
81
+ const inserted =
82
+ character && raw.startsWith(character) && raw.length > character.length
83
+ ? raw.slice(character.length)
84
+ : raw;
85
+
86
+ // The value is the source of the character: put back what the value says, and let the
87
+ // context answer the text. A refused character then leaves no trace.
88
+ node.value = character;
89
+ if (!inserted) return;
90
+ ctx.insertText(index, inserted, { reason: 'input', event });
91
+ }
92
+
93
+ function handleKeyDown(event: KeyboardEvent & { currentTarget: EventTarget & HTMLInputElement }) {
94
+ onkeydown?.(event as never);
95
+ if (event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey) return;
96
+
97
+ const forward = isRtl(inputRef) ? 'ArrowLeft' : 'ArrowRight';
98
+ const backward = isRtl(inputRef) ? 'ArrowRight' : 'ArrowLeft';
99
+
100
+ switch (event.key) {
101
+ case 'Backspace':
102
+ event.preventDefault();
103
+ if (filled) {
104
+ ctx.deleteCharacter(index, { reason: 'delete', event });
105
+ ctx.focusCell(index);
106
+ } else if (index > 0) {
107
+ ctx.deleteCharacter(index - 1, { reason: 'delete', event });
108
+ ctx.focusCell(index - 1);
109
+ }
110
+ break;
111
+ case 'Delete':
112
+ event.preventDefault();
113
+ ctx.deleteCharacter(index, { reason: 'delete', event });
114
+ break;
115
+ case forward:
116
+ event.preventDefault();
117
+ ctx.focusCell(index + 1);
118
+ break;
119
+ case backward:
120
+ event.preventDefault();
121
+ ctx.focusCell(index - 1);
122
+ break;
123
+ case 'Home':
124
+ event.preventDefault();
125
+ ctx.focusCell(0);
126
+ break;
127
+ case 'End':
128
+ event.preventDefault();
129
+ ctx.focusCell(ctx.length - 1);
130
+ break;
131
+ default:
132
+ break;
133
+ }
134
+ }
135
+
136
+ function handlePaste(event: ClipboardEvent & { currentTarget: EventTarget & HTMLInputElement }) {
137
+ onpaste?.(event as never);
138
+ if (event.defaultPrevented) return;
139
+ // The paste goes across the cells, thus the browser must not put it all in this one.
140
+ event.preventDefault();
141
+ const text = event.clipboardData?.getData('text') ?? '';
142
+ ctx.insertText(index, text, { reason: 'paste', event });
143
+ }
144
+
145
+ function handleFocus(event: FocusEvent & { currentTarget: EventTarget & HTMLInputElement }) {
146
+ onfocus?.(event as never);
147
+ trackInteractionModality(event, inputRef);
148
+ // The value has no holes: a press on a cell past the first empty one goes to that one.
149
+ if (index > ctx.value.length) {
150
+ ctx.focusCell(ctx.value.length);
151
+ return;
152
+ }
153
+ ctx.setCellFocus(index, true);
154
+ ctx.setFocusVisible(shouldShowFocusVisible(inputRef));
155
+ // The character is selected, thus the next key replaces it instead of a second one.
156
+ event.currentTarget.select();
157
+ }
158
+
159
+ function handleBlur(event: FocusEvent & { currentTarget: EventTarget & HTMLInputElement }) {
160
+ onblur?.(event as never);
161
+ ctx.setCellFocus(index, false);
162
+ }
163
+
164
+ function handlePointerDown(
165
+ event: PointerEvent & { currentTarget: EventTarget & HTMLInputElement }
166
+ ) {
167
+ onpointerdown?.(event as never);
168
+ if (event.defaultPrevented || ctx.isDisabled) return;
169
+ if (index <= ctx.value.length) return;
170
+ // A press on a cell past the first empty one must not put the caret there.
171
+ event.preventDefault();
172
+ ctx.focusCell(ctx.value.length, 'pointer');
173
+ }
174
+ </script>
175
+
176
+ <input
177
+ {...restProps}
178
+ bind:this={inputRef}
179
+ id={ctx.getCellId(index)}
180
+ type={ctx.mask ? 'password' : 'text'}
181
+ inputmode={ctx.type === 'numeric' ? 'numeric' : 'text'}
182
+ autocomplete={ctx.otp ? 'one-time-code' : 'off'}
183
+ autocorrect="off"
184
+ autocapitalize="off"
185
+ spellcheck="false"
186
+ value={character}
187
+ placeholder={ctx.placeholder}
188
+ disabled={ctx.isDisabled}
189
+ readonly={ctx.isReadOnly}
190
+ required={ctx.isRequired}
191
+ aria-label={ariaLabel ?? ctx.getCellLabel(index)}
192
+ aria-describedby={ctx.ariaDescribedBy}
193
+ aria-invalid={ctx.isInvalid || undefined}
194
+ class={className}
195
+ data-pin-input-cell="true"
196
+ data-index={index}
197
+ data-filled={filled || undefined}
198
+ data-active={active || undefined}
199
+ data-disabled={ctx.isDisabled || undefined}
200
+ data-readonly={ctx.isReadOnly || undefined}
201
+ data-invalid={ctx.isInvalid || undefined}
202
+ data-focused={focused || undefined}
203
+ data-focus-visible={focusVisible || undefined}
204
+ oninput={handleInput}
205
+ onkeydown={handleKeyDown}
206
+ onpaste={handlePaste}
207
+ onfocus={handleFocus}
208
+ onblur={handleBlur}
209
+ onpointerdown={handlePointerDown}
210
+ />
@@ -0,0 +1,4 @@
1
+ import type { PinInputCellProps } from '../types';
2
+ declare const PinInputCell: import("svelte").Component<PinInputCellProps, {}, "element">;
3
+ type PinInputCell = ReturnType<typeof PinInputCell>;
4
+ export default PinInputCell;
@@ -0,0 +1,6 @@
1
+ export * as PinInput from './index.parts.js';
2
+ export { default as PinInputRoot } from './root/pin-input-root.svelte';
3
+ export { default as PinInputLabel } from './label/pin-input-label.svelte';
4
+ export { default as PinInputCell } from './cell/pin-input-cell.svelte';
5
+ export type { PinInputRootProps, PinInputLabelProps, PinInputCellProps, PinInputType } from './types.js';
6
+ export { getPinInputContext, setPinInputContext, usePinInputContext, type PinInputChangeDetails, type PinInputChangeReason, type PinInputContext } from './root/context.js';
@@ -0,0 +1,5 @@
1
+ export * as PinInput from './index.parts.js';
2
+ export { default as PinInputRoot } from './root/pin-input-root.svelte';
3
+ export { default as PinInputLabel } from './label/pin-input-label.svelte';
4
+ export { default as PinInputCell } from './cell/pin-input-cell.svelte';
5
+ export { getPinInputContext, setPinInputContext, usePinInputContext } from './root/context.js';
@@ -0,0 +1,3 @@
1
+ export { default as Root } from './root/pin-input-root.svelte';
2
+ export { default as Label } from './label/pin-input-label.svelte';
3
+ export { default as Cell } from './cell/pin-input-cell.svelte';
@@ -0,0 +1,3 @@
1
+ export { default as Root } from './root/pin-input-root.svelte';
2
+ export { default as Label } from './label/pin-input-label.svelte';
3
+ export { default as Cell } from './cell/pin-input-cell.svelte';
@@ -0,0 +1,43 @@
1
+ <script lang="ts">
2
+ import { usePinInputContext } from '../root/context';
3
+ import type { PinInputLabelProps } from '../types';
4
+
5
+ /**
6
+ * PinInput.Label — the accessible name of the group.
7
+ *
8
+ * It registers its id with the root, which gives it to the root element as `aria-labelledby`.
9
+ * It renders a `<span>` and not a `<label>`, because a `<label>` names one control and the
10
+ * group holds one input for each character. A press on it puts the focus on the first cell
11
+ * that is open, as a `<label>` would.
12
+ */
13
+ let {
14
+ children,
15
+ class: className = '',
16
+ id: idProp,
17
+ onclick,
18
+ ...restProps
19
+ }: PinInputLabelProps = $props();
20
+
21
+ const ctx = usePinInputContext('PinInput.Label');
22
+ const id = $derived(idProp ?? `${ctx.rootId}-label`);
23
+
24
+ // The returned unregister runs on destroy, and again whenever the id changes.
25
+ $effect(() => ctx.registerLabel(id));
26
+
27
+ function handleClick(event: MouseEvent & { currentTarget: EventTarget & HTMLSpanElement }) {
28
+ onclick?.(event);
29
+ if (event.defaultPrevented || ctx.isDisabled) return;
30
+ ctx.focusCell(ctx.value.length, 'pointer');
31
+ }
32
+ </script>
33
+
34
+ <span
35
+ {...restProps}
36
+ {id}
37
+ class={className}
38
+ data-pin-input-label="true"
39
+ data-disabled={ctx.isDisabled || undefined}
40
+ onclick={handleClick}
41
+ >
42
+ {@render children?.()}
43
+ </span>
@@ -0,0 +1,4 @@
1
+ import type { PinInputLabelProps } from '../types';
2
+ declare const PinInputLabel: import("svelte").Component<PinInputLabelProps, {}, "">;
3
+ type PinInputLabel = ReturnType<typeof PinInputLabel>;
4
+ export default PinInputLabel;