@ews-admin/global-design-system 1.29.0 → 1.30.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 (35) hide show
  1. package/dist/components/LockScreen/LockScreen.d.ts +12 -0
  2. package/dist/components/LockScreen/LockScreen.d.ts.map +1 -0
  3. package/dist/components/LockScreen/LockScreen.types.d.ts +65 -0
  4. package/dist/components/LockScreen/LockScreen.types.d.ts.map +1 -0
  5. package/dist/components/LockScreen/index.d.ts +3 -0
  6. package/dist/components/LockScreen/index.d.ts.map +1 -0
  7. package/dist/components/PinPad/PinPad.d.ts +10 -0
  8. package/dist/components/PinPad/PinPad.d.ts.map +1 -0
  9. package/dist/components/PinPad/PinPad.types.d.ts +24 -0
  10. package/dist/components/PinPad/PinPad.types.d.ts.map +1 -0
  11. package/dist/components/PinPad/index.d.ts +3 -0
  12. package/dist/components/PinPad/index.d.ts.map +1 -0
  13. package/dist/hooks/index.d.ts +2 -0
  14. package/dist/hooks/index.d.ts.map +1 -1
  15. package/dist/hooks/useIdleLock.d.ts +24 -0
  16. package/dist/hooks/useIdleLock.d.ts.map +1 -0
  17. package/dist/index.css +1 -1
  18. package/dist/index.d.ts +128 -2
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.esm.css +1 -1
  21. package/dist/index.esm.js +375 -1
  22. package/dist/index.esm.js.map +1 -1
  23. package/dist/index.js +377 -0
  24. package/dist/index.js.map +1 -1
  25. package/package.json +1 -1
  26. package/src/components/LockScreen/LockScreen.tsx +324 -0
  27. package/src/components/LockScreen/LockScreen.types.ts +67 -0
  28. package/src/components/LockScreen/index.ts +6 -0
  29. package/src/components/PinPad/PinPad.tsx +150 -0
  30. package/src/components/PinPad/PinPad.types.ts +23 -0
  31. package/src/components/PinPad/index.ts +2 -0
  32. package/src/hooks/index.ts +2 -0
  33. package/src/hooks/useIdleLock.ts +181 -0
  34. package/src/index.ts +21 -2
  35. package/tailwind.preset.js +11 -0
@@ -0,0 +1,181 @@
1
+ import { useCallback, useEffect, useRef, useState } from "react";
2
+
3
+ export interface UseIdleLockOptions {
4
+ /** Inactivity delay before locking. Default 3 minutes. */
5
+ timeoutMs?: number;
6
+ /** Called each time the screen becomes locked (idle, `lock()` or another tab). */
7
+ onLock?: () => void;
8
+ /** localStorage key of the locked flag, also the BroadcastChannel name. */
9
+ storageKey?: string;
10
+ /** Turns the inactivity timer off (e.g. until the session is loaded). Default true. */
11
+ enabled?: boolean;
12
+ }
13
+
14
+ export interface UseIdleLockResult {
15
+ locked: boolean;
16
+ lock: () => void;
17
+ unlock: () => void;
18
+ }
19
+
20
+ const DEFAULT_TIMEOUT_MS = 180_000;
21
+ const DEFAULT_STORAGE_KEY = "ews-idle-lock";
22
+ const ACTIVITY_WRITE_INTERVAL_MS = 5_000;
23
+ const ACTIVITY_EVENTS = [
24
+ "pointerdown",
25
+ "pointermove",
26
+ "keydown",
27
+ "wheel",
28
+ "scroll",
29
+ "touchstart",
30
+ ] as const;
31
+
32
+ const readStorage = (key: string): string | null => {
33
+ try {
34
+ return typeof window === "undefined" ? null : window.localStorage.getItem(key);
35
+ } catch {
36
+ return null;
37
+ }
38
+ };
39
+
40
+ const writeStorage = (key: string, value: string | null) => {
41
+ try {
42
+ if (typeof window === "undefined") return;
43
+ if (value === null) window.localStorage.removeItem(key);
44
+ else window.localStorage.setItem(key, value);
45
+ } catch {
46
+ // Storage unavailable (private mode, quota): the lock still works in this tab.
47
+ }
48
+ };
49
+
50
+ /**
51
+ * Locks the screen after `timeoutMs` without user activity. The locked flag is
52
+ * kept in localStorage (it survives a reload) and shared between tabs through
53
+ * a BroadcastChannel, or the `storage` event where BroadcastChannel is missing:
54
+ * locking or unlocking one tab does the same in every tab. Activity in any tab
55
+ * keeps every tab unlocked.
56
+ */
57
+ export function useIdleLock({
58
+ timeoutMs = DEFAULT_TIMEOUT_MS,
59
+ onLock,
60
+ storageKey = DEFAULT_STORAGE_KEY,
61
+ enabled = true,
62
+ }: UseIdleLockOptions = {}): UseIdleLockResult {
63
+ const activityKey = `${storageKey}:activity`;
64
+ const [locked, setLocked] = useState(() => readStorage(storageKey) === "1");
65
+ const lockedRef = useRef(locked);
66
+ const onLockRef = useRef(onLock);
67
+ const lastActivity = useRef(Date.now());
68
+ const lastActivityWrite = useRef(0);
69
+ const channelRef = useRef<{ postMessage: (message: string) => void } | null>(
70
+ null,
71
+ );
72
+
73
+ useEffect(() => {
74
+ onLockRef.current = onLock;
75
+ }, [onLock]);
76
+
77
+ const markActivity = useCallback(
78
+ (now: number) => {
79
+ lastActivity.current = now;
80
+ if (now - lastActivityWrite.current >= ACTIVITY_WRITE_INTERVAL_MS) {
81
+ lastActivityWrite.current = now;
82
+ writeStorage(activityKey, String(now));
83
+ }
84
+ },
85
+ [activityKey],
86
+ );
87
+
88
+ const applyLocked = useCallback((next: boolean) => {
89
+ if (lockedRef.current === next) return;
90
+ lockedRef.current = next;
91
+ setLocked(next);
92
+ if (next) onLockRef.current?.();
93
+ }, []);
94
+
95
+ const lock = useCallback(() => {
96
+ if (lockedRef.current) return;
97
+ writeStorage(storageKey, "1");
98
+ channelRef.current?.postMessage("lock");
99
+ applyLocked(true);
100
+ }, [storageKey, applyLocked]);
101
+
102
+ const unlock = useCallback(() => {
103
+ lastActivityWrite.current = 0;
104
+ markActivity(Date.now());
105
+ writeStorage(storageKey, null);
106
+ channelRef.current?.postMessage("unlock");
107
+ applyLocked(false);
108
+ }, [storageKey, applyLocked, markActivity]);
109
+
110
+ // Cross-tab sync.
111
+ useEffect(() => {
112
+ if (typeof window === "undefined") return;
113
+ const onRemote = (nextLocked: boolean) => {
114
+ if (!nextLocked) lastActivity.current = Date.now();
115
+ applyLocked(nextLocked);
116
+ };
117
+
118
+ if (typeof window.BroadcastChannel === "function") {
119
+ const channel = new window.BroadcastChannel(storageKey);
120
+ channelRef.current = channel;
121
+ channel.onmessage = (event: MessageEvent) => {
122
+ if (event.data === "lock") onRemote(true);
123
+ else if (event.data === "unlock") onRemote(false);
124
+ };
125
+ return () => {
126
+ channelRef.current = null;
127
+ channel.close();
128
+ };
129
+ }
130
+
131
+ const onStorage = (event: StorageEvent) => {
132
+ if (event.key !== storageKey) return;
133
+ onRemote(event.newValue === "1");
134
+ };
135
+ window.addEventListener("storage", onStorage);
136
+ return () => window.removeEventListener("storage", onStorage);
137
+ }, [storageKey, applyLocked]);
138
+
139
+ // Inactivity timer.
140
+ useEffect(() => {
141
+ if (!enabled || locked || typeof window === "undefined") return;
142
+ lastActivity.current = Date.now();
143
+ let timer: ReturnType<typeof setTimeout>;
144
+
145
+ const idleFor = () => {
146
+ const shared = Number(readStorage(activityKey)) || 0;
147
+ return Date.now() - Math.max(lastActivity.current, shared);
148
+ };
149
+
150
+ const check = () => {
151
+ const idle = idleFor();
152
+ if (idle >= timeoutMs) lock();
153
+ else timer = setTimeout(check, timeoutMs - idle);
154
+ };
155
+
156
+ const onActivity = () => markActivity(Date.now());
157
+
158
+ // Timers are throttled in background tabs: check the delay on return.
159
+ const onVisibility = () => {
160
+ if (document.visibilityState !== "visible") return;
161
+ if (idleFor() >= timeoutMs) lock();
162
+ else onActivity();
163
+ };
164
+
165
+ timer = setTimeout(check, timeoutMs);
166
+ ACTIVITY_EVENTS.forEach((name) =>
167
+ document.addEventListener(name, onActivity, { capture: true, passive: true }),
168
+ );
169
+ document.addEventListener("visibilitychange", onVisibility);
170
+
171
+ return () => {
172
+ clearTimeout(timer);
173
+ ACTIVITY_EVENTS.forEach((name) =>
174
+ document.removeEventListener(name, onActivity, { capture: true }),
175
+ );
176
+ document.removeEventListener("visibilitychange", onVisibility);
177
+ };
178
+ }, [enabled, locked, timeoutMs, activityKey, lock, markActivity]);
179
+
180
+ return { locked, lock, unlock };
181
+ }
package/src/index.ts CHANGED
@@ -55,6 +55,16 @@ export type { PaymentDueBannerProps } from "./components/PaymentDueBanner";
55
55
  export { SecureLoader } from "./components/SecureLoader";
56
56
  export type { SecureLoaderProps } from "./components/SecureLoader";
57
57
 
58
+ export { PinPad } from "./components/PinPad";
59
+ export type { PinPadProps } from "./components/PinPad";
60
+
61
+ export { LockScreen } from "./components/LockScreen";
62
+ export type {
63
+ LockScreenMode,
64
+ LockScreenProps,
65
+ LockScreenTexts,
66
+ } from "./components/LockScreen";
67
+
58
68
  // Molecules
59
69
  export { SpecialtySearchAutocomplete } from "./molecules";
60
70
  export type { Specialty, SpecialtySearchAutocompleteProps } from "./molecules";
@@ -95,8 +105,17 @@ export type {
95
105
  } from "./utils";
96
106
 
97
107
  // Hooks
98
- export { useDebounce, useDebouncedCallback, useSelectField } from "./hooks";
99
- export type { UseSelectFieldProps } from "./hooks";
108
+ export {
109
+ useDebounce,
110
+ useDebouncedCallback,
111
+ useIdleLock,
112
+ useSelectField,
113
+ } from "./hooks";
114
+ export type {
115
+ UseIdleLockOptions,
116
+ UseIdleLockResult,
117
+ UseSelectFieldProps,
118
+ } from "./hooks";
100
119
 
101
120
  // Theme
102
121
  export { ThemeProvider, useTheme } from "./theme/ThemeProvider";
@@ -135,6 +135,17 @@ module.exports = {
135
135
  large:
136
136
  "0 10px 40px -10px rgba(0, 0, 0, 0.15), 0 2px 10px -2px rgba(0, 0, 0, 0.05)",
137
137
  },
138
+ // PinPad: wrong PIN feedback
139
+ keyframes: {
140
+ "pin-shake": {
141
+ "0%, 100%": { transform: "translateX(0)" },
142
+ "20%, 60%": { transform: "translateX(-8px)" },
143
+ "40%, 80%": { transform: "translateX(8px)" },
144
+ },
145
+ },
146
+ animation: {
147
+ "pin-shake": "pin-shake 0.4s ease-in-out",
148
+ },
138
149
  },
139
150
  },
140
151
  plugins: [