@panyam/tsappkit-dock 0.7.9 → 0.7.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/src/dock.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { DockviewComponent, themeDark, themeLight } from "dockview-core";
2
- import type { DockviewApi, IContentRenderer, IHeaderActionsRenderer } from "dockview-core";
2
+ import type { DockviewApi, DockviewGroupPanel, IContentRenderer, IHeaderActionsRenderer } from "dockview-core";
3
3
  import { DRAWER_DEFAULT, type DrawerEdge } from "./drawer.js";
4
4
  import { DrawerView, type DrawerConfig } from "./drawerView.js";
5
5
  import {
@@ -17,6 +17,23 @@ import {
17
17
 
18
18
  const EDGES: readonly DrawerEdge[] = ["left", "right", "top", "bottom"];
19
19
 
20
+ /**
21
+ * Where a panel is: a `Placement`, or a dockview popout window. Popouts come from `dock.api`, since
22
+ * the dock doesn't place panels there itself (see the dock experiments page for why).
23
+ */
24
+ export type PanelLocation = Placement | { kind: "popout" };
25
+
26
+ /** What `moved` gets besides the panel and its old and new place. */
27
+ export interface MoveInfo {
28
+ /**
29
+ * True when the panel's element is in a different document from the last time the dock saw it,
30
+ * which happens on the way into a popout window and on the way back. A canvas can't draw again
31
+ * once the window it was in has closed, so rebuild canvases (and WebGL contexts) when this is
32
+ * true. Moves within one document leave canvases working.
33
+ */
34
+ documentChanged: boolean;
35
+ }
36
+
20
37
  /** What `createDock` needs. */
21
38
  export interface DockOptions {
22
39
  /** The element dockview renders into. It needs a size, as dockview fills it. */
@@ -50,6 +67,21 @@ export interface DockOptions {
50
67
  * keeps the element. Clearing the layout, as `reset` does, calls it for every open panel.
51
68
  */
52
69
  release?: (id: string, el: HTMLElement) => void;
70
+ /**
71
+ * Called after a panel ends up somewhere new, with its element already in place. `to` and
72
+ * `from` are where it is and where it was, and null means closed. It covers every way a panel
73
+ * moves: `place`, `show`, `toggle`, Reset layout, the reader dragging a tab into a float, a popout
74
+ * made through `dock.api`, and a popout window closing. It isn't called for the layout the dock
75
+ * starts with. See `MoveInfo.documentChanged` for the one move that breaks a canvas.
76
+ */
77
+ moved?: (id: string, el: HTMLElement, to: PanelLocation | null, from: PanelLocation | null, info: MoveInfo) => void;
78
+ /**
79
+ * Called when a drawer opens, closes, or shows a different tab, with whether it's open and its
80
+ * active panel afterwards. A phone page uses it to highlight the button for the panel on show.
81
+ * It covers the reader's taps, drags and Escape as well as `show`, `place`, `toggleDrawer` and
82
+ * Reset layout, and isn't called for the drawers the dock starts with.
83
+ */
84
+ drawerChanged?: (edge: DrawerEdge, open: boolean, active: string | undefined) => void;
53
85
  /** Where the layout is saved, usually `localStorage`. Null or absent: nothing is saved. */
54
86
  storage?: DockStorage | null;
55
87
  /** The storage key. Required with `storage`. */
@@ -88,8 +120,8 @@ export interface Dock {
88
120
  * unknown id.
89
121
  */
90
122
  place(id: string, target: Placement): boolean;
91
- /** Where a panel is now, or null when it's closed. */
92
- where(id: string): Placement | null;
123
+ /** Where a panel is now, or null when it's closed. A panel in a dockview popout reports `popout`. */
124
+ where(id: string): PanelLocation | null;
93
125
  /** Opens a closed panel or closes an open one. False for an unknown id. */
94
126
  toggle(id: string): boolean;
95
127
  /**
@@ -119,8 +151,10 @@ export interface Dock {
119
151
  dispose(): void;
120
152
  }
121
153
 
122
- const placementLabel = (p: Placement | null): string =>
123
- !p || p.kind === "grid" ? "" : p.kind === "float" ? " · float" : ` · ${p.edge} drawer`;
154
+ const placementLabel = (p: PanelLocation | null): string =>
155
+ !p || p.kind === "grid" ? "" : p.kind === "drawer" ? ` · ${p.edge} drawer` : ` · ${p.kind}`;
156
+
157
+ const TOWARD_EDGE: Record<DrawerEdge, PanelPosition["direction"]> = { left: "left", right: "right", top: "above", bottom: "below" };
124
158
 
125
159
  /**
126
160
  * Builds a dockview dock from a panel registry. It restores the saved layout when there is one,
@@ -177,15 +211,19 @@ export function createDock(opts: DockOptions): Dock {
177
211
 
178
212
  const drawerWith = (id: string): DrawerView | undefined => [...drawers.values()].find((d) => d.has(id));
179
213
 
180
- const where = (id: string): Placement | null => {
214
+ const where = (id: string): PanelLocation | null => {
181
215
  const d = drawerWith(id);
182
216
  if (d) return { kind: "drawer", edge: d.edge };
183
217
  const panel = api.getPanel(id);
184
218
  if (!panel) return null;
185
- return panel.group.api.location.type === "floating" ? { kind: "float" } : { kind: "grid" };
219
+ const type = panel.group.api.location.type;
220
+ return type === "floating" ? { kind: "float" } : type === "popout" ? { kind: "popout" } : { kind: "grid" };
186
221
  };
187
222
 
188
- const same = (a: Placement | null, b: Placement): boolean =>
223
+ const elementOf = (id: string): HTMLElement | undefined =>
224
+ drawerWith(id)?.elementOf(id) ?? (api.getPanel(id)?.view.content.element as HTMLElement | undefined);
225
+
226
+ const same = (a: PanelLocation | null, b: Placement): boolean =>
189
227
  !!a && a.kind === b.kind && (a.kind !== "drawer" || (b.kind === "drawer" && a.edge === b.edge));
190
228
 
191
229
  // ---- drawers ----
@@ -194,7 +232,7 @@ export function createDock(opts: DockOptions): Dock {
194
232
  let d = drawers.get(edge);
195
233
  if (!d) {
196
234
  if (doc.defaultView?.getComputedStyle(drawerHost).position === "static") drawerHost.style.position = "relative";
197
- d = new DrawerView(edge, drawerHost, opts.drawers?.[edge] ?? {}, { changed: () => save(), menu: (anchor, id) => moveMenu(anchor, id) });
235
+ d = new DrawerView(edge, drawerHost, opts.drawers?.[edge] ?? {}, { changed: () => changed(), menu: (anchor, id) => moveMenu(anchor, id) });
198
236
  drawers.set(edge, d);
199
237
  }
200
238
  return d;
@@ -210,12 +248,31 @@ export function createDock(opts: DockOptions): Dock {
210
248
  // ---- moving panels ----
211
249
 
212
250
  const usable = (p?: PanelPosition): p is PanelPosition => !!p && (!p.referencePanel || !!api.getPanel(p.referencePanel));
251
+ const inGrid = (p?: PanelPosition): p is PanelPosition =>
252
+ !!p && (!p.referencePanel || (api.getPanel(p.referencePanel)?.group.api.location.type === "grid" && api.getPanel(p.referencePanel)!.group.api.isVisible));
213
253
 
214
- const addToGrid = (id: string): void => {
254
+ // Puts a panel in the grid, never in a float, a popout, or a popout's hidden placeholder. A position
255
+ // counts only if its reference panel is in the grid. Without one the panel tabs into the active group, or the first grid group
256
+ // when the active one is a float or a popout, and goes to the grid's edge when the grid is empty.
257
+ const addToGrid = (id: string, prefer?: PanelPosition): void => {
215
258
  const def = defOf(id)!;
216
259
  const spec = specs?.find((s) => s.id === id);
217
- const position = [def.position, spec?.position].find(usable);
218
- api.addPanel({ id, component: id, title: def.title, ...(position ? { position } : {}) });
260
+ const position = [prefer, def.position, spec?.position].find(inGrid);
261
+ if (position) {
262
+ api.addPanel({ id, component: id, title: def.title, position });
263
+ return;
264
+ }
265
+ // While a group is popped out, dockview keeps a hidden placeholder for it in the grid, and a
266
+ // panel tabbed into that would be invisible.
267
+ const showing = (g?: DockviewGroupPanel) => !!g && g.api.location.type === "grid" && g.api.isVisible;
268
+ const active = api.activeGroup;
269
+ const group = showing(active) ? active : api.groups.find(showing);
270
+ api.addPanel({
271
+ id,
272
+ component: id,
273
+ title: def.title,
274
+ position: group ? { referenceGroup: group, direction: "within" } : { direction: "right" },
275
+ });
219
276
  };
220
277
 
221
278
  const enter = (id: string, target: Placement): void => {
@@ -249,37 +306,59 @@ export function createDock(opts: DockOptions): Dock {
249
306
  api.getPanel(id)?.api.setActive();
250
307
  };
251
308
 
309
+ // Runs a move with saving and moved held back, then does both once, so a move that passes through
310
+ // a closed state on its way (leave, then enter) reports only where it ended up.
311
+ const batch = (fn: () => void): void => {
312
+ quiet++;
313
+ try {
314
+ fn();
315
+ } finally {
316
+ quiet--;
317
+ }
318
+ changed();
319
+ };
320
+
252
321
  const place = (id: string, target: Placement): boolean => {
253
322
  if (!defOf(id)) return false;
323
+ batch(() => placeNow(id, target));
324
+ return true;
325
+ };
326
+
327
+ const placeNow = (id: string, target: Placement): void => {
254
328
  const now = where(id);
255
329
  if (!same(now, target)) {
256
- // Grid to float stays inside dockview, which moves the group and keeps the element. Every
257
- // other move goes out through release and back in through hostFor.
258
- if (!(now?.kind === "grid" && target.kind === "float")) leave(id);
259
- enter(id, target);
330
+ if (now?.kind === "drawer" && target.kind === "grid") {
331
+ // Docking from a drawer puts the panel where the drawer was: on its edge, at its size.
332
+ const size = drawer(now.edge).pixels;
333
+ leave(id);
334
+ addToGrid(id, { direction: TOWARD_EDGE[now.edge] });
335
+ const horizontal = now.edge === "left" || now.edge === "right";
336
+ api.getPanel(id)?.api.setSize(horizontal ? { width: size } : { height: size });
337
+ } else {
338
+ // Grid to float stays inside dockview, which moves the group and keeps the element. Every
339
+ // other move goes out through release and back in through hostFor.
340
+ if (!(now?.kind === "grid" && target.kind === "float")) leave(id);
341
+ enter(id, target);
342
+ }
260
343
  }
261
344
  reveal(id);
262
- save();
263
- return true;
264
345
  };
265
346
 
266
347
  const show = (id: string): boolean => {
267
348
  const def = defOf(id);
268
349
  if (!def) return false;
269
- if (!where(id)) enter(id, def.defaultPlacement ?? { kind: "grid" });
270
- reveal(id);
271
- save();
350
+ batch(() => {
351
+ if (!where(id)) enter(id, def.defaultPlacement ?? { kind: "grid" });
352
+ reveal(id);
353
+ });
272
354
  return true;
273
355
  };
274
356
 
275
357
  const toggle = (id: string): boolean => {
276
358
  if (!defOf(id)) return false;
277
- if (where(id)) {
278
- leave(id);
279
- save();
280
- return true;
281
- }
282
- return show(id);
359
+ if (!where(id)) return show(id);
360
+ batch(() => leave(id));
361
+ return true;
283
362
  };
284
363
 
285
364
  // ---- default layout, restore, reset ----
@@ -354,6 +433,42 @@ export function createDock(opts: DockOptions): Dock {
354
433
  saveLayout(storage, key, panels, api.toJSON(), drawerStates());
355
434
  };
356
435
 
436
+ // What `moved` compares against: each panel's last place, element and document.
437
+ const seen = new Map<string, { at: PanelLocation | null; el?: HTMLElement; doc?: Document }>();
438
+ const key0 = (p: PanelLocation | null) => JSON.stringify(p);
439
+ // What `drawerChanged` compares against: each drawer's last "open active".
440
+ const drawerSeen = new Map<DrawerEdge, string>();
441
+
442
+ // Compares every panel's place and document with the last ones seen, and calls `moved` for each
443
+ // that differs. Running it after every change, whatever made it, catches moves made through
444
+ // dock.api and by dockview itself as well as the dock's own.
445
+ const sync = (notify = true): void => {
446
+ if (disposed || quiet) return;
447
+ const ids = new Set([...panels.map((p) => p.id), ...api.panels.map((p) => p.id), ...[...drawers.values()].flatMap((d) => d.ids), ...seen.keys()]);
448
+ for (const id of ids) {
449
+ if (!defOf(id)) continue;
450
+ const at = where(id);
451
+ const prev = seen.get(id);
452
+ const el = at ? elementOf(id) : prev?.el;
453
+ const doc = at && el ? el.ownerDocument : prev?.doc;
454
+ seen.set(id, { at, el, doc });
455
+ if (!notify || !opts.moved || !el) continue;
456
+ if (key0(at) === key0(prev?.at ?? null) && doc === prev?.doc) continue;
457
+ opts.moved(id, el, at, prev?.at ?? null, { documentChanged: !!prev?.doc && doc !== prev.doc });
458
+ }
459
+ for (const [edge, d] of drawers) {
460
+ const now = `${d.open} ${d.active ?? ""}`;
461
+ const before = drawerSeen.get(edge) ?? "false ";
462
+ drawerSeen.set(edge, now);
463
+ if (notify && now !== before) opts.drawerChanged?.(edge, d.open, d.active);
464
+ }
465
+ };
466
+
467
+ const changed = (): void => {
468
+ save();
469
+ sync();
470
+ };
471
+
357
472
  const reset = (): void => {
358
473
  quiet++;
359
474
  try {
@@ -364,7 +479,7 @@ export function createDock(opts: DockOptions): Dock {
364
479
  } finally {
365
480
  quiet--;
366
481
  }
367
- save();
482
+ changed();
368
483
  };
369
484
 
370
485
  quiet++;
@@ -373,12 +488,12 @@ export function createDock(opts: DockOptions): Dock {
373
488
  } finally {
374
489
  quiet--;
375
490
  }
376
- if (storage) {
377
- // Save what restore built, so the save names today's registry and a rejected save is replaced.
378
- save();
379
- const sub = api.onDidLayoutChange(() => save());
380
- cleanups.push(() => sub.dispose());
381
- }
491
+ // Save what restore built, so the save names today's registry and a rejected save is replaced,
492
+ // and note where everything starts without calling moved for it.
493
+ save();
494
+ sync(false);
495
+ const sub = api.onDidLayoutChange(() => changed());
496
+ cleanups.push(() => sub.dispose());
382
497
 
383
498
  // ---- theme ----
384
499
 
package/src/drawer.ts CHANGED
@@ -1,5 +1,5 @@
1
- // The DOM-free half of a drawer: its size from a drag on any of the four edges, clamped, and saved.
2
- // The drawer element itself comes in a later release (#133).
1
+ // The DOM-free half of a drawer: its size from a drag on any of the four edges, clamped and saved,
2
+ // and whether letting go of that drag closes it. drawerView.ts is the element.
3
3
 
4
4
  /** The page edge a drawer is anchored to. */
5
5
  export type DrawerEdge = "left" | "right" | "top" | "bottom";
@@ -37,6 +37,31 @@ export function drawerSize(edge: DrawerEdge, pointer: number, viewport: number,
37
37
  return clampSize(fromEdge, viewport, bounds);
38
38
  }
39
39
 
40
+ /**
41
+ * Turns a configured or saved size into px against the viewport on the drawer's axis, clamped. A
42
+ * size up to 1 is a share of the viewport, so `0.65` makes a phone sheet that covers 65 percent of
43
+ * the screen whatever its height. A larger size is px.
44
+ */
45
+ export function drawerPixels(size: number, viewport: number, bounds?: DrawerBounds): number {
46
+ return clampSize(size <= 1 ? size * viewport : size, viewport, bounds);
47
+ }
48
+
49
+ /**
50
+ * How fast, in px per ms toward the anchored edge, a touch drag has to be moving when it's let go
51
+ * to close the drawer wherever it ends.
52
+ */
53
+ export const DRAWER_FLICK = 0.5;
54
+
55
+ /**
56
+ * Whether letting go of a drag on a drawer's inner edge closes the drawer. It closes when the edge
57
+ * ends nearer the anchored edge than half of `min`, or when `speed` (px per ms toward the anchored
58
+ * edge, negative away from it) is over `DRAWER_FLICK`. Pass a speed of 0 for a mouse, so a quick
59
+ * resize on a desktop doesn't close anything.
60
+ */
61
+ export function closesOnRelease(fromEdge: number, min: number, speed: number): boolean {
62
+ return fromEdge < min / 2 || speed > DRAWER_FLICK;
63
+ }
64
+
40
65
  /** The part of Web Storage drawer sizes need. A null storage means sizes aren't saved. */
41
66
  export interface DrawerStorage {
42
67
  getItem(key: string): string | null;
package/src/drawerView.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // placed in it as tabs, and dock.ts moves their elements in and out. Styles are inline and minimal
3
3
  // (position, size, the scrim), so a drawer works with no CSS; everything else hangs off the
4
4
  // dock-drawer-* classes for the app to style.
5
- import { clampSize, drawerSize, DRAWER_DEFAULT, type DrawerBounds, type DrawerEdge } from "./drawer.js";
5
+ import { closesOnRelease, drawerPixels, DRAWER_DEFAULT, DRAWER_MIN, type DrawerBounds, type DrawerEdge } from "./drawer.js";
6
6
 
7
7
  /** What one drawer saves: its panels in tab order, the active one, its size, and whether it's open. */
8
8
  export interface DrawerState {
@@ -14,8 +14,18 @@ export interface DrawerState {
14
14
 
15
15
  /** Per-edge drawer settings for `createDock`. */
16
16
  export interface DrawerConfig extends DrawerBounds {
17
- /** The size, in px, when nothing is saved. Default `DRAWER_DEFAULT`. */
17
+ /**
18
+ * The size when nothing is saved. Over 1 it's px. Up to 1 it's a share of the drawer host on
19
+ * the drawer's axis, so `0.65` makes a bottom sheet 65 percent of the dock's height. Default
20
+ * `DRAWER_DEFAULT`.
21
+ */
18
22
  size?: number;
23
+ /**
24
+ * Whether the drawer shows its tab strip. Default true. Turn it off when the app picks the panel
25
+ * with its own buttons, such as a phone page's bottom bar calling `dock.show(id)`. The panels are
26
+ * still tabs underneath, and the drawer still drags from its edge.
27
+ */
28
+ tabs?: boolean;
19
29
  }
20
30
 
21
31
  interface Tab {
@@ -30,6 +40,13 @@ const Z_SCRIM = "calc(var(--dv-overlay-z-index, 999) + 200)";
30
40
  const Z_DRAWER = "calc(var(--dv-overlay-z-index, 999) + 201)";
31
41
 
32
42
  const HORIZONTAL: Record<DrawerEdge, boolean> = { left: true, right: true, top: false, bottom: false };
43
+ // +1 when the anchored edge is at the far end of its axis (right, bottom), so a pointer moving
44
+ // toward it moves in the positive direction.
45
+ const TOWARD: Record<DrawerEdge, 1 | -1> = { left: -1, right: 1, top: -1, bottom: 1 };
46
+ // A tab-strip press becomes a drag once it has moved this far, so a tap still picks a tab.
47
+ const DRAG_SLOP = 6;
48
+ // Only the last move counts toward a flick, and only if it came this recently before the release.
49
+ const FLICK_WINDOW_MS = 100;
33
50
 
34
51
  export class DrawerView {
35
52
  readonly root: HTMLElement;
@@ -77,7 +94,7 @@ export class DrawerView {
77
94
  this.tabStrip = doc.createElement("div");
78
95
  this.tabStrip.className = "dock-drawer-tabs";
79
96
  this.tabStrip.setAttribute("role", "tablist");
80
- Object.assign(this.tabStrip.style, { display: "flex", gap: "2px", alignItems: "center", flex: "none" });
97
+ Object.assign(this.tabStrip.style, { display: config.tabs === false ? "none" : "flex", gap: "2px", alignItems: "center", flex: "none" });
81
98
 
82
99
  this.body = doc.createElement("div");
83
100
  this.body.className = "dock-drawer-body";
@@ -86,13 +103,21 @@ export class DrawerView {
86
103
  this.handle = doc.createElement("div");
87
104
  this.handle.className = "dock-drawer-handle";
88
105
  const inner = { left: "right", right: "left", top: "bottom", bottom: "top" }[edge];
106
+ // A finger needs more than the 6px a mouse gets.
107
+ const thick = doc.defaultView?.matchMedia?.("(pointer: coarse)").matches ? 24 : 6;
89
108
  Object.assign(this.handle.style, {
90
109
  position: "absolute",
91
- [inner]: "-3px",
92
- ...(HORIZONTAL[edge] ? { top: "0", bottom: "0", width: "6px", cursor: "col-resize" } : { left: "0", right: "0", height: "6px", cursor: "row-resize" }),
110
+ [inner]: `-${thick / 2}px`,
111
+ ...(HORIZONTAL[edge] ? { top: "0", bottom: "0", width: `${thick}px`, cursor: "col-resize" } : { left: "0", right: "0", height: `${thick}px`, cursor: "row-resize" }),
93
112
  touchAction: "none",
94
113
  });
95
- this.handle.addEventListener("pointerdown", (e) => this.drag(e));
114
+ this.handle.addEventListener("pointerdown", (e) => this.drag(e, 0));
115
+ // A bottom drawer's tab strip runs along its inner edge, so it drags like the handle, the way a
116
+ // phone sheet drags from its header.
117
+ if (edge === "bottom") {
118
+ this.tabStrip.style.touchAction = "none";
119
+ this.tabStrip.addEventListener("pointerdown", (e) => this.drag(e, DRAG_SLOP));
120
+ }
96
121
 
97
122
  this.root.append(this.tabStrip, this.body, this.handle);
98
123
  host.append(this.scrim, this.root);
@@ -111,10 +136,22 @@ export class DrawerView {
111
136
  return this.isOpen;
112
137
  }
113
138
 
139
+ /** The size as configured, saved or dragged: px, or a share of the host when it's up to 1. */
114
140
  get size(): number {
115
141
  return this.px;
116
142
  }
117
143
 
144
+ /** The size in px against the host as it is now, or unclamped while the host has no size. */
145
+ get pixels(): number {
146
+ const span = this.span();
147
+ return span > 0 ? drawerPixels(this.px, span, this.config) : this.px;
148
+ }
149
+
150
+ /** The element a panel in this drawer shows, or undefined if it isn't here. */
151
+ elementOf(id: string): HTMLElement | undefined {
152
+ return this.tabs.find((t) => t.id === id)?.el;
153
+ }
154
+
118
155
  has(id: string): boolean {
119
156
  return this.tabs.some((t) => t.id === id);
120
157
  }
@@ -188,8 +225,8 @@ export class DrawerView {
188
225
  if (notify) this.hooks.changed();
189
226
  }
190
227
 
191
- setSize(px: number, notify = true): void {
192
- this.px = Math.round(px);
228
+ setSize(size: number, notify = true): void {
229
+ this.px = size > 1 ? Math.round(size) : size;
193
230
  this.applySize();
194
231
  if (notify) this.hooks.changed();
195
232
  }
@@ -203,31 +240,92 @@ export class DrawerView {
203
240
  this.root.remove();
204
241
  }
205
242
 
243
+ private span(): number {
244
+ const rect = this.host.getBoundingClientRect();
245
+ return HORIZONTAL[this.edge] ? rect.width : rect.height;
246
+ }
247
+
206
248
  // Clamps against the host as it is now, so a drawer saved on a big window still fits a small one.
249
+ // A host with no size yet (not laid out) gets a share as a percentage and px unclamped.
207
250
  private applySize(): void {
208
- const rect = this.host.getBoundingClientRect();
209
- const span = HORIZONTAL[this.edge] ? rect.width : rect.height;
210
- const px = span > 0 ? clampSize(this.px, span, this.config) : this.px;
211
- this.root.style[HORIZONTAL[this.edge] ? "width" : "height"] = `${px}px`;
251
+ const span = this.span();
252
+ const css = span > 0 ? `${drawerPixels(this.px, span, this.config)}px` : this.px <= 1 ? `${this.px * 100}%` : `${this.px}px`;
253
+ this.root.style[HORIZONTAL[this.edge] ? "width" : "height"] = css;
212
254
  }
213
255
 
214
- private drag(e: PointerEvent): void {
215
- e.preventDefault();
256
+ // A drag on the inner edge. It resizes while the edge is above `min`. Below it the drawer slides
257
+ // toward its edge under the pointer, and letting go there, or flicking a finger toward the edge,
258
+ // closes it at the size it had before the drag. `slop` is how far the pointer moves before this
259
+ // counts as a drag, so a tap on a tab in the strip still picks the tab.
260
+ private drag(e: PointerEvent, slop: number): void {
261
+ if (e.button > 0) return;
262
+ const doc = this.host.ownerDocument;
263
+ const horizontal = HORIZONTAL[this.edge];
264
+ const toward = TOWARD[this.edge];
265
+ // Only a finger or a pen flicks. A synthetic or unknown pointer counts as a mouse.
266
+ const touch = e.pointerType === "touch" || e.pointerType === "pen";
267
+ const min = this.config.min ?? DRAWER_MIN;
268
+ const at = (ev: PointerEvent) => (horizontal ? ev.clientX : ev.clientY);
269
+ const start = at(e);
270
+ const before = this.px;
271
+ let dragging = false;
272
+ let fromEdge = Number.NaN;
273
+ let last = { pos: start, t: e.timeStamp };
274
+ let speed = 0;
275
+
276
+ const begin = (): void => {
277
+ dragging = true;
278
+ this.root.classList.add("dock-drawer-resizing");
279
+ };
280
+ if (slop === 0) {
281
+ e.preventDefault();
282
+ begin();
283
+ }
216
284
  const move = (ev: PointerEvent): void => {
285
+ const pos = at(ev);
286
+ if (!dragging) {
287
+ if (Math.abs(pos - start) < slop) return;
288
+ begin();
289
+ }
290
+ ev.preventDefault();
217
291
  const rect = this.host.getBoundingClientRect();
218
- const horizontal = HORIZONTAL[this.edge];
219
- const pointer = horizontal ? ev.clientX - rect.left : ev.clientY - rect.top;
220
292
  const span = horizontal ? rect.width : rect.height;
221
- this.setSize(drawerSize(this.edge, pointer, span, this.config), false);
293
+ const pointer = pos - (horizontal ? rect.left : rect.top);
294
+ fromEdge = toward > 0 ? span - pointer : pointer;
295
+ const dt = ev.timeStamp - last.t;
296
+ if (dt > 0) speed = (toward * (pos - last.pos)) / dt;
297
+ last = { pos, t: ev.timeStamp };
298
+ const px = drawerPixels(fromEdge, span, this.config);
299
+ this.setSize(px, false);
300
+ const slide = Math.max(0, px - fromEdge);
301
+ this.root.style.transform = slide > 0 ? `translate${horizontal ? "X" : "Y"}(${toward * slide}px)` : "";
222
302
  };
223
- const up = (): void => {
224
- this.handle.ownerDocument.removeEventListener("pointermove", move);
225
- this.handle.ownerDocument.removeEventListener("pointerup", up);
303
+ const end = (ev: PointerEvent): void => {
304
+ doc.removeEventListener("pointermove", move);
305
+ doc.removeEventListener("pointerup", end);
306
+ doc.removeEventListener("pointercancel", end);
307
+ if (!dragging) return;
226
308
  this.root.classList.remove("dock-drawer-resizing");
309
+ this.root.style.transform = "";
310
+ // The click that follows a drag on the tab strip would otherwise pick a tab.
311
+ if (slop > 0) {
312
+ const swallow = (c: Event): void => {
313
+ c.stopPropagation();
314
+ c.preventDefault();
315
+ };
316
+ this.tabStrip.addEventListener("click", swallow, { capture: true, once: true });
317
+ setTimeout(() => this.tabStrip.removeEventListener("click", swallow, { capture: true }), 0);
318
+ }
319
+ const fresh = ev.timeStamp - last.t <= FLICK_WINDOW_MS;
320
+ if (ev.type === "pointerup" && Number.isFinite(fromEdge) && closesOnRelease(fromEdge, min, touch && fresh ? speed : 0)) {
321
+ this.setSize(before, false);
322
+ this.setOpen(false);
323
+ return;
324
+ }
227
325
  this.hooks.changed();
228
326
  };
229
- this.root.classList.add("dock-drawer-resizing");
230
- this.handle.ownerDocument.addEventListener("pointermove", move);
231
- this.handle.ownerDocument.addEventListener("pointerup", up);
327
+ doc.addEventListener("pointermove", move);
328
+ doc.addEventListener("pointerup", end);
329
+ doc.addEventListener("pointercancel", end);
232
330
  }
233
331
  }
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { createDock, type Dock, type DockOptions } from "./dock.js";
1
+ export { createDock, type Dock, type DockOptions, type MoveInfo, type PanelLocation } from "./dock.js";
2
2
  export {
3
3
  documentTheme,
4
4
  loadLayout,
@@ -18,10 +18,13 @@ export {
18
18
  export type { DrawerConfig } from "./drawerView.js";
19
19
  export {
20
20
  clampSize,
21
+ closesOnRelease,
22
+ drawerPixels,
21
23
  drawerSize,
22
24
  loadDrawerSize,
23
25
  saveDrawerSize,
24
26
  DRAWER_DEFAULT,
27
+ DRAWER_FLICK,
25
28
  DRAWER_MAX_FRAC,
26
29
  DRAWER_MIN,
27
30
  type DrawerBounds,