@flare-engine/ui 0.4.8 → 0.4.11
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/README.md +10 -1
- package/dist/focus-86LvCtcF.js +137 -0
- package/dist/focus-86LvCtcF.js.map +1 -0
- package/dist/focus-CtmAYznc.mjs +126 -0
- package/dist/focus-CtmAYznc.mjs.map +1 -0
- package/dist/focus-ILunQnll.d.mts +65 -0
- package/dist/focus-ILunQnll.d.mts.map +1 -0
- package/dist/focus-ILunQnll.d.ts +65 -0
- package/dist/focus-ILunQnll.d.ts.map +1 -0
- package/dist/focus.d.mts +2 -0
- package/dist/focus.d.ts +2 -0
- package/dist/focus.js +4 -0
- package/dist/focus.mjs +1 -0
- package/dist/index.d.mts +9 -52
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +9 -52
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +32 -130
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +30 -128
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -2
package/README.md
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
Skia-free UI kit for HUDs, menus, modals — `Panel`, `Button`, `Label`, `List`, `Modal`, `HUDBar`, `ProgressBar`, plus `DialoguePanel`, `MainMenuKit` higher-level components. `ThemeProvider` injects design tokens; `FocusProvider` + `FocusManager` drive focus navigation; `createFocusKeyboardDriver` wires arrows/Tab/Enter/Space onto the manager (gamepad driver pending); `resolveLayout` is a pure flex-like layout resolver. A shop list ships in `@flare-engine/ui-shop`, wired to `@flare-engine/iap`.
|
|
4
4
|
|
|
5
|
+
## Entry points
|
|
6
|
+
|
|
7
|
+
- **`.` (default)** — the whole kit: components, theme, focus, layout. Pulls in React Native at module scope.
|
|
8
|
+
- **`./focus`** — the RN-free focus core: `createFocusManager` + `createFocusKeyboardDriver` and their types (`FocusManager`, `FocusManagerOptions`, `FocusNode`, `FocusKeyboardDriver`, `FocusKeyboardDriverOptions`, `FocusKeySource`). No React or React Native import, so it is safe for `bun test` and Node harnesses. `.` re-exports the same names.
|
|
9
|
+
|
|
5
10
|
## Install
|
|
6
11
|
|
|
7
12
|
```sh
|
|
@@ -32,7 +37,7 @@ function PauseMenu({ onResume }: { onResume: () => void }) {
|
|
|
32
37
|
## Keyboard navigation
|
|
33
38
|
|
|
34
39
|
```ts
|
|
35
|
-
import { createFocusManager, createFocusKeyboardDriver, type FocusKeySource } from '@flare-engine/ui';
|
|
40
|
+
import { createFocusManager, createFocusKeyboardDriver, type FocusKeySource } from '@flare-engine/ui/focus';
|
|
36
41
|
// keys is any { wasPressed(code: string): boolean } — createKeyboard() from
|
|
37
42
|
// @flare-engine/input is structurally assignable, no hard dependency required.
|
|
38
43
|
declare const keys: FocusKeySource;
|
|
@@ -44,6 +49,10 @@ driver.update(); // ArrowDown/ArrowRight/Tab → next, ArrowUp/ArrowLeft → pre
|
|
|
44
49
|
|
|
45
50
|
See [API spec](https://github.com/grzott/flare-engine/blob/main/docs/design/api-specs.md#flare-engineui--6kb-gzip-target).
|
|
46
51
|
|
|
52
|
+
## Testing
|
|
53
|
+
|
|
54
|
+
The `.` entry loads `react-native` at module scope, and the `react-native` entry file is Flow source that bun does not parse, so importing it under `bun test` needs four `mock.module` stubs (`react`, `react/jsx-runtime`, `react/jsx-dev-runtime`, `react-native`). `./focus` needs none. The stubs, the load-order rules and a worked assertion: [Testing ui and ui-shop under bun test](https://github.com/grzott/flare-engine/blob/main/docs/recipes/testing-ui-under-bun.md).
|
|
55
|
+
|
|
47
56
|
## License
|
|
48
57
|
|
|
49
58
|
Apache-2.0
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// src/focus-keyboard-driver.ts
|
|
2
|
+
const DEFAULT_NEXT = [
|
|
3
|
+
"ArrowDown",
|
|
4
|
+
"ArrowRight",
|
|
5
|
+
"Tab"
|
|
6
|
+
];
|
|
7
|
+
const DEFAULT_PREV = ["ArrowUp", "ArrowLeft"];
|
|
8
|
+
const DEFAULT_ACTIVATE = ["Enter", "Space"];
|
|
9
|
+
function createFocusKeyboardDriver(manager, keys, options) {
|
|
10
|
+
const nextCodes = options?.next ?? DEFAULT_NEXT;
|
|
11
|
+
const prevCodes = options?.prev ?? DEFAULT_PREV;
|
|
12
|
+
const activateCodes = options?.activate ?? DEFAULT_ACTIVATE;
|
|
13
|
+
return { update() {
|
|
14
|
+
for (let i = 0; i < nextCodes.length; i++) if (keys.wasPressed(nextCodes[i])) {
|
|
15
|
+
manager.next();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
for (let i = 0; i < prevCodes.length; i++) if (keys.wasPressed(prevCodes[i])) {
|
|
19
|
+
manager.prev();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
for (let i = 0; i < activateCodes.length; i++) if (keys.wasPressed(activateCodes[i])) {
|
|
23
|
+
manager.activate();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
} };
|
|
27
|
+
}
|
|
28
|
+
// src/focus-manager.ts
|
|
29
|
+
function createFocusManager(opts) {
|
|
30
|
+
const refocusOnUnregister = opts?.refocusOnUnregister ?? true;
|
|
31
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
32
|
+
let seqCounter = 0;
|
|
33
|
+
let focusedId = null;
|
|
34
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
35
|
+
function notify() {
|
|
36
|
+
for (const l of listeners) l(focusedId);
|
|
37
|
+
}
|
|
38
|
+
function orderedIds() {
|
|
39
|
+
const entries = Array.from(nodes.values()).filter((e) => !e.disabled);
|
|
40
|
+
entries.sort((a, b) => {
|
|
41
|
+
if (a.tabIndex !== b.tabIndex) return a.tabIndex - b.tabIndex;
|
|
42
|
+
return a.seq - b.seq;
|
|
43
|
+
});
|
|
44
|
+
return entries.map((e) => e.id);
|
|
45
|
+
}
|
|
46
|
+
function setFocused(next) {
|
|
47
|
+
if (next === focusedId) return;
|
|
48
|
+
const prev = focusedId;
|
|
49
|
+
focusedId = next;
|
|
50
|
+
if (prev !== null) nodes.get(prev)?.onBlur?.();
|
|
51
|
+
if (next !== null) nodes.get(next)?.onFocus?.();
|
|
52
|
+
notify();
|
|
53
|
+
}
|
|
54
|
+
function move(delta) {
|
|
55
|
+
const order = orderedIds();
|
|
56
|
+
if (order.length === 0) {
|
|
57
|
+
setFocused(null);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (focusedId === null) {
|
|
61
|
+
setFocused(delta === 1 ? order[0] ?? null : order[order.length - 1] ?? null);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const idx = order.indexOf(focusedId);
|
|
65
|
+
if (idx === -1) {
|
|
66
|
+
setFocused(order[0] ?? null);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
setFocused(order[(idx + delta + order.length) % order.length] ?? null);
|
|
70
|
+
}
|
|
71
|
+
function unregister(id) {
|
|
72
|
+
if (!nodes.has(id)) return;
|
|
73
|
+
nodes.delete(id);
|
|
74
|
+
if (focusedId === id) setFocused(refocusOnUnregister ? orderedIds()[0] ?? null : null);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
register(node) {
|
|
78
|
+
const existing = nodes.get(node.id);
|
|
79
|
+
const entry = {
|
|
80
|
+
...node,
|
|
81
|
+
seq: existing?.seq ?? seqCounter++
|
|
82
|
+
};
|
|
83
|
+
nodes.set(node.id, entry);
|
|
84
|
+
return () => unregister(node.id);
|
|
85
|
+
},
|
|
86
|
+
unregister,
|
|
87
|
+
focus(id) {
|
|
88
|
+
const entry = nodes.get(id);
|
|
89
|
+
if (!entry || entry.disabled) return;
|
|
90
|
+
setFocused(id);
|
|
91
|
+
},
|
|
92
|
+
focusFirst() {
|
|
93
|
+
setFocused(orderedIds()[0] ?? null);
|
|
94
|
+
},
|
|
95
|
+
blur() {
|
|
96
|
+
setFocused(null);
|
|
97
|
+
},
|
|
98
|
+
next() {
|
|
99
|
+
move(1);
|
|
100
|
+
},
|
|
101
|
+
prev() {
|
|
102
|
+
move(-1);
|
|
103
|
+
},
|
|
104
|
+
activate() {
|
|
105
|
+
if (focusedId === null) return;
|
|
106
|
+
const entry = nodes.get(focusedId);
|
|
107
|
+
if (!entry || entry.disabled) return;
|
|
108
|
+
entry.onActivate?.();
|
|
109
|
+
},
|
|
110
|
+
getFocused() {
|
|
111
|
+
return focusedId;
|
|
112
|
+
},
|
|
113
|
+
getOrder() {
|
|
114
|
+
return orderedIds();
|
|
115
|
+
},
|
|
116
|
+
subscribe(listener) {
|
|
117
|
+
listeners.add(listener);
|
|
118
|
+
return () => {
|
|
119
|
+
listeners.delete(listener);
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
Object.defineProperty(exports, "createFocusKeyboardDriver", {
|
|
125
|
+
enumerable: true,
|
|
126
|
+
get: function() {
|
|
127
|
+
return createFocusKeyboardDriver;
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
Object.defineProperty(exports, "createFocusManager", {
|
|
131
|
+
enumerable: true,
|
|
132
|
+
get: function() {
|
|
133
|
+
return createFocusManager;
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
//# sourceMappingURL=focus-86LvCtcF.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"focus-86LvCtcF.js","names":[],"sources":["../src/focus-keyboard-driver.ts","../src/focus-manager.ts"],"sourcesContent":["import type { FocusManager } from './focus-manager.js';\n\n/** Structural key source — createKeyboard() from @flare-engine/input is assignable; no hard dep. */\nexport interface FocusKeySource {\n wasPressed(code: string): boolean;\n}\n\nexport interface FocusKeyboardDriver {\n /** Call once per frame; polls edges and drives the manager. */\n update(): void;\n}\n\nexport interface FocusKeyboardDriverOptions {\n next?: readonly string[];\n prev?: readonly string[];\n activate?: readonly string[];\n}\n\nconst DEFAULT_NEXT: readonly string[] = ['ArrowDown', 'ArrowRight', 'Tab'];\nconst DEFAULT_PREV: readonly string[] = ['ArrowUp', 'ArrowLeft'];\nconst DEFAULT_ACTIVATE: readonly string[] = ['Enter', 'Space'];\n\nexport function createFocusKeyboardDriver(\n manager: FocusManager,\n keys: FocusKeySource,\n options?: FocusKeyboardDriverOptions,\n): FocusKeyboardDriver {\n const nextCodes = options?.next ?? DEFAULT_NEXT;\n const prevCodes = options?.prev ?? DEFAULT_PREV;\n const activateCodes = options?.activate ?? DEFAULT_ACTIVATE;\n\n return {\n update(): void {\n for (let i = 0; i < nextCodes.length; i++) {\n if (keys.wasPressed(nextCodes[i]!)) {\n manager.next();\n return;\n }\n }\n for (let i = 0; i < prevCodes.length; i++) {\n if (keys.wasPressed(prevCodes[i]!)) {\n manager.prev();\n return;\n }\n }\n for (let i = 0; i < activateCodes.length; i++) {\n if (keys.wasPressed(activateCodes[i]!)) {\n manager.activate();\n return;\n }\n }\n },\n };\n}\n","/**\n * Keyboard + (future) gamepad focus navigation.\n *\n * Primitives register themselves with the focus manager by id + tab order.\n * The manager owns which id is currently focused and exposes `next` / `prev`\n * / `activate` actions for a driver (keyboard key handler, gamepad dpad)\n * to call. Components read `focusedId` to render a focus ring.\n */\n\nexport interface FocusNode {\n id: string;\n /** Lower tabIndex is focused first. Nodes with equal tabIndex fall back to registration order. */\n tabIndex: number;\n /** Called when this node is activated (Enter / A button). */\n onActivate?: () => void;\n /** Called when this node receives focus. */\n onFocus?: () => void;\n /** Called when this node loses focus. */\n onBlur?: () => void;\n /** If true, the node is skipped during navigation. */\n disabled?: boolean;\n}\n\nexport interface FocusManager {\n /**\n * Registering an already-registered id updates that node in place: it keeps\n * its position among equal `tabIndex` and focus is unchanged.\n */\n register(node: FocusNode): () => void;\n unregister(id: string): void;\n focus(id: string): void;\n /** Explicit opt-in initial focus: focuses the first node in tab order (or clears if empty). */\n focusFirst(): void;\n blur(): void;\n next(): void;\n prev(): void;\n activate(): void;\n getFocused(): string | null;\n getOrder(): readonly string[];\n subscribe(listener: (focusedId: string | null) => void): () => void;\n}\n\nexport interface FocusManagerOptions {\n /**\n * When the focused node unregisters, move focus to the first node in tab\n * order (`true`, the default — keyboard users keep a focus target) or clear\n * it to `null` (`false`). Subscribers are notified either way, and a later\n * `next()` / `prev()` starts from the first / last node as it does from `null`.\n */\n refocusOnUnregister?: boolean;\n}\n\ninterface Entry extends FocusNode {\n seq: number;\n}\n\nexport function createFocusManager(opts?: FocusManagerOptions): FocusManager {\n const refocusOnUnregister = opts?.refocusOnUnregister ?? true;\n const nodes = new Map<string, Entry>();\n let seqCounter = 0;\n let focusedId: string | null = null;\n const listeners = new Set<(id: string | null) => void>();\n\n function notify(): void {\n for (const l of listeners) l(focusedId);\n }\n\n function orderedIds(): string[] {\n const entries = Array.from(nodes.values()).filter((e) => !e.disabled);\n entries.sort((a, b) => {\n if (a.tabIndex !== b.tabIndex) return a.tabIndex - b.tabIndex;\n return a.seq - b.seq;\n });\n return entries.map((e) => e.id);\n }\n\n function setFocused(next: string | null): void {\n if (next === focusedId) return;\n const prev = focusedId;\n focusedId = next;\n if (prev !== null) nodes.get(prev)?.onBlur?.();\n if (next !== null) nodes.get(next)?.onFocus?.();\n notify();\n }\n\n function move(delta: 1 | -1): void {\n const order = orderedIds();\n if (order.length === 0) {\n setFocused(null);\n return;\n }\n if (focusedId === null) {\n setFocused(delta === 1 ? (order[0] ?? null) : (order[order.length - 1] ?? null));\n return;\n }\n const idx = order.indexOf(focusedId);\n if (idx === -1) {\n setFocused(order[0] ?? null);\n return;\n }\n const nextIdx = (idx + delta + order.length) % order.length;\n setFocused(order[nextIdx] ?? null);\n }\n\n function unregister(id: string): void {\n if (!nodes.has(id)) return;\n nodes.delete(id);\n if (focusedId === id) {\n setFocused(refocusOnUnregister ? (orderedIds()[0] ?? null) : null);\n }\n }\n\n return {\n register(node) {\n // No focus side-effect: on a touch-first engine an auto-focused first\n // registrant paints a permanent focus ring with no input. Focus stays null\n // until a driver (keyboard/gamepad) or an explicit focusFirst() acts.\n const existing = nodes.get(node.id);\n const entry: Entry = { ...node, seq: existing?.seq ?? seqCounter++ };\n nodes.set(node.id, entry);\n return () => unregister(node.id);\n },\n unregister,\n focus(id) {\n const entry = nodes.get(id);\n if (!entry || entry.disabled) return;\n setFocused(id);\n },\n focusFirst() {\n const order = orderedIds();\n setFocused(order[0] ?? null);\n },\n blur() {\n setFocused(null);\n },\n next() {\n move(1);\n },\n prev() {\n move(-1);\n },\n activate() {\n if (focusedId === null) return;\n const entry = nodes.get(focusedId);\n if (!entry || entry.disabled) return;\n entry.onActivate?.();\n },\n getFocused() {\n return focusedId;\n },\n getOrder() {\n return orderedIds();\n },\n subscribe(listener) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n };\n}\n"],"mappings":";AAkBA,MAAM,eAAkC;CAAC;CAAa;CAAc;AAAK;AACzE,MAAM,eAAkC,CAAC,WAAW,WAAW;AAC/D,MAAM,mBAAsC,CAAC,SAAS,OAAO;AAE7D,SAAgB,0BACd,SACA,MACA,SACqB;CACrB,MAAM,YAAY,SAAS,QAAQ;CACnC,MAAM,YAAY,SAAS,QAAQ;CACnC,MAAM,gBAAgB,SAAS,YAAY;CAE3C,OAAO,EACL,SAAe;EACb,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KACpC,IAAI,KAAK,WAAW,UAAU,EAAG,GAAG;GAClC,QAAQ,KAAK;GACb;EACF;EAEF,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KACpC,IAAI,KAAK,WAAW,UAAU,EAAG,GAAG;GAClC,QAAQ,KAAK;GACb;EACF;EAEF,KAAK,IAAI,IAAI,GAAG,IAAI,cAAc,QAAQ,KACxC,IAAI,KAAK,WAAW,cAAc,EAAG,GAAG;GACtC,QAAQ,SAAS;GACjB;EACF;CAEJ,EACF;AACF;;ACGA,SAAgB,mBAAmB,MAA0C;CAC3E,MAAM,sBAAsB,MAAM,uBAAuB;CACzD,MAAM,wBAAQ,IAAI,IAAmB;CACrC,IAAI,aAAa;CACjB,IAAI,YAA2B;CA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,EAAE;GAClC,MAAM,QAAe;IAAE,GAAG;IAAM,KAAK,UAAU,OAAO;GAAa;GACnE,MAAM,IAAI,KAAK,IAAI,KAAK;GACxB,aAAa,WAAW,KAAK,EAAE;EACjC;EACA;EACA,MAAM,IAAI;GACR,MAAM,QAAQ,MAAM,IAAI,EAAE;GAC1B,IAAI,CAAC,SAAS,MAAM,UAAU;GAC9B,WAAW,EAAE;EACf;EACA,aAAa;GAEX,WADc,WACC,CAAC,CAAC,MAAM,IAAI;EAC7B;EACA,OAAO;GACL,WAAW,IAAI;EACjB;EACA,OAAO;GACL,KAAK,CAAC;EACR;EACA,OAAO;GACL,KAAK,EAAE;EACT;EACA,WAAW;GACT,IAAI,cAAc,MAAM;GACxB,MAAM,QAAQ,MAAM,IAAI,SAAS;GACjC,IAAI,CAAC,SAAS,MAAM,UAAU;GAC9B,MAAM,aAAa;EACrB;EACA,aAAa;GACX,OAAO;EACT;EACA,WAAW;GACT,OAAO,WAAW;EACpB;EACA,UAAU,UAAU;GAClB,UAAU,IAAI,QAAQ;GACtB,aAAa;IACX,UAAU,OAAO,QAAQ;GAC3B;EACF;CACF;AACF"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// src/focus-keyboard-driver.ts
|
|
2
|
+
const DEFAULT_NEXT = [
|
|
3
|
+
"ArrowDown",
|
|
4
|
+
"ArrowRight",
|
|
5
|
+
"Tab"
|
|
6
|
+
];
|
|
7
|
+
const DEFAULT_PREV = ["ArrowUp", "ArrowLeft"];
|
|
8
|
+
const DEFAULT_ACTIVATE = ["Enter", "Space"];
|
|
9
|
+
function createFocusKeyboardDriver(manager, keys, options) {
|
|
10
|
+
const nextCodes = options?.next ?? DEFAULT_NEXT;
|
|
11
|
+
const prevCodes = options?.prev ?? DEFAULT_PREV;
|
|
12
|
+
const activateCodes = options?.activate ?? DEFAULT_ACTIVATE;
|
|
13
|
+
return { update() {
|
|
14
|
+
for (let i = 0; i < nextCodes.length; i++) if (keys.wasPressed(nextCodes[i])) {
|
|
15
|
+
manager.next();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
for (let i = 0; i < prevCodes.length; i++) if (keys.wasPressed(prevCodes[i])) {
|
|
19
|
+
manager.prev();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
for (let i = 0; i < activateCodes.length; i++) if (keys.wasPressed(activateCodes[i])) {
|
|
23
|
+
manager.activate();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
} };
|
|
27
|
+
}
|
|
28
|
+
// src/focus-manager.ts
|
|
29
|
+
function createFocusManager(opts) {
|
|
30
|
+
const refocusOnUnregister = opts?.refocusOnUnregister ?? true;
|
|
31
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
32
|
+
let seqCounter = 0;
|
|
33
|
+
let focusedId = null;
|
|
34
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
35
|
+
function notify() {
|
|
36
|
+
for (const l of listeners) l(focusedId);
|
|
37
|
+
}
|
|
38
|
+
function orderedIds() {
|
|
39
|
+
const entries = Array.from(nodes.values()).filter((e) => !e.disabled);
|
|
40
|
+
entries.sort((a, b) => {
|
|
41
|
+
if (a.tabIndex !== b.tabIndex) return a.tabIndex - b.tabIndex;
|
|
42
|
+
return a.seq - b.seq;
|
|
43
|
+
});
|
|
44
|
+
return entries.map((e) => e.id);
|
|
45
|
+
}
|
|
46
|
+
function setFocused(next) {
|
|
47
|
+
if (next === focusedId) return;
|
|
48
|
+
const prev = focusedId;
|
|
49
|
+
focusedId = next;
|
|
50
|
+
if (prev !== null) nodes.get(prev)?.onBlur?.();
|
|
51
|
+
if (next !== null) nodes.get(next)?.onFocus?.();
|
|
52
|
+
notify();
|
|
53
|
+
}
|
|
54
|
+
function move(delta) {
|
|
55
|
+
const order = orderedIds();
|
|
56
|
+
if (order.length === 0) {
|
|
57
|
+
setFocused(null);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (focusedId === null) {
|
|
61
|
+
setFocused(delta === 1 ? order[0] ?? null : order[order.length - 1] ?? null);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const idx = order.indexOf(focusedId);
|
|
65
|
+
if (idx === -1) {
|
|
66
|
+
setFocused(order[0] ?? null);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
setFocused(order[(idx + delta + order.length) % order.length] ?? null);
|
|
70
|
+
}
|
|
71
|
+
function unregister(id) {
|
|
72
|
+
if (!nodes.has(id)) return;
|
|
73
|
+
nodes.delete(id);
|
|
74
|
+
if (focusedId === id) setFocused(refocusOnUnregister ? orderedIds()[0] ?? null : null);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
register(node) {
|
|
78
|
+
const existing = nodes.get(node.id);
|
|
79
|
+
const entry = {
|
|
80
|
+
...node,
|
|
81
|
+
seq: existing?.seq ?? seqCounter++
|
|
82
|
+
};
|
|
83
|
+
nodes.set(node.id, entry);
|
|
84
|
+
return () => unregister(node.id);
|
|
85
|
+
},
|
|
86
|
+
unregister,
|
|
87
|
+
focus(id) {
|
|
88
|
+
const entry = nodes.get(id);
|
|
89
|
+
if (!entry || entry.disabled) return;
|
|
90
|
+
setFocused(id);
|
|
91
|
+
},
|
|
92
|
+
focusFirst() {
|
|
93
|
+
setFocused(orderedIds()[0] ?? null);
|
|
94
|
+
},
|
|
95
|
+
blur() {
|
|
96
|
+
setFocused(null);
|
|
97
|
+
},
|
|
98
|
+
next() {
|
|
99
|
+
move(1);
|
|
100
|
+
},
|
|
101
|
+
prev() {
|
|
102
|
+
move(-1);
|
|
103
|
+
},
|
|
104
|
+
activate() {
|
|
105
|
+
if (focusedId === null) return;
|
|
106
|
+
const entry = nodes.get(focusedId);
|
|
107
|
+
if (!entry || entry.disabled) return;
|
|
108
|
+
entry.onActivate?.();
|
|
109
|
+
},
|
|
110
|
+
getFocused() {
|
|
111
|
+
return focusedId;
|
|
112
|
+
},
|
|
113
|
+
getOrder() {
|
|
114
|
+
return orderedIds();
|
|
115
|
+
},
|
|
116
|
+
subscribe(listener) {
|
|
117
|
+
listeners.add(listener);
|
|
118
|
+
return () => {
|
|
119
|
+
listeners.delete(listener);
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
export { createFocusKeyboardDriver as n, createFocusManager as t };
|
|
125
|
+
|
|
126
|
+
//# sourceMappingURL=focus-CtmAYznc.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"focus-CtmAYznc.mjs","names":[],"sources":["../src/focus-keyboard-driver.ts","../src/focus-manager.ts"],"sourcesContent":["import type { FocusManager } from './focus-manager.js';\n\n/** Structural key source — createKeyboard() from @flare-engine/input is assignable; no hard dep. */\nexport interface FocusKeySource {\n wasPressed(code: string): boolean;\n}\n\nexport interface FocusKeyboardDriver {\n /** Call once per frame; polls edges and drives the manager. */\n update(): void;\n}\n\nexport interface FocusKeyboardDriverOptions {\n next?: readonly string[];\n prev?: readonly string[];\n activate?: readonly string[];\n}\n\nconst DEFAULT_NEXT: readonly string[] = ['ArrowDown', 'ArrowRight', 'Tab'];\nconst DEFAULT_PREV: readonly string[] = ['ArrowUp', 'ArrowLeft'];\nconst DEFAULT_ACTIVATE: readonly string[] = ['Enter', 'Space'];\n\nexport function createFocusKeyboardDriver(\n manager: FocusManager,\n keys: FocusKeySource,\n options?: FocusKeyboardDriverOptions,\n): FocusKeyboardDriver {\n const nextCodes = options?.next ?? DEFAULT_NEXT;\n const prevCodes = options?.prev ?? DEFAULT_PREV;\n const activateCodes = options?.activate ?? DEFAULT_ACTIVATE;\n\n return {\n update(): void {\n for (let i = 0; i < nextCodes.length; i++) {\n if (keys.wasPressed(nextCodes[i]!)) {\n manager.next();\n return;\n }\n }\n for (let i = 0; i < prevCodes.length; i++) {\n if (keys.wasPressed(prevCodes[i]!)) {\n manager.prev();\n return;\n }\n }\n for (let i = 0; i < activateCodes.length; i++) {\n if (keys.wasPressed(activateCodes[i]!)) {\n manager.activate();\n return;\n }\n }\n },\n };\n}\n","/**\n * Keyboard + (future) gamepad focus navigation.\n *\n * Primitives register themselves with the focus manager by id + tab order.\n * The manager owns which id is currently focused and exposes `next` / `prev`\n * / `activate` actions for a driver (keyboard key handler, gamepad dpad)\n * to call. Components read `focusedId` to render a focus ring.\n */\n\nexport interface FocusNode {\n id: string;\n /** Lower tabIndex is focused first. Nodes with equal tabIndex fall back to registration order. */\n tabIndex: number;\n /** Called when this node is activated (Enter / A button). */\n onActivate?: () => void;\n /** Called when this node receives focus. */\n onFocus?: () => void;\n /** Called when this node loses focus. */\n onBlur?: () => void;\n /** If true, the node is skipped during navigation. */\n disabled?: boolean;\n}\n\nexport interface FocusManager {\n /**\n * Registering an already-registered id updates that node in place: it keeps\n * its position among equal `tabIndex` and focus is unchanged.\n */\n register(node: FocusNode): () => void;\n unregister(id: string): void;\n focus(id: string): void;\n /** Explicit opt-in initial focus: focuses the first node in tab order (or clears if empty). */\n focusFirst(): void;\n blur(): void;\n next(): void;\n prev(): void;\n activate(): void;\n getFocused(): string | null;\n getOrder(): readonly string[];\n subscribe(listener: (focusedId: string | null) => void): () => void;\n}\n\nexport interface FocusManagerOptions {\n /**\n * When the focused node unregisters, move focus to the first node in tab\n * order (`true`, the default — keyboard users keep a focus target) or clear\n * it to `null` (`false`). Subscribers are notified either way, and a later\n * `next()` / `prev()` starts from the first / last node as it does from `null`.\n */\n refocusOnUnregister?: boolean;\n}\n\ninterface Entry extends FocusNode {\n seq: number;\n}\n\nexport function createFocusManager(opts?: FocusManagerOptions): FocusManager {\n const refocusOnUnregister = opts?.refocusOnUnregister ?? true;\n const nodes = new Map<string, Entry>();\n let seqCounter = 0;\n let focusedId: string | null = null;\n const listeners = new Set<(id: string | null) => void>();\n\n function notify(): void {\n for (const l of listeners) l(focusedId);\n }\n\n function orderedIds(): string[] {\n const entries = Array.from(nodes.values()).filter((e) => !e.disabled);\n entries.sort((a, b) => {\n if (a.tabIndex !== b.tabIndex) return a.tabIndex - b.tabIndex;\n return a.seq - b.seq;\n });\n return entries.map((e) => e.id);\n }\n\n function setFocused(next: string | null): void {\n if (next === focusedId) return;\n const prev = focusedId;\n focusedId = next;\n if (prev !== null) nodes.get(prev)?.onBlur?.();\n if (next !== null) nodes.get(next)?.onFocus?.();\n notify();\n }\n\n function move(delta: 1 | -1): void {\n const order = orderedIds();\n if (order.length === 0) {\n setFocused(null);\n return;\n }\n if (focusedId === null) {\n setFocused(delta === 1 ? (order[0] ?? null) : (order[order.length - 1] ?? null));\n return;\n }\n const idx = order.indexOf(focusedId);\n if (idx === -1) {\n setFocused(order[0] ?? null);\n return;\n }\n const nextIdx = (idx + delta + order.length) % order.length;\n setFocused(order[nextIdx] ?? null);\n }\n\n function unregister(id: string): void {\n if (!nodes.has(id)) return;\n nodes.delete(id);\n if (focusedId === id) {\n setFocused(refocusOnUnregister ? (orderedIds()[0] ?? null) : null);\n }\n }\n\n return {\n register(node) {\n // No focus side-effect: on a touch-first engine an auto-focused first\n // registrant paints a permanent focus ring with no input. Focus stays null\n // until a driver (keyboard/gamepad) or an explicit focusFirst() acts.\n const existing = nodes.get(node.id);\n const entry: Entry = { ...node, seq: existing?.seq ?? seqCounter++ };\n nodes.set(node.id, entry);\n return () => unregister(node.id);\n },\n unregister,\n focus(id) {\n const entry = nodes.get(id);\n if (!entry || entry.disabled) return;\n setFocused(id);\n },\n focusFirst() {\n const order = orderedIds();\n setFocused(order[0] ?? null);\n },\n blur() {\n setFocused(null);\n },\n next() {\n move(1);\n },\n prev() {\n move(-1);\n },\n activate() {\n if (focusedId === null) return;\n const entry = nodes.get(focusedId);\n if (!entry || entry.disabled) return;\n entry.onActivate?.();\n },\n getFocused() {\n return focusedId;\n },\n getOrder() {\n return orderedIds();\n },\n subscribe(listener) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n };\n}\n"],"mappings":";AAkBA,MAAM,eAAkC;CAAC;CAAa;CAAc;AAAK;AACzE,MAAM,eAAkC,CAAC,WAAW,WAAW;AAC/D,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B,MAAM,aAAa;EACrB;EACA,aAAa;GACX,OAAO;EACT;EACA,WAAW;GACT,OAAO,WAAW;EACpB;EACA,UAAU,UAAU;GAClB,UAAU,IAAI,QAAQ;GACtB,aAAa;IACX,UAAU,OAAO,QAAQ;GAC3B;EACF;CACF;AACF"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keyboard + (future) gamepad focus navigation.
|
|
3
|
+
*
|
|
4
|
+
* Primitives register themselves with the focus manager by id + tab order.
|
|
5
|
+
* The manager owns which id is currently focused and exposes `next` / `prev`
|
|
6
|
+
* / `activate` actions for a driver (keyboard key handler, gamepad dpad)
|
|
7
|
+
* to call. Components read `focusedId` to render a focus ring.
|
|
8
|
+
*/
|
|
9
|
+
interface FocusNode {
|
|
10
|
+
id: string;
|
|
11
|
+
/** Lower tabIndex is focused first. Nodes with equal tabIndex fall back to registration order. */
|
|
12
|
+
tabIndex: number;
|
|
13
|
+
/** Called when this node is activated (Enter / A button). */
|
|
14
|
+
onActivate?: () => void;
|
|
15
|
+
/** Called when this node receives focus. */
|
|
16
|
+
onFocus?: () => void;
|
|
17
|
+
/** Called when this node loses focus. */
|
|
18
|
+
onBlur?: () => void;
|
|
19
|
+
/** If true, the node is skipped during navigation. */
|
|
20
|
+
disabled?: boolean;
|
|
21
|
+
}
|
|
22
|
+
interface FocusManager {
|
|
23
|
+
/**
|
|
24
|
+
* Registering an already-registered id updates that node in place: it keeps
|
|
25
|
+
* its position among equal `tabIndex` and focus is unchanged.
|
|
26
|
+
*/
|
|
27
|
+
register(node: FocusNode): () => void;
|
|
28
|
+
unregister(id: string): void;
|
|
29
|
+
focus(id: string): void;
|
|
30
|
+
/** Explicit opt-in initial focus: focuses the first node in tab order (or clears if empty). */
|
|
31
|
+
focusFirst(): void;
|
|
32
|
+
blur(): void;
|
|
33
|
+
next(): void;
|
|
34
|
+
prev(): void;
|
|
35
|
+
activate(): void;
|
|
36
|
+
getFocused(): string | null;
|
|
37
|
+
getOrder(): readonly string[];
|
|
38
|
+
subscribe(listener: (focusedId: string | null) => void): () => void;
|
|
39
|
+
}
|
|
40
|
+
interface FocusManagerOptions {
|
|
41
|
+
/**
|
|
42
|
+
* When the focused node unregisters, move focus to the first node in tab
|
|
43
|
+
* order (`true`, the default — keyboard users keep a focus target) or clear
|
|
44
|
+
* it to `null` (`false`). Subscribers are notified either way, and a later
|
|
45
|
+
* `next()` / `prev()` starts from the first / last node as it does from `null`.
|
|
46
|
+
*/
|
|
47
|
+
refocusOnUnregister?: boolean;
|
|
48
|
+
}
|
|
49
|
+
declare function createFocusManager(opts?: FocusManagerOptions): FocusManager;
|
|
50
|
+
/** Structural key source — createKeyboard() from @flare-engine/input is assignable; no hard dep. */
|
|
51
|
+
interface FocusKeySource {
|
|
52
|
+
wasPressed(code: string): boolean;
|
|
53
|
+
}
|
|
54
|
+
interface FocusKeyboardDriver {
|
|
55
|
+
/** Call once per frame; polls edges and drives the manager. */
|
|
56
|
+
update(): void;
|
|
57
|
+
}
|
|
58
|
+
interface FocusKeyboardDriverOptions {
|
|
59
|
+
next?: readonly string[];
|
|
60
|
+
prev?: readonly string[];
|
|
61
|
+
activate?: readonly string[];
|
|
62
|
+
}
|
|
63
|
+
declare function createFocusKeyboardDriver(manager: FocusManager, keys: FocusKeySource, options?: FocusKeyboardDriverOptions): FocusKeyboardDriver;
|
|
64
|
+
export { FocusManager as a, createFocusManager as c, createFocusKeyboardDriver as i, FocusKeyboardDriver as n, FocusManagerOptions as o, FocusKeyboardDriverOptions as r, FocusNode as s, FocusKeySource as t };
|
|
65
|
+
//# sourceMappingURL=focus-ILunQnll.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"focus-ILunQnll.d.mts","names":[],"sources":["../src/focus-manager.ts","../src/focus-keyboard-driver.ts"],"mappings":";;;;;;;;UASiB;EACf;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;UAGe;;;;;EAKf,SAAS,MAAM;EACf,WAAW;EACX,MAAM;;EAEN;EACA;EACA;EACA;EACA;EACA;EACA;EACA,UAAU,WAAW;;UAGN;;;;;;;EAOf;;iBAOc,mBAAmB,OAAO,sBAAsB;;UCrD/C;EACf,WAAW;;UAGI;;EAEf;;UAGe;EACf;EACA;EACA;;iBAOc,0BACd,SAAS,cACT,MAAM,gBACN,UAAU,6BACT"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keyboard + (future) gamepad focus navigation.
|
|
3
|
+
*
|
|
4
|
+
* Primitives register themselves with the focus manager by id + tab order.
|
|
5
|
+
* The manager owns which id is currently focused and exposes `next` / `prev`
|
|
6
|
+
* / `activate` actions for a driver (keyboard key handler, gamepad dpad)
|
|
7
|
+
* to call. Components read `focusedId` to render a focus ring.
|
|
8
|
+
*/
|
|
9
|
+
interface FocusNode {
|
|
10
|
+
id: string;
|
|
11
|
+
/** Lower tabIndex is focused first. Nodes with equal tabIndex fall back to registration order. */
|
|
12
|
+
tabIndex: number;
|
|
13
|
+
/** Called when this node is activated (Enter / A button). */
|
|
14
|
+
onActivate?: () => void;
|
|
15
|
+
/** Called when this node receives focus. */
|
|
16
|
+
onFocus?: () => void;
|
|
17
|
+
/** Called when this node loses focus. */
|
|
18
|
+
onBlur?: () => void;
|
|
19
|
+
/** If true, the node is skipped during navigation. */
|
|
20
|
+
disabled?: boolean;
|
|
21
|
+
}
|
|
22
|
+
interface FocusManager {
|
|
23
|
+
/**
|
|
24
|
+
* Registering an already-registered id updates that node in place: it keeps
|
|
25
|
+
* its position among equal `tabIndex` and focus is unchanged.
|
|
26
|
+
*/
|
|
27
|
+
register(node: FocusNode): () => void;
|
|
28
|
+
unregister(id: string): void;
|
|
29
|
+
focus(id: string): void;
|
|
30
|
+
/** Explicit opt-in initial focus: focuses the first node in tab order (or clears if empty). */
|
|
31
|
+
focusFirst(): void;
|
|
32
|
+
blur(): void;
|
|
33
|
+
next(): void;
|
|
34
|
+
prev(): void;
|
|
35
|
+
activate(): void;
|
|
36
|
+
getFocused(): string | null;
|
|
37
|
+
getOrder(): readonly string[];
|
|
38
|
+
subscribe(listener: (focusedId: string | null) => void): () => void;
|
|
39
|
+
}
|
|
40
|
+
interface FocusManagerOptions {
|
|
41
|
+
/**
|
|
42
|
+
* When the focused node unregisters, move focus to the first node in tab
|
|
43
|
+
* order (`true`, the default — keyboard users keep a focus target) or clear
|
|
44
|
+
* it to `null` (`false`). Subscribers are notified either way, and a later
|
|
45
|
+
* `next()` / `prev()` starts from the first / last node as it does from `null`.
|
|
46
|
+
*/
|
|
47
|
+
refocusOnUnregister?: boolean;
|
|
48
|
+
}
|
|
49
|
+
declare function createFocusManager(opts?: FocusManagerOptions): FocusManager;
|
|
50
|
+
/** Structural key source — createKeyboard() from @flare-engine/input is assignable; no hard dep. */
|
|
51
|
+
interface FocusKeySource {
|
|
52
|
+
wasPressed(code: string): boolean;
|
|
53
|
+
}
|
|
54
|
+
interface FocusKeyboardDriver {
|
|
55
|
+
/** Call once per frame; polls edges and drives the manager. */
|
|
56
|
+
update(): void;
|
|
57
|
+
}
|
|
58
|
+
interface FocusKeyboardDriverOptions {
|
|
59
|
+
next?: readonly string[];
|
|
60
|
+
prev?: readonly string[];
|
|
61
|
+
activate?: readonly string[];
|
|
62
|
+
}
|
|
63
|
+
declare function createFocusKeyboardDriver(manager: FocusManager, keys: FocusKeySource, options?: FocusKeyboardDriverOptions): FocusKeyboardDriver;
|
|
64
|
+
export { FocusManager as a, createFocusManager as c, createFocusKeyboardDriver as i, FocusKeyboardDriver as n, FocusManagerOptions as o, FocusKeyboardDriverOptions as r, FocusNode as s, FocusKeySource as t };
|
|
65
|
+
//# sourceMappingURL=focus-ILunQnll.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"focus-ILunQnll.d.ts","names":[],"sources":["../src/focus-manager.ts","../src/focus-keyboard-driver.ts"],"mappings":";;;;;;;;UASiB;EACf;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;UAGe;;;;;EAKf,SAAS,MAAM;EACf,WAAW;EACX,MAAM;;EAEN;EACA;EACA;EACA;EACA;EACA;EACA;EACA,UAAU,WAAW;;UAGN;;;;;;;EAOf;;iBAOc,mBAAmB,OAAO,sBAAsB;;UCrD/C;EACf,WAAW;;UAGI;;EAEf;;UAGe;EACf;EACA;EACA;;iBAOc,0BACd,SAAS,cACT,MAAM,gBACN,UAAU,6BACT"}
|
package/dist/focus.d.mts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as FocusManager, c as createFocusManager, i as createFocusKeyboardDriver, n as FocusKeyboardDriver, o as FocusManagerOptions, r as FocusKeyboardDriverOptions, s as FocusNode, t as FocusKeySource } from "./focus-ILunQnll.mjs";
|
|
2
|
+
export { type FocusKeySource, type FocusKeyboardDriver, type FocusKeyboardDriverOptions, type FocusManager, type FocusManagerOptions, type FocusNode, createFocusKeyboardDriver, createFocusManager };
|
package/dist/focus.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as FocusManager, c as createFocusManager, i as createFocusKeyboardDriver, n as FocusKeyboardDriver, o as FocusManagerOptions, r as FocusKeyboardDriverOptions, s as FocusNode, t as FocusKeySource } from "./focus-ILunQnll.js";
|
|
2
|
+
export { type FocusKeySource, type FocusKeyboardDriver, type FocusKeyboardDriverOptions, type FocusManager, type FocusManagerOptions, type FocusNode, createFocusKeyboardDriver, createFocusManager };
|
package/dist/focus.js
ADDED
package/dist/focus.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { n as createFocusKeyboardDriver, t as createFocusManager } from "./focus-CtmAYznc.mjs";
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { a as FocusManager, c as createFocusManager, i as createFocusKeyboardDriver, n as FocusKeyboardDriver, o as FocusManagerOptions, r as FocusKeyboardDriverOptions, s as FocusNode, t as FocusKeySource } from "./focus-ILunQnll.mjs";
|
|
1
2
|
import { ReactNode } from "react";
|
|
2
3
|
import { StyleProp, TextProps, TextStyle, ViewStyle } from "react-native";
|
|
3
4
|
type ButtonVariant = 'primary' | 'secondary' | 'ghost' | 'danger';
|
|
@@ -10,9 +11,15 @@ interface ButtonProps {
|
|
|
10
11
|
tabIndex?: number;
|
|
11
12
|
/** Stable focus id. If omitted, a generated id is used. */
|
|
12
13
|
focusId?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Move focus to this button when it is pressed. Defaults to `true`. Set
|
|
16
|
+
* `false` on touch-first screens so a tap leaves no focus ring behind;
|
|
17
|
+
* `onPress` still fires.
|
|
18
|
+
*/
|
|
19
|
+
focusOnPress?: boolean;
|
|
13
20
|
style?: StyleProp<ViewStyle>;
|
|
14
21
|
}
|
|
15
|
-
export declare function Button({ label, onPress, variant, disabled, tabIndex, focusId, style }: ButtonProps): ReactNode;
|
|
22
|
+
export declare function Button({ label, onPress, variant, disabled, tabIndex, focusId, focusOnPress, style }: ButtonProps): ReactNode;
|
|
16
23
|
interface DialogueChoice {
|
|
17
24
|
/** Display label (already localized by the caller). */
|
|
18
25
|
readonly label: string;
|
|
@@ -47,42 +54,6 @@ interface DialoguePanelProps {
|
|
|
47
54
|
* Portrait resolution is caller-owned so the UI kit stays asset-agnostic.
|
|
48
55
|
*/
|
|
49
56
|
export declare function DialoguePanel({ speaker, text, portrait, choices, onAdvance, style }: DialoguePanelProps): ReactNode;
|
|
50
|
-
/**
|
|
51
|
-
* Keyboard + (future) gamepad focus navigation.
|
|
52
|
-
*
|
|
53
|
-
* Primitives register themselves with the focus manager by id + tab order.
|
|
54
|
-
* The manager owns which id is currently focused and exposes `next` / `prev`
|
|
55
|
-
* / `activate` actions for a driver (keyboard key handler, gamepad dpad)
|
|
56
|
-
* to call. Components read `focusedId` to render a focus ring.
|
|
57
|
-
*/
|
|
58
|
-
interface FocusNode {
|
|
59
|
-
id: string;
|
|
60
|
-
/** Lower tabIndex is focused first. Nodes with equal tabIndex fall back to registration order. */
|
|
61
|
-
tabIndex: number;
|
|
62
|
-
/** Called when this node is activated (Enter / A button). */
|
|
63
|
-
onActivate?: () => void;
|
|
64
|
-
/** Called when this node receives focus. */
|
|
65
|
-
onFocus?: () => void;
|
|
66
|
-
/** Called when this node loses focus. */
|
|
67
|
-
onBlur?: () => void;
|
|
68
|
-
/** If true, the node is skipped during navigation. */
|
|
69
|
-
disabled?: boolean;
|
|
70
|
-
}
|
|
71
|
-
interface FocusManager {
|
|
72
|
-
register(node: FocusNode): () => void;
|
|
73
|
-
unregister(id: string): void;
|
|
74
|
-
focus(id: string): void;
|
|
75
|
-
/** Explicit opt-in initial focus: focuses the first node in tab order (or clears if empty). */
|
|
76
|
-
focusFirst(): void;
|
|
77
|
-
blur(): void;
|
|
78
|
-
next(): void;
|
|
79
|
-
prev(): void;
|
|
80
|
-
activate(): void;
|
|
81
|
-
getFocused(): string | null;
|
|
82
|
-
getOrder(): readonly string[];
|
|
83
|
-
subscribe(listener: (focusedId: string | null) => void): () => void;
|
|
84
|
-
}
|
|
85
|
-
export declare function createFocusManager(): FocusManager;
|
|
86
57
|
interface FocusContextValue {
|
|
87
58
|
manager: FocusManager;
|
|
88
59
|
focusedId: string | null;
|
|
@@ -90,20 +61,6 @@ interface FocusContextValue {
|
|
|
90
61
|
export declare const FocusContext: import("react").Context<FocusContextValue | null>;
|
|
91
62
|
export declare function useFocusManager(): FocusManager | null;
|
|
92
63
|
export declare function useFocusedId(): string | null;
|
|
93
|
-
/** Structural key source — createKeyboard() from @flare-engine/input is assignable; no hard dep. */
|
|
94
|
-
interface FocusKeySource {
|
|
95
|
-
wasPressed(code: string): boolean;
|
|
96
|
-
}
|
|
97
|
-
interface FocusKeyboardDriver {
|
|
98
|
-
/** Call once per frame; polls edges and drives the manager. */
|
|
99
|
-
update(): void;
|
|
100
|
-
}
|
|
101
|
-
interface FocusKeyboardDriverOptions {
|
|
102
|
-
next?: readonly string[];
|
|
103
|
-
prev?: readonly string[];
|
|
104
|
-
activate?: readonly string[];
|
|
105
|
-
}
|
|
106
|
-
export declare function createFocusKeyboardDriver(manager: FocusManager, keys: FocusKeySource, options?: FocusKeyboardDriverOptions): FocusKeyboardDriver;
|
|
107
64
|
interface FocusProviderProps {
|
|
108
65
|
manager?: FocusManager;
|
|
109
66
|
/**
|
|
@@ -325,5 +282,5 @@ interface ThemeProviderProps {
|
|
|
325
282
|
}
|
|
326
283
|
export declare function ThemeProvider({ theme, children }: ThemeProviderProps): ReactNode;
|
|
327
284
|
export declare function useTheme(): Theme;
|
|
328
|
-
export type
|
|
285
|
+
export { type AlignItems, type ButtonProps, type ButtonVariant, type DialogueChoice, type DialoguePanelProps, type FlexDirection, type FocusContextValue, type FocusKeySource, type FocusKeyboardDriver, type FocusKeyboardDriverOptions, type FocusManager, type FocusManagerOptions, type FocusNode, type FocusProviderProps, type HUDBarKind, type HUDBarProps, type JustifyContent, type LabelProps, type LabelTone, type LabelVariant, type LayoutChild, type LayoutContainer, type LayoutRect, type ListProps, type MainMenuItem, type MainMenuKitProps, type ModalProps, type Padding, type PanelProps, type ProgressBarProps, type Theme, type ThemeColors, type ThemeProviderProps, type ThemeRadii, type ThemeSpacing, type ThemeTypography, createFocusKeyboardDriver, createFocusManager };
|
|
329
286
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/button.tsx","../src/dialogue-panel.tsx","../src/focus
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/button.tsx","../src/dialogue-panel.tsx","../src/focus-context.ts","../src/focus-provider.tsx","../src/hud-bar.tsx","../src/label.tsx","../src/layout.ts","../src/list.tsx","../src/main-menu-kit.tsx","../src/modal.tsx","../src/panel.tsx","../src/progress-bar.tsx","../src/theme.ts"],"mappings":";;;KAKY;UAEK;EACf;EACA;EACA,UAAU;EACV;;EAEA;;EAEA;;;;;;EAMA;EACA,QAAQ,UAAU;;wBAGJ,SACd,OACA,S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