@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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Present Day
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,72 @@
1
- # Temporary Holding Version
1
+ # @present-day/device
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Phone and tablet frames for screenshots, embedded pages and live content. Every frame is drawn in CSS from one set of proportions in [`src/core/devices.ts`](src/core/devices.ts), so a change there reaches every site that uses the package.
4
+
5
+ The frame takes its shape from the content: the phone screen is 390 × 844 CSS pixels (an iPhone 12 to 14), so a screenshot or a page laid out at that size fills it exactly.
6
+
7
+ ```sh
8
+ npm i @present-day/device
9
+ ```
10
+
11
+ ## React
12
+
13
+ ```tsx
14
+ import { Device } from '@present-day/device'
15
+
16
+ // A screenshot
17
+ <Device src="/shots/map.webp" alt="The map" className="w-72" />
18
+
19
+ // A live page, laid out at 390 × 844 and scaled to fit
20
+ <Device iframe="https://example.com/demo" title="Live demo" className="w-72" />
21
+
22
+ // Anything else
23
+ <Device className="w-72">
24
+ <MessageThread />
25
+ </Device>
26
+ ```
27
+
28
+ The stylesheet is imported for you. Give the device a width, or a `maxHeight` (any CSS length); its height follows from the shape.
29
+
30
+ | Prop | Default | |
31
+ | -------------------------------- | --------------- | ---------------------------------------------------------------------------------------------------------- |
32
+ | `device` | `'phone'` | `'phone'`, `'tablet'` (1180 × 820), or your own `DeviceSpec` |
33
+ | `cutout` | the device's | `'island'` or `'none'`. Over an iframe it defaults to `'none'`, since the embedded page has no safe area |
34
+ | `maxHeight` | | Any CSS length; the width shrinks to keep the whole device in it |
35
+ | `src`, `alt`, `imgProps` | | A screenshot, drawn at the screen's size |
36
+ | `iframe`, `title`, `iframeProps` | | A page laid out at the viewport and scaled to the screen. Wins over `src` and `children` |
37
+ | `children`, `fit` | `fit: 'screen'` | Any content. `fit: 'viewport'` lays it out at the viewport and scales it like an iframe |
38
+ | `interactive` | `false` | Lets pointer events reach the screen. Off, an iframe also leaves the tab order, so it never traps a scroll |
39
+ | `overlay` | | Drawn over the screen and stays interactive, e.g. a poster until the page has drawn |
40
+ | `fallback` | | Replaces the iframe, laid out at the viewport, once it has failed |
41
+ | `loadTimeout` | | Milliseconds the iframe gets after it first scrolls into view before it counts as failed |
42
+ | `ready`, `failed` | | Outside signals, e.g. from `postMessage`, since `load` can fire before hydration |
43
+ | `onStatusChange` | | `'loading'`, `'ready'` or `'failed'` |
44
+
45
+ ## Without React
46
+
47
+ `deviceHTML()` returns the same frame as an HTML string, for Markdown output or other server-rendered pages. It uses only `<span>` and `<img>`, so it can sit inside a `<button>` or `<figure>`. Load `@present-day/device/styles.css` on the page.
48
+
49
+ ```ts
50
+ import { deviceHTML } from '@present-day/device/core'
51
+
52
+ deviceHTML({ src: '/shots/map.webp', alt: 'The map', width: 390, height: 844 })
53
+ ```
54
+
55
+ `@present-day/device/core` has no React, no stylesheet import and no `"use client"`, so it is safe in build scripts and React Native. `deviceGeometry(device, width)` gives every measurement in your own units for renderers that can't use the stylesheet.
56
+
57
+ ## Theming
58
+
59
+ | Custom property | Default |
60
+ | -------------------------- | --------------------------------------------------------------------------- |
61
+ | `--pd-device-color` | `#1c1c1e` (body and buttons) |
62
+ | `--pd-device-screen-color` | `#000` (behind content while it loads) |
63
+ | `--pd-device-edge` | `hsl(0 0% 50% / 0.25)` (hairline that keeps the body visible on dark pages) |
64
+ | `--pd-device-shadow` | `0 24px 48px -24px rgb(0 0 0 / 0.45)` |
65
+
66
+ ## Development
67
+
68
+ ```sh
69
+ bun install
70
+ bun run storybook
71
+ bun run test
72
+ ```
@@ -0,0 +1,169 @@
1
+ type Cutout = 'island' | 'none';
2
+ interface DeviceButton {
3
+ side: 'left' | 'right' | 'top';
4
+ /** Where the button starts, as a fraction of that side's length. */
5
+ start: number;
6
+ /** Button length, as a fraction of that side's length. */
7
+ length: number;
8
+ }
9
+ interface DeviceSpec {
10
+ /**
11
+ * The CSS-pixel viewport the screen shows. A screenshot or page laid out at
12
+ * this size fills the screen exactly; the frame takes its shape from it.
13
+ */
14
+ viewport: {
15
+ width: number;
16
+ height: number;
17
+ };
18
+ /** Bezel thickness, as a fraction of the device's outer width. */
19
+ bezel: number;
20
+ /** Screen corner radius, as a fraction of the screen width. */
21
+ screenRadius: number;
22
+ cutout: Cutout;
23
+ buttons: readonly DeviceButton[];
24
+ }
25
+ /**
26
+ * Proportions follow the screen, not a particular model: the phone is a
27
+ * 390 × 844 iPhone-class screen (iPhone 12 to 14), the size most app
28
+ * screenshots and phone demos are laid out at; the tablet is an 1180 × 820
29
+ * iPad Air in landscape.
30
+ */
31
+ declare const DEVICES: {
32
+ readonly phone: {
33
+ readonly viewport: {
34
+ readonly width: 390;
35
+ readonly height: 844;
36
+ };
37
+ readonly bezel: 0.036;
38
+ readonly screenRadius: number;
39
+ readonly cutout: "island";
40
+ readonly buttons: readonly [{
41
+ readonly side: "left";
42
+ readonly start: 0.175;
43
+ readonly length: 0.045;
44
+ }, {
45
+ readonly side: "left";
46
+ readonly start: 0.255;
47
+ readonly length: 0.085;
48
+ }, {
49
+ readonly side: "left";
50
+ readonly start: 0.36;
51
+ readonly length: 0.085;
52
+ }, {
53
+ readonly side: "right";
54
+ readonly start: 0.29;
55
+ readonly length: 0.14;
56
+ }];
57
+ };
58
+ readonly tablet: {
59
+ readonly viewport: {
60
+ readonly width: 1180;
61
+ readonly height: 820;
62
+ };
63
+ readonly bezel: 0.032;
64
+ readonly screenRadius: number;
65
+ readonly cutout: "none";
66
+ readonly buttons: readonly [{
67
+ readonly side: "top";
68
+ readonly start: 0.84;
69
+ readonly length: 0.06;
70
+ }];
71
+ };
72
+ };
73
+ type DeviceName = keyof typeof DEVICES;
74
+ declare function resolveDevice(device?: DeviceName | DeviceSpec): DeviceSpec | {
75
+ readonly viewport: {
76
+ readonly width: 390;
77
+ readonly height: 844;
78
+ };
79
+ readonly bezel: 0.036;
80
+ readonly screenRadius: number;
81
+ readonly cutout: "island";
82
+ readonly buttons: readonly [{
83
+ readonly side: "left";
84
+ readonly start: 0.175;
85
+ readonly length: 0.045;
86
+ }, {
87
+ readonly side: "left";
88
+ readonly start: 0.255;
89
+ readonly length: 0.085;
90
+ }, {
91
+ readonly side: "left";
92
+ readonly start: 0.36;
93
+ readonly length: 0.085;
94
+ }, {
95
+ readonly side: "right";
96
+ readonly start: 0.29;
97
+ readonly length: 0.14;
98
+ }];
99
+ } | {
100
+ readonly viewport: {
101
+ readonly width: 1180;
102
+ readonly height: 820;
103
+ };
104
+ readonly bezel: 0.032;
105
+ readonly screenRadius: number;
106
+ readonly cutout: "none";
107
+ readonly buttons: readonly [{
108
+ readonly side: "top";
109
+ readonly start: 0.84;
110
+ readonly length: 0.06;
111
+ }];
112
+ };
113
+ interface DeviceGeometry {
114
+ width: number;
115
+ height: number;
116
+ bezel: number;
117
+ radius: number;
118
+ screen: {
119
+ width: number;
120
+ height: number;
121
+ radius: number;
122
+ };
123
+ /** Absent when the cutout is `none`. */
124
+ island?: {
125
+ width: number;
126
+ height: number;
127
+ top: number;
128
+ };
129
+ buttonDepth: number;
130
+ /** Multiply the viewport by this to fit it to the screen. */
131
+ scale: number;
132
+ }
133
+ /**
134
+ * Every measurement of a device drawn `width` units wide, in the same units.
135
+ * For renderers that can't use the stylesheet, such as React Native.
136
+ */
137
+ declare function deviceGeometry(device: DeviceName | DeviceSpec, width: number, cutout?: Cutout): DeviceGeometry;
138
+ /**
139
+ * The custom properties `styles.css` draws a device from. Lengths are in
140
+ * percent of the device's width and become `cqi` in the stylesheet.
141
+ * `maxHeight` is any CSS length; the width shrinks to keep the shape.
142
+ */
143
+ declare function deviceVars(device?: DeviceName | DeviceSpec, options?: {
144
+ cutout?: Cutout;
145
+ maxHeight?: string;
146
+ }): Record<`--pd-device-${string}`, string>;
147
+
148
+ interface DeviceHTMLOptions {
149
+ /** The screenshot. */
150
+ src: string;
151
+ alt: string;
152
+ device?: DeviceName | DeviceSpec;
153
+ /** Defaults to the device's own cutout. */
154
+ cutout?: Cutout;
155
+ maxHeight?: string;
156
+ className?: string;
157
+ /** The image's pixel size, so the page can reserve its space. */
158
+ width?: number;
159
+ height?: number;
160
+ /** Defaults to `lazy`. */
161
+ loading?: 'lazy' | 'eager';
162
+ }
163
+ /**
164
+ * A screenshot in a device frame, as an HTML string, for pages rendered
165
+ * without React such as Markdown output. Needs `styles.css`.
166
+ */
167
+ declare function deviceHTML(options: DeviceHTMLOptions): string;
168
+
169
+ export { type Cutout, DEVICES, type DeviceButton, type DeviceGeometry, type DeviceHTMLOptions, type DeviceName, type DeviceSpec, deviceGeometry, deviceHTML, deviceVars, resolveDevice };
package/dist/core.d.ts ADDED
@@ -0,0 +1,169 @@
1
+ type Cutout = 'island' | 'none';
2
+ interface DeviceButton {
3
+ side: 'left' | 'right' | 'top';
4
+ /** Where the button starts, as a fraction of that side's length. */
5
+ start: number;
6
+ /** Button length, as a fraction of that side's length. */
7
+ length: number;
8
+ }
9
+ interface DeviceSpec {
10
+ /**
11
+ * The CSS-pixel viewport the screen shows. A screenshot or page laid out at
12
+ * this size fills the screen exactly; the frame takes its shape from it.
13
+ */
14
+ viewport: {
15
+ width: number;
16
+ height: number;
17
+ };
18
+ /** Bezel thickness, as a fraction of the device's outer width. */
19
+ bezel: number;
20
+ /** Screen corner radius, as a fraction of the screen width. */
21
+ screenRadius: number;
22
+ cutout: Cutout;
23
+ buttons: readonly DeviceButton[];
24
+ }
25
+ /**
26
+ * Proportions follow the screen, not a particular model: the phone is a
27
+ * 390 × 844 iPhone-class screen (iPhone 12 to 14), the size most app
28
+ * screenshots and phone demos are laid out at; the tablet is an 1180 × 820
29
+ * iPad Air in landscape.
30
+ */
31
+ declare const DEVICES: {
32
+ readonly phone: {
33
+ readonly viewport: {
34
+ readonly width: 390;
35
+ readonly height: 844;
36
+ };
37
+ readonly bezel: 0.036;
38
+ readonly screenRadius: number;
39
+ readonly cutout: "island";
40
+ readonly buttons: readonly [{
41
+ readonly side: "left";
42
+ readonly start: 0.175;
43
+ readonly length: 0.045;
44
+ }, {
45
+ readonly side: "left";
46
+ readonly start: 0.255;
47
+ readonly length: 0.085;
48
+ }, {
49
+ readonly side: "left";
50
+ readonly start: 0.36;
51
+ readonly length: 0.085;
52
+ }, {
53
+ readonly side: "right";
54
+ readonly start: 0.29;
55
+ readonly length: 0.14;
56
+ }];
57
+ };
58
+ readonly tablet: {
59
+ readonly viewport: {
60
+ readonly width: 1180;
61
+ readonly height: 820;
62
+ };
63
+ readonly bezel: 0.032;
64
+ readonly screenRadius: number;
65
+ readonly cutout: "none";
66
+ readonly buttons: readonly [{
67
+ readonly side: "top";
68
+ readonly start: 0.84;
69
+ readonly length: 0.06;
70
+ }];
71
+ };
72
+ };
73
+ type DeviceName = keyof typeof DEVICES;
74
+ declare function resolveDevice(device?: DeviceName | DeviceSpec): DeviceSpec | {
75
+ readonly viewport: {
76
+ readonly width: 390;
77
+ readonly height: 844;
78
+ };
79
+ readonly bezel: 0.036;
80
+ readonly screenRadius: number;
81
+ readonly cutout: "island";
82
+ readonly buttons: readonly [{
83
+ readonly side: "left";
84
+ readonly start: 0.175;
85
+ readonly length: 0.045;
86
+ }, {
87
+ readonly side: "left";
88
+ readonly start: 0.255;
89
+ readonly length: 0.085;
90
+ }, {
91
+ readonly side: "left";
92
+ readonly start: 0.36;
93
+ readonly length: 0.085;
94
+ }, {
95
+ readonly side: "right";
96
+ readonly start: 0.29;
97
+ readonly length: 0.14;
98
+ }];
99
+ } | {
100
+ readonly viewport: {
101
+ readonly width: 1180;
102
+ readonly height: 820;
103
+ };
104
+ readonly bezel: 0.032;
105
+ readonly screenRadius: number;
106
+ readonly cutout: "none";
107
+ readonly buttons: readonly [{
108
+ readonly side: "top";
109
+ readonly start: 0.84;
110
+ readonly length: 0.06;
111
+ }];
112
+ };
113
+ interface DeviceGeometry {
114
+ width: number;
115
+ height: number;
116
+ bezel: number;
117
+ radius: number;
118
+ screen: {
119
+ width: number;
120
+ height: number;
121
+ radius: number;
122
+ };
123
+ /** Absent when the cutout is `none`. */
124
+ island?: {
125
+ width: number;
126
+ height: number;
127
+ top: number;
128
+ };
129
+ buttonDepth: number;
130
+ /** Multiply the viewport by this to fit it to the screen. */
131
+ scale: number;
132
+ }
133
+ /**
134
+ * Every measurement of a device drawn `width` units wide, in the same units.
135
+ * For renderers that can't use the stylesheet, such as React Native.
136
+ */
137
+ declare function deviceGeometry(device: DeviceName | DeviceSpec, width: number, cutout?: Cutout): DeviceGeometry;
138
+ /**
139
+ * The custom properties `styles.css` draws a device from. Lengths are in
140
+ * percent of the device's width and become `cqi` in the stylesheet.
141
+ * `maxHeight` is any CSS length; the width shrinks to keep the shape.
142
+ */
143
+ declare function deviceVars(device?: DeviceName | DeviceSpec, options?: {
144
+ cutout?: Cutout;
145
+ maxHeight?: string;
146
+ }): Record<`--pd-device-${string}`, string>;
147
+
148
+ interface DeviceHTMLOptions {
149
+ /** The screenshot. */
150
+ src: string;
151
+ alt: string;
152
+ device?: DeviceName | DeviceSpec;
153
+ /** Defaults to the device's own cutout. */
154
+ cutout?: Cutout;
155
+ maxHeight?: string;
156
+ className?: string;
157
+ /** The image's pixel size, so the page can reserve its space. */
158
+ width?: number;
159
+ height?: number;
160
+ /** Defaults to `lazy`. */
161
+ loading?: 'lazy' | 'eager';
162
+ }
163
+ /**
164
+ * A screenshot in a device frame, as an HTML string, for pages rendered
165
+ * without React such as Markdown output. Needs `styles.css`.
166
+ */
167
+ declare function deviceHTML(options: DeviceHTMLOptions): string;
168
+
169
+ export { type Cutout, DEVICES, type DeviceButton, type DeviceGeometry, type DeviceHTMLOptions, type DeviceName, type DeviceSpec, deviceGeometry, deviceHTML, deviceVars, resolveDevice };
package/dist/core.js ADDED
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/core/index.ts
21
+ var core_exports = {};
22
+ __export(core_exports, {
23
+ DEVICES: () => DEVICES,
24
+ deviceGeometry: () => deviceGeometry,
25
+ deviceHTML: () => deviceHTML,
26
+ deviceVars: () => deviceVars,
27
+ resolveDevice: () => resolveDevice
28
+ });
29
+ module.exports = __toCommonJS(core_exports);
30
+
31
+ // src/core/devices.ts
32
+ var DEVICES = {
33
+ phone: {
34
+ viewport: { width: 390, height: 844 },
35
+ bezel: 0.036,
36
+ // Apple's 47.33 pt display corner radius on a 390 pt screen.
37
+ screenRadius: 47.33 / 390,
38
+ cutout: "island",
39
+ buttons: [
40
+ { side: "left", start: 0.175, length: 0.045 },
41
+ { side: "left", start: 0.255, length: 0.085 },
42
+ { side: "left", start: 0.36, length: 0.085 },
43
+ { side: "right", start: 0.29, length: 0.14 }
44
+ ]
45
+ },
46
+ tablet: {
47
+ viewport: { width: 1180, height: 820 },
48
+ bezel: 0.032,
49
+ // 18 pt display corners on a 1180 pt screen.
50
+ screenRadius: 18 / 1180,
51
+ cutout: "none",
52
+ buttons: [{ side: "top", start: 0.84, length: 0.06 }]
53
+ }
54
+ };
55
+ var ISLAND = { width: 126 / 393, height: 37 / 393, top: 11 / 393 };
56
+ var BUTTON_DEPTH = 8e-3;
57
+ function resolveDevice(device = "phone") {
58
+ return typeof device === "string" ? DEVICES[device] : device;
59
+ }
60
+ function deviceGeometry(device, width, cutout) {
61
+ const spec = resolveDevice(device);
62
+ const bezel = spec.bezel * width;
63
+ const screenWidth = width - bezel * 2;
64
+ const screenHeight = screenWidth * spec.viewport.height / spec.viewport.width;
65
+ const screenRadius = spec.screenRadius * screenWidth;
66
+ return {
67
+ width,
68
+ height: screenHeight + bezel * 2,
69
+ bezel,
70
+ radius: screenRadius + bezel,
71
+ screen: { width: screenWidth, height: screenHeight, radius: screenRadius },
72
+ island: (cutout ?? spec.cutout) === "island" ? {
73
+ width: ISLAND.width * screenWidth,
74
+ height: ISLAND.height * screenWidth,
75
+ top: ISLAND.top * screenWidth
76
+ } : void 0,
77
+ buttonDepth: BUTTON_DEPTH * width,
78
+ scale: screenWidth / spec.viewport.width
79
+ };
80
+ }
81
+ var round = (n) => Math.round(n * 1e4) / 1e4;
82
+ function deviceVars(device = "phone", options = {}) {
83
+ const spec = resolveDevice(device);
84
+ const g = deviceGeometry(spec, 100, options.cutout);
85
+ const vars = {
86
+ "--pd-device-aspect": `${spec.viewport.width} / ${spec.viewport.height}`,
87
+ "--pd-device-bezel": `${round(g.bezel)}`,
88
+ "--pd-device-radius": `${round(g.radius)}`,
89
+ "--pd-device-screen-radius": `${round(g.screen.radius)}`,
90
+ "--pd-device-button-depth": `${round(g.buttonDepth)}`
91
+ };
92
+ if (g.island) {
93
+ vars["--pd-device-island-width"] = `${round(g.island.width)}`;
94
+ vars["--pd-device-island-height"] = `${round(g.island.height)}`;
95
+ vars["--pd-device-island-top"] = `${round(g.island.top)}`;
96
+ }
97
+ if (options.maxHeight)
98
+ vars["--pd-device-max-width"] = `calc(${options.maxHeight} * ${round(g.width / g.height)})`;
99
+ return vars;
100
+ }
101
+
102
+ // src/core/html.ts
103
+ var esc = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
104
+ function deviceHTML(options) {
105
+ const spec = resolveDevice(options.device);
106
+ const cutout = options.cutout ?? spec.cutout;
107
+ const style = Object.entries(
108
+ deviceVars(spec, { cutout, maxHeight: options.maxHeight })
109
+ ).map(([name, value]) => `${name}:${value}`).join(";");
110
+ const size = options.width && options.height ? ` width="${options.width}" height="${options.height}"` : "";
111
+ const buttons = spec.buttons.map(
112
+ (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>`
113
+ ).join("");
114
+ const className = options.className ? `pd-device ${esc(options.className)}` : "pd-device";
115
+ 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>`;
116
+ }
117
+ // Annotate the CommonJS export names for ESM import in node:
118
+ 0 && (module.exports = {
119
+ DEVICES,
120
+ deviceGeometry,
121
+ deviceHTML,
122
+ deviceVars,
123
+ resolveDevice
124
+ });
125
+ //# sourceMappingURL=core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/core/index.ts","../src/core/devices.ts","../src/core/html.ts"],"sourcesContent":["export {\n type Cutout,\n DEVICES,\n type DeviceButton,\n type DeviceGeometry,\n type DeviceName,\n type DeviceSpec,\n deviceGeometry,\n deviceVars,\n resolveDevice,\n} from './devices'\nexport { type DeviceHTMLOptions, deviceHTML } from './html'\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, '&amp;')\n .replace(/</g, '&lt;')\n .replace(/>/g, '&gt;')\n .replace(/\"/g, '&quot;')\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"],"mappings":";;;;;;;;;;;;;;;;;;;;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,IAAI;AAAA,IACrC,OAAO;AAAA;AAAA,IAEP,cAAc,KAAK;AAAA,IACnB,QAAQ;AAAA,IACR,SAAS,CAAC,EAAE,MAAM,OAAO,OAAO,MAAM,QAAQ,KAAK,CAAC;AAAA,EACtD;AACF;AAKA,IAAM,SAAS,EAAE,OAAO,MAAM,KAAK,QAAQ,KAAK,KAAK,KAAK,KAAK,IAAI;AAGnE,IAAM,eAAe;AAEd,SAAS,cAAc,SAAkC,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dist/core.mjs ADDED
@@ -0,0 +1,94 @@
1
+ // src/core/devices.ts
2
+ var DEVICES = {
3
+ phone: {
4
+ viewport: { width: 390, height: 844 },
5
+ bezel: 0.036,
6
+ // Apple's 47.33 pt display corner radius on a 390 pt screen.
7
+ screenRadius: 47.33 / 390,
8
+ cutout: "island",
9
+ buttons: [
10
+ { side: "left", start: 0.175, length: 0.045 },
11
+ { side: "left", start: 0.255, length: 0.085 },
12
+ { side: "left", start: 0.36, length: 0.085 },
13
+ { side: "right", start: 0.29, length: 0.14 }
14
+ ]
15
+ },
16
+ tablet: {
17
+ viewport: { width: 1180, height: 820 },
18
+ bezel: 0.032,
19
+ // 18 pt display corners on a 1180 pt screen.
20
+ screenRadius: 18 / 1180,
21
+ cutout: "none",
22
+ buttons: [{ side: "top", start: 0.84, length: 0.06 }]
23
+ }
24
+ };
25
+ var ISLAND = { width: 126 / 393, height: 37 / 393, top: 11 / 393 };
26
+ var BUTTON_DEPTH = 8e-3;
27
+ function resolveDevice(device = "phone") {
28
+ return typeof device === "string" ? DEVICES[device] : device;
29
+ }
30
+ function deviceGeometry(device, width, cutout) {
31
+ const spec = resolveDevice(device);
32
+ const bezel = spec.bezel * width;
33
+ const screenWidth = width - bezel * 2;
34
+ const screenHeight = screenWidth * spec.viewport.height / spec.viewport.width;
35
+ const screenRadius = spec.screenRadius * screenWidth;
36
+ return {
37
+ width,
38
+ height: screenHeight + bezel * 2,
39
+ bezel,
40
+ radius: screenRadius + bezel,
41
+ screen: { width: screenWidth, height: screenHeight, radius: screenRadius },
42
+ island: (cutout ?? spec.cutout) === "island" ? {
43
+ width: ISLAND.width * screenWidth,
44
+ height: ISLAND.height * screenWidth,
45
+ top: ISLAND.top * screenWidth
46
+ } : void 0,
47
+ buttonDepth: BUTTON_DEPTH * width,
48
+ scale: screenWidth / spec.viewport.width
49
+ };
50
+ }
51
+ var round = (n) => Math.round(n * 1e4) / 1e4;
52
+ function deviceVars(device = "phone", options = {}) {
53
+ const spec = resolveDevice(device);
54
+ const g = deviceGeometry(spec, 100, options.cutout);
55
+ const vars = {
56
+ "--pd-device-aspect": `${spec.viewport.width} / ${spec.viewport.height}`,
57
+ "--pd-device-bezel": `${round(g.bezel)}`,
58
+ "--pd-device-radius": `${round(g.radius)}`,
59
+ "--pd-device-screen-radius": `${round(g.screen.radius)}`,
60
+ "--pd-device-button-depth": `${round(g.buttonDepth)}`
61
+ };
62
+ if (g.island) {
63
+ vars["--pd-device-island-width"] = `${round(g.island.width)}`;
64
+ vars["--pd-device-island-height"] = `${round(g.island.height)}`;
65
+ vars["--pd-device-island-top"] = `${round(g.island.top)}`;
66
+ }
67
+ if (options.maxHeight)
68
+ vars["--pd-device-max-width"] = `calc(${options.maxHeight} * ${round(g.width / g.height)})`;
69
+ return vars;
70
+ }
71
+
72
+ // src/core/html.ts
73
+ var esc = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
74
+ function deviceHTML(options) {
75
+ const spec = resolveDevice(options.device);
76
+ const cutout = options.cutout ?? spec.cutout;
77
+ const style = Object.entries(
78
+ deviceVars(spec, { cutout, maxHeight: options.maxHeight })
79
+ ).map(([name, value]) => `${name}:${value}`).join(";");
80
+ const size = options.width && options.height ? ` width="${options.width}" height="${options.height}"` : "";
81
+ const buttons = spec.buttons.map(
82
+ (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>`
83
+ ).join("");
84
+ const className = options.className ? `pd-device ${esc(options.className)}` : "pd-device";
85
+ 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>`;
86
+ }
87
+ export {
88
+ DEVICES,
89
+ deviceGeometry,
90
+ deviceHTML,
91
+ deviceVars,
92
+ resolveDevice
93
+ };
94
+ //# sourceMappingURL=core.mjs.map