@present-day/device 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +71 -2
- package/dist/core.d.mts +169 -0
- package/dist/core.d.ts +169 -0
- package/dist/core.js +125 -0
- package/dist/core.js.map +1 -0
- package/dist/core.mjs +94 -0
- package/dist/core.mjs.map +1 -0
- package/dist/index.d.mts +224 -0
- package/dist/index.d.ts +224 -0
- package/dist/index.js +322 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +286 -0
- package/dist/index.mjs.map +1 -0
- package/dist/styles.css +92 -0
- package/dist/styles.css.map +1 -0
- package/package.json +96 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
require("./styles.css");
|
|
3
|
+
"use strict";
|
|
4
|
+
var __create = Object.create;
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __export = (target, all) => {
|
|
11
|
+
for (var name in all)
|
|
12
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
13
|
+
};
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
28
|
+
mod
|
|
29
|
+
));
|
|
30
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
31
|
+
|
|
32
|
+
// src/index.ts
|
|
33
|
+
var src_exports = {};
|
|
34
|
+
__export(src_exports, {
|
|
35
|
+
DEVICES: () => DEVICES,
|
|
36
|
+
Device: () => Device,
|
|
37
|
+
deviceGeometry: () => deviceGeometry,
|
|
38
|
+
deviceHTML: () => deviceHTML,
|
|
39
|
+
deviceVars: () => deviceVars,
|
|
40
|
+
resolveDevice: () => resolveDevice
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(src_exports);
|
|
43
|
+
|
|
44
|
+
// src/core/devices.ts
|
|
45
|
+
var DEVICES = {
|
|
46
|
+
phone: {
|
|
47
|
+
viewport: { width: 390, height: 844 },
|
|
48
|
+
bezel: 0.036,
|
|
49
|
+
// Apple's 47.33 pt display corner radius on a 390 pt screen.
|
|
50
|
+
screenRadius: 47.33 / 390,
|
|
51
|
+
cutout: "island",
|
|
52
|
+
buttons: [
|
|
53
|
+
{ side: "left", start: 0.175, length: 0.045 },
|
|
54
|
+
{ side: "left", start: 0.255, length: 0.085 },
|
|
55
|
+
{ side: "left", start: 0.36, length: 0.085 },
|
|
56
|
+
{ side: "right", start: 0.29, length: 0.14 }
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
tablet: {
|
|
60
|
+
viewport: { width: 1180, height: 820 },
|
|
61
|
+
bezel: 0.032,
|
|
62
|
+
// 18 pt display corners on a 1180 pt screen.
|
|
63
|
+
screenRadius: 18 / 1180,
|
|
64
|
+
cutout: "none",
|
|
65
|
+
buttons: [{ side: "top", start: 0.84, length: 0.06 }]
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
var ISLAND = { width: 126 / 393, height: 37 / 393, top: 11 / 393 };
|
|
69
|
+
var BUTTON_DEPTH = 8e-3;
|
|
70
|
+
function resolveDevice(device = "phone") {
|
|
71
|
+
return typeof device === "string" ? DEVICES[device] : device;
|
|
72
|
+
}
|
|
73
|
+
function deviceGeometry(device, width, cutout) {
|
|
74
|
+
const spec = resolveDevice(device);
|
|
75
|
+
const bezel = spec.bezel * width;
|
|
76
|
+
const screenWidth = width - bezel * 2;
|
|
77
|
+
const screenHeight = screenWidth * spec.viewport.height / spec.viewport.width;
|
|
78
|
+
const screenRadius = spec.screenRadius * screenWidth;
|
|
79
|
+
return {
|
|
80
|
+
width,
|
|
81
|
+
height: screenHeight + bezel * 2,
|
|
82
|
+
bezel,
|
|
83
|
+
radius: screenRadius + bezel,
|
|
84
|
+
screen: { width: screenWidth, height: screenHeight, radius: screenRadius },
|
|
85
|
+
island: (cutout ?? spec.cutout) === "island" ? {
|
|
86
|
+
width: ISLAND.width * screenWidth,
|
|
87
|
+
height: ISLAND.height * screenWidth,
|
|
88
|
+
top: ISLAND.top * screenWidth
|
|
89
|
+
} : void 0,
|
|
90
|
+
buttonDepth: BUTTON_DEPTH * width,
|
|
91
|
+
scale: screenWidth / spec.viewport.width
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
var round = (n) => Math.round(n * 1e4) / 1e4;
|
|
95
|
+
function deviceVars(device = "phone", options = {}) {
|
|
96
|
+
const spec = resolveDevice(device);
|
|
97
|
+
const g = deviceGeometry(spec, 100, options.cutout);
|
|
98
|
+
const vars = {
|
|
99
|
+
"--pd-device-aspect": `${spec.viewport.width} / ${spec.viewport.height}`,
|
|
100
|
+
"--pd-device-bezel": `${round(g.bezel)}`,
|
|
101
|
+
"--pd-device-radius": `${round(g.radius)}`,
|
|
102
|
+
"--pd-device-screen-radius": `${round(g.screen.radius)}`,
|
|
103
|
+
"--pd-device-button-depth": `${round(g.buttonDepth)}`
|
|
104
|
+
};
|
|
105
|
+
if (g.island) {
|
|
106
|
+
vars["--pd-device-island-width"] = `${round(g.island.width)}`;
|
|
107
|
+
vars["--pd-device-island-height"] = `${round(g.island.height)}`;
|
|
108
|
+
vars["--pd-device-island-top"] = `${round(g.island.top)}`;
|
|
109
|
+
}
|
|
110
|
+
if (options.maxHeight)
|
|
111
|
+
vars["--pd-device-max-width"] = `calc(${options.maxHeight} * ${round(g.width / g.height)})`;
|
|
112
|
+
return vars;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/core/html.ts
|
|
116
|
+
var esc = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
117
|
+
function deviceHTML(options) {
|
|
118
|
+
const spec = resolveDevice(options.device);
|
|
119
|
+
const cutout = options.cutout ?? spec.cutout;
|
|
120
|
+
const style = Object.entries(
|
|
121
|
+
deviceVars(spec, { cutout, maxHeight: options.maxHeight })
|
|
122
|
+
).map(([name, value]) => `${name}:${value}`).join(";");
|
|
123
|
+
const size = options.width && options.height ? ` width="${options.width}" height="${options.height}"` : "";
|
|
124
|
+
const buttons = spec.buttons.map(
|
|
125
|
+
(b) => `<span class="pd-device-button" data-side="${b.side}" style="--pd-device-button-start:${b.start * 100}%;--pd-device-button-length:${b.length * 100}%"></span>`
|
|
126
|
+
).join("");
|
|
127
|
+
const className = options.className ? `pd-device ${esc(options.className)}` : "pd-device";
|
|
128
|
+
return `<span class="${className}" style="${esc(style)}"><span class="pd-device-body">${buttons}<span class="pd-device-screen"><img class="pd-device-fill" src="${esc(options.src)}" alt="${esc(options.alt)}"${size} loading="${options.loading ?? "lazy"}" decoding="async">${cutout === "island" ? '<span class="pd-device-island"></span>' : ""}</span></span></span>`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/react/Device.tsx
|
|
132
|
+
var import_clsx = __toESM(require("clsx"));
|
|
133
|
+
var import_react = require("react");
|
|
134
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
135
|
+
var useIsoLayoutEffect = typeof window === "undefined" ? import_react.useEffect : import_react.useLayoutEffect;
|
|
136
|
+
function Device({
|
|
137
|
+
device = "phone",
|
|
138
|
+
cutout,
|
|
139
|
+
maxHeight,
|
|
140
|
+
className,
|
|
141
|
+
style,
|
|
142
|
+
src,
|
|
143
|
+
alt = "",
|
|
144
|
+
imgProps,
|
|
145
|
+
iframe,
|
|
146
|
+
title,
|
|
147
|
+
iframeProps,
|
|
148
|
+
children,
|
|
149
|
+
fit = "screen",
|
|
150
|
+
interactive = false,
|
|
151
|
+
overlay,
|
|
152
|
+
fallback,
|
|
153
|
+
loadTimeout,
|
|
154
|
+
ready,
|
|
155
|
+
failed,
|
|
156
|
+
onStatusChange
|
|
157
|
+
}) {
|
|
158
|
+
const spec = resolveDevice(device);
|
|
159
|
+
const shownCutout = cutout ?? (iframe ? "none" : spec.cutout);
|
|
160
|
+
const rootRef = (0, import_react.useRef)(null);
|
|
161
|
+
const screenRef = (0, import_react.useRef)(null);
|
|
162
|
+
const status = useLoadStatus({
|
|
163
|
+
active: !!iframe,
|
|
164
|
+
rootRef,
|
|
165
|
+
loadTimeout,
|
|
166
|
+
ready,
|
|
167
|
+
failed,
|
|
168
|
+
onStatusChange
|
|
169
|
+
});
|
|
170
|
+
const scaled = !!iframe || fit === "viewport";
|
|
171
|
+
useIsoLayoutEffect(() => {
|
|
172
|
+
const screen = screenRef.current;
|
|
173
|
+
if (!scaled || !screen) return;
|
|
174
|
+
const measure = () => {
|
|
175
|
+
if (!screen.clientWidth) return;
|
|
176
|
+
screen.style.setProperty(
|
|
177
|
+
"--pd-device-scale",
|
|
178
|
+
String(screen.clientWidth / spec.viewport.width)
|
|
179
|
+
);
|
|
180
|
+
screen.dataset.scaled = "";
|
|
181
|
+
};
|
|
182
|
+
measure();
|
|
183
|
+
const observer = new ResizeObserver(measure);
|
|
184
|
+
observer.observe(screen);
|
|
185
|
+
return () => observer.disconnect();
|
|
186
|
+
}, [scaled, spec.viewport.width]);
|
|
187
|
+
const viewportStyle = {
|
|
188
|
+
width: spec.viewport.width,
|
|
189
|
+
height: spec.viewport.height
|
|
190
|
+
};
|
|
191
|
+
let content;
|
|
192
|
+
if (iframe && status.value === "failed") {
|
|
193
|
+
content = fallback ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "pd-device-viewport", style: viewportStyle, children: fallback }) : null;
|
|
194
|
+
} else if (iframe) {
|
|
195
|
+
const { onLoad, ...rest } = iframeProps ?? {};
|
|
196
|
+
content = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
197
|
+
"iframe",
|
|
198
|
+
{
|
|
199
|
+
src: iframe,
|
|
200
|
+
title,
|
|
201
|
+
loading: "lazy",
|
|
202
|
+
tabIndex: interactive ? void 0 : -1,
|
|
203
|
+
...rest,
|
|
204
|
+
onLoad: (event) => {
|
|
205
|
+
status.markLoaded();
|
|
206
|
+
onLoad?.(event);
|
|
207
|
+
},
|
|
208
|
+
className: (0, import_clsx.default)("pd-device-viewport pd-device-content", rest.className),
|
|
209
|
+
style: { ...viewportStyle, ...rest.style }
|
|
210
|
+
}
|
|
211
|
+
);
|
|
212
|
+
} else if (src) {
|
|
213
|
+
content = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
214
|
+
"img",
|
|
215
|
+
{
|
|
216
|
+
src,
|
|
217
|
+
alt,
|
|
218
|
+
loading: "lazy",
|
|
219
|
+
decoding: "async",
|
|
220
|
+
...imgProps,
|
|
221
|
+
className: (0, import_clsx.default)("pd-device-fill", imgProps?.className)
|
|
222
|
+
}
|
|
223
|
+
);
|
|
224
|
+
} else if (children != null) {
|
|
225
|
+
content = fit === "viewport" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
226
|
+
"div",
|
|
227
|
+
{
|
|
228
|
+
className: "pd-device-viewport pd-device-content",
|
|
229
|
+
style: viewportStyle,
|
|
230
|
+
children
|
|
231
|
+
}
|
|
232
|
+
) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "pd-device-fill pd-device-content", children });
|
|
233
|
+
}
|
|
234
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
235
|
+
"div",
|
|
236
|
+
{
|
|
237
|
+
ref: rootRef,
|
|
238
|
+
className: (0, import_clsx.default)("pd-device", className),
|
|
239
|
+
"data-interactive": interactive,
|
|
240
|
+
"data-status": iframe ? status.value : void 0,
|
|
241
|
+
style: {
|
|
242
|
+
...deviceVars(spec, { cutout: shownCutout, maxHeight }),
|
|
243
|
+
...style
|
|
244
|
+
},
|
|
245
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "pd-device-body", children: [
|
|
246
|
+
spec.buttons.map((button) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
247
|
+
"span",
|
|
248
|
+
{
|
|
249
|
+
className: "pd-device-button",
|
|
250
|
+
"data-side": button.side,
|
|
251
|
+
style: {
|
|
252
|
+
"--pd-device-button-start": `${button.start * 100}%`,
|
|
253
|
+
"--pd-device-button-length": `${button.length * 100}%`
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
`${button.side}-${button.start}`
|
|
257
|
+
)),
|
|
258
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { ref: screenRef, className: "pd-device-screen", children: [
|
|
259
|
+
content,
|
|
260
|
+
overlay != null ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "pd-device-fill", children: overlay }) : null,
|
|
261
|
+
shownCutout === "island" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "pd-device-island" }) : null
|
|
262
|
+
] })
|
|
263
|
+
] })
|
|
264
|
+
}
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
function useLoadStatus({
|
|
268
|
+
active,
|
|
269
|
+
rootRef,
|
|
270
|
+
loadTimeout,
|
|
271
|
+
ready,
|
|
272
|
+
failed,
|
|
273
|
+
onStatusChange
|
|
274
|
+
}) {
|
|
275
|
+
const [own, setOwn] = (0, import_react.useState)("loading");
|
|
276
|
+
const value = failed ? "failed" : ready || own === "ready" ? "ready" : own;
|
|
277
|
+
const onChangeRef = (0, import_react.useRef)(onStatusChange);
|
|
278
|
+
(0, import_react.useEffect)(() => {
|
|
279
|
+
onChangeRef.current = onStatusChange;
|
|
280
|
+
});
|
|
281
|
+
(0, import_react.useEffect)(() => {
|
|
282
|
+
if (active) onChangeRef.current?.(value);
|
|
283
|
+
}, [active, value]);
|
|
284
|
+
const valueRef = (0, import_react.useRef)(value);
|
|
285
|
+
(0, import_react.useEffect)(() => {
|
|
286
|
+
valueRef.current = value;
|
|
287
|
+
});
|
|
288
|
+
(0, import_react.useEffect)(() => {
|
|
289
|
+
const root = rootRef.current;
|
|
290
|
+
if (!active || !loadTimeout || !root) return;
|
|
291
|
+
let timer;
|
|
292
|
+
const observer = new IntersectionObserver(
|
|
293
|
+
(entries) => {
|
|
294
|
+
if (!entries.some((entry) => entry.isIntersecting)) return;
|
|
295
|
+
observer.disconnect();
|
|
296
|
+
timer = setTimeout(() => {
|
|
297
|
+
if (valueRef.current === "loading") setOwn("failed");
|
|
298
|
+
}, loadTimeout);
|
|
299
|
+
},
|
|
300
|
+
{ threshold: 0.1 }
|
|
301
|
+
);
|
|
302
|
+
observer.observe(root);
|
|
303
|
+
return () => {
|
|
304
|
+
observer.disconnect();
|
|
305
|
+
clearTimeout(timer);
|
|
306
|
+
};
|
|
307
|
+
}, [active, loadTimeout, rootRef]);
|
|
308
|
+
return {
|
|
309
|
+
value,
|
|
310
|
+
markLoaded: () => setOwn((s) => s === "loading" ? "ready" : s)
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
314
|
+
0 && (module.exports = {
|
|
315
|
+
DEVICES,
|
|
316
|
+
Device,
|
|
317
|
+
deviceGeometry,
|
|
318
|
+
deviceHTML,
|
|
319
|
+
deviceVars,
|
|
320
|
+
resolveDevice
|
|
321
|
+
});
|
|
322
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/core/devices.ts","../src/core/html.ts","../src/react/Device.tsx"],"sourcesContent":["export * from './core'\nexport { Device, type DeviceProps, type DeviceStatus } from './react/Device'\n","export type Cutout = 'island' | 'none'\n\nexport interface DeviceButton {\n side: 'left' | 'right' | 'top'\n /** Where the button starts, as a fraction of that side's length. */\n start: number\n /** Button length, as a fraction of that side's length. */\n length: number\n}\n\nexport interface DeviceSpec {\n /**\n * The CSS-pixel viewport the screen shows. A screenshot or page laid out at\n * this size fills the screen exactly; the frame takes its shape from it.\n */\n viewport: { width: number; height: number }\n /** Bezel thickness, as a fraction of the device's outer width. */\n bezel: number\n /** Screen corner radius, as a fraction of the screen width. */\n screenRadius: number\n cutout: Cutout\n buttons: readonly DeviceButton[]\n}\n\n/**\n * Proportions follow the screen, not a particular model: the phone is a\n * 390 × 844 iPhone-class screen (iPhone 12 to 14), the size most app\n * screenshots and phone demos are laid out at; the tablet is an 1180 × 820\n * iPad Air in landscape.\n */\nexport const DEVICES = {\n phone: {\n viewport: { width: 390, height: 844 },\n bezel: 0.036,\n // Apple's 47.33 pt display corner radius on a 390 pt screen.\n screenRadius: 47.33 / 390,\n cutout: 'island',\n buttons: [\n { side: 'left', start: 0.175, length: 0.045 },\n { side: 'left', start: 0.255, length: 0.085 },\n { side: 'left', start: 0.36, length: 0.085 },\n { side: 'right', start: 0.29, length: 0.14 },\n ],\n },\n tablet: {\n viewport: { width: 1180, height: 820 },\n bezel: 0.032,\n // 18 pt display corners on a 1180 pt screen.\n screenRadius: 18 / 1180,\n cutout: 'none',\n buttons: [{ side: 'top', start: 0.84, length: 0.06 }],\n },\n} as const satisfies Record<string, DeviceSpec>\n\nexport type DeviceName = keyof typeof DEVICES\n\n/** The Dynamic Island, as fractions of the screen width (126 × 37 pt, 11 pt down, on a 393 pt screen). */\nconst ISLAND = { width: 126 / 393, height: 37 / 393, top: 11 / 393 }\n\n/** How far the side buttons stand out from the body, as a fraction of the device width. */\nconst BUTTON_DEPTH = 0.008\n\nexport function resolveDevice(device: DeviceName | DeviceSpec = 'phone') {\n return typeof device === 'string' ? DEVICES[device] : device\n}\n\nexport interface DeviceGeometry {\n width: number\n height: number\n bezel: number\n radius: number\n screen: { width: number; height: number; radius: number }\n /** Absent when the cutout is `none`. */\n island?: { width: number; height: number; top: number }\n buttonDepth: number\n /** Multiply the viewport by this to fit it to the screen. */\n scale: number\n}\n\n/**\n * Every measurement of a device drawn `width` units wide, in the same units.\n * For renderers that can't use the stylesheet, such as React Native.\n */\nexport function deviceGeometry(\n device: DeviceName | DeviceSpec,\n width: number,\n cutout?: Cutout,\n): DeviceGeometry {\n const spec = resolveDevice(device)\n const bezel = spec.bezel * width\n const screenWidth = width - bezel * 2\n const screenHeight =\n (screenWidth * spec.viewport.height) / spec.viewport.width\n const screenRadius = spec.screenRadius * screenWidth\n return {\n width,\n height: screenHeight + bezel * 2,\n bezel,\n radius: screenRadius + bezel,\n screen: { width: screenWidth, height: screenHeight, radius: screenRadius },\n island:\n (cutout ?? spec.cutout) === 'island'\n ? {\n width: ISLAND.width * screenWidth,\n height: ISLAND.height * screenWidth,\n top: ISLAND.top * screenWidth,\n }\n : undefined,\n buttonDepth: BUTTON_DEPTH * width,\n scale: screenWidth / spec.viewport.width,\n }\n}\n\nconst round = (n: number) => Math.round(n * 1e4) / 1e4\n\n/**\n * The custom properties `styles.css` draws a device from. Lengths are in\n * percent of the device's width and become `cqi` in the stylesheet.\n * `maxHeight` is any CSS length; the width shrinks to keep the shape.\n */\nexport function deviceVars(\n device: DeviceName | DeviceSpec = 'phone',\n options: { cutout?: Cutout; maxHeight?: string } = {},\n): Record<`--pd-device-${string}`, string> {\n const spec = resolveDevice(device)\n const g = deviceGeometry(spec, 100, options.cutout)\n const vars: Record<`--pd-device-${string}`, string> = {\n '--pd-device-aspect': `${spec.viewport.width} / ${spec.viewport.height}`,\n '--pd-device-bezel': `${round(g.bezel)}`,\n '--pd-device-radius': `${round(g.radius)}`,\n '--pd-device-screen-radius': `${round(g.screen.radius)}`,\n '--pd-device-button-depth': `${round(g.buttonDepth)}`,\n }\n if (g.island) {\n vars['--pd-device-island-width'] = `${round(g.island.width)}`\n vars['--pd-device-island-height'] = `${round(g.island.height)}`\n vars['--pd-device-island-top'] = `${round(g.island.top)}`\n }\n if (options.maxHeight)\n vars['--pd-device-max-width'] =\n `calc(${options.maxHeight} * ${round(g.width / g.height)})`\n return vars\n}\n","import {\n type Cutout,\n type DeviceName,\n type DeviceSpec,\n deviceVars,\n resolveDevice,\n} from './devices'\n\nconst esc = (s: string) =>\n s\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"')\n\nexport interface DeviceHTMLOptions {\n /** The screenshot. */\n src: string\n alt: string\n device?: DeviceName | DeviceSpec\n /** Defaults to the device's own cutout. */\n cutout?: Cutout\n maxHeight?: string\n className?: string\n /** The image's pixel size, so the page can reserve its space. */\n width?: number\n height?: number\n /** Defaults to `lazy`. */\n loading?: 'lazy' | 'eager'\n}\n\n/**\n * A screenshot in a device frame, as an HTML string, for pages rendered\n * without React such as Markdown output. Needs `styles.css`.\n */\nexport function deviceHTML(options: DeviceHTMLOptions): string {\n const spec = resolveDevice(options.device)\n const cutout = options.cutout ?? spec.cutout\n const style = Object.entries(\n deviceVars(spec, { cutout, maxHeight: options.maxHeight }),\n )\n .map(([name, value]) => `${name}:${value}`)\n .join(';')\n const size =\n options.width && options.height\n ? ` width=\"${options.width}\" height=\"${options.height}\"`\n : ''\n const buttons = spec.buttons\n .map(\n (b) =>\n `<span class=\"pd-device-button\" data-side=\"${b.side}\" style=\"--pd-device-button-start:${b.start * 100}%;--pd-device-button-length:${b.length * 100}%\"></span>`,\n )\n .join('')\n const className = options.className\n ? `pd-device ${esc(options.className)}`\n : 'pd-device'\n\n return `<span class=\"${className}\" style=\"${esc(style)}\"><span class=\"pd-device-body\">${buttons}<span class=\"pd-device-screen\"><img class=\"pd-device-fill\" src=\"${esc(options.src)}\" alt=\"${esc(options.alt)}\"${size} loading=\"${options.loading ?? 'lazy'}\" decoding=\"async\">${cutout === 'island' ? '<span class=\"pd-device-island\"></span>' : ''}</span></span></span>`\n}\n","import clsx from 'clsx'\nimport {\n type CSSProperties,\n type IframeHTMLAttributes,\n type ImgHTMLAttributes,\n type ReactNode,\n type RefObject,\n useEffect,\n useLayoutEffect,\n useRef,\n useState,\n} from 'react'\n\nimport {\n type Cutout,\n type DeviceName,\n type DeviceSpec,\n deviceVars,\n resolveDevice,\n} from '../core/devices'\n\nexport type DeviceStatus = 'loading' | 'ready' | 'failed'\n\nexport interface DeviceProps {\n /** A preset or your own spec. Defaults to `phone`. */\n device?: DeviceName | DeviceSpec\n /** Defaults to the device's own cutout, except over an iframe, where the\n * embedded page has no safe area and the island would cover its header. */\n cutout?: Cutout\n /** Any CSS length. The width shrinks so the whole device fits. */\n maxHeight?: string\n className?: string\n style?: CSSProperties\n\n /** A screenshot, drawn at the screen's size. */\n src?: string\n alt?: string\n imgProps?: ImgHTMLAttributes<HTMLImageElement>\n\n /** A page, laid out at the device's viewport and scaled to the screen. Takes\n * precedence over `src` and `children`. */\n iframe?: string\n /** Required with `iframe`; the frame's accessible name. */\n title?: string\n iframeProps?: IframeHTMLAttributes<HTMLIFrameElement>\n\n /** Anything else on the screen. */\n children?: ReactNode\n /** `screen` (default) draws children at the screen's size; `viewport` lays\n * them out at the device's viewport and scales them, like an iframe. */\n fit?: 'screen' | 'viewport'\n\n /** Lets pointer events reach the screen content. Off by default, so an\n * embedded page never traps a scroll gesture; an off iframe also leaves\n * the tab order. */\n interactive?: boolean\n /** Drawn over the screen at the screen's size, e.g. a poster until the\n * embedded page has drawn. Stays interactive. */\n overlay?: ReactNode\n\n /** Replaces the iframe, laid out at the viewport, once it has failed. */\n fallback?: ReactNode\n /** Milliseconds the iframe gets to load after it first scrolls into view\n * before it counts as failed. No timeout when omitted. */\n loadTimeout?: number\n /** Marks the iframe loaded, for pages that report it themselves (say with\n * `postMessage`) because `load` can fire before hydration. */\n ready?: boolean\n /** Marks the iframe failed and shows `fallback`. */\n failed?: boolean\n onStatusChange?: (status: DeviceStatus) => void\n}\n\nconst useIsoLayoutEffect =\n typeof window === 'undefined' ? useEffect : useLayoutEffect\n\n/**\n * A device frame around a screenshot, an embedded page or any content, drawn\n * in CSS from the proportions in `DEVICES`. Give it a width (or `maxHeight`);\n * the height follows from the device's shape.\n */\nexport function Device({\n device = 'phone',\n cutout,\n maxHeight,\n className,\n style,\n src,\n alt = '',\n imgProps,\n iframe,\n title,\n iframeProps,\n children,\n fit = 'screen',\n interactive = false,\n overlay,\n fallback,\n loadTimeout,\n ready,\n failed,\n onStatusChange,\n}: DeviceProps) {\n const spec = resolveDevice(device)\n const shownCutout = cutout ?? (iframe ? 'none' : spec.cutout)\n const rootRef = useRef<HTMLDivElement>(null)\n const screenRef = useRef<HTMLDivElement>(null)\n const status = useLoadStatus({\n active: !!iframe,\n rootRef,\n loadTimeout,\n ready,\n failed,\n onStatusChange,\n })\n const scaled = !!iframe || fit === 'viewport'\n\n useIsoLayoutEffect(() => {\n const screen = screenRef.current\n if (!scaled || !screen) return\n const measure = () => {\n if (!screen.clientWidth) return\n screen.style.setProperty(\n '--pd-device-scale',\n String(screen.clientWidth / spec.viewport.width),\n )\n screen.dataset.scaled = ''\n }\n measure()\n const observer = new ResizeObserver(measure)\n observer.observe(screen)\n return () => observer.disconnect()\n }, [scaled, spec.viewport.width])\n\n const viewportStyle = {\n width: spec.viewport.width,\n height: spec.viewport.height,\n }\n\n let content: ReactNode\n if (iframe && status.value === 'failed') {\n content = fallback ? (\n <div className=\"pd-device-viewport\" style={viewportStyle}>\n {fallback}\n </div>\n ) : null\n } else if (iframe) {\n const { onLoad, ...rest } = iframeProps ?? {}\n content = (\n <iframe\n src={iframe}\n title={title}\n loading=\"lazy\"\n tabIndex={interactive ? undefined : -1}\n {...rest}\n onLoad={(event) => {\n status.markLoaded()\n onLoad?.(event)\n }}\n className={clsx('pd-device-viewport pd-device-content', rest.className)}\n style={{ ...viewportStyle, ...rest.style }}\n />\n )\n } else if (src) {\n content = (\n <img\n src={src}\n alt={alt}\n loading=\"lazy\"\n decoding=\"async\"\n {...imgProps}\n className={clsx('pd-device-fill', imgProps?.className)}\n />\n )\n } else if (children != null) {\n content =\n fit === 'viewport' ? (\n <div\n className=\"pd-device-viewport pd-device-content\"\n style={viewportStyle}\n >\n {children}\n </div>\n ) : (\n <div className=\"pd-device-fill pd-device-content\">{children}</div>\n )\n }\n\n return (\n <div\n ref={rootRef}\n className={clsx('pd-device', className)}\n data-interactive={interactive}\n data-status={iframe ? status.value : undefined}\n style={\n {\n ...deviceVars(spec, { cutout: shownCutout, maxHeight }),\n ...style,\n } as CSSProperties\n }\n >\n <div className=\"pd-device-body\">\n {spec.buttons.map((button) => (\n <span\n key={`${button.side}-${button.start}`}\n className=\"pd-device-button\"\n data-side={button.side}\n style={\n {\n '--pd-device-button-start': `${button.start * 100}%`,\n '--pd-device-button-length': `${button.length * 100}%`,\n } as CSSProperties\n }\n />\n ))}\n <div ref={screenRef} className=\"pd-device-screen\">\n {content}\n {overlay != null ? (\n <div className=\"pd-device-fill\">{overlay}</div>\n ) : null}\n {shownCutout === 'island' ? (\n <span className=\"pd-device-island\" />\n ) : null}\n </div>\n </div>\n </div>\n )\n}\n\n/**\n * `loading` until the iframe fires `load` (or `ready` is passed), `failed` if\n * `loadTimeout` runs out first. Once ready it never regresses: a slow\n * navigation inside the page is not an outage. The clock starts when the\n * device first scrolls into view, since the iframe loads lazily.\n */\nfunction useLoadStatus({\n active,\n rootRef,\n loadTimeout,\n ready,\n failed,\n onStatusChange,\n}: {\n active: boolean\n rootRef: RefObject<HTMLDivElement | null>\n loadTimeout?: number\n ready?: boolean\n failed?: boolean\n onStatusChange?: (status: DeviceStatus) => void\n}) {\n const [own, setOwn] = useState<DeviceStatus>('loading')\n const value: DeviceStatus = failed\n ? 'failed'\n : ready || own === 'ready'\n ? 'ready'\n : own\n\n const onChangeRef = useRef(onStatusChange)\n useEffect(() => {\n onChangeRef.current = onStatusChange\n })\n useEffect(() => {\n if (active) onChangeRef.current?.(value)\n }, [active, value])\n\n const valueRef = useRef(value)\n useEffect(() => {\n valueRef.current = value\n })\n\n useEffect(() => {\n const root = rootRef.current\n if (!active || !loadTimeout || !root) return\n let timer: ReturnType<typeof setTimeout> | undefined\n const observer = new IntersectionObserver(\n (entries) => {\n if (!entries.some((entry) => entry.isIntersecting)) return\n observer.disconnect()\n timer = setTimeout(() => {\n if (valueRef.current === 'loading') setOwn('failed')\n }, loadTimeout)\n },\n { threshold: 0.1 },\n )\n observer.observe(root)\n return () => {\n observer.disconnect()\n clearTimeout(timer)\n }\n }, [active, loadTimeout, rootRef])\n\n return {\n value,\n markLoaded: () => setOwn((s) => (s === 'loading' ? 'ready' : s)),\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC8BO,IAAM,UAAU;AAAA,EACrB,OAAO;AAAA,IACL,UAAU,EAAE,OAAO,KAAK,QAAQ,IAAI;AAAA,IACpC,OAAO;AAAA;AAAA,IAEP,cAAc,QAAQ;AAAA,IACtB,QAAQ;AAAA,IACR,SAAS;AAAA,MACP,EAAE,MAAM,QAAQ,OAAO,OAAO,QAAQ,MAAM;AAAA,MAC5C,EAAE,MAAM,QAAQ,OAAO,OAAO,QAAQ,MAAM;AAAA,MAC5C,EAAE,MAAM,QAAQ,OAAO,MAAM,QAAQ,MAAM;AAAA,MAC3C,EAAE,MAAM,SAAS,OAAO,MAAM,QAAQ,KAAK;AAAA,IAC7C;AAAA,EACF;AAAA,EACA,QAAQ;AAAA,IACN,UAAU,EAAE,OAAO,MAAM,QAAQ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U,CAAC,eAAe,CAAC,KAAM;AACtC,QAAI;AACJ,UAAM,WAAW,IAAI;AAAA,MACnB,CAAC,YAAY;AACX,YAAI,CAAC,QAAQ,KAAK,CAAC,UAAU,MAAM,cAAc,EAAG;AACpD,iBAAS,WAAW;AACpB,gBAAQ,WAAW,MAAM;AACvB,cAAI,SAAS,YAAY,UAAW,QAAO,QAAQ;AAAA,QACrD,GAAG,WAAW;AAAA,MAChB;AAAA,MACA,EAAE,WAAW,IAAI;AAAA,IACnB;AACA,aAAS,QAAQ,IAAI;AACrB,WAAO,MAAM;AACX,eAAS,WAAW;AACpB,mBAAa,KAAK;AAAA,IACpB;AAAA,EACF,GAAG,CAAC,QAAQ,aAAa,OAAO,CAAC;AAEjC,SAAO;AAAA,IACL;AAAA,IACA,YAAY,MAAM,OAAO,CAAC,MAAO,MAAM,YAAY,UAAU,CAAE;AAAA,EACjE;AACF;","names":["clsx"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import "./styles.css";
|
|
3
|
+
|
|
4
|
+
// src/core/devices.ts
|
|
5
|
+
var DEVICES = {
|
|
6
|
+
phone: {
|
|
7
|
+
viewport: { width: 390, height: 844 },
|
|
8
|
+
bezel: 0.036,
|
|
9
|
+
// Apple's 47.33 pt display corner radius on a 390 pt screen.
|
|
10
|
+
screenRadius: 47.33 / 390,
|
|
11
|
+
cutout: "island",
|
|
12
|
+
buttons: [
|
|
13
|
+
{ side: "left", start: 0.175, length: 0.045 },
|
|
14
|
+
{ side: "left", start: 0.255, length: 0.085 },
|
|
15
|
+
{ side: "left", start: 0.36, length: 0.085 },
|
|
16
|
+
{ side: "right", start: 0.29, length: 0.14 }
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
tablet: {
|
|
20
|
+
viewport: { width: 1180, height: 820 },
|
|
21
|
+
bezel: 0.032,
|
|
22
|
+
// 18 pt display corners on a 1180 pt screen.
|
|
23
|
+
screenRadius: 18 / 1180,
|
|
24
|
+
cutout: "none",
|
|
25
|
+
buttons: [{ side: "top", start: 0.84, length: 0.06 }]
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var ISLAND = { width: 126 / 393, height: 37 / 393, top: 11 / 393 };
|
|
29
|
+
var BUTTON_DEPTH = 8e-3;
|
|
30
|
+
function resolveDevice(device = "phone") {
|
|
31
|
+
return typeof device === "string" ? DEVICES[device] : device;
|
|
32
|
+
}
|
|
33
|
+
function deviceGeometry(device, width, cutout) {
|
|
34
|
+
const spec = resolveDevice(device);
|
|
35
|
+
const bezel = spec.bezel * width;
|
|
36
|
+
const screenWidth = width - bezel * 2;
|
|
37
|
+
const screenHeight = screenWidth * spec.viewport.height / spec.viewport.width;
|
|
38
|
+
const screenRadius = spec.screenRadius * screenWidth;
|
|
39
|
+
return {
|
|
40
|
+
width,
|
|
41
|
+
height: screenHeight + bezel * 2,
|
|
42
|
+
bezel,
|
|
43
|
+
radius: screenRadius + bezel,
|
|
44
|
+
screen: { width: screenWidth, height: screenHeight, radius: screenRadius },
|
|
45
|
+
island: (cutout ?? spec.cutout) === "island" ? {
|
|
46
|
+
width: ISLAND.width * screenWidth,
|
|
47
|
+
height: ISLAND.height * screenWidth,
|
|
48
|
+
top: ISLAND.top * screenWidth
|
|
49
|
+
} : void 0,
|
|
50
|
+
buttonDepth: BUTTON_DEPTH * width,
|
|
51
|
+
scale: screenWidth / spec.viewport.width
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
var round = (n) => Math.round(n * 1e4) / 1e4;
|
|
55
|
+
function deviceVars(device = "phone", options = {}) {
|
|
56
|
+
const spec = resolveDevice(device);
|
|
57
|
+
const g = deviceGeometry(spec, 100, options.cutout);
|
|
58
|
+
const vars = {
|
|
59
|
+
"--pd-device-aspect": `${spec.viewport.width} / ${spec.viewport.height}`,
|
|
60
|
+
"--pd-device-bezel": `${round(g.bezel)}`,
|
|
61
|
+
"--pd-device-radius": `${round(g.radius)}`,
|
|
62
|
+
"--pd-device-screen-radius": `${round(g.screen.radius)}`,
|
|
63
|
+
"--pd-device-button-depth": `${round(g.buttonDepth)}`
|
|
64
|
+
};
|
|
65
|
+
if (g.island) {
|
|
66
|
+
vars["--pd-device-island-width"] = `${round(g.island.width)}`;
|
|
67
|
+
vars["--pd-device-island-height"] = `${round(g.island.height)}`;
|
|
68
|
+
vars["--pd-device-island-top"] = `${round(g.island.top)}`;
|
|
69
|
+
}
|
|
70
|
+
if (options.maxHeight)
|
|
71
|
+
vars["--pd-device-max-width"] = `calc(${options.maxHeight} * ${round(g.width / g.height)})`;
|
|
72
|
+
return vars;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/core/html.ts
|
|
76
|
+
var esc = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
77
|
+
function deviceHTML(options) {
|
|
78
|
+
const spec = resolveDevice(options.device);
|
|
79
|
+
const cutout = options.cutout ?? spec.cutout;
|
|
80
|
+
const style = Object.entries(
|
|
81
|
+
deviceVars(spec, { cutout, maxHeight: options.maxHeight })
|
|
82
|
+
).map(([name, value]) => `${name}:${value}`).join(";");
|
|
83
|
+
const size = options.width && options.height ? ` width="${options.width}" height="${options.height}"` : "";
|
|
84
|
+
const buttons = spec.buttons.map(
|
|
85
|
+
(b) => `<span class="pd-device-button" data-side="${b.side}" style="--pd-device-button-start:${b.start * 100}%;--pd-device-button-length:${b.length * 100}%"></span>`
|
|
86
|
+
).join("");
|
|
87
|
+
const className = options.className ? `pd-device ${esc(options.className)}` : "pd-device";
|
|
88
|
+
return `<span class="${className}" style="${esc(style)}"><span class="pd-device-body">${buttons}<span class="pd-device-screen"><img class="pd-device-fill" src="${esc(options.src)}" alt="${esc(options.alt)}"${size} loading="${options.loading ?? "lazy"}" decoding="async">${cutout === "island" ? '<span class="pd-device-island"></span>' : ""}</span></span></span>`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/react/Device.tsx
|
|
92
|
+
import clsx from "clsx";
|
|
93
|
+
import {
|
|
94
|
+
useEffect,
|
|
95
|
+
useLayoutEffect,
|
|
96
|
+
useRef,
|
|
97
|
+
useState
|
|
98
|
+
} from "react";
|
|
99
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
100
|
+
var useIsoLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
|
|
101
|
+
function Device({
|
|
102
|
+
device = "phone",
|
|
103
|
+
cutout,
|
|
104
|
+
maxHeight,
|
|
105
|
+
className,
|
|
106
|
+
style,
|
|
107
|
+
src,
|
|
108
|
+
alt = "",
|
|
109
|
+
imgProps,
|
|
110
|
+
iframe,
|
|
111
|
+
title,
|
|
112
|
+
iframeProps,
|
|
113
|
+
children,
|
|
114
|
+
fit = "screen",
|
|
115
|
+
interactive = false,
|
|
116
|
+
overlay,
|
|
117
|
+
fallback,
|
|
118
|
+
loadTimeout,
|
|
119
|
+
ready,
|
|
120
|
+
failed,
|
|
121
|
+
onStatusChange
|
|
122
|
+
}) {
|
|
123
|
+
const spec = resolveDevice(device);
|
|
124
|
+
const shownCutout = cutout ?? (iframe ? "none" : spec.cutout);
|
|
125
|
+
const rootRef = useRef(null);
|
|
126
|
+
const screenRef = useRef(null);
|
|
127
|
+
const status = useLoadStatus({
|
|
128
|
+
active: !!iframe,
|
|
129
|
+
rootRef,
|
|
130
|
+
loadTimeout,
|
|
131
|
+
ready,
|
|
132
|
+
failed,
|
|
133
|
+
onStatusChange
|
|
134
|
+
});
|
|
135
|
+
const scaled = !!iframe || fit === "viewport";
|
|
136
|
+
useIsoLayoutEffect(() => {
|
|
137
|
+
const screen = screenRef.current;
|
|
138
|
+
if (!scaled || !screen) return;
|
|
139
|
+
const measure = () => {
|
|
140
|
+
if (!screen.clientWidth) return;
|
|
141
|
+
screen.style.setProperty(
|
|
142
|
+
"--pd-device-scale",
|
|
143
|
+
String(screen.clientWidth / spec.viewport.width)
|
|
144
|
+
);
|
|
145
|
+
screen.dataset.scaled = "";
|
|
146
|
+
};
|
|
147
|
+
measure();
|
|
148
|
+
const observer = new ResizeObserver(measure);
|
|
149
|
+
observer.observe(screen);
|
|
150
|
+
return () => observer.disconnect();
|
|
151
|
+
}, [scaled, spec.viewport.width]);
|
|
152
|
+
const viewportStyle = {
|
|
153
|
+
width: spec.viewport.width,
|
|
154
|
+
height: spec.viewport.height
|
|
155
|
+
};
|
|
156
|
+
let content;
|
|
157
|
+
if (iframe && status.value === "failed") {
|
|
158
|
+
content = fallback ? /* @__PURE__ */ jsx("div", { className: "pd-device-viewport", style: viewportStyle, children: fallback }) : null;
|
|
159
|
+
} else if (iframe) {
|
|
160
|
+
const { onLoad, ...rest } = iframeProps ?? {};
|
|
161
|
+
content = /* @__PURE__ */ jsx(
|
|
162
|
+
"iframe",
|
|
163
|
+
{
|
|
164
|
+
src: iframe,
|
|
165
|
+
title,
|
|
166
|
+
loading: "lazy",
|
|
167
|
+
tabIndex: interactive ? void 0 : -1,
|
|
168
|
+
...rest,
|
|
169
|
+
onLoad: (event) => {
|
|
170
|
+
status.markLoaded();
|
|
171
|
+
onLoad?.(event);
|
|
172
|
+
},
|
|
173
|
+
className: clsx("pd-device-viewport pd-device-content", rest.className),
|
|
174
|
+
style: { ...viewportStyle, ...rest.style }
|
|
175
|
+
}
|
|
176
|
+
);
|
|
177
|
+
} else if (src) {
|
|
178
|
+
content = /* @__PURE__ */ jsx(
|
|
179
|
+
"img",
|
|
180
|
+
{
|
|
181
|
+
src,
|
|
182
|
+
alt,
|
|
183
|
+
loading: "lazy",
|
|
184
|
+
decoding: "async",
|
|
185
|
+
...imgProps,
|
|
186
|
+
className: clsx("pd-device-fill", imgProps?.className)
|
|
187
|
+
}
|
|
188
|
+
);
|
|
189
|
+
} else if (children != null) {
|
|
190
|
+
content = fit === "viewport" ? /* @__PURE__ */ jsx(
|
|
191
|
+
"div",
|
|
192
|
+
{
|
|
193
|
+
className: "pd-device-viewport pd-device-content",
|
|
194
|
+
style: viewportStyle,
|
|
195
|
+
children
|
|
196
|
+
}
|
|
197
|
+
) : /* @__PURE__ */ jsx("div", { className: "pd-device-fill pd-device-content", children });
|
|
198
|
+
}
|
|
199
|
+
return /* @__PURE__ */ jsx(
|
|
200
|
+
"div",
|
|
201
|
+
{
|
|
202
|
+
ref: rootRef,
|
|
203
|
+
className: clsx("pd-device", className),
|
|
204
|
+
"data-interactive": interactive,
|
|
205
|
+
"data-status": iframe ? status.value : void 0,
|
|
206
|
+
style: {
|
|
207
|
+
...deviceVars(spec, { cutout: shownCutout, maxHeight }),
|
|
208
|
+
...style
|
|
209
|
+
},
|
|
210
|
+
children: /* @__PURE__ */ jsxs("div", { className: "pd-device-body", children: [
|
|
211
|
+
spec.buttons.map((button) => /* @__PURE__ */ jsx(
|
|
212
|
+
"span",
|
|
213
|
+
{
|
|
214
|
+
className: "pd-device-button",
|
|
215
|
+
"data-side": button.side,
|
|
216
|
+
style: {
|
|
217
|
+
"--pd-device-button-start": `${button.start * 100}%`,
|
|
218
|
+
"--pd-device-button-length": `${button.length * 100}%`
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
`${button.side}-${button.start}`
|
|
222
|
+
)),
|
|
223
|
+
/* @__PURE__ */ jsxs("div", { ref: screenRef, className: "pd-device-screen", children: [
|
|
224
|
+
content,
|
|
225
|
+
overlay != null ? /* @__PURE__ */ jsx("div", { className: "pd-device-fill", children: overlay }) : null,
|
|
226
|
+
shownCutout === "island" ? /* @__PURE__ */ jsx("span", { className: "pd-device-island" }) : null
|
|
227
|
+
] })
|
|
228
|
+
] })
|
|
229
|
+
}
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
function useLoadStatus({
|
|
233
|
+
active,
|
|
234
|
+
rootRef,
|
|
235
|
+
loadTimeout,
|
|
236
|
+
ready,
|
|
237
|
+
failed,
|
|
238
|
+
onStatusChange
|
|
239
|
+
}) {
|
|
240
|
+
const [own, setOwn] = useState("loading");
|
|
241
|
+
const value = failed ? "failed" : ready || own === "ready" ? "ready" : own;
|
|
242
|
+
const onChangeRef = useRef(onStatusChange);
|
|
243
|
+
useEffect(() => {
|
|
244
|
+
onChangeRef.current = onStatusChange;
|
|
245
|
+
});
|
|
246
|
+
useEffect(() => {
|
|
247
|
+
if (active) onChangeRef.current?.(value);
|
|
248
|
+
}, [active, value]);
|
|
249
|
+
const valueRef = useRef(value);
|
|
250
|
+
useEffect(() => {
|
|
251
|
+
valueRef.current = value;
|
|
252
|
+
});
|
|
253
|
+
useEffect(() => {
|
|
254
|
+
const root = rootRef.current;
|
|
255
|
+
if (!active || !loadTimeout || !root) return;
|
|
256
|
+
let timer;
|
|
257
|
+
const observer = new IntersectionObserver(
|
|
258
|
+
(entries) => {
|
|
259
|
+
if (!entries.some((entry) => entry.isIntersecting)) return;
|
|
260
|
+
observer.disconnect();
|
|
261
|
+
timer = setTimeout(() => {
|
|
262
|
+
if (valueRef.current === "loading") setOwn("failed");
|
|
263
|
+
}, loadTimeout);
|
|
264
|
+
},
|
|
265
|
+
{ threshold: 0.1 }
|
|
266
|
+
);
|
|
267
|
+
observer.observe(root);
|
|
268
|
+
return () => {
|
|
269
|
+
observer.disconnect();
|
|
270
|
+
clearTimeout(timer);
|
|
271
|
+
};
|
|
272
|
+
}, [active, loadTimeout, rootRef]);
|
|
273
|
+
return {
|
|
274
|
+
value,
|
|
275
|
+
markLoaded: () => setOwn((s) => s === "loading" ? "ready" : s)
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
export {
|
|
279
|
+
DEVICES,
|
|
280
|
+
Device,
|
|
281
|
+
deviceGeometry,
|
|
282
|
+
deviceHTML,
|
|
283
|
+
deviceVars,
|
|
284
|
+
resolveDevice
|
|
285
|
+
};
|
|
286
|
+
//# sourceMappingURL=index.mjs.map
|