@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/dist/index.mjs CHANGED
@@ -15,6 +15,13 @@ function drawerSize(edge, pointer, viewport, bounds) {
15
15
  const fromEdge = edge === "left" || edge === "top" ? pointer : viewport - pointer;
16
16
  return clampSize(fromEdge, viewport, bounds);
17
17
  }
18
+ function drawerPixels(size, viewport, bounds) {
19
+ return clampSize(size <= 1 ? size * viewport : size, viewport, bounds);
20
+ }
21
+ var DRAWER_FLICK = 0.5;
22
+ function closesOnRelease(fromEdge, min, speed) {
23
+ return fromEdge < min / 2 || speed > DRAWER_FLICK;
24
+ }
18
25
  function loadDrawerSize(storage, key, fallback = DRAWER_DEFAULT) {
19
26
  let raw = null;
20
27
  try {
@@ -36,6 +43,9 @@ function saveDrawerSize(storage, key, px) {
36
43
  var Z_SCRIM = "calc(var(--dv-overlay-z-index, 999) + 200)";
37
44
  var Z_DRAWER = "calc(var(--dv-overlay-z-index, 999) + 201)";
38
45
  var HORIZONTAL = { left: true, right: true, top: false, bottom: false };
46
+ var TOWARD = { left: -1, right: 1, top: -1, bottom: 1 };
47
+ var DRAG_SLOP = 6;
48
+ var FLICK_WINDOW_MS = 100;
39
49
  var DrawerView = class {
40
50
  constructor(edge, host, config, hooks) {
41
51
  this.edge = edge;
@@ -67,20 +77,25 @@ var DrawerView = class {
67
77
  this.tabStrip = doc.createElement("div");
68
78
  this.tabStrip.className = "dock-drawer-tabs";
69
79
  this.tabStrip.setAttribute("role", "tablist");
70
- Object.assign(this.tabStrip.style, { display: "flex", gap: "2px", alignItems: "center", flex: "none" });
80
+ Object.assign(this.tabStrip.style, { display: config.tabs === false ? "none" : "flex", gap: "2px", alignItems: "center", flex: "none" });
71
81
  this.body = doc.createElement("div");
72
82
  this.body.className = "dock-drawer-body";
73
83
  Object.assign(this.body.style, { flex: "1", minHeight: "0", overflow: "auto", position: "relative" });
74
84
  this.handle = doc.createElement("div");
75
85
  this.handle.className = "dock-drawer-handle";
76
86
  const inner = { left: "right", right: "left", top: "bottom", bottom: "top" }[edge];
87
+ const thick = doc.defaultView?.matchMedia?.("(pointer: coarse)").matches ? 24 : 6;
77
88
  Object.assign(this.handle.style, {
78
89
  position: "absolute",
79
- [inner]: "-3px",
80
- ...HORIZONTAL[edge] ? { top: "0", bottom: "0", width: "6px", cursor: "col-resize" } : { left: "0", right: "0", height: "6px", cursor: "row-resize" },
90
+ [inner]: `-${thick / 2}px`,
91
+ ...HORIZONTAL[edge] ? { top: "0", bottom: "0", width: `${thick}px`, cursor: "col-resize" } : { left: "0", right: "0", height: `${thick}px`, cursor: "row-resize" },
81
92
  touchAction: "none"
82
93
  });
83
- this.handle.addEventListener("pointerdown", (e) => this.drag(e));
94
+ this.handle.addEventListener("pointerdown", (e) => this.drag(e, 0));
95
+ if (edge === "bottom") {
96
+ this.tabStrip.style.touchAction = "none";
97
+ this.tabStrip.addEventListener("pointerdown", (e) => this.drag(e, DRAG_SLOP));
98
+ }
84
99
  this.root.append(this.tabStrip, this.body, this.handle);
85
100
  host.append(this.scrim, this.root);
86
101
  this.applySize();
@@ -94,9 +109,19 @@ var DrawerView = class {
94
109
  get open() {
95
110
  return this.isOpen;
96
111
  }
112
+ /** The size as configured, saved or dragged: px, or a share of the host when it's up to 1. */
97
113
  get size() {
98
114
  return this.px;
99
115
  }
116
+ /** The size in px against the host as it is now, or unclamped while the host has no size. */
117
+ get pixels() {
118
+ const span = this.span();
119
+ return span > 0 ? drawerPixels(this.px, span, this.config) : this.px;
120
+ }
121
+ /** The element a panel in this drawer shows, or undefined if it isn't here. */
122
+ elementOf(id) {
123
+ return this.tabs.find((t) => t.id === id)?.el;
124
+ }
100
125
  has(id) {
101
126
  return this.tabs.some((t) => t.id === id);
102
127
  }
@@ -165,8 +190,8 @@ var DrawerView = class {
165
190
  if (show) this.applySize();
166
191
  if (notify) this.hooks.changed();
167
192
  }
168
- setSize(px, notify = true) {
169
- this.px = Math.round(px);
193
+ setSize(size, notify = true) {
194
+ this.px = size > 1 ? Math.round(size) : size;
170
195
  this.applySize();
171
196
  if (notify) this.hooks.changed();
172
197
  }
@@ -177,31 +202,88 @@ var DrawerView = class {
177
202
  this.scrim.remove();
178
203
  this.root.remove();
179
204
  }
205
+ span() {
206
+ const rect = this.host.getBoundingClientRect();
207
+ return HORIZONTAL[this.edge] ? rect.width : rect.height;
208
+ }
180
209
  // Clamps against the host as it is now, so a drawer saved on a big window still fits a small one.
210
+ // A host with no size yet (not laid out) gets a share as a percentage and px unclamped.
181
211
  applySize() {
182
- const rect = this.host.getBoundingClientRect();
183
- const span = HORIZONTAL[this.edge] ? rect.width : rect.height;
184
- const px = span > 0 ? clampSize(this.px, span, this.config) : this.px;
185
- this.root.style[HORIZONTAL[this.edge] ? "width" : "height"] = `${px}px`;
212
+ const span = this.span();
213
+ const css = span > 0 ? `${drawerPixels(this.px, span, this.config)}px` : this.px <= 1 ? `${this.px * 100}%` : `${this.px}px`;
214
+ this.root.style[HORIZONTAL[this.edge] ? "width" : "height"] = css;
186
215
  }
187
- drag(e) {
188
- e.preventDefault();
216
+ // A drag on the inner edge. It resizes while the edge is above `min`. Below it the drawer slides
217
+ // toward its edge under the pointer, and letting go there, or flicking a finger toward the edge,
218
+ // closes it at the size it had before the drag. `slop` is how far the pointer moves before this
219
+ // counts as a drag, so a tap on a tab in the strip still picks the tab.
220
+ drag(e, slop) {
221
+ if (e.button > 0) return;
222
+ const doc = this.host.ownerDocument;
223
+ const horizontal = HORIZONTAL[this.edge];
224
+ const toward = TOWARD[this.edge];
225
+ const touch = e.pointerType === "touch" || e.pointerType === "pen";
226
+ const min = this.config.min ?? DRAWER_MIN;
227
+ const at = (ev) => horizontal ? ev.clientX : ev.clientY;
228
+ const start = at(e);
229
+ const before = this.px;
230
+ let dragging = false;
231
+ let fromEdge = Number.NaN;
232
+ let last = { pos: start, t: e.timeStamp };
233
+ let speed = 0;
234
+ const begin = () => {
235
+ dragging = true;
236
+ this.root.classList.add("dock-drawer-resizing");
237
+ };
238
+ if (slop === 0) {
239
+ e.preventDefault();
240
+ begin();
241
+ }
189
242
  const move = (ev) => {
243
+ const pos = at(ev);
244
+ if (!dragging) {
245
+ if (Math.abs(pos - start) < slop) return;
246
+ begin();
247
+ }
248
+ ev.preventDefault();
190
249
  const rect = this.host.getBoundingClientRect();
191
- const horizontal = HORIZONTAL[this.edge];
192
- const pointer = horizontal ? ev.clientX - rect.left : ev.clientY - rect.top;
193
250
  const span = horizontal ? rect.width : rect.height;
194
- this.setSize(drawerSize(this.edge, pointer, span, this.config), false);
251
+ const pointer = pos - (horizontal ? rect.left : rect.top);
252
+ fromEdge = toward > 0 ? span - pointer : pointer;
253
+ const dt = ev.timeStamp - last.t;
254
+ if (dt > 0) speed = toward * (pos - last.pos) / dt;
255
+ last = { pos, t: ev.timeStamp };
256
+ const px = drawerPixels(fromEdge, span, this.config);
257
+ this.setSize(px, false);
258
+ const slide = Math.max(0, px - fromEdge);
259
+ this.root.style.transform = slide > 0 ? `translate${horizontal ? "X" : "Y"}(${toward * slide}px)` : "";
195
260
  };
196
- const up = () => {
197
- this.handle.ownerDocument.removeEventListener("pointermove", move);
198
- this.handle.ownerDocument.removeEventListener("pointerup", up);
261
+ const end = (ev) => {
262
+ doc.removeEventListener("pointermove", move);
263
+ doc.removeEventListener("pointerup", end);
264
+ doc.removeEventListener("pointercancel", end);
265
+ if (!dragging) return;
199
266
  this.root.classList.remove("dock-drawer-resizing");
267
+ this.root.style.transform = "";
268
+ if (slop > 0) {
269
+ const swallow = (c) => {
270
+ c.stopPropagation();
271
+ c.preventDefault();
272
+ };
273
+ this.tabStrip.addEventListener("click", swallow, { capture: true, once: true });
274
+ setTimeout(() => this.tabStrip.removeEventListener("click", swallow, { capture: true }), 0);
275
+ }
276
+ const fresh = ev.timeStamp - last.t <= FLICK_WINDOW_MS;
277
+ if (ev.type === "pointerup" && Number.isFinite(fromEdge) && closesOnRelease(fromEdge, min, touch && fresh ? speed : 0)) {
278
+ this.setSize(before, false);
279
+ this.setOpen(false);
280
+ return;
281
+ }
200
282
  this.hooks.changed();
201
283
  };
202
- this.root.classList.add("dock-drawer-resizing");
203
- this.handle.ownerDocument.addEventListener("pointermove", move);
204
- this.handle.ownerDocument.addEventListener("pointerup", up);
284
+ doc.addEventListener("pointermove", move);
285
+ doc.addEventListener("pointerup", end);
286
+ doc.addEventListener("pointercancel", end);
205
287
  }
206
288
  };
207
289
 
@@ -254,7 +336,8 @@ function documentTheme(doc = document) {
254
336
 
255
337
  // src/dock.ts
256
338
  var EDGES = ["left", "right", "top", "bottom"];
257
- var placementLabel = (p) => !p || p.kind === "grid" ? "" : p.kind === "float" ? " \xB7 float" : ` \xB7 ${p.edge} drawer`;
339
+ var placementLabel = (p) => !p || p.kind === "grid" ? "" : p.kind === "drawer" ? ` \xB7 ${p.edge} drawer` : ` \xB7 ${p.kind}`;
340
+ var TOWARD_EDGE = { left: "left", right: "right", top: "above", bottom: "below" };
258
341
  function createDock(opts) {
259
342
  const { container, panels, hostFor, release } = opts;
260
343
  const storage = opts.storage ?? null;
@@ -299,14 +382,16 @@ function createDock(opts) {
299
382
  if (d) return { kind: "drawer", edge: d.edge };
300
383
  const panel = api.getPanel(id);
301
384
  if (!panel) return null;
302
- return panel.group.api.location.type === "floating" ? { kind: "float" } : { kind: "grid" };
385
+ const type = panel.group.api.location.type;
386
+ return type === "floating" ? { kind: "float" } : type === "popout" ? { kind: "popout" } : { kind: "grid" };
303
387
  };
388
+ const elementOf = (id) => drawerWith(id)?.elementOf(id) ?? api.getPanel(id)?.view.content.element;
304
389
  const same = (a, b) => !!a && a.kind === b.kind && (a.kind !== "drawer" || b.kind === "drawer" && a.edge === b.edge);
305
390
  const drawer = (edge) => {
306
391
  let d = drawers.get(edge);
307
392
  if (!d) {
308
393
  if (doc.defaultView?.getComputedStyle(drawerHost).position === "static") drawerHost.style.position = "relative";
309
- d = new DrawerView(edge, drawerHost, opts.drawers?.[edge] ?? {}, { changed: () => save(), menu: (anchor, id) => moveMenu(anchor, id) });
394
+ d = new DrawerView(edge, drawerHost, opts.drawers?.[edge] ?? {}, { changed: () => changed(), menu: (anchor, id) => moveMenu(anchor, id) });
310
395
  drawers.set(edge, d);
311
396
  }
312
397
  return d;
@@ -317,11 +402,24 @@ function createDock(opts) {
317
402
  return out;
318
403
  };
319
404
  const usable = (p) => !!p && (!p.referencePanel || !!api.getPanel(p.referencePanel));
320
- const addToGrid = (id) => {
405
+ const inGrid = (p) => !!p && (!p.referencePanel || api.getPanel(p.referencePanel)?.group.api.location.type === "grid" && api.getPanel(p.referencePanel).group.api.isVisible);
406
+ const addToGrid = (id, prefer) => {
321
407
  const def = defOf(id);
322
408
  const spec = specs?.find((s) => s.id === id);
323
- const position = [def.position, spec?.position].find(usable);
324
- api.addPanel({ id, component: id, title: def.title, ...position ? { position } : {} });
409
+ const position = [prefer, def.position, spec?.position].find(inGrid);
410
+ if (position) {
411
+ api.addPanel({ id, component: id, title: def.title, position });
412
+ return;
413
+ }
414
+ const showing = (g) => !!g && g.api.location.type === "grid" && g.api.isVisible;
415
+ const active = api.activeGroup;
416
+ const group = showing(active) ? active : api.groups.find(showing);
417
+ api.addPanel({
418
+ id,
419
+ component: id,
420
+ title: def.title,
421
+ position: group ? { referenceGroup: group, direction: "within" } : { direction: "right" }
422
+ });
325
423
  };
326
424
  const enter = (id, target) => {
327
425
  if (target.kind === "drawer") {
@@ -351,33 +449,50 @@ function createDock(opts) {
351
449
  }
352
450
  api.getPanel(id)?.api.setActive();
353
451
  };
452
+ const batch = (fn) => {
453
+ quiet++;
454
+ try {
455
+ fn();
456
+ } finally {
457
+ quiet--;
458
+ }
459
+ changed();
460
+ };
354
461
  const place = (id, target) => {
355
462
  if (!defOf(id)) return false;
463
+ batch(() => placeNow(id, target));
464
+ return true;
465
+ };
466
+ const placeNow = (id, target) => {
356
467
  const now = where(id);
357
468
  if (!same(now, target)) {
358
- if (!(now?.kind === "grid" && target.kind === "float")) leave(id);
359
- enter(id, target);
469
+ if (now?.kind === "drawer" && target.kind === "grid") {
470
+ const size = drawer(now.edge).pixels;
471
+ leave(id);
472
+ addToGrid(id, { direction: TOWARD_EDGE[now.edge] });
473
+ const horizontal = now.edge === "left" || now.edge === "right";
474
+ api.getPanel(id)?.api.setSize(horizontal ? { width: size } : { height: size });
475
+ } else {
476
+ if (!(now?.kind === "grid" && target.kind === "float")) leave(id);
477
+ enter(id, target);
478
+ }
360
479
  }
361
480
  reveal(id);
362
- save();
363
- return true;
364
481
  };
365
482
  const show = (id) => {
366
483
  const def = defOf(id);
367
484
  if (!def) return false;
368
- if (!where(id)) enter(id, def.defaultPlacement ?? { kind: "grid" });
369
- reveal(id);
370
- save();
485
+ batch(() => {
486
+ if (!where(id)) enter(id, def.defaultPlacement ?? { kind: "grid" });
487
+ reveal(id);
488
+ });
371
489
  return true;
372
490
  };
373
491
  const toggle = (id) => {
374
492
  if (!defOf(id)) return false;
375
- if (where(id)) {
376
- leave(id);
377
- save();
378
- return true;
379
- }
380
- return show(id);
493
+ if (!where(id)) return show(id);
494
+ batch(() => leave(id));
495
+ return true;
381
496
  };
382
497
  const applyDefault = () => {
383
498
  const elsewhere = (def) => !!def?.defaultPlacement && def.defaultPlacement.kind === "drawer";
@@ -444,6 +559,34 @@ function createDock(opts) {
444
559
  if (quiet || disposed || !storage) return;
445
560
  saveLayout(storage, key, panels, api.toJSON(), drawerStates());
446
561
  };
562
+ const seen = /* @__PURE__ */ new Map();
563
+ const key0 = (p) => JSON.stringify(p);
564
+ const drawerSeen = /* @__PURE__ */ new Map();
565
+ const sync = (notify = true) => {
566
+ if (disposed || quiet) return;
567
+ const ids = /* @__PURE__ */ new Set([...panels.map((p) => p.id), ...api.panels.map((p) => p.id), ...[...drawers.values()].flatMap((d) => d.ids), ...seen.keys()]);
568
+ for (const id of ids) {
569
+ if (!defOf(id)) continue;
570
+ const at = where(id);
571
+ const prev = seen.get(id);
572
+ const el = at ? elementOf(id) : prev?.el;
573
+ const doc2 = at && el ? el.ownerDocument : prev?.doc;
574
+ seen.set(id, { at, el, doc: doc2 });
575
+ if (!notify || !opts.moved || !el) continue;
576
+ if (key0(at) === key0(prev?.at ?? null) && doc2 === prev?.doc) continue;
577
+ opts.moved(id, el, at, prev?.at ?? null, { documentChanged: !!prev?.doc && doc2 !== prev.doc });
578
+ }
579
+ for (const [edge, d] of drawers) {
580
+ const now = `${d.open} ${d.active ?? ""}`;
581
+ const before = drawerSeen.get(edge) ?? "false ";
582
+ drawerSeen.set(edge, now);
583
+ if (notify && now !== before) opts.drawerChanged?.(edge, d.open, d.active);
584
+ }
585
+ };
586
+ const changed = () => {
587
+ save();
588
+ sync();
589
+ };
447
590
  const reset = () => {
448
591
  quiet++;
449
592
  try {
@@ -454,7 +597,7 @@ function createDock(opts) {
454
597
  } finally {
455
598
  quiet--;
456
599
  }
457
- save();
600
+ changed();
458
601
  };
459
602
  quiet++;
460
603
  try {
@@ -462,11 +605,10 @@ function createDock(opts) {
462
605
  } finally {
463
606
  quiet--;
464
607
  }
465
- if (storage) {
466
- save();
467
- const sub = api.onDidLayoutChange(() => save());
468
- cleanups.push(() => sub.dispose());
469
- }
608
+ save();
609
+ sync(false);
610
+ const sub = api.onDidLayoutChange(() => changed());
611
+ cleanups.push(() => sub.dispose());
470
612
  const applyTheme = () => component.updateOptions({ theme: theme() === "dark" ? themeDark : themeLight });
471
613
  const view = doc.defaultView;
472
614
  if (view?.MutationObserver) {
@@ -639,6 +781,6 @@ function createDock(opts) {
639
781
  };
640
782
  }
641
783
 
642
- export { DRAWER_DEFAULT, DRAWER_MAX_FRAC, DRAWER_MIN, clampSize, createDock, documentTheme, drawerSize, loadDrawerSize, loadLayout, panelsToPrune, panelsToReconcile, resolveTheme, saveDrawerSize, saveLayout };
784
+ export { DRAWER_DEFAULT, DRAWER_FLICK, DRAWER_MAX_FRAC, DRAWER_MIN, clampSize, closesOnRelease, createDock, documentTheme, drawerPixels, drawerSize, loadDrawerSize, loadLayout, panelsToPrune, panelsToReconcile, resolveTheme, saveDrawerSize, saveLayout };
643
785
  //# sourceMappingURL=index.mjs.map
644
786
  //# sourceMappingURL=index.mjs.map