@present-day/device 0.0.0-stage → 0.2.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,112 @@
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. The real-phone presets also draw the status bar and tell the content where the cutout is, the way a phone's safe area does.
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'` | A preset (below) or your own `DeviceSpec` |
33
+ | `cutout` | the device's | `'island'`, `'none'`, or a shape: `{ type: 'island', width, height, top }` or `{ type: 'hole', diameter, top, x }`. Lengths are viewport px; a hole's `x` is its center as a fraction of the screen width (default `0.5`). On `phone` it defaults to `'none'` over an iframe. `'none'` hides the drawing; the insets still come from the device |
34
+ | `safeArea` | see below | `'expose'`, `'reserve'` or `'none'`, on presets with a safe area |
35
+ | `statusBarStyle` | `'dark'` | `'dark'` text for light content, `'light'` for dark |
36
+ | `maxHeight` | | Any CSS length; the width shrinks to keep the whole device in it |
37
+ | `src`, `alt`, `imgProps` | | A screenshot, drawn at the screen's size |
38
+ | `iframe`, `title`, `iframeProps` | | A page laid out at the viewport and scaled to the screen. Wins over `src` and `children` |
39
+ | `children`, `fit` | `fit: 'screen'` | Any content. `fit: 'viewport'` lays it out at the viewport and scales it like an iframe |
40
+ | `interactive` | `false` | Lets pointer events reach the screen. Off, an iframe also leaves the tab order, so it never traps a scroll |
41
+ | `overlay` | | Drawn over the screen and stays interactive, e.g. a poster until the page has drawn |
42
+ | `fallback` | | Replaces the iframe, laid out at the viewport, once it has failed |
43
+ | `loadTimeout` | | Milliseconds the iframe gets after it first scrolls into view before it counts as failed |
44
+ | `ready`, `failed` | | Outside signals, e.g. from `postMessage`, since `load` can fire before hydration |
45
+ | `onStatusChange` | | `'loading'`, `'ready'` or `'failed'` |
46
+
47
+ ## Presets
48
+
49
+ | `device` | Viewport | Cutout | Safe area (top, bottom) |
50
+ | --------------- | ---------- | --------------------------------- | ----------------------- |
51
+ | `phone` | 390 × 844 | Dynamic Island | none |
52
+ | `tablet` | 1180 × 820 | none | none |
53
+ | `iphone-16-pro` | 402 × 874 | Dynamic Island, 125 × 36.67 | 62, 34 |
54
+ | `pixel-9` | 412 × 923 | Punch hole, 30.5 across, centered | 50, 24 |
55
+
56
+ The iPhone 16 Pro numbers come from the iOS Simulator; the Pixel 9's from the Android emulator's device definition and skin. The Pixel's insets are approximate, since its status bar height isn't published.
57
+
58
+ ## The safe area
59
+
60
+ On a preset with a safe area, `safeArea` decides how the content meets the cutout:
61
+
62
+ | Mode | Default for | What happens |
63
+ | --------- | ----------------------- | --------------------------------------------------------------------------------------------------------------- |
64
+ | `reserve` | `iframe` and `children` | Solid status bar and home bands; the content gets the screen between them. Works with any content |
65
+ | `expose` | | Content runs edge to edge under a transparent status bar and gets the insets, so it can clear the cutout itself |
66
+ | `none` | `src` | No status bar or insets; a screenshot carries its own |
67
+
68
+ In `expose`, content reads four CSS variables, `--pd-safe-top`, `--pd-safe-right`, `--pd-safe-bottom` and `--pd-safe-left`. Fall back to `env()` and the same CSS works on a real phone:
69
+
70
+ ```css
71
+ padding-top: var(--pd-safe-top, env(safe-area-inset-top, 0px));
72
+ ```
73
+
74
+ React children get the variables on their wrapper. An iframe gets them through its URL: the frame adds `pd-safe-area=top,right,bottom,left` (CSS px), and the page applies it once at startup:
75
+
76
+ ```ts
77
+ import { applySafeArea } from '@present-day/device/core'
78
+
79
+ applySafeArea() // sets --pd-safe-* on <html>; does nothing without the parameter
80
+ ```
81
+
82
+ `withSafeArea(url, insets)` builds the same URL yourself. Changing the device or mode changes the URL, which reloads the iframe.
83
+
84
+ ## Without React
85
+
86
+ `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.
87
+
88
+ ```ts
89
+ import { deviceHTML } from '@present-day/device/core'
90
+
91
+ deviceHTML({ src: '/shots/map.webp', alt: 'The map', width: 390, height: 844 })
92
+ ```
93
+
94
+ `@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.
95
+
96
+ ## Theming
97
+
98
+ | Custom property | Default |
99
+ | -------------------------- | --------------------------------------------------------------------------- |
100
+ | `--pd-device-color` | `#1c1c1e` (body and buttons) |
101
+ | `--pd-device-screen-color` | `#000` (behind content while it loads) |
102
+ | `--pd-device-edge` | `hsl(0 0% 50% / 0.25)` (hairline that keeps the body visible on dark pages) |
103
+ | `--pd-device-shadow` | `0 24px 48px -24px rgb(0 0 0 / 0.45)` |
104
+ | `--pd-device-status-color` | `#fff`, or `#000` with `statusBarStyle="light"` (the bands in `reserve`) |
105
+
106
+ ## Development
107
+
108
+ ```sh
109
+ bun install
110
+ bun run storybook
111
+ bun run test
112
+ ```
@@ -0,0 +1,290 @@
1
+ /** A cutout's shape and place, in viewport CSS px. */
2
+ type CutoutSpec = {
3
+ type: 'island';
4
+ width: number;
5
+ height: number;
6
+ top: number;
7
+ } | {
8
+ type: 'hole';
9
+ diameter: number;
10
+ /** Distance from the screen's top edge to the hole's top. */
11
+ top: number;
12
+ /** The hole's center as a fraction of the screen width. Defaults to 0.5. */
13
+ x?: number;
14
+ };
15
+ /** `'island'` is the Dynamic Island in proportion to the screen width. */
16
+ type Cutout = 'island' | 'none' | CutoutSpec;
17
+ /** Insets from each screen edge that content should clear, in viewport CSS px, portrait. */
18
+ interface SafeArea {
19
+ top: number;
20
+ right: number;
21
+ bottom: number;
22
+ left: number;
23
+ }
24
+ interface DeviceButton {
25
+ side: 'left' | 'right' | 'top';
26
+ /** Where the button starts, as a fraction of that side's length. */
27
+ start: number;
28
+ /** Button length, as a fraction of that side's length. */
29
+ length: number;
30
+ }
31
+ interface DeviceSpec {
32
+ /**
33
+ * The CSS-pixel viewport the screen shows. A screenshot or page laid out at
34
+ * this size fills the screen exactly; the frame takes its shape from it.
35
+ */
36
+ viewport: {
37
+ width: number;
38
+ height: number;
39
+ };
40
+ /** Bezel thickness, as a fraction of the device's outer width. */
41
+ bezel: number;
42
+ /** Screen corner radius, as a fraction of the screen width. */
43
+ screenRadius: number;
44
+ cutout: Cutout;
45
+ buttons: readonly DeviceButton[];
46
+ /** Hardware insets content should clear. Without it the device draws no status bar. */
47
+ safeArea?: SafeArea;
48
+ statusBar?: StatusBarSpec;
49
+ homeIndicator?: HomeIndicatorSpec;
50
+ }
51
+ /** Where the status bar sits, in viewport CSS px. */
52
+ interface StatusBarSpec {
53
+ /** The time's center, from the screen's left edge. */
54
+ time: number;
55
+ /** The icons' center, from the screen's right edge. */
56
+ icons: number;
57
+ /** The text's vertical center, from the screen's top edge. */
58
+ center: number;
59
+ fontSize: number;
60
+ }
61
+ /** The home indicator or gesture bar, in viewport CSS px. */
62
+ interface HomeIndicatorSpec {
63
+ width: number;
64
+ height: number;
65
+ /** Gap between the bar and the screen's bottom edge. */
66
+ bottom: number;
67
+ }
68
+ /**
69
+ * Proportions follow the screen, not a particular model: the phone is a
70
+ * 390 × 844 iPhone-class screen (iPhone 12 to 14), the size most app
71
+ * screenshots and phone demos are laid out at; the tablet is an 1180 × 820
72
+ * iPad Air in landscape.
73
+ */
74
+ declare const DEVICES: {
75
+ readonly phone: {
76
+ readonly viewport: {
77
+ readonly width: 390;
78
+ readonly height: 844;
79
+ };
80
+ readonly bezel: 0.036;
81
+ readonly screenRadius: number;
82
+ readonly cutout: "island";
83
+ readonly buttons: readonly [{
84
+ readonly side: "left";
85
+ readonly start: 0.175;
86
+ readonly length: 0.045;
87
+ }, {
88
+ readonly side: "left";
89
+ readonly start: 0.255;
90
+ readonly length: 0.085;
91
+ }, {
92
+ readonly side: "left";
93
+ readonly start: 0.36;
94
+ readonly length: 0.085;
95
+ }, {
96
+ readonly side: "right";
97
+ readonly start: 0.29;
98
+ readonly length: 0.14;
99
+ }];
100
+ };
101
+ readonly tablet: {
102
+ readonly viewport: {
103
+ readonly width: 1180;
104
+ readonly height: 820;
105
+ };
106
+ readonly bezel: 0.032;
107
+ readonly screenRadius: number;
108
+ readonly cutout: "none";
109
+ readonly buttons: readonly [{
110
+ readonly side: "top";
111
+ readonly start: 0.84;
112
+ readonly length: 0.06;
113
+ }];
114
+ };
115
+ readonly 'iphone-16-pro': {
116
+ readonly viewport: {
117
+ readonly width: 402;
118
+ readonly height: 874;
119
+ };
120
+ readonly bezel: 0.034;
121
+ readonly screenRadius: number;
122
+ readonly cutout: {
123
+ readonly type: "island";
124
+ readonly width: 125;
125
+ readonly height: 36.67;
126
+ readonly top: 14;
127
+ };
128
+ readonly safeArea: {
129
+ readonly top: 62;
130
+ readonly right: 0;
131
+ readonly bottom: 34;
132
+ readonly left: 0;
133
+ };
134
+ readonly statusBar: {
135
+ readonly time: 74;
136
+ readonly icons: 73;
137
+ readonly center: 33;
138
+ readonly fontSize: 17;
139
+ };
140
+ readonly homeIndicator: {
141
+ readonly width: 134;
142
+ readonly height: 5;
143
+ readonly bottom: 8;
144
+ };
145
+ readonly buttons: readonly [{
146
+ readonly side: "left";
147
+ readonly start: 0.175;
148
+ readonly length: 0.045;
149
+ }, {
150
+ readonly side: "left";
151
+ readonly start: 0.255;
152
+ readonly length: 0.085;
153
+ }, {
154
+ readonly side: "left";
155
+ readonly start: 0.36;
156
+ readonly length: 0.085;
157
+ }, {
158
+ readonly side: "right";
159
+ readonly start: 0.29;
160
+ readonly length: 0.14;
161
+ }, {
162
+ readonly side: "right";
163
+ readonly start: 0.6;
164
+ readonly length: 0.07;
165
+ }];
166
+ };
167
+ readonly 'pixel-9': {
168
+ readonly viewport: {
169
+ readonly width: 412;
170
+ readonly height: 923;
171
+ };
172
+ readonly bezel: 0.034;
173
+ readonly screenRadius: number;
174
+ readonly cutout: {
175
+ readonly type: "hole";
176
+ readonly diameter: 30.5;
177
+ readonly top: 17.5;
178
+ readonly x: 0.5;
179
+ };
180
+ readonly safeArea: {
181
+ readonly top: 50;
182
+ readonly right: 0;
183
+ readonly bottom: 24;
184
+ readonly left: 0;
185
+ };
186
+ readonly statusBar: {
187
+ readonly time: 42;
188
+ readonly icons: 50;
189
+ readonly center: 26;
190
+ readonly fontSize: 14;
191
+ };
192
+ readonly homeIndicator: {
193
+ readonly width: 108;
194
+ readonly height: 4;
195
+ readonly bottom: 10;
196
+ };
197
+ readonly buttons: readonly [{
198
+ readonly side: "right";
199
+ readonly start: 0.22;
200
+ readonly length: 0.07;
201
+ }, {
202
+ readonly side: "right";
203
+ readonly start: 0.33;
204
+ readonly length: 0.14;
205
+ }];
206
+ };
207
+ };
208
+ type DeviceName = keyof typeof DEVICES;
209
+ declare function resolveDevice(device?: DeviceName | DeviceSpec): DeviceSpec;
210
+ interface DeviceGeometry {
211
+ width: number;
212
+ height: number;
213
+ bezel: number;
214
+ radius: number;
215
+ screen: {
216
+ width: number;
217
+ height: number;
218
+ radius: number;
219
+ };
220
+ island?: {
221
+ width: number;
222
+ height: number;
223
+ top: number;
224
+ };
225
+ /** `x` is the hole's center as a fraction of the screen width. */
226
+ hole?: {
227
+ diameter: number;
228
+ top: number;
229
+ x: number;
230
+ };
231
+ /** Present when the spec has one, whatever the cutout. */
232
+ safeArea?: SafeArea;
233
+ buttonDepth: number;
234
+ /** Multiply the viewport by this to fit it to the screen. */
235
+ scale: number;
236
+ }
237
+ /**
238
+ * Every measurement of a device drawn `width` units wide, in the same units.
239
+ * For renderers that can't use the stylesheet, such as React Native.
240
+ */
241
+ declare function deviceGeometry(device: DeviceName | DeviceSpec, width: number, cutout?: Cutout): DeviceGeometry;
242
+ /**
243
+ * The custom properties `styles.css` draws a device from. Lengths are in
244
+ * percent of the device's width and become `cqi` in the stylesheet.
245
+ * `maxHeight` is any CSS length; the width shrinks to keep the shape.
246
+ */
247
+ declare function deviceVars(device?: DeviceName | DeviceSpec, options?: {
248
+ cutout?: Cutout;
249
+ maxHeight?: string;
250
+ }): Record<`--pd-device-${string}`, string>;
251
+
252
+ interface DeviceHTMLOptions {
253
+ /** The screenshot. */
254
+ src: string;
255
+ alt: string;
256
+ device?: DeviceName | DeviceSpec;
257
+ /** Defaults to the device's own cutout. */
258
+ cutout?: Cutout;
259
+ maxHeight?: string;
260
+ className?: string;
261
+ /** The image's pixel size, so the page can reserve its space. */
262
+ width?: number;
263
+ height?: number;
264
+ /** Defaults to `lazy`. */
265
+ loading?: 'lazy' | 'eager';
266
+ }
267
+ /**
268
+ * A screenshot in a device frame, as an HTML string, for pages rendered
269
+ * without React such as Markdown output. Needs `styles.css`.
270
+ */
271
+ declare function deviceHTML(options: DeviceHTMLOptions): string;
272
+
273
+ /** The query parameter a framed page reads its insets from: `top,right,bottom,left` in CSS px. */
274
+ declare const SAFE_AREA_PARAM = "pd-safe-area";
275
+ /**
276
+ * `url` with `pd-safe-area` set to the insets, rounded to whole pixels. The
277
+ * rest of the URL is kept exactly as given, relative or not. URLs that aren't
278
+ * http(s) or relative, such as `data:` and `about:blank`, come back unchanged.
279
+ */
280
+ declare function withSafeArea(url: string, insets: SafeArea): string;
281
+ /** The insets in a query string, or null when the parameter is absent or malformed. */
282
+ declare function readSafeArea(search: string): SafeArea | null;
283
+ /**
284
+ * Sets `--pd-safe-top`, `--pd-safe-right`, `--pd-safe-bottom` and
285
+ * `--pd-safe-left` on `target` from the page URL's `pd-safe-area`. Does
286
+ * nothing when it's absent, so CSS falls back to `env(safe-area-inset-*)`.
287
+ */
288
+ declare function applySafeArea(target?: HTMLElement, search?: string): SafeArea | null;
289
+
290
+ export { type Cutout, type CutoutSpec, DEVICES, type DeviceButton, type DeviceGeometry, type DeviceHTMLOptions, type DeviceName, type DeviceSpec, type HomeIndicatorSpec, SAFE_AREA_PARAM, type SafeArea, type StatusBarSpec, applySafeArea, deviceGeometry, deviceHTML, deviceVars, readSafeArea, resolveDevice, withSafeArea };