@andrewcyuan/focusgrid 0.2.1 → 0.3.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.
@@ -1,5 +1,5 @@
1
1
  import { createContext, useCallback, useContext, useEffect, useRef, useState, useSyncExternalStore } from "react";
2
- import { isEditableTarget, mountFocusGrid } from "@andrewcyuan/focusgrid/dom";
2
+ import { FocusGridDomController, isEditableTarget, mountFocusGrid } from "@andrewcyuan/focusgrid/dom";
3
3
  import { ShortcutScope, useShortcuts } from "@andrewcyuan/shortcut-engine/react";
4
4
  import "@andrewcyuan/shortcut-engine";
5
5
  import { FocusGridController } from "@andrewcyuan/focusgrid/core";
@@ -18,114 +18,120 @@ const defaultPaneShortcutActions = [
18
18
  id: "split-right",
19
19
  label: "Split right",
20
20
  action: (controller) => controller.splitActive("right"),
21
- defaultSequence: "Ctrl-B %"
21
+ defaultSequence: "Ctrl-S |"
22
22
  },
23
23
  {
24
24
  id: "split-down",
25
25
  label: "Split down",
26
26
  action: (controller) => controller.splitActive("down"),
27
- defaultSequence: "Ctrl-B \""
27
+ defaultSequence: "Ctrl-S -"
28
28
  },
29
29
  {
30
30
  id: "close",
31
31
  label: "Close active",
32
32
  action: (controller) => controller.removeActive(),
33
- defaultSequence: "Ctrl-B X"
33
+ defaultSequence: "Ctrl-S X"
34
34
  },
35
35
  {
36
36
  id: "focus-left",
37
37
  label: "Focus left",
38
38
  action: (controller) => controller.focusAdjacent("left"),
39
- defaultSequence: "Ctrl-B Left"
39
+ defaultSequence: "Ctrl-H"
40
40
  },
41
41
  {
42
42
  id: "focus-right",
43
43
  label: "Focus right",
44
44
  action: (controller) => controller.focusAdjacent("right"),
45
- defaultSequence: "Ctrl-B Right"
45
+ defaultSequence: "Ctrl-L"
46
46
  },
47
47
  {
48
48
  id: "focus-up",
49
49
  label: "Focus up",
50
50
  action: (controller) => controller.focusAdjacent("up"),
51
- defaultSequence: "Ctrl-B Up"
51
+ defaultSequence: "Ctrl-K"
52
52
  },
53
53
  {
54
54
  id: "focus-down",
55
55
  label: "Focus down",
56
56
  action: (controller) => controller.focusAdjacent("down"),
57
- defaultSequence: "Ctrl-B Down"
57
+ defaultSequence: "Ctrl-J"
58
58
  },
59
59
  {
60
60
  id: "swap-left",
61
61
  label: "Swap left",
62
62
  action: (controller) => controller.swapAdjacent("left"),
63
- defaultSequence: "Ctrl-B Shift-Left"
63
+ defaultSequence: "Ctrl-S Shift-Left"
64
64
  },
65
65
  {
66
66
  id: "swap-right",
67
67
  label: "Swap right",
68
68
  action: (controller) => controller.swapAdjacent("right"),
69
- defaultSequence: "Ctrl-B Shift-Right"
69
+ defaultSequence: "Ctrl-S Shift-Right"
70
70
  },
71
71
  {
72
72
  id: "swap-up",
73
73
  label: "Swap up",
74
74
  action: (controller) => controller.swapAdjacent("up"),
75
- defaultSequence: "Ctrl-B Shift-Up"
75
+ defaultSequence: "Ctrl-S Shift-Up"
76
76
  },
77
77
  {
78
78
  id: "swap-down",
79
79
  label: "Swap down",
80
80
  action: (controller) => controller.swapAdjacent("down"),
81
- defaultSequence: "Ctrl-B Shift-Down"
81
+ defaultSequence: "Ctrl-S Shift-Down"
82
82
  },
83
83
  {
84
84
  id: "resize-left",
85
85
  label: "Resize left",
86
86
  action: (controller) => controller.resizeActive("left", 48),
87
- defaultSequence: "Ctrl-B H",
87
+ defaultSequence: "Ctrl-Shift-H",
88
88
  repeat: true
89
89
  },
90
90
  {
91
91
  id: "resize-right",
92
92
  label: "Resize right",
93
93
  action: (controller) => controller.resizeActive("right", 48),
94
- defaultSequence: "Ctrl-B L",
94
+ defaultSequence: "Ctrl-Shift-L",
95
95
  repeat: true
96
96
  },
97
97
  {
98
98
  id: "resize-up",
99
99
  label: "Resize up",
100
100
  action: (controller) => controller.resizeActive("up", 48),
101
- defaultSequence: "Ctrl-B K",
101
+ defaultSequence: "Ctrl-Shift-K",
102
102
  repeat: true
103
103
  },
104
104
  {
105
105
  id: "resize-down",
106
106
  label: "Resize down",
107
107
  action: (controller) => controller.resizeActive("down", 48),
108
- defaultSequence: "Ctrl-B J",
108
+ defaultSequence: "Ctrl-Shift-J",
109
109
  repeat: true
110
110
  }
111
111
  ];
112
112
  function createDefaultPaneShortcuts() {
113
113
  return Object.fromEntries(defaultPaneShortcutActions.map((action) => [action.id, action.defaultSequence]));
114
114
  }
115
- function createDefaultPaneKeymap(controller) {
115
+ function createDefaultPaneKeymap(controller, shortcuts = createDefaultPaneShortcuts()) {
116
116
  return defaultPaneShortcutActions.map((definition) => ({
117
- sequence: definition.defaultSequence,
117
+ sequence: shortcuts[definition.id],
118
118
  action: () => {
119
119
  definition.action(controller);
120
120
  },
121
- repeat: "repeat" in definition ? definition.repeat : void 0
121
+ repeat: "repeat" in definition && definition.repeat
122
122
  }));
123
123
  }
124
124
  //#endregion
125
125
  //#region react/src/hooks.ts
126
- function useFocusGridController(createProps, domController) {
127
- const [controller] = useState(() => new FocusGridController(createProps(), domController));
128
- return controller;
126
+ function useFocusGridController(createProps) {
127
+ const [controllers] = useState(() => {
128
+ const domController = new FocusGridDomController();
129
+ return {
130
+ controller: new FocusGridController(createProps(), domController),
131
+ domController
132
+ };
133
+ });
134
+ return controllers;
129
135
  }
130
136
  function useControllerLayout(controller) {
131
137
  const subscribe = useCallback((notify) => controller.subscribe(notify), [controller]);
@@ -133,34 +139,25 @@ function useControllerLayout(controller) {
133
139
  return useSyncExternalStore(subscribe, getLayout, getLayout);
134
140
  }
135
141
  //#endregion
136
- //#region react/src/ResizeHandle.tsx
137
- function ResizeHandle({ handle }) {
138
- const style = {
139
- left: handle.rect.x,
140
- top: handle.rect.y,
141
- width: handle.rect.width,
142
- height: handle.rect.height
143
- };
144
- return /* @__PURE__ */ jsx("div", {
145
- className: "FocusgridResizeHandle",
146
- "data-direction": handle.direction,
147
- style,
148
- role: "separator",
149
- "aria-orientation": handle.direction === "horizontal" ? "vertical" : "horizontal",
150
- "data-resize-handle": handle.id
151
- });
152
- }
153
- //#endregion
154
142
  //#region react/src/PaneView.tsx
155
- function PaneView({ node, renderPane }) {
156
- return renderNode(node, renderPane);
157
- }
158
143
  function renderNode(node, renderPane) {
159
144
  if (node.kind === "pane") return [/* @__PURE__ */ jsx(PaneShell, {
160
145
  pane: node.pane,
161
146
  renderPane
162
147
  }, `pane:${node.pane.paneId}`)];
163
- return node.children.flatMap((child, index) => [...renderNode(child, renderPane), ...node.handles[index] ? [/* @__PURE__ */ jsx(ResizeHandle, { handle: node.handles[index] }, `handle:${node.handles[index].id}`)] : []]);
148
+ return node.children.flatMap((child, index) => {
149
+ const elements = renderNode(child, renderPane);
150
+ const handle = node.handles[index];
151
+ return handle ? [...elements, /* @__PURE__ */ jsx(ResizeHandle, { handle }, `handle:${handle.id}`)] : elements;
152
+ });
153
+ }
154
+ function rectStyle(rect) {
155
+ return {
156
+ left: rect.x,
157
+ top: rect.y,
158
+ width: rect.width,
159
+ height: rect.height
160
+ };
164
161
  }
165
162
  function PaneShell({ pane, renderPane }) {
166
163
  const { controller } = useGridControllers();
@@ -169,12 +166,7 @@ function PaneShell({ pane, renderPane }) {
169
166
  "data-active": pane.active,
170
167
  "data-pane-id": pane.paneId,
171
168
  tabIndex: -1,
172
- style: {
173
- left: pane.rect.x,
174
- top: pane.rect.y,
175
- width: pane.rect.width,
176
- height: pane.rect.height
177
- },
169
+ style: rectStyle(pane.rect),
178
170
  children: renderPane({
179
171
  paneId: pane.paneId,
180
172
  rect: pane.rect,
@@ -183,6 +175,16 @@ function PaneShell({ pane, renderPane }) {
183
175
  })
184
176
  });
185
177
  }
178
+ function ResizeHandle({ handle }) {
179
+ return /* @__PURE__ */ jsx("div", {
180
+ className: "FocusgridResizeHandle",
181
+ "data-direction": handle.direction,
182
+ style: rectStyle(handle.rect),
183
+ role: "separator",
184
+ "aria-orientation": handle.direction === "horizontal" ? "vertical" : "horizontal",
185
+ "data-resize-handle": handle.id
186
+ });
187
+ }
186
188
  //#endregion
187
189
  //#region react/src/FocusGrid.tsx
188
190
  function FocusGrid({ className, onPaneLayoutChange, onPaneClose, ...content }) {
@@ -210,10 +212,7 @@ function GridContent({ keymap, renderPane }) {
210
212
  const { controller } = useGridControllers();
211
213
  const layout = useControllerLayout(controller);
212
214
  useShortcuts(keymap ?? createDefaultPaneKeymap(controller));
213
- return /* @__PURE__ */ jsx(PaneView, {
214
- node: layout.root,
215
- renderPane
216
- });
215
+ return renderNode(layout.root, renderPane);
217
216
  }
218
217
  //#endregion
219
218
  //#region react/src/composite-shortcuts.ts
@@ -15,7 +15,7 @@
15
15
  min-height: 0;
16
16
  }
17
17
 
18
- :where(.FocusgridPaneView[data-active="true"]) {
18
+ :where(.FocusgridFocusGrid:focus-within .FocusgridPaneView[data-active="true"]) {
19
19
  outline: 1px solid var(--focusgrid-focus-color, #4d8dff);
20
20
  outline-offset: -1px;
21
21
  }
package/package.json CHANGED
@@ -1,32 +1,29 @@
1
1
  {
2
2
  "name": "@andrewcyuan/focusgrid",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Keyboard-native pane layouts and deterministic focus management for web applications.",
5
5
  "keywords": [
6
- "keyboard-navigation",
7
6
  "focus-management",
7
+ "keyboard-navigation",
8
8
  "pane-layout",
9
9
  "react",
10
10
  "typescript"
11
11
  ],
12
- "type": "module",
13
12
  "license": "MIT",
14
- "sideEffects": [
15
- "./dist/react/styles.css",
16
- "./react/styles.css"
17
- ],
18
- "files": [
19
- "dist",
20
- "LICENSE"
21
- ],
22
13
  "repository": {
23
14
  "type": "git",
24
15
  "url": "git+https://github.com/andrewcyuan/focusgrid.git",
25
16
  "directory": "packages/focusgrid"
26
17
  },
27
- "publishConfig": {
28
- "access": "public"
29
- },
18
+ "files": [
19
+ "dist",
20
+ "LICENSE"
21
+ ],
22
+ "type": "module",
23
+ "sideEffects": [
24
+ "./dist/react/styles.css",
25
+ "./react/styles.css"
26
+ ],
30
27
  "exports": {
31
28
  "./core": {
32
29
  "types": "./dist/core/index.d.ts",
@@ -45,22 +42,19 @@
45
42
  },
46
43
  "./react/styles.css": "./dist/react/styles.css"
47
44
  },
45
+ "publishConfig": {
46
+ "access": "public"
47
+ },
48
48
  "scripts": {
49
49
  "build": "tsdown core/src/index.ts --config buildConfig.mjs --format esm,cjs --out-dir dist/core && tsdown dom/src/index.ts --config buildConfig.mjs --format esm,cjs --out-dir dist/dom --no-clean && tsdown react/src/index.tsx --config buildConfig.mjs --format esm,cjs --out-dir dist/react --no-clean && cp react/src/styles.css dist/react/styles.css",
50
50
  "test": "vitest run --config ../../vitest.config.ts",
51
- "typecheck": "tsc --noEmit",
52
- "lint": "tsc --noEmit"
51
+ "typecheck": "tsc --noEmit"
52
+ },
53
+ "dependencies": {
54
+ "@andrewcyuan/shortcut-engine": "0.2.0"
53
55
  },
54
56
  "peerDependencies": {
55
57
  "react": ">=18",
56
58
  "react-dom": ">=18"
57
- },
58
- "dependencies": {
59
- "@andrewcyuan/shortcut-engine": "0.1.0"
60
- },
61
- "devDependencies": {
62
- "@types/react": "^18.3.18",
63
- "@types/react-dom": "^18.3.5",
64
- "vitest": "^2.1.8"
65
59
  }
66
60
  }