@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/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,24 @@ 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;
29
+ /**
30
+ * Whether a drawer saved open opens again on the next load. Default false: it comes back closed,
31
+ * with its panels, active tab and size, since a reader opens a drawer to visit it and an open one
32
+ * puts its scrim over the page. Set it for a drawer the page treats as part of its layout.
33
+ */
34
+ restoreOpen?: boolean;
19
35
  }
20
36
 
21
37
  interface Tab {
@@ -30,6 +46,13 @@ const Z_SCRIM = "calc(var(--dv-overlay-z-index, 999) + 200)";
30
46
  const Z_DRAWER = "calc(var(--dv-overlay-z-index, 999) + 201)";
31
47
 
32
48
  const HORIZONTAL: Record<DrawerEdge, boolean> = { left: true, right: true, top: false, bottom: false };
49
+ // +1 when the anchored edge is at the far end of its axis (right, bottom), so a pointer moving
50
+ // toward it moves in the positive direction.
51
+ const TOWARD: Record<DrawerEdge, 1 | -1> = { left: -1, right: 1, top: -1, bottom: 1 };
52
+ // A tab-strip press becomes a drag once it has moved this far, so a tap still picks a tab.
53
+ const DRAG_SLOP = 6;
54
+ // Only the last move counts toward a flick, and only if it came this recently before the release.
55
+ const FLICK_WINDOW_MS = 100;
33
56
 
34
57
  export class DrawerView {
35
58
  readonly root: HTMLElement;
@@ -77,7 +100,7 @@ export class DrawerView {
77
100
  this.tabStrip = doc.createElement("div");
78
101
  this.tabStrip.className = "dock-drawer-tabs";
79
102
  this.tabStrip.setAttribute("role", "tablist");
80
- Object.assign(this.tabStrip.style, { display: "flex", gap: "2px", alignItems: "center", flex: "none" });
103
+ Object.assign(this.tabStrip.style, { display: config.tabs === false ? "none" : "flex", gap: "2px", alignItems: "center", flex: "none" });
81
104
 
82
105
  this.body = doc.createElement("div");
83
106
  this.body.className = "dock-drawer-body";
@@ -86,13 +109,21 @@ export class DrawerView {
86
109
  this.handle = doc.createElement("div");
87
110
  this.handle.className = "dock-drawer-handle";
88
111
  const inner = { left: "right", right: "left", top: "bottom", bottom: "top" }[edge];
112
+ // A finger needs more than the 6px a mouse gets.
113
+ const thick = doc.defaultView?.matchMedia?.("(pointer: coarse)").matches ? 24 : 6;
89
114
  Object.assign(this.handle.style, {
90
115
  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" }),
116
+ [inner]: `-${thick / 2}px`,
117
+ ...(HORIZONTAL[edge] ? { top: "0", bottom: "0", width: `${thick}px`, cursor: "col-resize" } : { left: "0", right: "0", height: `${thick}px`, cursor: "row-resize" }),
93
118
  touchAction: "none",
94
119
  });
95
- this.handle.addEventListener("pointerdown", (e) => this.drag(e));
120
+ this.handle.addEventListener("pointerdown", (e) => this.drag(e, 0));
121
+ // A bottom drawer's tab strip runs along its inner edge, so it drags like the handle, the way a
122
+ // phone sheet drags from its header.
123
+ if (edge === "bottom") {
124
+ this.tabStrip.style.touchAction = "none";
125
+ this.tabStrip.addEventListener("pointerdown", (e) => this.drag(e, DRAG_SLOP));
126
+ }
96
127
 
97
128
  this.root.append(this.tabStrip, this.body, this.handle);
98
129
  host.append(this.scrim, this.root);
@@ -111,10 +142,17 @@ export class DrawerView {
111
142
  return this.isOpen;
112
143
  }
113
144
 
145
+ /** The size as configured, saved or dragged: px, or a share of the host when it's up to 1. */
114
146
  get size(): number {
115
147
  return this.px;
116
148
  }
117
149
 
150
+ /** The size in px against the host as it is now, or unclamped while the host has no size. */
151
+ get pixels(): number {
152
+ const span = this.span();
153
+ return span > 0 ? drawerPixels(this.px, span, this.config) : this.px;
154
+ }
155
+
118
156
  /** The element a panel in this drawer shows, or undefined if it isn't here. */
119
157
  elementOf(id: string): HTMLElement | undefined {
120
158
  return this.tabs.find((t) => t.id === id)?.el;
@@ -193,8 +231,8 @@ export class DrawerView {
193
231
  if (notify) this.hooks.changed();
194
232
  }
195
233
 
196
- setSize(px: number, notify = true): void {
197
- this.px = Math.round(px);
234
+ setSize(size: number, notify = true): void {
235
+ this.px = size > 1 ? Math.round(size) : size;
198
236
  this.applySize();
199
237
  if (notify) this.hooks.changed();
200
238
  }
@@ -208,31 +246,92 @@ export class DrawerView {
208
246
  this.root.remove();
209
247
  }
210
248
 
249
+ private span(): number {
250
+ const rect = this.host.getBoundingClientRect();
251
+ return HORIZONTAL[this.edge] ? rect.width : rect.height;
252
+ }
253
+
211
254
  // Clamps against the host as it is now, so a drawer saved on a big window still fits a small one.
255
+ // A host with no size yet (not laid out) gets a share as a percentage and px unclamped.
212
256
  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`;
257
+ const span = this.span();
258
+ const css = span > 0 ? `${drawerPixels(this.px, span, this.config)}px` : this.px <= 1 ? `${this.px * 100}%` : `${this.px}px`;
259
+ this.root.style[HORIZONTAL[this.edge] ? "width" : "height"] = css;
217
260
  }
218
261
 
219
- private drag(e: PointerEvent): void {
220
- e.preventDefault();
262
+ // A drag on the inner edge. It resizes while the edge is above `min`. Below it the drawer slides
263
+ // toward its edge under the pointer, and letting go there, or flicking a finger toward the edge,
264
+ // closes it at the size it had before the drag. `slop` is how far the pointer moves before this
265
+ // counts as a drag, so a tap on a tab in the strip still picks the tab.
266
+ private drag(e: PointerEvent, slop: number): void {
267
+ if (e.button > 0) return;
268
+ const doc = this.host.ownerDocument;
269
+ const horizontal = HORIZONTAL[this.edge];
270
+ const toward = TOWARD[this.edge];
271
+ // Only a finger or a pen flicks. A synthetic or unknown pointer counts as a mouse.
272
+ const touch = e.pointerType === "touch" || e.pointerType === "pen";
273
+ const min = this.config.min ?? DRAWER_MIN;
274
+ const at = (ev: PointerEvent) => (horizontal ? ev.clientX : ev.clientY);
275
+ const start = at(e);
276
+ const before = this.px;
277
+ let dragging = false;
278
+ let fromEdge = Number.NaN;
279
+ let last = { pos: start, t: e.timeStamp };
280
+ let speed = 0;
281
+
282
+ const begin = (): void => {
283
+ dragging = true;
284
+ this.root.classList.add("dock-drawer-resizing");
285
+ };
286
+ if (slop === 0) {
287
+ e.preventDefault();
288
+ begin();
289
+ }
221
290
  const move = (ev: PointerEvent): void => {
291
+ const pos = at(ev);
292
+ if (!dragging) {
293
+ if (Math.abs(pos - start) < slop) return;
294
+ begin();
295
+ }
296
+ ev.preventDefault();
222
297
  const rect = this.host.getBoundingClientRect();
223
- const horizontal = HORIZONTAL[this.edge];
224
- const pointer = horizontal ? ev.clientX - rect.left : ev.clientY - rect.top;
225
298
  const span = horizontal ? rect.width : rect.height;
226
- this.setSize(drawerSize(this.edge, pointer, span, this.config), false);
299
+ const pointer = pos - (horizontal ? rect.left : rect.top);
300
+ fromEdge = toward > 0 ? span - pointer : pointer;
301
+ const dt = ev.timeStamp - last.t;
302
+ if (dt > 0) speed = (toward * (pos - last.pos)) / dt;
303
+ last = { pos, t: ev.timeStamp };
304
+ const px = drawerPixels(fromEdge, span, this.config);
305
+ this.setSize(px, false);
306
+ const slide = Math.max(0, px - fromEdge);
307
+ this.root.style.transform = slide > 0 ? `translate${horizontal ? "X" : "Y"}(${toward * slide}px)` : "";
227
308
  };
228
- const up = (): void => {
229
- this.handle.ownerDocument.removeEventListener("pointermove", move);
230
- this.handle.ownerDocument.removeEventListener("pointerup", up);
309
+ const end = (ev: PointerEvent): void => {
310
+ doc.removeEventListener("pointermove", move);
311
+ doc.removeEventListener("pointerup", end);
312
+ doc.removeEventListener("pointercancel", end);
313
+ if (!dragging) return;
231
314
  this.root.classList.remove("dock-drawer-resizing");
315
+ this.root.style.transform = "";
316
+ // The click that follows a drag on the tab strip would otherwise pick a tab.
317
+ if (slop > 0) {
318
+ const swallow = (c: Event): void => {
319
+ c.stopPropagation();
320
+ c.preventDefault();
321
+ };
322
+ this.tabStrip.addEventListener("click", swallow, { capture: true, once: true });
323
+ setTimeout(() => this.tabStrip.removeEventListener("click", swallow, { capture: true }), 0);
324
+ }
325
+ const fresh = ev.timeStamp - last.t <= FLICK_WINDOW_MS;
326
+ if (ev.type === "pointerup" && Number.isFinite(fromEdge) && closesOnRelease(fromEdge, min, touch && fresh ? speed : 0)) {
327
+ this.setSize(before, false);
328
+ this.setOpen(false);
329
+ return;
330
+ }
232
331
  this.hooks.changed();
233
332
  };
234
- this.root.classList.add("dock-drawer-resizing");
235
- this.handle.ownerDocument.addEventListener("pointermove", move);
236
- this.handle.ownerDocument.addEventListener("pointerup", up);
333
+ doc.addEventListener("pointermove", move);
334
+ doc.addEventListener("pointerup", end);
335
+ doc.addEventListener("pointercancel", end);
237
336
  }
238
337
  }
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,