@pack/react 4.4.0 → 4.6.0

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.
@@ -0,0 +1,730 @@
1
+ import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState, useSyncExternalStore, } from "react";
2
+ import * as React from "react";
3
+ const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
4
+ const PREVIEW_TOOLBAR_DOCK_KEY = "pack-preview-toolbar-dock";
5
+ const TOOLBAR_EDGE_MARGIN = 16;
6
+ const TOOLBAR_PILL_SIZE = 48;
7
+ const TOOLBAR_PANEL_GAP = 12;
8
+ const TOOLBAR_DRAG_THRESHOLD = 4;
9
+ const TOOLBAR_PILL_TRANSITION_MS = 220;
10
+ const TOOLBAR_MIN_TAP_TARGET = 44;
11
+ const DEFAULT_TOOLBAR_DOCK = { edge: "bottom", offset: 0.5 };
12
+ const Eye = (props) => (React.createElement("svg", { width: 24, height: 24, viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/svg", "aria-hidden": "true", ...props },
13
+ React.createElement("path", { fillRule: "evenodd", clipRule: "evenodd", d: "M2.75 12c0-.338.136-.905.493-1.591a7.858 7.858 0 0 1 1.64-2.108C6.374 6.926 8.694 5.75 12 5.75c3.308 0 5.627 1.176 7.116 2.551a7.86 7.86 0 0 1 1.64 2.108c.358.686.494 1.253.494 1.591 0 .338-.136.905-.493 1.591a7.86 7.86 0 0 1-1.64 2.108c-1.49 1.375-3.81 2.551-7.117 2.551-3.308 0-5.627-1.176-7.116-2.551a7.858 7.858 0 0 1-1.64-2.108c-.358-.686-.494-1.253-.494-1.591ZM12 4.25c-3.692 0-6.373 1.324-8.134 2.949a9.356 9.356 0 0 0-1.953 2.517c-.424.814-.663 1.622-.663 2.284 0 .662.24 1.47.663 2.284a9.356 9.356 0 0 0 1.953 2.517C5.627 18.426 8.308 19.75 12 19.75s6.373-1.324 8.134-2.949a9.356 9.356 0 0 0 1.953-2.517c.424-.814.663-1.622.663-2.284 0-.662-.24-1.47-.663-2.284a9.355 9.355 0 0 0-1.953-2.517C18.373 5.574 15.692 4.25 12 4.25ZM9.75 12a2.25 2.25 0 1 1 4.5 0 2.25 2.25 0 0 1-4.5 0ZM12 8.25a3.75 3.75 0 1 0 0 7.5 3.75 3.75 0 0 0 0-7.5Z", fill: "currentColor" })));
14
+ function getPreviewContextLabel(previewContext) {
15
+ if (previewContext?.linkName) {
16
+ return `preview link: ${previewContext.linkName}`;
17
+ }
18
+ if (previewContext?.releaseName) {
19
+ return `content release: ${previewContext.releaseName}`;
20
+ }
21
+ return previewContext?.releaseId ? "content release" : undefined;
22
+ }
23
+ export function buildPreviewDetails(environment, previewContext) {
24
+ const details = [];
25
+ if (previewContext?.linkName) {
26
+ details.push({ label: "Preview link", value: previewContext.linkName });
27
+ }
28
+ if (previewContext?.releaseName || previewContext?.releaseId) {
29
+ details.push({
30
+ label: "Content release",
31
+ value: previewContext.releaseName || "Active",
32
+ });
33
+ }
34
+ if (environment) {
35
+ details.push({ label: "Environment", value: environment });
36
+ }
37
+ if (previewContext?.locale) {
38
+ details.push({ label: "Locale", value: previewContext.locale });
39
+ }
40
+ // Backpack uses a lone `control` variant as a neutral preview fallback.
41
+ // Only describe an A/B test when the preview carries a complete test context.
42
+ if (previewContext?.testHandle && previewContext.testVariantHandle) {
43
+ details.push({
44
+ label: "A/B test",
45
+ value: `${previewContext.testHandle} · ${previewContext.testVariantHandle}`,
46
+ });
47
+ }
48
+ if (previewContext?.pageDraft) {
49
+ details.push({ label: "Page draft", value: previewContext.pageDraft });
50
+ }
51
+ if (previewContext?.siteSettingsDraft) {
52
+ details.push({
53
+ label: "Site settings",
54
+ value: previewContext.siteSettingsDraft,
55
+ });
56
+ }
57
+ return details;
58
+ }
59
+ export function buildPreviewSummary(environment, previewContext) {
60
+ const parts = [];
61
+ const contextLabel = getPreviewContextLabel(previewContext);
62
+ if (contextLabel)
63
+ parts.push(contextLabel);
64
+ if (environment)
65
+ parts.push(environment);
66
+ if (previewContext?.locale)
67
+ parts.push(previewContext.locale);
68
+ if (previewContext?.testHandle && previewContext.testVariantHandle) {
69
+ parts.push(`test: ${previewContext.testVariantHandle}`);
70
+ }
71
+ if (previewContext?.pageDraft) {
72
+ parts.push(`page: ${previewContext.pageDraft}`);
73
+ }
74
+ if (previewContext?.siteSettingsDraft) {
75
+ parts.push(`settings: ${previewContext.siteSettingsDraft}`);
76
+ }
77
+ // This is a tooltip and accessible label, not visible pill text. Keep the
78
+ // full context available without requiring the user to open the panel.
79
+ return parts.length > 0 ? parts.join(" · ") : "Preview mode";
80
+ }
81
+ // Whether this document is the top-level window is fixed for its lifetime, so
82
+ // there is nothing to subscribe to. `useSyncExternalStore` is used purely as a
83
+ // hydration-safe client-only gate: the server snapshot is `false`, and React
84
+ // re-renders once after hydration when the client snapshot differs.
85
+ function canShowPreviewToolbar() {
86
+ if (typeof window === "undefined")
87
+ return false;
88
+ try {
89
+ return window.self === window.top;
90
+ }
91
+ catch {
92
+ return false;
93
+ }
94
+ }
95
+ function subscribeToNothing() {
96
+ return () => { };
97
+ }
98
+ function getServerSnapshot() {
99
+ return false;
100
+ }
101
+ function clamp(value, min, max) {
102
+ return Math.min(Math.max(value, min), max);
103
+ }
104
+ function isVerticalEdge(edge) {
105
+ return edge === "left" || edge === "right";
106
+ }
107
+ function getViewport() {
108
+ if (typeof window === "undefined")
109
+ return { width: 0, height: 0 };
110
+ return { width: window.innerWidth, height: window.innerHeight };
111
+ }
112
+ function getStoredDock() {
113
+ if (typeof window === "undefined")
114
+ return DEFAULT_TOOLBAR_DOCK;
115
+ try {
116
+ const raw = window.localStorage.getItem(PREVIEW_TOOLBAR_DOCK_KEY);
117
+ if (!raw)
118
+ return DEFAULT_TOOLBAR_DOCK;
119
+ const parsed = JSON.parse(raw);
120
+ const isEdge = parsed.edge === "top" ||
121
+ parsed.edge === "right" ||
122
+ parsed.edge === "bottom" ||
123
+ parsed.edge === "left";
124
+ if (isEdge &&
125
+ typeof parsed.offset === "number" &&
126
+ parsed.offset >= 0 &&
127
+ parsed.offset <= 1) {
128
+ return { edge: parsed.edge, offset: parsed.offset };
129
+ }
130
+ }
131
+ catch {
132
+ // Ignore unavailable or malformed local storage.
133
+ }
134
+ return DEFAULT_TOOLBAR_DOCK;
135
+ }
136
+ function setStoredDock(dock) {
137
+ if (typeof window === "undefined")
138
+ return;
139
+ try {
140
+ window.localStorage.setItem(PREVIEW_TOOLBAR_DOCK_KEY, JSON.stringify(dock));
141
+ }
142
+ catch {
143
+ // Dock persistence is optional.
144
+ }
145
+ }
146
+ function getDockedPosition(dock, viewport) {
147
+ const size = TOOLBAR_PILL_SIZE;
148
+ if (isVerticalEdge(dock.edge)) {
149
+ const left = dock.edge === "left" ? 0 : Math.max(0, viewport.width - size);
150
+ const top = clamp(dock.offset * viewport.height - size / 2, 0, Math.max(0, viewport.height - size));
151
+ return { left, top };
152
+ }
153
+ const top = dock.edge === "top" ? 0 : Math.max(0, viewport.height - size);
154
+ const left = clamp(dock.offset * viewport.width - size / 2, 0, Math.max(0, viewport.width - size));
155
+ return { left, top };
156
+ }
157
+ function getDragPosition(event, drag, viewport) {
158
+ const size = TOOLBAR_PILL_SIZE;
159
+ return {
160
+ left: clamp(event.clientX - drag.offsetX, 0, Math.max(0, viewport.width - size)),
161
+ top: clamp(event.clientY - drag.offsetY, 0, Math.max(0, viewport.height - size)),
162
+ };
163
+ }
164
+ const SAFE_AREA_PROPERTY = {
165
+ top: "--pack-preview-toolbar-safe-area-top",
166
+ right: "--pack-preview-toolbar-safe-area-right",
167
+ bottom: "--pack-preview-toolbar-safe-area-bottom",
168
+ left: "--pack-preview-toolbar-safe-area-left",
169
+ };
170
+ function getSafeAreaInset(edge) {
171
+ return `var(${SAFE_AREA_PROPERTY[edge]})`;
172
+ }
173
+ function getSafeAreaOffset(edge, offset) {
174
+ return `max(${getSafeAreaInset(edge)}, ${Math.max(0, offset)}px)`;
175
+ }
176
+ export function getConstrainedPillPosition(position, viewport) {
177
+ const size = TOOLBAR_PILL_SIZE;
178
+ return {
179
+ left: `clamp(${getSafeAreaInset("left")}, ${position.left}px, calc(${viewport.width}px - ${getSafeAreaInset("right")} - ${size}px))`,
180
+ top: `clamp(${getSafeAreaInset("top")}, ${position.top}px, calc(${viewport.height}px - ${getSafeAreaInset("bottom")} - ${size}px))`,
181
+ };
182
+ }
183
+ function getNearestDock(centerX, centerY, viewport) {
184
+ const distances = [
185
+ { edge: "left", distance: centerX },
186
+ { edge: "right", distance: viewport.width - centerX },
187
+ { edge: "top", distance: centerY },
188
+ { edge: "bottom", distance: viewport.height - centerY },
189
+ ];
190
+ const nearest = distances.reduce((best, current) => current.distance < best.distance ? current : best);
191
+ const offset = isVerticalEdge(nearest.edge)
192
+ ? viewport.height > 0
193
+ ? centerY / viewport.height
194
+ : 0.5
195
+ : viewport.width > 0
196
+ ? centerX / viewport.width
197
+ : 0.5;
198
+ return { edge: nearest.edge, offset: clamp(offset, 0, 1) };
199
+ }
200
+ /**
201
+ * Resolves which side of one axis the panel anchors to, and how far from the
202
+ * viewport edge. When the pill is docked on this axis the panel sits on the
203
+ * far side of the pill plus a gap; otherwise it aligns with the pill's near
204
+ * side, in whichever half of the viewport the pill's center falls.
205
+ */
206
+ function resolveAxisAnchor(dockEdge, [startEdge, endEdge], rectStart, rectEnd, viewportSize, gap) {
207
+ if (dockEdge === startEdge) {
208
+ return { edge: startEdge, offset: rectEnd + gap };
209
+ }
210
+ if (dockEdge === endEdge) {
211
+ return { edge: endEdge, offset: viewportSize - rectStart + gap };
212
+ }
213
+ const center = (rectStart + rectEnd) / 2;
214
+ if (center < viewportSize / 2) {
215
+ return { edge: startEdge, offset: rectStart };
216
+ }
217
+ return { edge: endEdge, offset: viewportSize - rectEnd };
218
+ }
219
+ export function getPanelPosition(edge, pillRect, viewport) {
220
+ const gap = TOOLBAR_PANEL_GAP;
221
+ // Before the pill has been measured, place the panel where a default
222
+ // bottom-docked pill would put it.
223
+ const rect = pillRect ?? {
224
+ left: 0,
225
+ top: viewport.height - TOOLBAR_PILL_SIZE,
226
+ right: TOOLBAR_PILL_SIZE,
227
+ bottom: viewport.height,
228
+ };
229
+ const horizontal = resolveAxisAnchor(pillRect ? edge : "bottom", ["left", "right"], rect.left, rect.right, viewport.width, gap);
230
+ const vertical = resolveAxisAnchor(pillRect ? edge : "bottom", ["top", "bottom"], rect.top, rect.bottom, viewport.height, gap);
231
+ const horizontalOffset = getSafeAreaOffset(horizontal.edge, horizontal.offset);
232
+ const verticalOffset = getSafeAreaOffset(vertical.edge, vertical.offset);
233
+ const oppositeHorizontalEdge = horizontal.edge === "left" ? "right" : "left";
234
+ const oppositeVerticalEdge = vertical.edge === "top" ? "bottom" : "top";
235
+ // Size against the measured viewport (window.innerWidth/innerHeight) rather
236
+ // than 100vw/100vh: on iOS Safari 100vh is the large viewport, which would
237
+ // let the panel extend under the browser's bottom bar.
238
+ return {
239
+ position: "fixed",
240
+ [horizontal.edge]: horizontalOffset,
241
+ [vertical.edge]: verticalOffset,
242
+ width: `min(360px, max(0px, calc(${viewport.width}px - ${horizontalOffset} - ${getSafeAreaInset(oppositeHorizontalEdge)})))`,
243
+ maxHeight: `max(0px, calc(${viewport.height}px - ${verticalOffset} - ${getSafeAreaInset(oppositeVerticalEdge)}))`,
244
+ };
245
+ }
246
+ const PILL_SURFACE = "#1a1a1a";
247
+ const rootStyle = {
248
+ position: "fixed",
249
+ inset: 0,
250
+ zIndex: 2147483647,
251
+ pointerEvents: "none",
252
+ fontFamily: '"Inter", system-ui, sans-serif',
253
+ [SAFE_AREA_PROPERTY.top]: `max(${TOOLBAR_EDGE_MARGIN}px, env(safe-area-inset-top, 0px))`,
254
+ [SAFE_AREA_PROPERTY.right]: `max(${TOOLBAR_EDGE_MARGIN}px, env(safe-area-inset-right, 0px))`,
255
+ [SAFE_AREA_PROPERTY.bottom]: `max(${TOOLBAR_EDGE_MARGIN}px, env(safe-area-inset-bottom, 0px))`,
256
+ [SAFE_AREA_PROPERTY.left]: `max(${TOOLBAR_EDGE_MARGIN}px, env(safe-area-inset-left, 0px))`,
257
+ };
258
+ const pillStyle = {
259
+ position: "fixed",
260
+ pointerEvents: "auto",
261
+ display: "flex",
262
+ alignItems: "center",
263
+ justifyContent: "center",
264
+ width: TOOLBAR_PILL_SIZE,
265
+ height: TOOLBAR_PILL_SIZE,
266
+ padding: 0,
267
+ border: "1px solid rgba(255, 255, 255, 0.14)",
268
+ borderRadius: "50%",
269
+ backgroundColor: PILL_SURFACE,
270
+ color: "#fafafa",
271
+ cursor: "pointer",
272
+ touchAction: "none",
273
+ boxShadow: "0 10px 30px rgba(0, 0, 0, 0.35), 0 2px 6px rgba(0, 0, 0, 0.25)",
274
+ };
275
+ const panelStyle = {
276
+ pointerEvents: "auto",
277
+ overflowY: "auto",
278
+ backgroundColor: PILL_SURFACE,
279
+ color: "#fafafa",
280
+ border: "1px solid rgba(255, 255, 255, 0.12)",
281
+ borderRadius: 14,
282
+ boxShadow: "0 16px 50px rgba(0, 0, 0, 0.4), 0 2px 8px rgba(0, 0, 0, 0.3)",
283
+ };
284
+ const panelHeaderStyle = {
285
+ display: "flex",
286
+ alignItems: "flex-start",
287
+ gap: 10,
288
+ padding: "14px 14px 12px",
289
+ };
290
+ const panelTitleStyle = {
291
+ margin: 0,
292
+ fontSize: 13,
293
+ fontWeight: 600,
294
+ lineHeight: "18px",
295
+ };
296
+ const panelSummaryStyle = {
297
+ margin: "3px 0 0",
298
+ fontSize: 12,
299
+ lineHeight: "16px",
300
+ color: "#a1a1aa",
301
+ overflowWrap: "anywhere",
302
+ };
303
+ const detailListStyle = {
304
+ display: "grid",
305
+ gap: 10,
306
+ margin: 0,
307
+ padding: "12px 14px 14px",
308
+ borderTop: "1px solid rgba(255, 255, 255, 0.1)",
309
+ };
310
+ const detailRowStyle = {
311
+ display: "grid",
312
+ gridTemplateColumns: "minmax(88px, 0.8fr) minmax(0, 1.2fr)",
313
+ gap: 12,
314
+ alignItems: "baseline",
315
+ };
316
+ const detailLabelStyle = {
317
+ color: "#a1a1aa",
318
+ fontSize: 12,
319
+ lineHeight: "16px",
320
+ };
321
+ const detailValueStyle = {
322
+ margin: 0,
323
+ color: "#fafafa",
324
+ fontSize: 12,
325
+ lineHeight: "16px",
326
+ overflowWrap: "anywhere",
327
+ };
328
+ const panelFooterStyle = {
329
+ display: "flex",
330
+ alignItems: "center",
331
+ justifyContent: "flex-end",
332
+ gap: 8,
333
+ borderTop: "1px solid rgba(255, 255, 255, 0.1)",
334
+ padding: "6px 14px",
335
+ };
336
+ const buttonStyle = {
337
+ display: "inline-flex",
338
+ alignItems: "center",
339
+ justifyContent: "center",
340
+ minHeight: TOOLBAR_MIN_TAP_TARGET,
341
+ minWidth: TOOLBAR_MIN_TAP_TARGET,
342
+ border: 0,
343
+ borderRadius: 8,
344
+ background: "transparent",
345
+ color: "#a1a1aa",
346
+ cursor: "pointer",
347
+ fontSize: 12,
348
+ fontWeight: 500,
349
+ lineHeight: "16px",
350
+ padding: "7px 10px",
351
+ };
352
+ const exitConfirmButtonStyle = {
353
+ ...buttonStyle,
354
+ border: "1px solid rgba(239, 68, 68, 0.4)",
355
+ background: "rgba(239, 68, 68, 0.12)",
356
+ color: "#fca5a5",
357
+ fontWeight: 600,
358
+ padding: "6px 12px",
359
+ };
360
+ const toolbarStyles = `
361
+ .pack-preview-toolbar *,
362
+ .pack-preview-toolbar *::before,
363
+ .pack-preview-toolbar *::after { box-sizing: border-box; }
364
+ .pack-preview-toolbar button { font-family: inherit; }
365
+ .pack-preview-toolbar__pill { transition: background-color 120ms ease, box-shadow 160ms ease, left ${TOOLBAR_PILL_TRANSITION_MS}ms cubic-bezier(0.16, 1, 0.3, 1), top ${TOOLBAR_PILL_TRANSITION_MS}ms cubic-bezier(0.16, 1, 0.3, 1); }
366
+ .pack-preview-toolbar__pill:hover { background-color: #262626; }
367
+ .pack-preview-toolbar__pill--dragging { transition: none; box-shadow: 0 20px 48px rgba(0, 0, 0, 0.55); }
368
+ .pack-preview-toolbar__panel { animation: pack-preview-panel-in 160ms cubic-bezier(0.16, 1, 0.3, 1); }
369
+ .pack-preview-toolbar__button:hover { background-color: rgba(255, 255, 255, 0.08); color: #fafafa; }
370
+ .pack-preview-toolbar__exit:hover { color: #fca5a5; }
371
+ .pack-preview-toolbar__exit-confirm:hover { background-color: rgba(239, 68, 68, 0.2); }
372
+ .pack-preview-toolbar button:focus-visible { outline: 2px solid #3b82f6; outline-offset: 2px; }
373
+ .pack-preview-toolbar button:focus:not(:focus-visible),
374
+ .pack-preview-toolbar__panel:focus { outline: none; }
375
+ @keyframes pack-preview-panel-in {
376
+ from { opacity: 0; transform: translateY(8px) scale(0.98); }
377
+ to { opacity: 1; transform: translateY(0) scale(1); }
378
+ }
379
+ @media (prefers-reduced-motion: reduce) {
380
+ .pack-preview-toolbar__panel { animation: none; }
381
+ .pack-preview-toolbar__pill { transition: none; }
382
+ }
383
+ `;
384
+ function PreviewDetails({ details }) {
385
+ if (details.length === 0) {
386
+ return (React.createElement("dl", { style: detailListStyle },
387
+ React.createElement("div", { style: detailRowStyle },
388
+ React.createElement("dt", { style: detailLabelStyle }, "Content"),
389
+ React.createElement("dd", { style: detailValueStyle }, "Draft content"))));
390
+ }
391
+ return (React.createElement("dl", { style: detailListStyle }, details.map((detail) => (React.createElement("div", { key: detail.label, style: detailRowStyle },
392
+ React.createElement("dt", { style: detailLabelStyle }, detail.label),
393
+ React.createElement("dd", { style: detailValueStyle }, detail.value))))));
394
+ }
395
+ function PreviewPanel({ details, panelId, panelRef, position, onHide, }) {
396
+ const [isConfirmingExit, setIsConfirmingExit] = useState(false);
397
+ const [isExiting, setIsExiting] = useState(false);
398
+ const [exitError, setExitError] = useState(null);
399
+ const exitPreviewMode = async () => {
400
+ if (isExiting)
401
+ return;
402
+ setIsExiting(true);
403
+ setExitError(null);
404
+ try {
405
+ const response = await fetch("/api/edit", {
406
+ method: "POST",
407
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
408
+ });
409
+ if (!response.ok) {
410
+ throw new Error(`ERR_RE_API_ERROR: HTTP error! Status: ${response.status} Doc: https://docs.packdigital.com/err/ERR_RE_API_ERROR`);
411
+ }
412
+ window.location.reload();
413
+ }
414
+ catch (error) {
415
+ console.error("Error exiting preview:", error.message);
416
+ setExitError("Couldn’t exit preview. Please try again.");
417
+ setIsExiting(false);
418
+ }
419
+ };
420
+ return (React.createElement("div", { ref: panelRef, id: panelId, role: "region", "aria-label": "Preview details", tabIndex: -1, "data-pack-preview-toolbar-panel": true, className: "pack-preview-toolbar__panel", style: { ...panelStyle, ...position } },
421
+ React.createElement("div", { style: panelHeaderStyle },
422
+ React.createElement(Eye, { style: { width: 18, height: 18, minWidth: 18, marginTop: 1 } }),
423
+ React.createElement("p", { style: panelTitleStyle }, "Preview mode")),
424
+ React.createElement(PreviewDetails, { details: details }),
425
+ React.createElement("div", { style: panelFooterStyle }, isConfirmingExit ? (React.createElement("div", { style: { display: "grid", gap: 8, width: "100%" } },
426
+ exitError && (React.createElement("p", { style: { ...panelSummaryStyle, color: "#fca5a5" }, role: "alert" }, exitError)),
427
+ React.createElement("div", { style: {
428
+ display: "flex",
429
+ alignItems: "center",
430
+ justifyContent: "space-between",
431
+ gap: 8,
432
+ } },
433
+ React.createElement("span", { style: { color: "#a1a1aa", fontSize: 12 } }, "Exit preview mode?"),
434
+ React.createElement("div", { style: { display: "flex", gap: 6 } },
435
+ React.createElement("button", { type: "button", className: "pack-preview-toolbar__button", disabled: isExiting, onClick: () => {
436
+ setIsConfirmingExit(false);
437
+ setExitError(null);
438
+ }, style: { ...buttonStyle, opacity: isExiting ? 0.55 : 1 } }, "Cancel"),
439
+ React.createElement("button", { type: "button", className: "pack-preview-toolbar__exit-confirm", disabled: isExiting, onClick: exitPreviewMode, style: {
440
+ ...exitConfirmButtonStyle,
441
+ opacity: isExiting ? 0.65 : 1,
442
+ } }, isExiting ? "Exiting…" : "Exit"))))) : (React.createElement(React.Fragment, null,
443
+ React.createElement("button", { type: "button", className: "pack-preview-toolbar__button", "data-pack-preview-toolbar-hide": true, title: "Hide the preview button until you reload or open another page", onClick: onHide, style: { ...buttonStyle, marginRight: "auto" } }, "Hide"),
444
+ React.createElement("button", { type: "button", className: "pack-preview-toolbar__exit", onClick: () => setIsConfirmingExit(true), style: buttonStyle }, "Exit preview"))))));
445
+ }
446
+ /**
447
+ * While the toolbar is hidden, watches for a client-side route change
448
+ * (pushState/replaceState from the storefront router, or back/forward) and
449
+ * resets the hidden flag. A full reload resets it naturally since it lives in
450
+ * component state. Nothing is patched while the toolbar is visible.
451
+ */
452
+ function useUnhideOnNavigation(isHidden, setIsHidden) {
453
+ useEffect(() => {
454
+ if (!isHidden)
455
+ return;
456
+ let active = true;
457
+ const hiddenAtPathname = window.location.pathname;
458
+ const unhideIfNavigated = () => {
459
+ if (active && window.location.pathname !== hiddenAtPathname) {
460
+ setIsHidden(false);
461
+ }
462
+ };
463
+ const originalPushState = history.pushState;
464
+ const originalReplaceState = history.replaceState;
465
+ const patchedPushState = function pushState(...args) {
466
+ originalPushState.apply(this, args);
467
+ unhideIfNavigated();
468
+ };
469
+ const patchedReplaceState = function replaceState(...args) {
470
+ originalReplaceState.apply(this, args);
471
+ unhideIfNavigated();
472
+ };
473
+ history.pushState = patchedPushState;
474
+ history.replaceState = patchedReplaceState;
475
+ window.addEventListener("popstate", unhideIfNavigated);
476
+ return () => {
477
+ active = false;
478
+ window.removeEventListener("popstate", unhideIfNavigated);
479
+ // A later script may retain our wrapper. Leave its patch intact and make
480
+ // our retained callback inert after cleanup.
481
+ if (history.pushState === patchedPushState) {
482
+ history.pushState = originalPushState;
483
+ }
484
+ if (history.replaceState === patchedReplaceState) {
485
+ history.replaceState = originalReplaceState;
486
+ }
487
+ };
488
+ }, [isHidden, setIsHidden]);
489
+ }
490
+ function isSameRect(a, b) {
491
+ return (a !== null &&
492
+ a.top === b.top &&
493
+ a.left === b.left &&
494
+ a.right === b.right &&
495
+ a.bottom === b.bottom);
496
+ }
497
+ function PreviewToolbarSurface({ environment, previewContext, }) {
498
+ const panelId = useId();
499
+ const rootRef = useRef(null);
500
+ const pillRef = useRef(null);
501
+ const panelRef = useRef(null);
502
+ const statusRef = useRef(null);
503
+ const previousFocusRef = useRef(null);
504
+ const dragStateRef = useRef(null);
505
+ const [isPanelOpen, setIsPanelOpen] = useState(false);
506
+ const [isHidden, setIsHidden] = useState(false);
507
+ const [dock, setDock] = useState(getStoredDock);
508
+ const [viewport, setViewport] = useState(getViewport);
509
+ const [pillRect, setPillRect] = useState(null);
510
+ const [dragPosition, setDragPosition] = useState(null);
511
+ const isDragging = dragPosition !== null;
512
+ useEffect(() => {
513
+ const onResize = () => {
514
+ setViewport((current) => {
515
+ const next = getViewport();
516
+ return current.width === next.width && current.height === next.height
517
+ ? current
518
+ : next;
519
+ });
520
+ };
521
+ window.addEventListener("resize", onResize);
522
+ return () => window.removeEventListener("resize", onResize);
523
+ }, []);
524
+ const measurePill = useCallback(() => {
525
+ const node = pillRef.current;
526
+ if (!node)
527
+ return;
528
+ const rect = node.getBoundingClientRect();
529
+ const next = {
530
+ top: rect.top,
531
+ left: rect.left,
532
+ right: rect.right,
533
+ bottom: rect.bottom,
534
+ };
535
+ setPillRect((current) => (isSameRect(current, next) ? current : next));
536
+ }, []);
537
+ // The panel is anchored to the pill's rendered rect, which the CSS safe-area
538
+ // clamp can shift away from the JS position. Measure only while the panel is
539
+ // open (it is closed during drags). A synchronous read right after a dock or
540
+ // viewport change captures the pre-transition position, so re-measure once
541
+ // the pill's left/top transition has had time to settle, and again on the
542
+ // transition events themselves for the case where the timer runs early.
543
+ useIsomorphicLayoutEffect(() => {
544
+ if (!isPanelOpen)
545
+ return;
546
+ measurePill();
547
+ const timeoutId = window.setTimeout(measurePill, TOOLBAR_PILL_TRANSITION_MS + 50);
548
+ return () => window.clearTimeout(timeoutId);
549
+ }, [isPanelOpen, dock, viewport, measurePill]);
550
+ useEffect(() => {
551
+ const node = pillRef.current;
552
+ if (!isPanelOpen || !node)
553
+ return;
554
+ const onTransitionSettled = (event) => {
555
+ if (event.propertyName === "left" || event.propertyName === "top") {
556
+ measurePill();
557
+ }
558
+ };
559
+ node.addEventListener("transitionend", onTransitionSettled);
560
+ node.addEventListener("transitioncancel", onTransitionSettled);
561
+ return () => {
562
+ node.removeEventListener("transitionend", onTransitionSettled);
563
+ node.removeEventListener("transitioncancel", onTransitionSettled);
564
+ };
565
+ }, [isPanelOpen, measurePill]);
566
+ useEffect(() => {
567
+ if (!isPanelOpen)
568
+ return;
569
+ const handlePointerDown = (event) => {
570
+ const target = event.target;
571
+ if (target && !rootRef.current?.contains(target)) {
572
+ setIsPanelOpen(false);
573
+ }
574
+ };
575
+ // Only claim Escape while focus is inside the toolbar, so storefront
576
+ // dialogs and inputs keep their own Escape handling.
577
+ const handleKeyDown = (event) => {
578
+ if (event.key !== "Escape" || event.defaultPrevented)
579
+ return;
580
+ if (!rootRef.current?.contains(document.activeElement))
581
+ return;
582
+ setIsPanelOpen(false);
583
+ pillRef.current?.focus();
584
+ };
585
+ document.addEventListener("pointerdown", handlePointerDown, true);
586
+ document.addEventListener("keydown", handleKeyDown);
587
+ panelRef.current?.focus();
588
+ return () => {
589
+ document.removeEventListener("pointerdown", handlePointerDown, true);
590
+ document.removeEventListener("keydown", handleKeyDown);
591
+ };
592
+ }, [isPanelOpen]);
593
+ const togglePanel = () => setIsPanelOpen((open) => !open);
594
+ const beginDrag = (event) => {
595
+ if (event.pointerType === "mouse" && event.button !== 0)
596
+ return;
597
+ const rect = pillRef.current?.getBoundingClientRect();
598
+ if (!rect)
599
+ return;
600
+ dragStateRef.current = {
601
+ pointerId: event.pointerId,
602
+ startX: event.clientX,
603
+ startY: event.clientY,
604
+ offsetX: event.clientX - rect.left,
605
+ offsetY: event.clientY - rect.top,
606
+ moved: false,
607
+ captured: false,
608
+ };
609
+ };
610
+ const updateDrag = (event) => {
611
+ const drag = dragStateRef.current;
612
+ if (!drag || drag.pointerId !== event.pointerId)
613
+ return;
614
+ const distance = Math.hypot(event.clientX - drag.startX, event.clientY - drag.startY);
615
+ if (!drag.moved && distance < TOOLBAR_DRAG_THRESHOLD)
616
+ return;
617
+ if (!drag.moved) {
618
+ drag.moved = true;
619
+ setIsPanelOpen(false);
620
+ try {
621
+ pillRef.current?.setPointerCapture(event.pointerId);
622
+ drag.captured = true;
623
+ }
624
+ catch {
625
+ // Without capture, pointerup may go to the storefront. Cancel instead
626
+ // of leaving the pill stranded in its dragging state.
627
+ cancelDrag();
628
+ return;
629
+ }
630
+ }
631
+ setDragPosition(getDragPosition(event, drag, viewport));
632
+ };
633
+ // A pointer that never moved past the drag threshold is a tap: toggle here
634
+ // rather than in `onClick`, so a completed drag never has to suppress the
635
+ // click that may or may not follow it (touch drags produce no click).
636
+ const endDrag = (event) => {
637
+ const drag = dragStateRef.current;
638
+ if (!drag || drag.pointerId !== event.pointerId)
639
+ return;
640
+ dragStateRef.current = null;
641
+ if (drag.captured) {
642
+ try {
643
+ pillRef.current?.releasePointerCapture(event.pointerId);
644
+ }
645
+ catch {
646
+ // Capture is released implicitly on pointerup; ignore a stale id.
647
+ }
648
+ }
649
+ if (!drag.moved) {
650
+ togglePanel();
651
+ return;
652
+ }
653
+ const position = getDragPosition(event, drag, viewport);
654
+ const nextDock = getNearestDock(position.left + TOOLBAR_PILL_SIZE / 2, position.top + TOOLBAR_PILL_SIZE / 2, viewport);
655
+ setDock(nextDock);
656
+ setStoredDock(nextDock);
657
+ setDragPosition(null);
658
+ };
659
+ const cancelDrag = () => {
660
+ dragStateRef.current = null;
661
+ setDragPosition(null);
662
+ };
663
+ // Pointer taps are handled in `endDrag`; `click` is only needed for
664
+ // keyboard and assistive-technology activation, which carries `detail === 0`.
665
+ const handlePillClick = (event) => {
666
+ if (event.detail === 0)
667
+ togglePanel();
668
+ };
669
+ const hideToolbar = () => {
670
+ const previousFocus = previousFocusRef.current;
671
+ if (previousFocus?.isConnected)
672
+ previousFocus.focus({ preventScroll: true });
673
+ if (!previousFocus || document.activeElement !== previousFocus) {
674
+ statusRef.current?.focus({ preventScroll: true });
675
+ }
676
+ setIsPanelOpen(false);
677
+ setIsHidden(true);
678
+ };
679
+ useUnhideOnNavigation(isHidden, setIsHidden);
680
+ const summary = buildPreviewSummary(environment, previewContext);
681
+ const details = buildPreviewDetails(environment, previewContext);
682
+ const pillPosition = dragPosition ?? getDockedPosition(dock, viewport);
683
+ const panelPosition = getPanelPosition(dock.edge, pillRect, viewport);
684
+ const constrainedPillPosition = getConstrainedPillPosition(pillPosition, viewport);
685
+ return (React.createElement(React.Fragment, null,
686
+ React.createElement("div", { ref: statusRef, role: "status", tabIndex: -1, style: {
687
+ position: "fixed",
688
+ width: 1,
689
+ height: 1,
690
+ overflow: "hidden",
691
+ clipPath: "inset(50%)",
692
+ whiteSpace: "nowrap",
693
+ } }, isHidden
694
+ ? "Preview toolbar hidden. It returns when you open another page or reload."
695
+ : ""),
696
+ !isHidden && (React.createElement("div", { ref: rootRef, className: "pack-preview-toolbar", "data-pack-preview-toolbar": true, style: rootStyle, onFocusCapture: (event) => {
697
+ const previous = event.relatedTarget;
698
+ if (!event.currentTarget.contains(previous)) {
699
+ previousFocusRef.current =
700
+ previous instanceof HTMLElement &&
701
+ previous !== document.body &&
702
+ previous !== statusRef.current
703
+ ? previous
704
+ : null;
705
+ }
706
+ } },
707
+ React.createElement("style", null, toolbarStyles),
708
+ isPanelOpen && (React.createElement(PreviewPanel, { details: details, panelId: panelId, panelRef: panelRef, position: panelPosition, onHide: hideToolbar })),
709
+ React.createElement("button", { ref: pillRef, type: "button", "data-pack-preview-toolbar-pill": true, className: isDragging
710
+ ? "pack-preview-toolbar__pill pack-preview-toolbar__pill--dragging"
711
+ : "pack-preview-toolbar__pill", "aria-expanded": isPanelOpen, "aria-controls": isPanelOpen ? panelId : undefined, "aria-label": isPanelOpen
712
+ ? "Close preview details"
713
+ : `Open preview details. Current context: ${summary}. Drag to reposition.`, title: summary, onPointerDown: beginDrag, onPointerMove: updateDrag, onPointerUp: endDrag, onPointerCancel: cancelDrag, onClick: handlePillClick, style: {
714
+ ...pillStyle,
715
+ ...constrainedPillPosition,
716
+ cursor: isDragging ? "grabbing" : "pointer",
717
+ } },
718
+ React.createElement(Eye, { style: { width: 20, height: 20 } }))))));
719
+ }
720
+ /**
721
+ * Thin gate so production storefront pages (preview off) mount no listeners,
722
+ * timers, storage reads, or effects. The top-window check is a hydration-safe
723
+ * client-only snapshot; it renders nothing on the server and during hydration.
724
+ */
725
+ export const PreviewToolbar = ({ isPreviewModeEnabled, environment, previewContext, }) => {
726
+ const canRenderToolbar = useSyncExternalStore(subscribeToNothing, canShowPreviewToolbar, getServerSnapshot);
727
+ if (!isPreviewModeEnabled || !canRenderToolbar)
728
+ return null;
729
+ return (React.createElement(PreviewToolbarSurface, { environment: environment, previewContext: previewContext }));
730
+ };