@panyam/tsappkit-dock 0.7.10 → 0.7.12

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,15 @@ 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
+ }
100
121
  /** The element a panel in this drawer shows, or undefined if it isn't here. */
101
122
  elementOf(id) {
102
123
  return this.tabs.find((t) => t.id === id)?.el;
@@ -169,8 +190,8 @@ var DrawerView = class {
169
190
  if (show) this.applySize();
170
191
  if (notify) this.hooks.changed();
171
192
  }
172
- setSize(px, notify = true) {
173
- this.px = Math.round(px);
193
+ setSize(size, notify = true) {
194
+ this.px = size > 1 ? Math.round(size) : size;
174
195
  this.applySize();
175
196
  if (notify) this.hooks.changed();
176
197
  }
@@ -181,31 +202,88 @@ var DrawerView = class {
181
202
  this.scrim.remove();
182
203
  this.root.remove();
183
204
  }
205
+ span() {
206
+ const rect = this.host.getBoundingClientRect();
207
+ return HORIZONTAL[this.edge] ? rect.width : rect.height;
208
+ }
184
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.
185
211
  applySize() {
186
- const rect = this.host.getBoundingClientRect();
187
- const span = HORIZONTAL[this.edge] ? rect.width : rect.height;
188
- const px = span > 0 ? clampSize(this.px, span, this.config) : this.px;
189
- 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;
190
215
  }
191
- drag(e) {
192
- 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
+ }
193
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();
194
249
  const rect = this.host.getBoundingClientRect();
195
- const horizontal = HORIZONTAL[this.edge];
196
- const pointer = horizontal ? ev.clientX - rect.left : ev.clientY - rect.top;
197
250
  const span = horizontal ? rect.width : rect.height;
198
- 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)` : "";
199
260
  };
200
- const up = () => {
201
- this.handle.ownerDocument.removeEventListener("pointermove", move);
202
- 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;
203
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
+ }
204
282
  this.hooks.changed();
205
283
  };
206
- this.root.classList.add("dock-drawer-resizing");
207
- this.handle.ownerDocument.addEventListener("pointermove", move);
208
- this.handle.ownerDocument.addEventListener("pointerup", up);
284
+ doc.addEventListener("pointermove", move);
285
+ doc.addEventListener("pointerup", end);
286
+ doc.addEventListener("pointercancel", end);
209
287
  }
210
288
  };
211
289
 
@@ -389,7 +467,7 @@ function createDock(opts) {
389
467
  const now = where(id);
390
468
  if (!same(now, target)) {
391
469
  if (now?.kind === "drawer" && target.kind === "grid") {
392
- const size = drawer(now.edge).size;
470
+ const size = drawer(now.edge).pixels;
393
471
  leave(id);
394
472
  addToGrid(id, { direction: TOWARD_EDGE[now.edge] });
395
473
  const horizontal = now.edge === "left" || now.edge === "right";
@@ -453,7 +531,7 @@ function createDock(opts) {
453
531
  if (def && !where(id)) d.add(id, def.title, hostFor(id));
454
532
  }
455
533
  if (s.active) d.activate(s.active, false);
456
- if (s.open) d.setOpen(true, false);
534
+ if (s.open && opts.drawers?.[edge]?.restoreOpen) d.setOpen(true, false);
457
535
  }
458
536
  };
459
537
  const restore = () => {
@@ -478,11 +556,12 @@ function createDock(opts) {
478
556
  }
479
557
  };
480
558
  const save = () => {
481
- if (quiet || disposed || !storage) return;
559
+ if (quiet || disposed || !storage || !laidOut) return;
482
560
  saveLayout(storage, key, panels, api.toJSON(), drawerStates());
483
561
  };
484
562
  const seen = /* @__PURE__ */ new Map();
485
563
  const key0 = (p) => JSON.stringify(p);
564
+ const drawerSeen = /* @__PURE__ */ new Map();
486
565
  const sync = (notify = true) => {
487
566
  if (disposed || quiet) return;
488
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()]);
@@ -497,6 +576,12 @@ function createDock(opts) {
497
576
  if (key0(at) === key0(prev?.at ?? null) && doc2 === prev?.doc) continue;
498
577
  opts.moved(id, el, at, prev?.at ?? null, { documentChanged: !!prev?.doc && doc2 !== prev.doc });
499
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
+ }
500
585
  };
501
586
  const changed = () => {
502
587
  save();
@@ -514,6 +599,24 @@ function createDock(opts) {
514
599
  }
515
600
  changed();
516
601
  };
602
+ const layOut = () => {
603
+ const w = container.clientWidth;
604
+ const h = container.clientHeight;
605
+ if (w <= 0 || h <= 0) return false;
606
+ api.layout(w, h);
607
+ return true;
608
+ };
609
+ let laidOut = layOut();
610
+ if (!laidOut) {
611
+ const ro = new ResizeObserver(() => {
612
+ if (disposed || !layOut()) return;
613
+ laidOut = true;
614
+ ro.disconnect();
615
+ save();
616
+ });
617
+ ro.observe(container);
618
+ cleanups.push(() => ro.disconnect());
619
+ }
517
620
  quiet++;
518
621
  try {
519
622
  restore();
@@ -696,6 +799,6 @@ function createDock(opts) {
696
799
  };
697
800
  }
698
801
 
699
- export { DRAWER_DEFAULT, DRAWER_MAX_FRAC, DRAWER_MIN, clampSize, createDock, documentTheme, drawerSize, loadDrawerSize, loadLayout, panelsToPrune, panelsToReconcile, resolveTheme, saveDrawerSize, saveLayout };
802
+ export { DRAWER_DEFAULT, DRAWER_FLICK, DRAWER_MAX_FRAC, DRAWER_MIN, clampSize, closesOnRelease, createDock, documentTheme, drawerPixels, drawerSize, loadDrawerSize, loadLayout, panelsToPrune, panelsToReconcile, resolveTheme, saveDrawerSize, saveLayout };
700
803
  //# sourceMappingURL=index.mjs.map
701
804
  //# sourceMappingURL=index.mjs.map