@jcsoftdev/ui 0.4.0 → 0.5.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 (131) hide show
  1. package/CHANGELOG.md +97 -0
  2. package/README.md +1 -1
  3. package/dist/atoms/button/button.d.ts +8 -1
  4. package/dist/atoms/button/button.js +10 -3
  5. package/dist/atoms/divider/divider.d.ts +15 -0
  6. package/dist/atoms/divider/divider.js +27 -0
  7. package/dist/atoms/divider/index.d.ts +1 -0
  8. package/dist/atoms/divider/index.js +1 -0
  9. package/dist/atoms/icon-button/icon-button.d.ts +1 -1
  10. package/dist/atoms/index.d.ts +1 -0
  11. package/dist/atoms/index.js +1 -0
  12. package/dist/auth/auth-illustration/auth-illustration.d.ts +35 -0
  13. package/dist/auth/auth-illustration/auth-illustration.js +72 -0
  14. package/dist/auth/auth-illustration/index.d.ts +3 -0
  15. package/dist/auth/auth-illustration/index.js +11 -0
  16. package/dist/auth/auth-illustration/look.d.ts +30 -0
  17. package/dist/auth/auth-illustration/look.js +0 -0
  18. package/dist/auth/auth-illustration/model.d.ts +133 -0
  19. package/dist/auth/auth-illustration/model.js +202 -0
  20. package/dist/auth/auth-illustration/use-art-pointer.d.ts +9 -0
  21. package/dist/auth/auth-illustration/use-art-pointer.js +69 -0
  22. package/dist/auth/auth-layout/auth-layout.d.ts +80 -0
  23. package/dist/auth/auth-layout/auth-layout.js +190 -0
  24. package/dist/auth/auth-layout/index.d.ts +1 -0
  25. package/dist/auth/auth-layout/index.js +1 -0
  26. package/dist/auth/character/character.d.ts +12 -0
  27. package/dist/auth/character/character.js +333 -0
  28. package/dist/auth/character/index.d.ts +1 -0
  29. package/dist/auth/character/index.js +1 -0
  30. package/dist/auth/index.d.ts +4 -0
  31. package/dist/auth/index.js +4 -0
  32. package/dist/auth/internal/auth-context.d.ts +17 -0
  33. package/dist/auth/internal/auth-context.js +10 -0
  34. package/dist/auth/internal/auth-styles.d.ts +1 -0
  35. package/dist/auth/internal/auth-styles.js +29 -0
  36. package/dist/auth/internal/portal-glyphs.d.ts +6 -0
  37. package/dist/auth/internal/portal-glyphs.js +39 -0
  38. package/dist/auth/login-form/context.d.ts +51 -0
  39. package/dist/auth/login-form/context.js +12 -0
  40. package/dist/auth/login-form/index.d.ts +2 -0
  41. package/dist/auth/login-form/index.js +23 -0
  42. package/dist/auth/login-form/login-form.d.ts +66 -0
  43. package/dist/auth/login-form/login-form.js +233 -0
  44. package/dist/auth/login-form/parts.d.ts +71 -0
  45. package/dist/auth/login-form/parts.js +371 -0
  46. package/dist/auth/looks/auth-illustration-any.d.ts +16 -0
  47. package/dist/auth/looks/auth-illustration-any.js +24 -0
  48. package/dist/auth/looks/index.d.ts +1 -0
  49. package/dist/auth/looks/index.js +1 -0
  50. package/dist/auth/mosaic/index.d.ts +1 -0
  51. package/dist/auth/mosaic/index.js +1 -0
  52. package/dist/auth/mosaic/mosaic.d.ts +11 -0
  53. package/dist/auth/mosaic/mosaic.js +195 -0
  54. package/dist/auth/slabs/index.d.ts +1 -0
  55. package/dist/auth/slabs/index.js +1 -0
  56. package/dist/auth/slabs/slabs.d.ts +13 -0
  57. package/dist/auth/slabs/slabs.js +136 -0
  58. package/dist/auth/stack/index.d.ts +1 -0
  59. package/dist/auth/stack/index.js +1 -0
  60. package/dist/auth/stack/stack.d.ts +6 -0
  61. package/dist/auth/stack/stack.js +222 -0
  62. package/dist/auth/statement/index.d.ts +1 -0
  63. package/dist/auth/statement/index.js +1 -0
  64. package/dist/auth/statement/statement.d.ts +13 -0
  65. package/dist/auth/statement/statement.js +99 -0
  66. package/dist/forms/index.d.ts +1 -0
  67. package/dist/forms/index.js +1 -0
  68. package/dist/forms/input/input.js +2 -1
  69. package/dist/forms/password-input/index.d.ts +1 -0
  70. package/dist/forms/password-input/index.js +1 -0
  71. package/dist/forms/password-input/password-input.d.ts +16 -0
  72. package/dist/forms/password-input/password-input.js +45 -0
  73. package/dist/i18n/strings.d.ts +64 -0
  74. package/dist/i18n/strings.js +34 -2
  75. package/dist/icons/eye.d.ts +2 -0
  76. package/dist/icons/eye.js +11 -0
  77. package/dist/icons/graduation-cap.d.ts +2 -0
  78. package/dist/icons/graduation-cap.js +12 -0
  79. package/dist/icons/house.d.ts +2 -0
  80. package/dist/icons/house.js +11 -0
  81. package/dist/icons/index.d.ts +4 -0
  82. package/dist/icons/index.js +8 -0
  83. package/dist/icons/mail.d.ts +2 -0
  84. package/dist/icons/mail.js +11 -0
  85. package/icons/svg/eye.svg +4 -0
  86. package/icons/svg/graduation-cap.svg +5 -0
  87. package/icons/svg/house.svg +4 -0
  88. package/icons/svg/mail.svg +4 -0
  89. package/package.json +2 -2
  90. package/src/atoms/button/button.tsx +25 -3
  91. package/src/atoms/divider/divider.tsx +40 -0
  92. package/src/atoms/divider/index.ts +1 -0
  93. package/src/atoms/icon-button/icon-button.tsx +1 -1
  94. package/src/atoms/index.ts +1 -0
  95. package/src/auth/auth-illustration/auth-illustration.tsx +100 -0
  96. package/src/auth/auth-illustration/index.ts +8 -0
  97. package/src/auth/auth-illustration/look.ts +32 -0
  98. package/src/auth/auth-illustration/model.ts +330 -0
  99. package/src/auth/auth-illustration/use-art-pointer.ts +78 -0
  100. package/src/auth/auth-layout/auth-layout.tsx +282 -0
  101. package/src/auth/auth-layout/index.ts +1 -0
  102. package/src/auth/character/character.tsx +346 -0
  103. package/src/auth/character/index.ts +1 -0
  104. package/src/auth/index.ts +4 -0
  105. package/src/auth/internal/auth-context.ts +26 -0
  106. package/src/auth/internal/auth-styles.tsx +39 -0
  107. package/src/auth/internal/portal-glyphs.tsx +32 -0
  108. package/src/auth/login-form/context.ts +78 -0
  109. package/src/auth/login-form/index.ts +21 -0
  110. package/src/auth/login-form/login-form.tsx +330 -0
  111. package/src/auth/login-form/parts.tsx +457 -0
  112. package/src/auth/looks/auth-illustration-any.tsx +38 -0
  113. package/src/auth/looks/index.ts +1 -0
  114. package/src/auth/mosaic/index.ts +1 -0
  115. package/src/auth/mosaic/mosaic.tsx +255 -0
  116. package/src/auth/slabs/index.ts +1 -0
  117. package/src/auth/slabs/slabs.tsx +181 -0
  118. package/src/auth/stack/index.ts +1 -0
  119. package/src/auth/stack/stack.tsx +267 -0
  120. package/src/auth/statement/index.ts +1 -0
  121. package/src/auth/statement/statement.tsx +122 -0
  122. package/src/forms/index.ts +1 -0
  123. package/src/forms/input/input.tsx +2 -0
  124. package/src/forms/password-input/index.ts +1 -0
  125. package/src/forms/password-input/password-input.tsx +57 -0
  126. package/src/i18n/strings.ts +64 -0
  127. package/src/icons/eye.tsx +12 -0
  128. package/src/icons/graduation-cap.tsx +13 -0
  129. package/src/icons/house.tsx +12 -0
  130. package/src/icons/index.ts +4 -0
  131. package/src/icons/mail.tsx +12 -0
@@ -0,0 +1,40 @@
1
+ import type { ComponentPropsWithoutRef, ReactNode } from "react";
2
+ import type { ClassNames } from "../../utils/class-names.js";
3
+ import { cn } from "../../utils/cn.js";
4
+
5
+ export type DividerPart = "root" | "line" | "label";
6
+
7
+ export interface DividerProps extends Omit<ComponentPropsWithoutRef<"div">, "children"> {
8
+ /** Text centered on the rule. Without it the divider is a plain hairline. */
9
+ children?: ReactNode;
10
+ /** Per-part classes merged with `cn`; consumer classes win. `className` still targets the root and is merged last. */
11
+ classNames?: ClassNames<DividerPart>;
12
+ }
13
+
14
+ /**
15
+ * Horizontal rule, optionally carrying a short label ("or", "with your account"). It is a
16
+ * separator for assistive technology, so the label is read as part of the separator's name
17
+ * rather than as page content.
18
+ */
19
+ export function Divider({ children, className, classNames, ...props }: DividerProps) {
20
+ const labelled = children !== undefined && children !== null && children !== false;
21
+ const line = cn("h-px min-w-0 flex-1 bg-line", classNames?.line);
22
+ return (
23
+ // biome-ignore lint/a11y/useSemanticElements: an <hr> cannot carry the label between the two rules
24
+ <div
25
+ role="separator"
26
+ aria-orientation="horizontal"
27
+ aria-label={labelled && typeof children === "string" ? children : undefined}
28
+ {...props}
29
+ className={cn("flex items-center gap-3 text-ink-3 text-meta", classNames?.root, className)}
30
+ >
31
+ <span aria-hidden="true" className={line} />
32
+ {labelled ? (
33
+ <span aria-hidden="true" className={cn("shrink-0", classNames?.label)}>
34
+ {children}
35
+ </span>
36
+ ) : null}
37
+ {labelled ? <span aria-hidden="true" className={line} /> : null}
38
+ </div>
39
+ );
40
+ }
@@ -0,0 +1 @@
1
+ export * from "./divider.js";
@@ -20,7 +20,7 @@ export interface IconButtonProps
20
20
  label: string;
21
21
  variant?: ButtonVariant;
22
22
  size?: IconButtonSize;
23
- /** Swaps the icon for a spinner and disables the button; announces the localized `loading` string. */
23
+ /** Swaps the icon for a spinner and makes the button busy like `Button` `loading`: it keeps focus, ignores clicks and announces the localized `loading` string. */
24
24
  loading?: boolean;
25
25
  /** Render the child element (via Slot) instead of a `<button>`, e.g. a link wrapping the icon. */
26
26
  asChild?: boolean;
@@ -4,6 +4,7 @@ export * from "./big-date/index.js";
4
4
  export * from "./button/index.js";
5
5
  export * from "./checkbox/index.js";
6
6
  export * from "./chip/index.js";
7
+ export * from "./divider/index.js";
7
8
  export * from "./due-chip/index.js";
8
9
  export * from "./icon/index.js";
9
10
  export * from "./icon-button/index.js";
@@ -0,0 +1,100 @@
1
+ "use client";
2
+
3
+ import {
4
+ type ComponentPropsWithoutRef,
5
+ type ReactNode,
6
+ useEffect,
7
+ useId,
8
+ useRef,
9
+ useState,
10
+ } from "react";
11
+ import { cn } from "../../utils/cn.js";
12
+ import { useOptionalAuth } from "../internal/auth-context.js";
13
+ import { AuthStyles } from "../internal/auth-styles.js";
14
+ import { Slabs } from "../slabs/slabs.js";
15
+ import type { AuthLook } from "./look.js";
16
+ import { type AuthState, clamp01, ERROR_HOLD_MS, shownState } from "./model.js";
17
+ import { useArtPointer } from "./use-art-pointer.js";
18
+
19
+ export interface AuthIllustrationProps extends Omit<ComponentPropsWithoutRef<"div">, "children"> {
20
+ /**
21
+ * The look, a component from its own entry (`Stack` from `@jcsoftdev/ui/auth/stack`, and so on).
22
+ * Defaults to `Slabs`; looks you do not pass stay out of your bundle. For a string `variant`
23
+ * use `AuthIllustrationAny` from `@jcsoftdev/ui/auth/looks`.
24
+ */
25
+ look?: AuthLook;
26
+ /** What the screen is doing. Inside an `AuthLayout` it follows the layout's state. */
27
+ state?: AuthState;
28
+ /** Fill of the whole form, 0 to 1. Inside an `AuthLayout` it follows the layout's progress. */
29
+ progress?: number;
30
+ /** Index of the portal being used (0 to 2); that slab rises above the others. */
31
+ activeIndex?: number;
32
+ /**
33
+ * Accessible name of the image. Without it the illustration is decorative and hidden from
34
+ * assistive technology. Ignored by text looks (`Statement`): their text is the content, so they
35
+ * are never an `img` and never hidden.
36
+ */
37
+ label?: string;
38
+ /** Text looks (`Statement`) only: the headline (a string animates word by word). Defaults to the UI string `welcomeBack`. */
39
+ headline?: ReactNode;
40
+ /** Text looks only: a supporting line under the headline. */
41
+ caption?: ReactNode;
42
+ /** Text looks only: content beside the arrow pill, such as a link. */
43
+ action?: ReactNode;
44
+ }
45
+
46
+ /**
47
+ * Isometric illustration of the three portals that reacts to the sign-in state with CSS
48
+ * transitions only. Colours come from the theme tokens (`--accent`, `--surface`, `--ink`), so dark
49
+ * mode and every accent apply on their own. With reduced motion it rests assembled and still.
50
+ */
51
+ export function AuthIllustration({
52
+ look: Look = Slabs,
53
+ state: stateProp,
54
+ progress: progressProp,
55
+ activeIndex: activeIndexProp,
56
+ label,
57
+ headline,
58
+ caption,
59
+ action,
60
+ className,
61
+ ...props
62
+ }: AuthIllustrationProps) {
63
+ const auth = useOptionalAuth();
64
+ const uid = useId().replace(/[^a-zA-Z0-9_-]/g, "");
65
+ const state = stateProp ?? auth?.state ?? "idle";
66
+ const progress = clamp01(progressProp ?? auth?.progress ?? 0);
67
+ const activeIndex = activeIndexProp ?? auth?.activeIndex ?? 0;
68
+ const ref = useRef<HTMLDivElement>(null);
69
+ useArtPointer(ref);
70
+ const [errorHeld, setErrorHeld] = useState(false);
71
+ useEffect(() => {
72
+ setErrorHeld(false);
73
+ if (state !== "error") return;
74
+ const timer = setTimeout(() => setErrorHeld(true), ERROR_HOLD_MS);
75
+ return () => clearTimeout(timer);
76
+ }, [state]);
77
+ const shown = shownState(state, errorHeld);
78
+ return (
79
+ <div
80
+ {...props}
81
+ ref={ref}
82
+ {...(Look.text ? {} : label ? { role: "img", "aria-label": label } : { "aria-hidden": true })}
83
+ data-state={shown}
84
+ data-panel={auth?.panel}
85
+ data-variant={Look.variant}
86
+ className={cn("ui-auth-art @container/art w-full min-w-0", className)}
87
+ >
88
+ <AuthStyles />
89
+ <Look
90
+ state={shown}
91
+ progress={progress}
92
+ activeIndex={activeIndex}
93
+ uid={uid}
94
+ headline={headline}
95
+ caption={caption}
96
+ action={action}
97
+ />
98
+ </div>
99
+ );
100
+ }
@@ -0,0 +1,8 @@
1
+ export * from "./auth-illustration.js";
2
+ export type { AuthLook, AuthVariantProps } from "./look.js";
3
+ export {
4
+ AUTH_STATES,
5
+ type AuthState,
6
+ slabPlacement,
7
+ slabSpread,
8
+ } from "./model.js";
@@ -0,0 +1,32 @@
1
+ import type { ComponentType, ReactNode } from "react";
2
+ import type { AuthState } from "./model.js";
3
+
4
+ /** What every illustration look receives. State and progress come already clamped. */
5
+ export interface AuthVariantProps {
6
+ state: AuthState;
7
+ /** Fill of the whole form, 0 to 1. */
8
+ progress: number;
9
+ /** Index of the portal in use (0 to 2). */
10
+ activeIndex: number;
11
+ /** Unique per rendered illustration; use it to prefix SVG ids (patterns, gradients, clip paths). */
12
+ uid: string;
13
+ /** Text looks only (`statement`): the main line; a string animates word by word. */
14
+ headline?: ReactNode;
15
+ /** Text looks only: a supporting line under the headline. */
16
+ caption?: ReactNode;
17
+ /** Text looks only: content placed beside the arrow pill, such as a link. */
18
+ action?: ReactNode;
19
+ }
20
+
21
+ /**
22
+ * A look is a component with {@link AuthVariantProps}. It renders inside a `@container/art` element,
23
+ * so it adapts to the width it is given with `@sm/art:` classes or `@container art (min-width: ...)`
24
+ * rules, and it reads `data-state` from the wrapper for state-driven CSS. Two optional static
25
+ * properties describe it to `AuthIllustration` without a registry: `variant` is the name written
26
+ * to `data-variant`, and `text` marks a look made of text people read, which is then not hidden
27
+ * from assistive technology.
28
+ */
29
+ export type AuthLook = ComponentType<AuthVariantProps> & {
30
+ variant?: string;
31
+ text?: boolean;
32
+ };
@@ -0,0 +1,330 @@
1
+ /** What the sign-in screen is doing. It is the only input of the illustration besides `progress`. */
2
+ export type AuthState = "idle" | "typing" | "password" | "error" | "success";
3
+
4
+ export const AUTH_STATES: readonly AuthState[] = [
5
+ "idle",
6
+ "typing",
7
+ "password",
8
+ "error",
9
+ "success",
10
+ ] as const;
11
+
12
+ export const clamp01 = (value: number): number =>
13
+ Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : 0;
14
+
15
+ /** `progress` is the fill of the whole form: the identifier is the first half, the password the second. */
16
+ export const IDENTIFIER_SHARE = 0.5;
17
+
18
+ const SPREAD = { idle: 0.85, error: 1.35, success: 0 } as const;
19
+ const TYPING = { start: 0.65, end: 0.35 } as const;
20
+ const PASSWORD = { start: 0.22, end: 0.05 } as const;
21
+
22
+ const lerp = (from: number, to: number, t: number) => from + (to - from) * clamp01(t);
23
+
24
+ /**
25
+ * How far the three slabs sit apart: 0 is assembled, 1 is the loose resting layout. The slabs
26
+ * drift together as the form fills (first the identifier, then the password), fly apart on an
27
+ * error and lock together on success.
28
+ */
29
+ export function slabSpread(state: AuthState, progress: number): number {
30
+ const p = clamp01(progress);
31
+ switch (state) {
32
+ case "typing":
33
+ return lerp(TYPING.start, TYPING.end, p / IDENTIFIER_SHARE);
34
+ case "password":
35
+ return lerp(PASSWORD.start, PASSWORD.end, (p - IDENTIFIER_SHARE) / (1 - IDENTIFIER_SHARE));
36
+ default:
37
+ return SPREAD[state];
38
+ }
39
+ }
40
+
41
+ export const SLAB_COUNT = 3;
42
+
43
+ /** Apex of the first slab's top face in the 640x520 art box. */
44
+ const SLAB_BASE = { x: 277, y: 149 } as const;
45
+ /** Position of each slab when assembled, relative to {@link SLAB_BASE}. */
46
+ const SLAB_ASSEMBLED = [
47
+ { x: 0, y: 0 },
48
+ { x: 86.6, y: 50 },
49
+ { x: 0, y: 100 },
50
+ ] as const;
51
+ /** How far each slab travels (x, y) and rises (lift) at `spread` 1. */
52
+ const SLAB_TRAVEL = [
53
+ { x: -120, y: 14, lift: 34 },
54
+ { x: 104, y: -44, lift: 62 },
55
+ { x: 14, y: 78, lift: 14 },
56
+ ] as const;
57
+ const ACTIVE_LIFT = 20;
58
+
59
+ export interface SlabPlacement {
60
+ x: number;
61
+ y: number;
62
+ lift: number;
63
+ }
64
+
65
+ const round = (value: number) => Math.round(value * 100) / 100;
66
+
67
+ /** Position and height of one slab. The slab at `activeIndex` rises a little above the rest. */
68
+ export function slabPlacement(index: number, spread: number, activeIndex: number): SlabPlacement {
69
+ const assembled = SLAB_ASSEMBLED[index];
70
+ const travel = SLAB_TRAVEL[index];
71
+ if (!assembled || !travel) return { x: SLAB_BASE.x, y: SLAB_BASE.y, lift: 0 };
72
+ return {
73
+ x: round(SLAB_BASE.x + assembled.x + travel.x * spread),
74
+ y: round(SLAB_BASE.y + assembled.y + travel.y * spread),
75
+ lift: round(travel.lift * spread + (index === activeIndex ? ACTIVE_LIFT : 0)),
76
+ };
77
+ }
78
+
79
+ export interface SparkVector {
80
+ dx: number;
81
+ dy: number;
82
+ }
83
+
84
+ /** Offsets of the success burst. Deterministic, so server and client render the same markup. */
85
+ export function sparkVectors(count = 12): SparkVector[] {
86
+ return Array.from({ length: count }, (_, n) => {
87
+ const angle = (n / count) * Math.PI * 2;
88
+ const distance = 90 + ((n * 37) % 90);
89
+ return { dx: round(Math.cos(angle) * distance), dy: round(Math.sin(angle) * distance * 0.7) };
90
+ });
91
+ }
92
+
93
+ /** How long an error stays on screen before the illustration settles back to the password state. */
94
+ export const ERROR_HOLD_MS = 1100;
95
+
96
+ /** The state the art shows: an error that has been held long enough reads as `password` again. */
97
+ export function shownState(state: AuthState, errorHeld: boolean): AuthState {
98
+ return state === "error" && errorHeld ? "password" : state;
99
+ }
100
+
101
+ /** Characters of the identifier at which its half of the form reads full; one keystroke moves `progress` by 1/(2 x this). */
102
+ export const KEYSTROKES_PER_FIELD = 10;
103
+
104
+ /**
105
+ * Fill of the whole form: the identifier grows its half by length, the password adds its half in
106
+ * one step as soon as it is not empty. The password's length never reaches `progress`, because
107
+ * `progress` is written to the DOM (styles, classes) where session-replay tools do not mask it.
108
+ */
109
+ export function formProgress(identifierLength: number, passwordFilled: boolean): number {
110
+ const identifier = Math.min(1, Math.max(0, identifierLength) / KEYSTROKES_PER_FIELD);
111
+ return (identifier + (passwordFilled ? 1 : 0)) / 2;
112
+ }
113
+
114
+ export interface Keystroke {
115
+ /** Keystrokes so far across both fields. */
116
+ count: number;
117
+ /** Which of the three pieces reacts to this keystroke. */
118
+ target: number;
119
+ /**
120
+ * Alternates every time the same piece reacts again, so CSS can swap between two identical
121
+ * keyframes and restart the animation without JavaScript timers.
122
+ */
123
+ phase: 0 | 1;
124
+ }
125
+
126
+ /** The keystroke a `progress` value stands for. */
127
+ export function keystroke(progress: number, pieces = 3): Keystroke {
128
+ const count = Math.round(clamp01(progress) * KEYSTROKES_PER_FIELD * 2);
129
+ return { count, target: count % pieces, phase: (Math.floor(count / pieces) % 2) as 0 | 1 };
130
+ }
131
+
132
+ export interface PointerOffset {
133
+ x: number;
134
+ y: number;
135
+ }
136
+
137
+ /**
138
+ * Where a pointer is relative to a box, as -1 to 1 on each axis from its center (clamped, so a
139
+ * pointer far over the form still reads as "all the way to that side").
140
+ */
141
+ export function pointerOffset(
142
+ point: { x: number; y: number },
143
+ box: { left: number; top: number; width: number; height: number },
144
+ ): PointerOffset {
145
+ const axis = (value: number, start: number, size: number) =>
146
+ size > 0
147
+ ? Math.round(Math.max(-1, Math.min(1, (value - start - size / 2) / (size / 2))) * 1000) / 1000
148
+ : 0;
149
+ return { x: axis(point.x, box.left, box.width), y: axis(point.y, box.top, box.height) };
150
+ }
151
+
152
+ /* ---------------------------------------------------------------- isometric geometry */
153
+
154
+ const ISO_X = 0.866;
155
+ const ISO_Y = 0.5;
156
+
157
+ /** Screen position of a point in isometric space: `u` runs right-down, `v` left-down, `z` up. */
158
+ export function isoPoint(u: number, v: number, z = 0): { x: number; y: number } {
159
+ return { x: round(ISO_X * (u - v)), y: round(ISO_Y * (u + v) - z) };
160
+ }
161
+
162
+ const pathOf = (points: readonly { x: number; y: number }[]) =>
163
+ `M${points.map((p) => `${p.x} ${p.y}`).join("L")}Z`;
164
+
165
+ export interface IsoBox {
166
+ top: string;
167
+ /** Face along the front-left edge (`v` at its maximum). */
168
+ left: string;
169
+ /** Face along the front-right edge (`u` at its maximum). */
170
+ right: string;
171
+ }
172
+
173
+ /** The three visible faces of a `w` by `d` by `h` box standing on the origin, centred on it. */
174
+ export function isoBox(w: number, d: number, h: number): IsoBox {
175
+ const [u0, u1, v0, v1] = [-w / 2, w / 2, -d / 2, d / 2];
176
+ return {
177
+ top: pathOf([
178
+ isoPoint(u0, v0, h),
179
+ isoPoint(u1, v0, h),
180
+ isoPoint(u1, v1, h),
181
+ isoPoint(u0, v1, h),
182
+ ]),
183
+ left: pathOf([
184
+ isoPoint(u0, v1, h),
185
+ isoPoint(u1, v1, h),
186
+ isoPoint(u1, v1, 0),
187
+ isoPoint(u0, v1, 0),
188
+ ]),
189
+ right: pathOf([
190
+ isoPoint(u1, v0, h),
191
+ isoPoint(u1, v1, h),
192
+ isoPoint(u1, v1, 0),
193
+ isoPoint(u1, v0, 0),
194
+ ]),
195
+ };
196
+ }
197
+
198
+ /* ---------------------------------------------------------------- stack */
199
+
200
+ export interface StackBook {
201
+ /** Footprint along `u` and `v`, and thickness. */
202
+ w: number;
203
+ d: number;
204
+ h: number;
205
+ /** Offset from the tower's axis, in isometric units, so the pile looks hand stacked. */
206
+ du: number;
207
+ dv: number;
208
+ /** Colour role, 0 to 3. */
209
+ tone: number;
210
+ }
211
+
212
+ /** The books of the tower from the bottom up. Narrow containers show only the first ones. */
213
+ export const STACK_BOOKS: readonly StackBook[] = [
214
+ { w: 178, d: 128, h: 30, du: 0, dv: 0, tone: 0 },
215
+ { w: 152, d: 112, h: 22, du: 8, dv: -6, tone: 1 },
216
+ { w: 164, d: 104, h: 26, du: -10, dv: 6, tone: 2 },
217
+ { w: 140, d: 112, h: 20, du: 6, dv: 4, tone: 3 },
218
+ { w: 150, d: 98, h: 24, du: -6, dv: -8, tone: 1 },
219
+ { w: 128, d: 100, h: 20, du: 4, dv: 6, tone: 0 },
220
+ ];
221
+
222
+ /** How many books a container shows: fewer when it is narrow. */
223
+ export const STACK_COUNTS = [4, 5, 6] as const;
224
+ export type StackCount = (typeof STACK_COUNTS)[number];
225
+
226
+ /**
227
+ * Books resting in a tower of `count`: the foundation is always there, the rest land one by one
228
+ * as the form fills, and all of them are down on success.
229
+ */
230
+ export function stackPlaced(state: AuthState, progress: number, count: number): number {
231
+ if (state === "success") return count;
232
+ return Math.min(count, 1 + Math.floor(clamp01(progress) * (count - 1) + 1e-9));
233
+ }
234
+
235
+ export interface StackSlot {
236
+ /** Screen offset of the book's ground centre from the tower's base. */
237
+ x: number;
238
+ y: number;
239
+ }
240
+
241
+ /** Where each book sits, from its offsets and the thickness of the books under it. */
242
+ export function stackSlots(books: readonly StackBook[] = STACK_BOOKS): StackSlot[] {
243
+ let z = 0;
244
+ return books.map((book) => {
245
+ const at = isoPoint(book.du, book.dv, z);
246
+ z += book.h;
247
+ return at;
248
+ });
249
+ }
250
+
251
+ /** Top centre of the tower of `count` books, where the success flag is planted. */
252
+ export function stackTop(count: number, books: readonly StackBook[] = STACK_BOOKS): StackSlot {
253
+ const shown = books.slice(0, count);
254
+ const last = shown[shown.length - 1];
255
+ if (!last) return { x: 0, y: 0 };
256
+ const z = shown.reduce((sum, book) => sum + book.h, 0);
257
+ return isoPoint(last.du, last.dv, z);
258
+ }
259
+
260
+ /* ---------------------------------------------------------------- mosaic */
261
+
262
+ /** Tiles per side of the mosaic floor; its container picks one by width. */
263
+ export const MOSAIC_SIZES = [4, 5, 6] as const;
264
+ export type MosaicSize = (typeof MOSAIC_SIZES)[number];
265
+
266
+ /** Diagonals of an `n` by `n` floor that are lit: they fill from the back corner as the form fills. */
267
+ export function mosaicFilled(state: AuthState, progress: number, n: number): number {
268
+ const diagonals = n * 2 - 1;
269
+ if (state === "success") return diagonals;
270
+ return Math.min(diagonals, Math.floor(clamp01(progress) * diagonals + 1e-9));
271
+ }
272
+
273
+ export interface MosaicTile {
274
+ row: number;
275
+ col: number;
276
+ /** Diagonal from the back corner, 0 to 2n-2: the fill and the error sweep follow it. */
277
+ diagonal: number;
278
+ /** Steps from the centre: the success ripple follows it. */
279
+ ring: number;
280
+ }
281
+
282
+ export function mosaicTiles(n: number): MosaicTile[] {
283
+ const middle = (n - 1) / 2;
284
+ return Array.from({ length: n * n }, (_, k) => {
285
+ const row = Math.floor(k / n);
286
+ const col = k % n;
287
+ return {
288
+ row,
289
+ col,
290
+ diagonal: row + col,
291
+ ring: Math.round(Math.abs(row - middle) + Math.abs(col - middle)),
292
+ };
293
+ });
294
+ }
295
+
296
+ /** Row and column of the tile carrying each portal's glyph, for each floor size. */
297
+ export const MOSAIC_GLYPH_TILES: Readonly<
298
+ Record<MosaicSize, readonly (readonly [number, number])[]>
299
+ > = {
300
+ 4: [
301
+ [1, 0],
302
+ [0, 2],
303
+ [2, 3],
304
+ ],
305
+ 5: [
306
+ [1, 1],
307
+ [0, 3],
308
+ [3, 3],
309
+ ],
310
+ 6: [
311
+ [1, 1],
312
+ [1, 4],
313
+ [4, 3],
314
+ ],
315
+ };
316
+
317
+ /** The tile that glints on a keystroke: it jumps around the floor instead of marching in order. */
318
+ export function mosaicGlint(count: number, n: number): number {
319
+ return count > 0 ? (count * 7) % (n * n) : -1;
320
+ }
321
+
322
+ /* ------------------------------------------------------------- character */
323
+
324
+ /** Lamps along the character's board; they fill with progress. */
325
+ export const BOARD_LIGHTS = 5;
326
+
327
+ export function boardLit(state: AuthState, progress: number): number {
328
+ if (state === "success") return BOARD_LIGHTS;
329
+ return Math.min(BOARD_LIGHTS, Math.floor(clamp01(progress) * BOARD_LIGHTS + 1e-9));
330
+ }
@@ -0,0 +1,78 @@
1
+ "use client";
2
+
3
+ import { type RefObject, useEffect } from "react";
4
+ import { pointerOffset } from "./model.js";
5
+
6
+ const FINE_POINTER = "(hover: hover) and (pointer: fine)";
7
+ const REDUCED_MOTION = "(prefers-reduced-motion: reduce)";
8
+
9
+ /**
10
+ * Writes where the pointer is, relative to the art, as `--px` and `--py` (-1 to 1) on the art
11
+ * element. It listens on the surrounding `AuthLayout` so the art also reacts while the pointer is
12
+ * over the form. One write per animation frame at most; nothing runs for touch, coarse pointers or
13
+ * reduced motion, and it follows those settings when they change while the page is open (the art
14
+ * recentres when the parallax switches off). The listeners go away with the component.
15
+ */
16
+ export function useArtPointer(ref: RefObject<HTMLElement | null>): void {
17
+ useEffect(() => {
18
+ const art = ref.current;
19
+ if (!art || typeof window.matchMedia !== "function") return;
20
+ const fine = window.matchMedia(FINE_POINTER);
21
+ const reduced = window.matchMedia(REDUCED_MOTION);
22
+ const area: HTMLElement = art.closest<HTMLElement>("[data-layout]") ?? art;
23
+ let frame = 0;
24
+ let point: { x: number; y: number } | null = null;
25
+ let listening = false;
26
+
27
+ const write = () => {
28
+ frame = 0;
29
+ if (!point) {
30
+ art.style.setProperty("--px", "0");
31
+ art.style.setProperty("--py", "0");
32
+ return;
33
+ }
34
+ const offset = pointerOffset(point, art.getBoundingClientRect());
35
+ art.style.setProperty("--px", String(offset.x));
36
+ art.style.setProperty("--py", String(offset.y));
37
+ };
38
+ const schedule = () => {
39
+ if (!frame) frame = requestAnimationFrame(write);
40
+ };
41
+ const move = (event: PointerEvent) => {
42
+ if (event.pointerType !== "mouse" && event.pointerType !== "pen") return;
43
+ point = { x: event.clientX, y: event.clientY };
44
+ schedule();
45
+ };
46
+ const leave = () => {
47
+ point = null;
48
+ schedule();
49
+ };
50
+ const stop = () => {
51
+ if (!listening) return;
52
+ listening = false;
53
+ area.removeEventListener("pointermove", move);
54
+ area.removeEventListener("pointerleave", leave);
55
+ if (frame) cancelAnimationFrame(frame);
56
+ frame = 0;
57
+ point = null;
58
+ art.style.removeProperty("--px");
59
+ art.style.removeProperty("--py");
60
+ };
61
+ const sync = () => {
62
+ if (!fine.matches || reduced.matches) return stop();
63
+ if (listening) return;
64
+ listening = true;
65
+ area.addEventListener("pointermove", move, { passive: true });
66
+ area.addEventListener("pointerleave", leave, { passive: true });
67
+ };
68
+
69
+ sync();
70
+ fine.addEventListener("change", sync);
71
+ reduced.addEventListener("change", sync);
72
+ return () => {
73
+ fine.removeEventListener("change", sync);
74
+ reduced.removeEventListener("change", sync);
75
+ stop();
76
+ };
77
+ }, [ref]);
78
+ }