@tangible/ui 0.2.13 → 0.2.14

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,9 @@
1
+ export declare const SecretInput: import("react").ForwardRefExoticComponent<Omit<import("..").TextInputProps, "type" | "prefix" | "suffix"> & {
2
+ isSet?: boolean;
3
+ defaultRevealed?: boolean;
4
+ revealed?: boolean;
5
+ onRevealChange?: (revealed: boolean) => void;
6
+ locked?: boolean;
7
+ lockedMessage?: string;
8
+ labels?: import("./types").SecretInputLabels;
9
+ } & import("react").RefAttributes<HTMLInputElement>>;
@@ -0,0 +1,146 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { forwardRef, useCallback, useEffect, useId, useRef, useState } from 'react';
3
+ import { cx } from '../../utils/cx.js';
4
+ import { composeRefs } from '../../utils/compose-refs.js';
5
+ import { isDev } from '../../utils/is-dev.js';
6
+ import { Icon } from '../Icon/index.js';
7
+ import { TextInput } from '../TextInput/index.js';
8
+ import { defaultSecretInputLabels } from './types.js';
9
+ // =============================================================================
10
+ // SecretInput Component
11
+ // =============================================================================
12
+ //
13
+ // TextInput variant for stored secrets — API keys, tokens. See types.ts for the
14
+ // server-side contract the prop surface is shaped around; the short version is
15
+ // that a stored secret must never reach the client, so this component only ever
16
+ // holds a value the user typed in this session and `isSet` is a boolean.
17
+ //
18
+ // Three states, and the reveal toggle exists in only one of them:
19
+ // - editable masked <input type="password"> + reveal toggle
20
+ // - set same, plus the "•••••••• Saved" placeholder treatment
21
+ // - locked readOnly + lock icon, no toggle (nothing client-side to reveal)
22
+ //
23
+ // Styling lives in styles.scss. The field chrome is the shared input group.
24
+ //
25
+ // =============================================================================
26
+ export const SecretInput = forwardRef(function SecretInput({ isSet = false, revealed: revealedProp, defaultRevealed = false, onRevealChange, locked = false, lockedMessage, labels, value, defaultValue, onChange, placeholder, className, inputClassName, disabled, 'aria-describedby': ariaDescribedBy, ...rest }, forwardedRef) {
27
+ // Every rendered string comes from here — see SecretInputLabels for why
28
+ // none of them is assembled from fragments.
29
+ const text = { ...defaultSecretInputLabels, ...labels };
30
+ const inputRef = useRef(null);
31
+ const [revealedState, setRevealedState] = useState(defaultRevealed);
32
+ // Locked in at mount, like the rest of the family: a component that flips
33
+ // between controlled and uncontrolled mid-life pollutes its own state.
34
+ const [isRevealControlled] = useState(revealedProp !== undefined);
35
+ const revealed = isRevealControlled ? !!revealedProp : revealedState;
36
+ const [isValueControlled] = useState(value !== undefined);
37
+ // Emptiness decides the set-state, which drives an ARIA description as well
38
+ // as the placeholder — so a wrong answer here tells a screen-reader user
39
+ // their secret is safely stored when the field is about to overwrite it.
40
+ // That rules out tracking it from React change events alone: `form.reset()`
41
+ // and a post-mount `defaultValue` both change the input's value WITHOUT
42
+ // firing one. The DOM node is the only honest source for the uncontrolled
43
+ // case, so we read it and re-read it on those two events.
44
+ // The seed is deliberately redundant with the effect below, which would
45
+ // reach the same answer one tick later. It exists for the FIRST render: the
46
+ // set-state feeds an ARIA description, and a frame in which the description
47
+ // says "a value is saved and hidden" over a field that already holds a
48
+ // value is a frame of the component asserting the opposite of the truth.
49
+ // Consequence worth knowing: deleting it leaves the suite green, because
50
+ // every assertion runs after effects have flushed.
51
+ const [domHasValue, setDomHasValue] = useState(() => defaultValue !== undefined && String(defaultValue).length > 0);
52
+ const syncFromDom = useCallback(() => {
53
+ setDomHasValue((inputRef.current?.value.length ?? 0) > 0);
54
+ }, []);
55
+ // A later `defaultValue` reaches an un-dirtied input without an event.
56
+ useEffect(() => {
57
+ if (!isValueControlled)
58
+ syncFromDom();
59
+ }, [defaultValue, isValueControlled, syncFromDom]);
60
+ // form.reset() (and React 19's form-action auto-reset) clears the field
61
+ // silently. Read after the reset has actually been applied.
62
+ useEffect(() => {
63
+ const form = inputRef.current?.form;
64
+ if (!form || isValueControlled)
65
+ return;
66
+ const onReset = () => queueMicrotask(syncFromDom);
67
+ form.addEventListener('reset', onReset);
68
+ return () => form.removeEventListener('reset', onReset);
69
+ }, [isValueControlled, syncFromDom]);
70
+ const isEmpty = isValueControlled ? (value ?? '') === '' : !domHasValue;
71
+ // The contract is that the server sends a boolean, never the secret. A
72
+ // prefilled `isSet` field means someone echoed the stored value back into
73
+ // the page — the exact leak this component exists to prevent, and invisible
74
+ // because it renders perfectly.
75
+ if (isDev() && isSet && (value || defaultValue)) {
76
+ console.warn('[SecretInput] `isSet` means "a value exists server-side" and the field ' +
77
+ 'must start empty. Passing `value`/`defaultValue` alongside it sends the ' +
78
+ 'stored secret to the client, which is what `isSet` exists to avoid.');
79
+ }
80
+ const handleChange = (e) => {
81
+ setDomHasValue(e.target.value.length > 0);
82
+ onChange?.(e);
83
+ };
84
+ // Announced state, for the screen-reader-plus-mouse case: pointerdown is
85
+ // prevented so focus stays in the field, which means the toggle is never
86
+ // focused on a mouse click and neither its name nor its state is ever read.
87
+ // Without this, a user flips their key into the clear in total silence.
88
+ // Empty until the first toggle so nothing is announced on mount.
89
+ const [announcement, setAnnouncement] = useState('');
90
+ const toggleReveal = () => {
91
+ const next = !revealed;
92
+ if (!isRevealControlled)
93
+ setRevealedState(next);
94
+ setAnnouncement(next ? text.shown : text.hidden);
95
+ onRevealChange?.(next);
96
+ };
97
+ const handleToggleClick = () => {
98
+ toggleReveal();
99
+ // Pointer activation leaves focus wherever it was — including on <body>
100
+ // if nothing was focused, which strands the user at the top of the
101
+ // document. Recover only in that case: on keyboard activation focus is
102
+ // already on the button, and stealing it would break the toggle.
103
+ const input = inputRef.current;
104
+ const active = input?.ownerDocument.activeElement;
105
+ if (input && !active?.closest('.tui-secret-input'))
106
+ input.focus();
107
+ };
108
+ // The set-state treatment only makes sense while there is nothing typed —
109
+ // once there is, the field shows what the user is entering.
110
+ const showSetState = isSet && isEmpty;
111
+ // ...and the description only makes sense where typing is possible. On a
112
+ // locked field "Typing replaces it" is a straight contradiction, so the
113
+ // locked description supersedes rather than joins it.
114
+ const describeSetState = showSetState && !locked;
115
+ const setDescId = useId();
116
+ const lockedDescId = useId();
117
+ // Composed, never replaced: Field.Control injects aria-describedby pointing
118
+ // at the field's error and helper text, and clobbering it would silence
119
+ // both. Same reason the ids are appended rather than assigned.
120
+ const describedBy = [ariaDescribedBy, describeSetState && setDescId, locked && lockedDescId]
121
+ .filter(Boolean)
122
+ .join(' ') || undefined;
123
+ return (_jsx(TextInput, { ...rest, ref: composeRefs(forwardedRef, inputRef),
124
+ // Masked unless the user asked to see what they typed. A locked field
125
+ // holds nothing to reveal, so it stays masked regardless.
126
+ type: revealed && !locked ? 'text' : 'password', value: value, defaultValue: defaultValue, onChange: handleChange, readOnly: locked || rest.readOnly, disabled: disabled, "aria-describedby": describedBy,
127
+ // The bullets are a placeholder, never a value — a real value would
128
+ // submit and overwrite the stored secret with eight bullet characters.
129
+ // A consumer-supplied placeholder wins; the description still carries
130
+ // the set-state to AT either way.
131
+ placeholder: placeholder ?? (showSetState ? text.valueSet : undefined), className: cx('tui-secret-input', showSetState && 'is-set', locked && 'is-locked', className),
132
+ // `off` is the one value browsers deliberately ignore on a password
133
+ // field; `new-password` is what actually stops Chrome/Safari/Firefox
134
+ // offering a saved credential. That matters beyond annoyance: an
135
+ // autofilled value fires an input event, so the field looks legitimately
136
+ // typed and the save handler would overwrite the stored secret with a
137
+ // credential the user never chose. The two vendor attributes below cover
138
+ // the 1Password/LastPass extensions, which ignore autocomplete.
139
+ autoComplete: rest.autoComplete ?? 'new-password', spellCheck: rest.spellCheck ?? false, "data-1p-ignore": "", "data-lpignore": "true",
140
+ // tui-input-reset strips native in-field chrome — here Edge's own
141
+ // ::-ms-reveal eye, which would sit beside our toggle AND unmask the
142
+ // field without changing `type`, so the toggle's state would be wrong.
143
+ inputClassName: cx('tui-input-reset', 'tui-secret-input__input', inputClassName), suffix: _jsxs(_Fragment, { children: [describeSetState && (_jsx("span", { id: setDescId, className: "tui-visually-hidden", children: text.valueSetDescription })), locked ? (_jsxs(_Fragment, { children: [_jsx("span", { id: lockedDescId, className: "tui-visually-hidden", children: lockedMessage ?? text.locked }), _jsx(Icon, { name: "system/lock-small", className: "tui-secret-input__lock" })] })) : (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", className: "tui-secret-input__reveal", "aria-label": revealed ? text.hide : text.reveal, "data-revealed": revealed ? 'true' : undefined, disabled: disabled, onClick: handleToggleClick,
144
+ // Keep pointerdown from stealing focus before the click fires.
145
+ onPointerDown: (e) => e.preventDefault(), children: _jsx(Icon, { name: revealed ? 'system/view-off' : 'system/view' }) }), _jsx("span", { role: "status", "aria-live": "polite", className: "tui-visually-hidden", children: announcement })] }))] }) }));
146
+ });
@@ -0,0 +1,3 @@
1
+ export { SecretInput } from './SecretInput';
2
+ export { defaultSecretInputLabels } from './types';
3
+ export type { SecretInputProps, SecretInputLabels } from './types';
@@ -0,0 +1,2 @@
1
+ export { SecretInput } from './SecretInput.js';
2
+ export { defaultSecretInputLabels } from './types.js';
@@ -0,0 +1,138 @@
1
+ import type { TextInputProps } from '../TextInput/types';
2
+ /**
3
+ * Overridable label strings for i18n.
4
+ *
5
+ * Every string the component renders or exposes to AT is here — nothing is
6
+ * assembled from fragments at runtime, so a translator controls whole sentences
7
+ * including word order, punctuation and the bullet run.
8
+ */
9
+ export type SecretInputLabels = {
10
+ /**
11
+ * Accessible name for the reveal toggle while the value is masked.
12
+ * @default "Show value"
13
+ */
14
+ reveal?: string;
15
+ /**
16
+ * Accessible name for the reveal toggle while the value is showing.
17
+ *
18
+ * @remarks The name flips rather than staying constant with `aria-pressed`:
19
+ * for "is my API key on screen right now" a name is announced reliably by
20
+ * every AT where `aria-pressed` is not, and stays unambiguous on AT that
21
+ * ignores the state entirely.
22
+ * @default "Hide value"
23
+ */
24
+ hide?: string;
25
+ /**
26
+ * Announced when the value becomes visible. A live region carries this
27
+ * because pointer activation deliberately leaves focus in the field, so the
28
+ * toggle itself is never read on a mouse click.
29
+ * @default "Value shown."
30
+ */
31
+ shown?: string;
32
+ /**
33
+ * Announced when the value is masked again.
34
+ * @default "Value hidden."
35
+ */
36
+ hidden?: string;
37
+ /**
38
+ * The whole placeholder shown while `isSet` — bullets included, deliberately.
39
+ * A translator owns the bullet count, the separator and the word order (some
40
+ * locales want the label first; RTL wants it last), none of which survives
41
+ * being concatenated in code.
42
+ *
43
+ * @remarks Placeholder text only. It is never a value, so nothing submits
44
+ * unless the user types. A `placeholder` prop overrides it.
45
+ * @default "•••••••• Saved"
46
+ */
47
+ valueSet?: string;
48
+ /**
49
+ * Accessible description announced while `isSet`, explaining what an empty
50
+ * field means. Sighted users get the placeholder above; this is its
51
+ * equivalent for AT, and it says more because a placeholder cannot.
52
+ * @default "A value is saved and hidden. Typing replaces it."
53
+ */
54
+ valueSetDescription?: string;
55
+ /**
56
+ * Accessible description when `locked` and no `lockedMessage` is given.
57
+ * @default "This value is managed outside this screen and cannot be edited here."
58
+ */
59
+ locked?: string;
60
+ };
61
+ export declare const defaultSecretInputLabels: Required<SecretInputLabels>;
62
+ /**
63
+ * SecretInput is a TextInput variant for API keys, tokens and other stored
64
+ * secrets. It is shaped around a server-side contract, and only makes sense
65
+ * with it:
66
+ *
67
+ * 1. **A stored secret is never sent to the client** — not as a `value`, not in
68
+ * a config blob. The server sends a boolean (`isSet`) saying a value exists.
69
+ * 2. **"Unchanged" round-trips as absence.** An untouched field submits empty
70
+ * and the save handler treats empty as "keep the stored value". Replacing
71
+ * means typing a new value; clearing is a separate, explicit action.
72
+ * 3. **A config-defined secret wins and locks the field** — `locked` renders it
73
+ * as externally managed: readable, never editable, never revealable.
74
+ *
75
+ * So the component never displays a stored secret. The only value it ever holds
76
+ * is one the user typed in this session, which is why there is an `isSet` flag
77
+ * rather than a masked-value prop — and why the reveal toggle can never leak
78
+ * anything the user did not just type.
79
+ *
80
+ * @remarks Unlike SearchInput, this is a thin native wrapper rather than a value
81
+ * component: `value`/`defaultValue`/`onChange` stay native so the field works
82
+ * inside a plain `<form>` with nothing but a `name`, which is how the PHP-driven
83
+ * consumers submit it. Emptiness is tracked internally for presentation only.
84
+ */
85
+ export type SecretInputProps = Omit<TextInputProps, 'type' | 'prefix' | 'suffix'> & {
86
+ /**
87
+ * A value exists server-side that is deliberately not shown. Renders the
88
+ * set-state treatment while the input is empty.
89
+ *
90
+ * The stored value must never be passed to this component — see the contract
91
+ * above; a dev-only warning fires if it is alongside `isSet`. Nothing is submitted unless the user types, so an empty submit means
92
+ * "unchanged" and the set-state is an accurate description of what the server
93
+ * still holds (including after the user types and deletes back to empty).
94
+ * @default false
95
+ */
96
+ isSet?: boolean;
97
+ /**
98
+ * Initial reveal state (uncontrolled).
99
+ * @default false
100
+ */
101
+ defaultRevealed?: boolean;
102
+ /**
103
+ * Reveal state (controlled) — for policies like re-masking on blur.
104
+ */
105
+ revealed?: boolean;
106
+ /**
107
+ * Called when the reveal toggle is activated, with the new state.
108
+ */
109
+ onRevealChange?: (revealed: boolean) => void;
110
+ /**
111
+ * The value is managed outside this UI (a `wp-config.php` constant, an env
112
+ * var, a policy). Renders read-only with a lock icon and no reveal toggle —
113
+ * there is nothing client-side to reveal.
114
+ *
115
+ * @remarks Deliberately `readOnly`, not `disabled`: a native `disabled` input
116
+ * leaves the tab order and the accessible name goes with it, so AT users lose
117
+ * the information that the field exists and is managed elsewhere. Same
118
+ * reasoning as MoveHandle's locked state.
119
+ * @default false
120
+ */
121
+ locked?: boolean;
122
+ /**
123
+ * Why the field is locked, e.g. "Defined in wp-config.php." Replaces the
124
+ * generic `labels.locked` sentence as the field's accessible description.
125
+ *
126
+ * @remarks Visually hidden, and composed onto any `aria-describedby` the field
127
+ * already has rather than replacing it. Render the visible copy in
128
+ * `Field.HelperText` — and when you do, prefer putting the sentence *only*
129
+ * there (leave this unset) so it is not announced twice.
130
+ */
131
+ lockedMessage?: string;
132
+ /**
133
+ * Overridable label strings for i18n. All keys are optional — defaults are
134
+ * English, and are exported as {@link defaultSecretInputLabels} so a consumer
135
+ * can override one key without restating the rest.
136
+ */
137
+ labels?: SecretInputLabels;
138
+ };
@@ -0,0 +1,11 @@
1
+ export const defaultSecretInputLabels = {
2
+ reveal: 'Show value',
3
+ hide: 'Hide value',
4
+ shown: 'Value shown.',
5
+ hidden: 'Value hidden.',
6
+ // Eight bullets is a deliberate fiction — it must not hint at the real
7
+ // length of the stored secret, which the client does not know anyway.
8
+ valueSet: '•••••••• Saved',
9
+ valueSetDescription: 'A value is saved and hidden. Typing replaces it.',
10
+ locked: 'This value is managed outside this screen and cannot be edited here.',
11
+ };
@@ -37,6 +37,7 @@ export { StepIndicator } from './StepIndicator';
37
37
  export { StepList } from './StepList';
38
38
  export { DataTable } from './Table';
39
39
  export { SearchInput } from './SearchInput';
40
+ export { SecretInput, defaultSecretInputLabels } from './SecretInput';
40
41
  export { SearchSelect, SearchSelectTrigger, SearchSelectContent, SearchSelectOption, SearchSelectGroup, SearchSelectLabel, useSearchSelect, } from './SearchSelect';
41
42
  export { Switch } from './Switch';
42
43
  export { Tabs } from './Tabs';
@@ -80,6 +81,7 @@ export type { StepListProps, StepListItemProps } from './StepList';
80
81
  export type { DataTableProps } from './Table';
81
82
  export type { TabsProps, TabsListProps, TabProps, TabPanelProps, TabsVariant, TabsActivationMode, TabsOrientation, } from './Tabs';
82
83
  export type { SearchInputProps } from './SearchInput';
84
+ export type { SecretInputProps, SecretInputLabels } from './SecretInput';
83
85
  export type { SearchSelectProps, SearchSelectTriggerProps, SearchSelectContentProps, SearchSelectOptionProps, SearchSelectGroupProps, SearchSelectLabelProps, SearchSelectLabels, } from './SearchSelect';
84
86
  export type { SwitchProps } from './Switch';
85
87
  export type { TextareaProps, TextareaResize } from './Textarea';
@@ -35,6 +35,7 @@ export { StepIndicator } from './StepIndicator/index.js';
35
35
  export { StepList } from './StepList/index.js';
36
36
  export { DataTable } from './Table/index.js';
37
37
  export { SearchInput } from './SearchInput/index.js';
38
+ export { SecretInput, defaultSecretInputLabels } from './SecretInput/index.js';
38
39
  export { SearchSelect, SearchSelectTrigger, SearchSelectContent, SearchSelectOption, SearchSelectGroup, SearchSelectLabel, useSearchSelect, } from './SearchSelect/index.js';
39
40
  export { Switch } from './Switch/index.js';
40
41
  export { Tabs } from './Tabs/index.js';
package/figma-tokens.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.2.13",
2
+ "version": "0.2.14",
3
3
  "source": "publish/styles/all.expanded.css",
4
4
  "prefix": "tui-",
5
5
  "note": "Layer 1 primitives only. Component API tokens (--tui-{component}-*) are never defined in CSS — they exist only as fallback reads — so they have no Figma representation.",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tangible/ui",
3
- "version": "0.2.13",
3
+ "version": "0.2.14",
4
4
  "description": "Tangible Design System",
5
5
  "type": "module",
6
6
  "main": "./components/index.js",
@@ -170,6 +170,10 @@
170
170
  "import": "./components/SearchSelect/index.js",
171
171
  "types": "./components/SearchSelect/index.d.ts"
172
172
  },
173
+ "./components/SecretInput": {
174
+ "import": "./components/SecretInput/index.js",
175
+ "types": "./components/SecretInput/index.d.ts"
176
+ },
173
177
  "./components/SegmentedControl": {
174
178
  "import": "./components/SegmentedControl/index.js",
175
179
  "types": "./components/SegmentedControl/index.d.ts"