@tangible/ui 0.2.44 → 0.2.46
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/components/ColorPicker/ColorPicker.js +1 -1
- package/components/GradientPicker/GradientPicker.js +1 -1
- package/components/PasswordInput/PasswordInput.d.ts +7 -0
- package/components/PasswordInput/PasswordInput.js +64 -0
- package/components/PasswordInput/index.d.ts +3 -0
- package/components/PasswordInput/index.js +2 -0
- package/components/PasswordInput/types.d.ts +45 -0
- package/components/PasswordInput/types.js +6 -0
- package/components/SecretInput/SecretInput.js +10 -33
- package/components/Tooltip/Tooltip.d.ts +1 -1
- package/components/Tooltip/Tooltip.js +302 -19
- package/components/Tooltip/types.d.ts +30 -0
- package/components/index.d.ts +2 -0
- package/components/index.js +1 -0
- package/figma-tokens.json +1 -1
- package/icons/icons.svg +1 -1
- package/icons/sprite-version.js +1 -1
- package/icons/sprite.php +1 -1
- package/package.json +5 -1
- package/skills/building-with-tui/SKILL.md +9 -3
- package/styles/all.css +1 -1
- package/styles/all.expanded.css +51 -0
- package/styles/all.expanded.unlayered.css +51 -0
- package/styles/all.unlayered.css +1 -1
- package/styles/components/_bundle.scss +5 -1
- package/styles/components/password-input.scss +24 -0
- package/styles/components/secret-input.scss +1 -65
- package/styles/system/_reveal.scss +82 -0
- package/styles/system/index.scss +1 -0
- package/tui-manifest.json +93 -2
- package/utils/reveal/RevealToggle.d.ts +32 -0
- package/utils/reveal/RevealToggle.js +57 -0
- package/utils/reveal/index.d.ts +3 -0
- package/utils/reveal/index.js +4 -0
- package/utils/reveal/useReveal.d.ts +26 -0
- package/utils/reveal/useReveal.js +55 -0
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
@use "./move-handle" as movehandle;
|
|
28
28
|
@use "./notice" as notice;
|
|
29
29
|
@use "./overlap-stack" as overlapstack;
|
|
30
|
+
@use "./password-input" as passwordinput;
|
|
30
31
|
@use "./pager" as pager;
|
|
31
32
|
@use "./popover" as popover;
|
|
32
33
|
@use "./radio" as radio;
|
|
@@ -65,7 +66,7 @@
|
|
|
65
66
|
// search-input (its panel embeds one). The dependents mechanism below only
|
|
66
67
|
// covers the input core; extend it to full dependency sets if subset builds
|
|
67
68
|
// become a real workflow (Pager has the same latent gap).
|
|
68
|
-
$-input-dependents: (input, field, text-input, textarea, pager, search-input, search-select, secret-input, gradient-picker);
|
|
69
|
+
$-input-dependents: (input, field, text-input, textarea, pager, search-input, search-select, secret-input, password-input, gradient-picker);
|
|
69
70
|
|
|
70
71
|
@function -needs-input($only) {
|
|
71
72
|
@if $only == null {
|
|
@@ -118,6 +119,9 @@ $-input-dependents: (input, field, text-input, textarea, pager, search-input, se
|
|
|
118
119
|
@if -wants($only, notice) { @include notice.styles(); }
|
|
119
120
|
@if -wants($only, overlap-stack) { @include overlapstack.styles(); }
|
|
120
121
|
@if -wants($only, pager) { @include pager.styles(); }
|
|
122
|
+
// password-input wears .tui-input-group (pulled in above) and its reveal
|
|
123
|
+
// button is a system mixin, so it needs nothing else requested.
|
|
124
|
+
@if -wants($only, password-input) { @include passwordinput.styles(); }
|
|
121
125
|
@if -wants($only, popover) { @include popover.styles(); }
|
|
122
126
|
@if -wants($only, radio) { @include radio.styles(); }
|
|
123
127
|
@if -wants($only, rating) { @include rating.styles(); }
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
@use "../system" as sys;
|
|
2
|
+
|
|
3
|
+
/* @tui-tokens
|
|
4
|
+
--tui-password-input-button-radius
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// =============================================================================
|
|
8
|
+
// PasswordInput Styles
|
|
9
|
+
// =============================================================================
|
|
10
|
+
//
|
|
11
|
+
// The field is the shared input group (see TextInput styles). The only bespoke
|
|
12
|
+
// part is the reveal toggle, the system `reveal-button` mixin SecretInput
|
|
13
|
+
// wears too, so the two stay identical.
|
|
14
|
+
//
|
|
15
|
+
// Component API tokens (never defined here, only read via fallback):
|
|
16
|
+
// --tui-password-input-button-radius Reveal button hit-area radius
|
|
17
|
+
//
|
|
18
|
+
// =============================================================================
|
|
19
|
+
|
|
20
|
+
@mixin styles() {
|
|
21
|
+
:where(.#{sys.$prefix}interface) .#{sys.$prefix}password-input__reveal {
|
|
22
|
+
@include sys.reveal-button(--#{sys.$prefix}password-input-button-radius);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -87,70 +87,6 @@
|
|
|
87
87
|
|
|
88
88
|
// Reveal toggle. Mirrors SearchInput's owned in-field buttons.
|
|
89
89
|
:where(.#{sys.$prefix}interface) .#{sys.$prefix}secret-input__reveal {
|
|
90
|
-
|
|
91
|
-
align-items: center;
|
|
92
|
-
justify-content: center;
|
|
93
|
-
flex: none;
|
|
94
|
-
// Hit-area floor: em-relative padding alone lands ~21px at size="sm"
|
|
95
|
-
// (14px control font), under the 24x24 WCAG 2.5.8 minimum. The glyph still
|
|
96
|
-
// sizes with the control text inside the padded box.
|
|
97
|
-
min-inline-size: 1.5rem;
|
|
98
|
-
min-block-size: 1.5rem;
|
|
99
|
-
padding: 0.1875em;
|
|
100
|
-
// ...but a 24px box plus the group's own padding-block overflows the sm
|
|
101
|
-
// control height (24 + 8 + 2 = 34 against a 32px floor), which is exactly
|
|
102
|
-
// the "a sm Button and a sm field in the same row won't line up" bug. Let
|
|
103
|
-
// the target eat the group's padding instead of adding to it: the hit area
|
|
104
|
-
// stays 24px, the control stays on the ladder. (SearchInput's identical
|
|
105
|
-
// buttons still have the 34px version — tracked as a follow-up.)
|
|
106
|
-
margin-block: calc(-1 * var(--_py, var(--#{sys.$prefix}spacing-xxs)));
|
|
107
|
-
border: 0;
|
|
108
|
-
border-radius: var(--#{sys.$prefix}secret-input-button-radius, var(--#{sys.$prefix}radius-full, 999px));
|
|
109
|
-
background: transparent;
|
|
110
|
-
color: var(--#{sys.$prefix}color-fg-secondary);
|
|
111
|
-
cursor: pointer;
|
|
112
|
-
line-height: 0;
|
|
113
|
-
transition-duration: var(--#{sys.$prefix}motion-duration);
|
|
114
|
-
transition-timing-function: var(--#{sys.$prefix}motion-timing);
|
|
115
|
-
transition-property: color, background-color, outline-color;
|
|
116
|
-
|
|
117
|
-
.#{sys.$prefix}icon {
|
|
118
|
-
font-size: 1.125em;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
@media (hover: hover) {
|
|
122
|
-
&:hover {
|
|
123
|
-
color: var(--#{sys.$prefix}color-fg);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
&:focus-visible {
|
|
128
|
-
color: var(--#{sys.$prefix}color-fg);
|
|
129
|
-
outline: var(--#{sys.$prefix}focus-ring-width) solid var(--#{sys.$prefix}focus-ring-color);
|
|
130
|
-
outline-offset: var(--#{sys.$prefix}focus-ring-offset);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Revealed is a state worth seeing at a glance — the field is showing a
|
|
134
|
-
// secret in the clear. Keyed off data-revealed rather than aria-pressed:
|
|
135
|
-
// the button communicates state through its accessible NAME (see the
|
|
136
|
-
// component), so there is no aria-pressed to hang styling on.
|
|
137
|
-
&[data-revealed="true"] {
|
|
138
|
-
background-color: var(--#{sys.$prefix}color-fill-subtle);
|
|
139
|
-
color: var(--#{sys.$prefix}color-fg);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// Forced-colors: a transparent button is invisible without a system colour.
|
|
143
|
-
@media (forced-colors: active) {
|
|
144
|
-
color: ButtonText;
|
|
145
|
-
|
|
146
|
-
&[data-revealed="true"] {
|
|
147
|
-
background-color: Highlight;
|
|
148
|
-
color: HighlightText;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
&:focus-visible {
|
|
152
|
-
outline-color: Highlight;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
90
|
+
@include sys.reveal-button(--#{sys.$prefix}secret-input-button-radius);
|
|
155
91
|
}
|
|
156
92
|
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
@use "constants" as sys;
|
|
2
|
+
|
|
3
|
+
// =============================================================================
|
|
4
|
+
// Reveal button — the show/hide toggle in a masked field's suffix
|
|
5
|
+
// =============================================================================
|
|
6
|
+
//
|
|
7
|
+
// Worn by SecretInput (`tui-secret-input__reveal`) and PasswordInput
|
|
8
|
+
// (`tui-password-input__reveal`), so the two can't drift. Its React half is
|
|
9
|
+
// src/utils/reveal (RevealToggle + useReveal).
|
|
10
|
+
//
|
|
11
|
+
//
|
|
12
|
+
// - Include it on a button inside a `.tui-input-group` suffix: its
|
|
13
|
+
// `margin-block` reads the group's private `--_py` (from control-sizing).
|
|
14
|
+
// - `$radius-token` is the host's own radius knob, as an unquoted custom
|
|
15
|
+
// property name: `@include sys.reveal-button(--#{sys.$prefix}password-input-button-radius);`
|
|
16
|
+
@mixin reveal-button($radius-token) {
|
|
17
|
+
display: inline-flex;
|
|
18
|
+
align-items: center;
|
|
19
|
+
justify-content: center;
|
|
20
|
+
flex: none;
|
|
21
|
+
// Hit-area floor: em-relative padding alone lands ~21px at size="sm"
|
|
22
|
+
// (14px control font), under the 24x24 WCAG 2.5.8 minimum. The glyph still
|
|
23
|
+
// sizes with the control text inside the padded box.
|
|
24
|
+
min-inline-size: 1.5rem;
|
|
25
|
+
min-block-size: 1.5rem;
|
|
26
|
+
padding: 0.1875em;
|
|
27
|
+
// ...but a 24px box plus the group's own padding-block overflows the sm
|
|
28
|
+
// control height (24 + 8 + 2 = 34 against a 32px floor), which is exactly
|
|
29
|
+
// the "a sm Button and a sm field in the same row won't line up" bug. Let
|
|
30
|
+
// the target eat the group's padding instead of adding to it: the hit area
|
|
31
|
+
// stays 24px, the control stays on the ladder. (SearchInput's identical
|
|
32
|
+
// buttons still have the 34px version — tracked as a follow-up.)
|
|
33
|
+
margin-block: calc(-1 * var(--_py, var(--#{sys.$prefix}spacing-xxs)));
|
|
34
|
+
border: 0;
|
|
35
|
+
border-radius: var(#{$radius-token}, var(--#{sys.$prefix}radius-full, 999px));
|
|
36
|
+
background: transparent;
|
|
37
|
+
color: var(--#{sys.$prefix}color-fg-secondary);
|
|
38
|
+
cursor: pointer;
|
|
39
|
+
line-height: 0;
|
|
40
|
+
transition-duration: var(--#{sys.$prefix}motion-duration);
|
|
41
|
+
transition-timing-function: var(--#{sys.$prefix}motion-timing);
|
|
42
|
+
transition-property: color, background-color, outline-color;
|
|
43
|
+
|
|
44
|
+
.#{sys.$prefix}icon {
|
|
45
|
+
font-size: 1.125em;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@media (hover: hover) {
|
|
49
|
+
&:hover {
|
|
50
|
+
color: var(--#{sys.$prefix}color-fg);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
&:focus-visible {
|
|
55
|
+
color: var(--#{sys.$prefix}color-fg);
|
|
56
|
+
outline: var(--#{sys.$prefix}focus-ring-width) solid var(--#{sys.$prefix}focus-ring-color);
|
|
57
|
+
outline-offset: var(--#{sys.$prefix}focus-ring-offset);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Revealed is a state worth seeing at a glance — the field is showing a
|
|
61
|
+
// secret in the clear. Keyed off data-revealed rather than aria-pressed:
|
|
62
|
+
// the button communicates state through its accessible NAME (see the
|
|
63
|
+
// component), so there is no aria-pressed to hang styling on.
|
|
64
|
+
&[data-revealed="true"] {
|
|
65
|
+
background-color: var(--#{sys.$prefix}color-fill-subtle);
|
|
66
|
+
color: var(--#{sys.$prefix}color-fg);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Forced-colors: a transparent button is invisible without a system colour.
|
|
70
|
+
@media (forced-colors: active) {
|
|
71
|
+
color: ButtonText;
|
|
72
|
+
|
|
73
|
+
&[data-revealed="true"] {
|
|
74
|
+
background-color: Highlight;
|
|
75
|
+
color: HighlightText;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
&:focus-visible {
|
|
79
|
+
outline-color: Highlight;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
package/styles/system/index.scss
CHANGED
package/tui-manifest.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.2.
|
|
3
|
-
"generated": "2026-11-
|
|
2
|
+
"version": "0.2.46",
|
|
3
|
+
"generated": "2026-11-22T21:47:49.879Z",
|
|
4
4
|
"components": {
|
|
5
5
|
"Accordion": {
|
|
6
6
|
"props": {
|
|
@@ -2783,6 +2783,91 @@
|
|
|
2783
2783
|
"use": null,
|
|
2784
2784
|
"notFor": null
|
|
2785
2785
|
},
|
|
2786
|
+
"PasswordInput": {
|
|
2787
|
+
"props": {
|
|
2788
|
+
"size": {
|
|
2789
|
+
"type": "\"sm\" | \"md\" | \"lg\"",
|
|
2790
|
+
"required": false,
|
|
2791
|
+
"defaultValue": "'md'",
|
|
2792
|
+
"description": "Control size."
|
|
2793
|
+
},
|
|
2794
|
+
"disabled": {
|
|
2795
|
+
"type": "boolean",
|
|
2796
|
+
"required": false
|
|
2797
|
+
},
|
|
2798
|
+
"onChange": {
|
|
2799
|
+
"type": "ChangeEventHandler<HTMLInputElement>",
|
|
2800
|
+
"required": false
|
|
2801
|
+
},
|
|
2802
|
+
"className": {
|
|
2803
|
+
"type": "string",
|
|
2804
|
+
"required": false,
|
|
2805
|
+
"description": "Additional class name applied to the root wrapper element."
|
|
2806
|
+
},
|
|
2807
|
+
"id": {
|
|
2808
|
+
"type": "string",
|
|
2809
|
+
"required": false
|
|
2810
|
+
},
|
|
2811
|
+
"style": {
|
|
2812
|
+
"type": "CSSProperties",
|
|
2813
|
+
"required": false
|
|
2814
|
+
},
|
|
2815
|
+
"title": {
|
|
2816
|
+
"type": "string",
|
|
2817
|
+
"required": false
|
|
2818
|
+
},
|
|
2819
|
+
"role": {
|
|
2820
|
+
"type": "AriaRole",
|
|
2821
|
+
"required": false
|
|
2822
|
+
},
|
|
2823
|
+
"rel": {
|
|
2824
|
+
"type": "string",
|
|
2825
|
+
"required": false
|
|
2826
|
+
},
|
|
2827
|
+
"onClick": {
|
|
2828
|
+
"type": "MouseEventHandler<HTMLInputElement>",
|
|
2829
|
+
"required": false
|
|
2830
|
+
},
|
|
2831
|
+
"inputClassName": {
|
|
2832
|
+
"type": "string",
|
|
2833
|
+
"required": false,
|
|
2834
|
+
"description": "Class name applied directly to the `<input>` element.\nUse for utilities like `tui-input-reset` (removes browser-native search\ndecoration, clear buttons, and spinners) that must target the input itself."
|
|
2835
|
+
},
|
|
2836
|
+
"purpose": {
|
|
2837
|
+
"type": "\"current\" | \"new\"",
|
|
2838
|
+
"required": true,
|
|
2839
|
+
"description": "`current` to sign in or confirm who you are; `new` to choose a password\n(sign up, reset, change). Sets `autocomplete` to `current-password` or\n`new-password`."
|
|
2840
|
+
},
|
|
2841
|
+
"revealed": {
|
|
2842
|
+
"type": "boolean",
|
|
2843
|
+
"required": false,
|
|
2844
|
+
"description": "Controlled: whether the password shows as text"
|
|
2845
|
+
},
|
|
2846
|
+
"defaultRevealed": {
|
|
2847
|
+
"type": "boolean",
|
|
2848
|
+
"required": false,
|
|
2849
|
+
"defaultValue": "false",
|
|
2850
|
+
"description": "Uncontrolled: start shown."
|
|
2851
|
+
}
|
|
2852
|
+
},
|
|
2853
|
+
"cssTokens": [
|
|
2854
|
+
"--tui-password-input-button-radius"
|
|
2855
|
+
],
|
|
2856
|
+
"story": {
|
|
2857
|
+
"title": "Forms/PasswordInput",
|
|
2858
|
+
"tags": [
|
|
2859
|
+
"autodocs",
|
|
2860
|
+
"stable"
|
|
2861
|
+
]
|
|
2862
|
+
},
|
|
2863
|
+
"subcomponents": null,
|
|
2864
|
+
"description": null,
|
|
2865
|
+
"gotchas": null,
|
|
2866
|
+
"examples": null,
|
|
2867
|
+
"related": null,
|
|
2868
|
+
"use": null,
|
|
2869
|
+
"notFor": null
|
|
2870
|
+
},
|
|
2786
2871
|
"Popover": {
|
|
2787
2872
|
"props": {
|
|
2788
2873
|
"open": {
|
|
@@ -4453,6 +4538,12 @@
|
|
|
4453
4538
|
"required": false,
|
|
4454
4539
|
"defaultValue": "'description'",
|
|
4455
4540
|
"description": "`label` when the tooltip repeats the trigger's accessible name, as on an\nicon-only button; see `TooltipType`."
|
|
4541
|
+
},
|
|
4542
|
+
"closeOnPress": {
|
|
4543
|
+
"type": "boolean",
|
|
4544
|
+
"required": false,
|
|
4545
|
+
"defaultValue": "true",
|
|
4546
|
+
"description": "Close when the trigger is pressed (pointerdown on the primary button,\nEnter or Space, or the click they produce), and stay closed until the\npointer leaves or focus leaves and comes back. That keeps a label from\nsitting over the popover, menu or drawer its trigger just opened.\n\nSet `false` when the tooltip is the press's feedback, such as a copy\nbutton whose tooltip swaps to \"Copied\". The tooltip still never opens\nwhile the trigger's popup is open (`aria-expanded=\"true\"` with\n`aria-haspopup`), whatever this is set to."
|
|
4456
4547
|
}
|
|
4457
4548
|
},
|
|
4458
4549
|
"cssTokens": [
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type RefObject } from 'react';
|
|
2
|
+
export interface RevealToggleProps {
|
|
3
|
+
revealed: boolean;
|
|
4
|
+
onToggle: () => void;
|
|
5
|
+
/** The name while masked: "Show password" */
|
|
6
|
+
revealLabel: string;
|
|
7
|
+
/** The name while shown: "Hide password" */
|
|
8
|
+
hideLabel: string;
|
|
9
|
+
/** From useReveal: said once per change */
|
|
10
|
+
announcement: string;
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
/** The host's own class for the button, e.g. `tui-secret-input__reveal` */
|
|
13
|
+
className: string;
|
|
14
|
+
inputRef: RefObject<HTMLInputElement | null>;
|
|
15
|
+
/** The host's root class, for "is focus still inside the field?" */
|
|
16
|
+
rootClassName: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The show/hide button in a masked field's suffix, shared by SecretInput and
|
|
20
|
+
* PasswordInput. @internal
|
|
21
|
+
*
|
|
22
|
+
* The name flips rather than staying constant with aria-pressed. For "is my
|
|
23
|
+
* secret on screen right now", a name is announced reliably by every AT where
|
|
24
|
+
* aria-pressed is not (VoiceOver with Safari especially), and stays
|
|
25
|
+
* unambiguous on AT that ignores the state. Deliberately not both: a flipping
|
|
26
|
+
* name plus aria-pressed reads as "Hide password, pressed".
|
|
27
|
+
*
|
|
28
|
+
* Pointerdown is prevented so focus stays in the field. That means a mouse
|
|
29
|
+
* click never focuses the button, so neither its name nor its state is read;
|
|
30
|
+
* the polite status region says what changed instead.
|
|
31
|
+
*/
|
|
32
|
+
export declare function RevealToggle({ revealed, onToggle, revealLabel, hideLabel, announcement, disabled, className, inputRef, rootClassName, }: RevealToggleProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useLayoutEffect, useRef } from 'react';
|
|
3
|
+
import { Icon } from '../../components/Icon/index.js';
|
|
4
|
+
import { View as ViewIcon, ViewOff as ViewOffIcon } from '../../icons/system/index.js';
|
|
5
|
+
/**
|
|
6
|
+
* The show/hide button in a masked field's suffix, shared by SecretInput and
|
|
7
|
+
* PasswordInput. @internal
|
|
8
|
+
*
|
|
9
|
+
* The name flips rather than staying constant with aria-pressed. For "is my
|
|
10
|
+
* secret on screen right now", a name is announced reliably by every AT where
|
|
11
|
+
* aria-pressed is not (VoiceOver with Safari especially), and stays
|
|
12
|
+
* unambiguous on AT that ignores the state. Deliberately not both: a flipping
|
|
13
|
+
* name plus aria-pressed reads as "Hide password, pressed".
|
|
14
|
+
*
|
|
15
|
+
* Pointerdown is prevented so focus stays in the field. That means a mouse
|
|
16
|
+
* click never focuses the button, so neither its name nor its state is read;
|
|
17
|
+
* the polite status region says what changed instead.
|
|
18
|
+
*/
|
|
19
|
+
export function RevealToggle({ revealed, onToggle, revealLabel, hideLabel, announcement, disabled, className, inputRef, rootClassName, }) {
|
|
20
|
+
// Swapping `type` resets the caret to the start in Chromium, so someone
|
|
21
|
+
// checking a character mid-password would carry on typing at the front.
|
|
22
|
+
// Keep the selection across the swap, while the field has focus.
|
|
23
|
+
const selection = useRef(null);
|
|
24
|
+
useLayoutEffect(() => {
|
|
25
|
+
const input = inputRef.current;
|
|
26
|
+
const saved = selection.current;
|
|
27
|
+
selection.current = null;
|
|
28
|
+
if (!input || !saved || input.ownerDocument.activeElement !== input)
|
|
29
|
+
return;
|
|
30
|
+
try {
|
|
31
|
+
// Chromium resets the selection when it rebuilds the field for the new
|
|
32
|
+
// type, at the next layout, which is after this effect. Force that
|
|
33
|
+
// layout now, so the restore lands after the reset, not before it.
|
|
34
|
+
void input.offsetWidth;
|
|
35
|
+
input.setSelectionRange(saved[0], saved[1], saved[2] ?? undefined);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// Some input types refuse selection ranges; the caret just stays put
|
|
39
|
+
}
|
|
40
|
+
}, [revealed, inputRef]);
|
|
41
|
+
const handleClick = () => {
|
|
42
|
+
const field = inputRef.current;
|
|
43
|
+
if (field && field.ownerDocument.activeElement === field) {
|
|
44
|
+
selection.current = [field.selectionStart, field.selectionEnd, field.selectionDirection];
|
|
45
|
+
}
|
|
46
|
+
onToggle();
|
|
47
|
+
// Pointer activation leaves focus wherever it was, including on <body> if
|
|
48
|
+
// nothing was focused, which strands the user at the top of the document.
|
|
49
|
+
// Recover only in that case: on keyboard activation focus is already on
|
|
50
|
+
// the button, and stealing it would break the toggle.
|
|
51
|
+
const input = inputRef.current;
|
|
52
|
+
const active = input?.ownerDocument.activeElement;
|
|
53
|
+
if (input && !active?.closest(`.${rootClassName}`))
|
|
54
|
+
input.focus();
|
|
55
|
+
};
|
|
56
|
+
return (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: className, "aria-label": revealed ? hideLabel : revealLabel, "data-revealed": revealed ? 'true' : undefined, disabled: disabled, onClick: handleClick, onPointerDown: (e) => e.preventDefault(), children: _jsx(Icon, { name: revealed ? ViewOffIcon : ViewIcon }) }), _jsx("span", { role: "status", className: "tui-visually-hidden", children: announcement })] }));
|
|
57
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Masked-or-shown state for a field with a reveal toggle (SecretInput,
|
|
3
|
+
* PasswordInput). @internal
|
|
4
|
+
*
|
|
5
|
+
* Controlled or not, decided once at mount like the rest of the family: a
|
|
6
|
+
* component that flips between the two mid-life pollutes its own state.
|
|
7
|
+
*
|
|
8
|
+
* `announcement` is what the toggle's status region says. It's empty until
|
|
9
|
+
* the first change (nothing on mount), and it's cleared again after a few
|
|
10
|
+
* seconds and on any change made without announcing. Otherwise the region
|
|
11
|
+
* keeps saying "Password shown." over a masked field, and the next show
|
|
12
|
+
* sets the same text, which is no change, so it's never announced.
|
|
13
|
+
*/
|
|
14
|
+
export declare function useReveal({ revealed: revealedProp, defaultRevealed, onRevealChange, shown, hidden, }: {
|
|
15
|
+
revealed?: boolean;
|
|
16
|
+
defaultRevealed?: boolean;
|
|
17
|
+
onRevealChange?: (revealed: boolean) => void;
|
|
18
|
+
shown: string;
|
|
19
|
+
hidden: string;
|
|
20
|
+
}): {
|
|
21
|
+
revealed: boolean;
|
|
22
|
+
setRevealed: (next: boolean, { announce }?: {
|
|
23
|
+
announce?: boolean;
|
|
24
|
+
}) => void;
|
|
25
|
+
announcement: string;
|
|
26
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
/** How long a status message stays before it's cleared, as Toast's announcers do */
|
|
3
|
+
const CLEAR_AFTER_MS = 3000;
|
|
4
|
+
/**
|
|
5
|
+
* Masked-or-shown state for a field with a reveal toggle (SecretInput,
|
|
6
|
+
* PasswordInput). @internal
|
|
7
|
+
*
|
|
8
|
+
* Controlled or not, decided once at mount like the rest of the family: a
|
|
9
|
+
* component that flips between the two mid-life pollutes its own state.
|
|
10
|
+
*
|
|
11
|
+
* `announcement` is what the toggle's status region says. It's empty until
|
|
12
|
+
* the first change (nothing on mount), and it's cleared again after a few
|
|
13
|
+
* seconds and on any change made without announcing. Otherwise the region
|
|
14
|
+
* keeps saying "Password shown." over a masked field, and the next show
|
|
15
|
+
* sets the same text, which is no change, so it's never announced.
|
|
16
|
+
*/
|
|
17
|
+
export function useReveal({ revealed: revealedProp, defaultRevealed = false, onRevealChange, shown, hidden, }) {
|
|
18
|
+
const [revealedState, setRevealedState] = useState(defaultRevealed);
|
|
19
|
+
const [isControlled] = useState(revealedProp !== undefined);
|
|
20
|
+
const revealed = isControlled ? !!revealedProp : revealedState;
|
|
21
|
+
const [announcement, setAnnouncement] = useState('');
|
|
22
|
+
const clearTimer = useRef(undefined);
|
|
23
|
+
useEffect(() => () => clearTimeout(clearTimer.current), []);
|
|
24
|
+
// A controlled parent that moves `revealed` itself (or refuses a toggle)
|
|
25
|
+
// makes any message stale: say nothing rather than the wrong thing.
|
|
26
|
+
const announcedFor = useRef(null);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (announcedFor.current !== null && announcedFor.current !== revealed) {
|
|
29
|
+
announcedFor.current = null;
|
|
30
|
+
setAnnouncement('');
|
|
31
|
+
}
|
|
32
|
+
}, [revealed]);
|
|
33
|
+
/** `announce: false` for a change the person didn't ask for (re-masking on submit) */
|
|
34
|
+
const setRevealed = (next, { announce = true } = {}) => {
|
|
35
|
+
if (next === revealed)
|
|
36
|
+
return;
|
|
37
|
+
if (!isControlled)
|
|
38
|
+
setRevealedState(next);
|
|
39
|
+
clearTimeout(clearTimer.current);
|
|
40
|
+
if (announce) {
|
|
41
|
+
announcedFor.current = next;
|
|
42
|
+
setAnnouncement(next ? shown : hidden);
|
|
43
|
+
clearTimer.current = setTimeout(() => {
|
|
44
|
+
announcedFor.current = null;
|
|
45
|
+
setAnnouncement('');
|
|
46
|
+
}, CLEAR_AFTER_MS);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
announcedFor.current = null;
|
|
50
|
+
setAnnouncement('');
|
|
51
|
+
}
|
|
52
|
+
onRevealChange?.(next);
|
|
53
|
+
};
|
|
54
|
+
return { revealed, setRevealed, announcement };
|
|
55
|
+
}
|