nucleus-core-ts 0.10.265 → 0.10.267

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/dist/.build-ok CHANGED
@@ -1 +1 @@
1
- 0.10.265
1
+ 0.10.267
@@ -12,6 +12,7 @@ import { useTextInputHandlers } from '../hooks/useTextInputHandlers';
12
12
  import { DEFAULT_TEXT_INPUT_LABELS } from '../labels';
13
13
  import { nucleusTextInputTheme } from '../theme';
14
14
  import { cn } from '../utils/cn';
15
+ import { getEntryAttributes } from '../utils/entryAttributes';
15
16
  import { getAutoCompleteForType, getInputModeForType } from '../utils/validation';
16
17
  import { getDisplayValue } from '../utils/valueView';
17
18
  import { PasswordStrengthIndicator } from './PasswordStrengthIndicator';
@@ -37,8 +38,7 @@ export function NucleusTextInput({ value = '', onChange, type = 'text', label, l
37
38
  const displayError = errorMessage || internalError;
38
39
  const hasError = Boolean(displayError);
39
40
  const hasValue = Boolean(value && value.length > 0);
40
- const autofilled = useAutofilled(inputRef);
41
- const { isFocused, inputHandlers } = useTextInputHandlers({
41
+ const { isFocused, inputHandlers, syncFromDom } = useTextInputHandlers({
42
42
  props: {
43
43
  onChange,
44
44
  onFocus,
@@ -65,6 +65,7 @@ export function NucleusTextInput({ value = '', onChange, type = 'text', label, l
65
65
  setIsValid,
66
66
  handleValidation
67
67
  });
68
+ const autofilled = useAutofilled(inputRef, syncFromDom);
68
69
  const { isHovered, handleMouseEnter, handleMouseLeave } = useHoverBorder({
69
70
  contextSafe,
70
71
  wrapperRef,
@@ -118,6 +119,7 @@ export function NucleusTextInput({ value = '', onChange, type = 'text', label, l
118
119
  type: type === 'number' && formatNumber ? 'text' : inputType,
119
120
  inputMode: getInputModeForType(type),
120
121
  autoComplete: getAutoCompleteForType(type, isNewPassword),
122
+ ...getEntryAttributes(type, restProps.autoComplete ?? getAutoCompleteForType(type, isNewPassword)),
121
123
  maxLength: maxInputLength,
122
124
  value: getDisplayValue(value, {
123
125
  type,
@@ -4,6 +4,10 @@ export declare function isBrowserFilled(input: HTMLInputElement): boolean;
4
4
  /**
5
5
  * Whether the browser autofilled the input, so the floating label can lift above the value instead of
6
6
  * sitting on top of it. Checked for a few seconds after mount (when saved sign-ins are filled) and on
7
- * every input or animation event; an autofilled field the visitor then clears reports false again.
7
+ * every input, change or animation event; an autofilled field the visitor then clears reports false again.
8
+ *
9
+ * `onFilled` runs whenever a check finds the field autofilled and on every change or animationstart
10
+ * event: a password manager may write the DOM value without an input event React sees, and the caller
11
+ * uses it to pass that value on to its own state before the next render overwrites it.
8
12
  */
9
- export declare function useAutofilled(ref: RefObject<HTMLInputElement | null>): boolean;
13
+ export declare function useAutofilled(ref: RefObject<HTMLInputElement | null>, onFilled?: () => void): boolean;
@@ -18,12 +18,23 @@ const POLL_TRIES = 24;
18
18
  /**
19
19
  * Whether the browser autofilled the input, so the floating label can lift above the value instead of
20
20
  * sitting on top of it. Checked for a few seconds after mount (when saved sign-ins are filled) and on
21
- * every input or animation event; an autofilled field the visitor then clears reports false again.
22
- */ export function useAutofilled(ref) {
21
+ * every input, change or animation event; an autofilled field the visitor then clears reports false again.
22
+ *
23
+ * `onFilled` runs whenever a check finds the field autofilled and on every change or animationstart
24
+ * event: a password manager may write the DOM value without an input event React sees, and the caller
25
+ * uses it to pass that value on to its own state before the next render overwrites it.
26
+ */ export function useAutofilled(ref, onFilled) {
23
27
  const [filled, setFilled] = useState(false);
24
28
  const check = useEffectEvent(()=>{
25
29
  const input = ref.current;
26
- if (input) setFilled(isBrowserFilled(input));
30
+ if (!input) return;
31
+ const autofilled = isBrowserFilled(input);
32
+ setFilled(autofilled);
33
+ if (autofilled) onFilled?.();
34
+ });
35
+ const sync = useEffectEvent(()=>{
36
+ onFilled?.();
37
+ check();
27
38
  });
28
39
  useEffect(()=>{
29
40
  const input = ref.current;
@@ -35,11 +46,13 @@ const POLL_TRIES = 24;
35
46
  }, POLL_MS);
36
47
  check();
37
48
  input?.addEventListener('input', check);
38
- input?.addEventListener('change', check);
49
+ input?.addEventListener('change', sync);
50
+ input?.addEventListener('animationstart', sync);
39
51
  return ()=>{
40
52
  clearInterval(timer);
41
53
  input?.removeEventListener('input', check);
42
- input?.removeEventListener('change', check);
54
+ input?.removeEventListener('change', sync);
55
+ input?.removeEventListener('animationstart', sync);
43
56
  };
44
57
  }, [
45
58
  ref
@@ -14,6 +14,8 @@ interface UseTextInputHandlersOptions {
14
14
  /** Focus, blur, change and clipboard handling of the text input. */
15
15
  export declare function useTextInputHandlers({ props, inputRef, contextSafe, say, internalError, setInternalError, setIsValid, handleValidation, }: UseTextInputHandlersOptions): {
16
16
  isFocused: boolean;
17
+ /** Reports a DOM value React was not told about (autofill) through the change path. */
18
+ syncFromDom: () => void;
17
19
  /** Event props for the native input. */
18
20
  inputHandlers: {
19
21
  onChange: (e: ChangeEvent<HTMLInputElement>) => void;
@@ -2,7 +2,7 @@
2
2
  import { useState } from 'react';
3
3
  import { computeCaretPosition } from '../utils/cursor';
4
4
  import { formatNumberWithSeparator, formatPhoneNumber, unformatNumber } from '../utils/format';
5
- import { getUnformattedValue } from '../utils/valueView';
5
+ import { getDisplayValue, getUnformattedValue } from '../utils/valueView';
6
6
  /** Focus, blur, change and clipboard handling of the text input. */ export function useTextInputHandlers({ props, inputRef, contextSafe, say, internalError, setInternalError, setIsValid, handleValidation }) {
7
7
  const { onChange, onFocus, onBlur, type = 'text', disabled = false, value = '', confirmValue, trimOnBlur = false, validateOnBlur = true, validateOnChange = false, formatNumber = false, thousandSeparator = ' ', phoneFormat = 'tr', preventCopy = false, preventPaste = false, preventContextMenu = false } = props;
8
8
  const [isFocused, setIsFocused] = useState(false);
@@ -17,6 +17,7 @@ import { getUnformattedValue } from '../utils/valueView';
17
17
  });
18
18
  const handleBlur = contextSafe((e)=>{
19
19
  setIsFocused(false);
20
+ syncFromDom();
20
21
  if (trimOnBlur && value) {
21
22
  const trimmed = value.trim();
22
23
  if (trimmed !== value) {
@@ -43,13 +44,32 @@ import { getUnformattedValue } from '../utils/valueView';
43
44
  }
44
45
  };
45
46
  const handleContextMenu = (e)=>{
46
- if (preventContextMenu || type === 'password') {
47
+ if (preventContextMenu) {
47
48
  e.preventDefault();
48
49
  }
49
50
  };
50
51
  const handleChange = (e)=>{
51
- const cursorPos = e.target.selectionStart ?? 0;
52
- let newValue = e.target.value;
52
+ emitChange(e.target);
53
+ };
54
+ /**
55
+ * Some password managers write the DOM value without an input event React sees; the controlled
56
+ * `value` would then overwrite it on the next render. When the DOM shows something other than what
57
+ * the field displays, report it through the normal change path. Once reported the display matches
58
+ * the DOM, so this never repeats.
59
+ */ function syncFromDom() {
60
+ const input = inputRef.current;
61
+ if (!input || disabled) return;
62
+ const shown = getDisplayValue(value, {
63
+ type,
64
+ formatNumber,
65
+ thousandSeparator,
66
+ phoneFormat
67
+ });
68
+ if (input.value !== shown) emitChange(input);
69
+ }
70
+ function emitChange(target) {
71
+ const cursorPos = target.selectionStart ?? 0;
72
+ let newValue = target.value;
53
73
  const isDeleting = unformatNumber(newValue).length < unformatNumber(value).length;
54
74
  if (type === 'number' && formatNumber) {
55
75
  newValue = formatNumberWithSeparator(unformatNumber(newValue), thousandSeparator);
@@ -61,7 +81,7 @@ import { getUnformattedValue } from '../utils/valueView';
61
81
  const emitted = newValue;
62
82
  requestAnimationFrame(()=>{
63
83
  if (!inputRef.current) return;
64
- const caret = computeCaretPosition(e.target.value, emitted, cursorPos, isDeleting, thousandSeparator);
84
+ const caret = computeCaretPosition(target.value, emitted, cursorPos, isDeleting, thousandSeparator);
65
85
  inputRef.current.setSelectionRange(caret, caret);
66
86
  });
67
87
  }
@@ -78,9 +98,10 @@ import { getUnformattedValue } from '../utils/valueView';
78
98
  setInternalError(undefined);
79
99
  setIsValid(null);
80
100
  }
81
- };
101
+ }
82
102
  return {
83
103
  isFocused,
104
+ /** Reports a DOM value React was not told about (autofill) through the change path. */ syncFromDom,
84
105
  /** Event props for the native input. */ inputHandlers: {
85
106
  onChange: handleChange,
86
107
  onFocus: handleFocus,
@@ -0,0 +1,10 @@
1
+ import type { InputHTMLAttributes } from 'react';
2
+ import type { InputType } from './validation';
3
+ /** True when the text is an address, secret or number a keyboard must never "fix". */
4
+ export declare function isLiteralEntry(type: InputType, autoComplete: string | undefined): boolean;
5
+ /**
6
+ * Keyboard hints for the native input. A phone keyboard capitalises, autocorrects and spell-checks
7
+ * ordinary text; for an e-mail, password, URL or phone number that silently changes what was typed
8
+ * (mesirecafe becomes mesiracafe). Callers' own attributes are applied after these and win.
9
+ */
10
+ export declare function getEntryAttributes(type: InputType, autoComplete: string | undefined): Pick<InputHTMLAttributes<HTMLInputElement>, 'autoCapitalize' | 'autoCorrect' | 'spellCheck'>;
@@ -0,0 +1,27 @@
1
+ const LITERAL_TYPES = [
2
+ 'email',
3
+ 'password',
4
+ 'url',
5
+ 'tel'
6
+ ];
7
+ const LITERAL_AUTOCOMPLETE = [
8
+ 'username',
9
+ 'email'
10
+ ];
11
+ /** True when the text is an address, secret or number a keyboard must never "fix". */ export function isLiteralEntry(type, autoComplete) {
12
+ if (LITERAL_TYPES.includes(type)) return true;
13
+ if (!autoComplete) return false;
14
+ return autoComplete.toLowerCase().split(/\s+/).some((token)=>LITERAL_AUTOCOMPLETE.includes(token));
15
+ }
16
+ /**
17
+ * Keyboard hints for the native input. A phone keyboard capitalises, autocorrects and spell-checks
18
+ * ordinary text; for an e-mail, password, URL or phone number that silently changes what was typed
19
+ * (mesirecafe becomes mesiracafe). Callers' own attributes are applied after these and win.
20
+ */ export function getEntryAttributes(type, autoComplete) {
21
+ if (!isLiteralEntry(type, autoComplete)) return {};
22
+ return {
23
+ autoCapitalize: 'none',
24
+ autoCorrect: 'off',
25
+ spellCheck: false
26
+ };
27
+ }
@@ -25,6 +25,8 @@ export interface ReplayFrameOptions {
25
25
  startChapter?: string | null;
26
26
  typingDelayMs?: number;
27
27
  tickMs?: number;
28
+ /** How often a frame that is rebuilding its starting state tells the parent it is alive (default 2 s). */
29
+ heartbeatMs?: number;
28
30
  pointTimeoutMs?: number;
29
31
  targetTimeoutMs?: number;
30
32
  visible?: VisibleCheck;
@@ -93,6 +93,7 @@ const realClock = {
93
93
  if (recordingId !== store.manifest?.id) return;
94
94
  if (preparing) {
95
95
  if (!ready) handshake.rearm(); // alive but not ready: the wait starts over, play and seek stay queued
96
+ else seek.frameAlive(); // a reloaded frame still rebuilding its prelude: the reload guard starts over
96
97
  return;
97
98
  }
98
99
  const first = !ready;
@@ -17,6 +17,8 @@ export interface SeekGate {
17
17
  acknowledged: (reloading: boolean) => void;
18
18
  /** A frame reported ready: whatever seek was in flight is done. */
19
19
  frameReady: () => void;
20
+ /** A reloaded frame reported it is still rebuilding: the wait for its `ready` starts over (progress, not silence). */
21
+ frameAlive: () => void;
20
22
  /** True between the seek and its acknowledgement: ticks in that window predate the seek. */
21
23
  isStale: () => boolean;
22
24
  }
@@ -55,6 +55,9 @@
55
55
  if (acksOwed === 0 && readiesOwed === 0) return done();
56
56
  armReloadGuard();
57
57
  },
58
+ frameAlive () {
59
+ if (readiesOwed > 0) armReloadGuard();
60
+ },
58
61
  isStale: ()=>acksOwed > 0
59
62
  };
60
63
  }
@@ -194,7 +194,7 @@ const MAX_KEY_LENGTH = 200;
194
194
  const earlier = start ? steps.filter((step)=>step.at < start.from) : [];
195
195
  if (start && earlier.some((step)=>step.actions.length > 0)) {
196
196
  preluding = true;
197
- const { navigate, visible, targetTimeoutMs } = options;
197
+ const { navigate, visible, targetTimeoutMs, heartbeatMs } = options;
198
198
  const attribute = manifest.targetAttribute;
199
199
  void runPrelude({
200
200
  ...{
@@ -208,10 +208,13 @@ const MAX_KEY_LENGTH = 200;
208
208
  navigate,
209
209
  visible,
210
210
  targetTimeoutMs,
211
+ heartbeatMs,
211
212
  settle,
212
213
  cancelled: ()=>stopped
213
214
  },
214
- onSkip: options.skipFailedSteps === false ? undefined : reportSkip
215
+ onSkip: options.skipFailedSteps === false ? undefined : reportSkip,
216
+ // A reloaded frame gets no hello (the parent stopped at its first ready): a `preparing` heartbeat keeps its guard alive.
217
+ onHeartbeat: announce
215
218
  }).then(()=>{
216
219
  preluding = false;
217
220
  if (!stopped) begin();
@@ -13,14 +13,11 @@ export interface FramePreludeOptions {
13
13
  settle: () => Promise<void>;
14
14
  /** Told of a step the prelude dropped (its target never appeared); `actionIndex` is the action that failed. */
15
15
  onSkip?: (stepId: string, actionIndex: number, reason: FailureReason) => void;
16
+ /** Called every `heartbeatMs` while the prelude runs (never once cancelled): the frame is rebuilding, not hung. */
17
+ onHeartbeat?: () => void;
18
+ heartbeatMs?: number;
16
19
  /** True once the frame was stopped: the prelude ends at the next action. */
17
20
  cancelled: () => boolean;
18
21
  }
19
- /**
20
- * Rebuilds the product's state at a chapter start. A frame that starts at chapter N (a seek reloads it there) has
21
- * none of what the earlier chapters' steps did: the conversation was never opened, the workspace never chosen. So
22
- * every earlier step's state-changing actions run again at once, with no cursor, no typing delay and no waiting: the
23
- * paused playhead jumps to each step's time (the recorded answers and stream frames due by then arrive on the way)
24
- * and a wait inside a step becomes a jump. A step that does not apply (its target never appears) is skipped, not fatal.
25
- */
22
+ /** `rebuild` with a heartbeat on top: the parent hears from the frame for exactly as long as the prelude runs. */
26
23
  export declare function runPrelude(options: FramePreludeOptions): Promise<void>;
@@ -1,4 +1,5 @@
1
1
  import { runStep } from '../../../../src/Client/Replay';
2
+ import { startPreludeBeat } from './preludeBeat';
2
3
  /** Waiting for a target longer than this in a prelude means the step does not apply here; the prelude moves on. */ const PRELUDE_TARGET_TIMEOUT_MS = 3000;
3
4
  /** Actions that read or wait rather than change what the screen holds; a prelude leaves them out. */ function changesState(action) {
4
5
  return action.kind !== 'scrollTo' && action.kind !== 'waitFor';
@@ -9,7 +10,7 @@ import { runStep } from '../../../../src/Client/Replay';
9
10
  * every earlier step's state-changing actions run again at once, with no cursor, no typing delay and no waiting: the
10
11
  * paused playhead jumps to each step's time (the recorded answers and stream frames due by then arrive on the way)
11
12
  * and a wait inside a step becomes a jump. A step that does not apply (its target never appears) is skipped, not fatal.
12
- */ export async function runPrelude(options) {
13
+ */ async function rebuild(options) {
13
14
  const { timeline } = options;
14
15
  timeline.pause();
15
16
  for (const step of options.steps){
@@ -35,3 +36,12 @@ import { runStep } from '../../../../src/Client/Replay';
35
36
  if (!result.ok) options.onSkip?.(step.id, result.actionIndex, result.reason);
36
37
  }
37
38
  }
39
+ /** `rebuild` with a heartbeat on top: the parent hears from the frame for exactly as long as the prelude runs. */ export async function runPrelude(options) {
40
+ const { onHeartbeat } = options;
41
+ const stopBeat = onHeartbeat ? startPreludeBeat(options.win, ()=>options.cancelled() || onHeartbeat(), options.heartbeatMs) : ()=>{};
42
+ try {
43
+ await rebuild(options);
44
+ } finally{
45
+ stopBeat();
46
+ }
47
+ }
@@ -0,0 +1,9 @@
1
+ import type { Win } from '../../../../src/Client/Replay';
2
+ /** A step can sit silent for tens of seconds (many absent targets at 3 s each); the parent's reload guard is 20 s. */
3
+ export declare const PRELUDE_HEARTBEAT_MS = 2000;
4
+ /**
5
+ * Calls `beat` on a fixed interval until the returned function runs. The prelude's own steps report nothing while
6
+ * they wait, so progress is time-driven: a frame that is still rebuilding keeps beating, a frame that stopped
7
+ * (cancelled, crashed, torn down) beats no more and the parent's guard then fails it as it should.
8
+ */
9
+ export declare function startPreludeBeat(win: Win, beat: () => void, everyMs?: number): () => void;
@@ -0,0 +1,9 @@
1
+ /** A step can sit silent for tens of seconds (many absent targets at 3 s each); the parent's reload guard is 20 s. */ export const PRELUDE_HEARTBEAT_MS = 2000;
2
+ /**
3
+ * Calls `beat` on a fixed interval until the returned function runs. The prelude's own steps report nothing while
4
+ * they wait, so progress is time-driven: a frame that is still rebuilding keeps beating, a frame that stopped
5
+ * (cancelled, crashed, torn down) beats no more and the parent's guard then fails it as it should.
6
+ */ export function startPreludeBeat(win, beat, everyMs = PRELUDE_HEARTBEAT_MS) {
7
+ const id = win.setInterval(beat, everyMs);
8
+ return ()=>win.clearInterval(id);
9
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nucleus-core-ts",
3
- "version": "0.10.265",
3
+ "version": "0.10.267",
4
4
  "description": "Production-ready, enterprise-grade TypeScript framework for building multi-tenant APIs",
5
5
  "author": "Hidayet Can Özcan <hidayetcan@gmail.com>",
6
6
  "license": "SEE LICENSE IN LICENSE",