@panyam/tsappkit-dock 0.7.10 → 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/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,17 @@ 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
+
118
150
  /** The element a panel in this drawer shows, or undefined if it isn't here. */
119
151
  elementOf(id: string): HTMLElement | undefined {
120
152
  return this.tabs.find((t) => t.id === id)?.el;
@@ -193,8 +225,8 @@ export class DrawerView {
193
225
  if (notify) this.hooks.changed();
194
226
  }
195
227
 
196
- setSize(px: number, notify = true): void {
197
- this.px = Math.round(px);
228
+ setSize(size: number, notify = true): void {
229
+ this.px = size > 1 ? Math.round(size) : size;
198
230
  this.applySize();
199
231
  if (notify) this.hooks.changed();
200
232
  }
@@ -208,31 +240,92 @@ export class DrawerView {
208
240
  this.root.remove();
209
241
  }
210
242
 
243
+ private span(): number {
244
+ const rect = this.host.getBoundingClientRect();
245
+ return HORIZONTAL[this.edge] ? rect.width : rect.height;
246
+ }
247
+
211
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.
212
250
  private applySize(): void {
213
- const rect = this.host.getBoundingClientRect();
214
- const span = HORIZONTAL[this.edge] ? rect.width : rect.height;
215
- const px = span > 0 ? clampSize(this.px, span, this.config) : this.px;
216
- 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;
217
254
  }
218
255
 
219
- private drag(e: PointerEvent): void {
220
- 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
+ }
221
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();
222
291
  const rect = this.host.getBoundingClientRect();
223
- const horizontal = HORIZONTAL[this.edge];
224
- const pointer = horizontal ? ev.clientX - rect.left : ev.clientY - rect.top;
225
292
  const span = horizontal ? rect.width : rect.height;
226
- 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)` : "";
227
302
  };
228
- const up = (): void => {
229
- this.handle.ownerDocument.removeEventListener("pointermove", move);
230
- 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;
231
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
+ }
232
325
  this.hooks.changed();
233
326
  };
234
- this.root.classList.add("dock-drawer-resizing");
235
- this.handle.ownerDocument.addEventListener("pointermove", move);
236
- this.handle.ownerDocument.addEventListener("pointerup", up);
327
+ doc.addEventListener("pointermove", move);
328
+ doc.addEventListener("pointerup", end);
329
+ doc.addEventListener("pointercancel", end);
237
330
  }
238
331
  }
package/src/index.ts CHANGED
@@ -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,