@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.
@@ -0,0 +1,358 @@
1
+ import * as react from 'react';
2
+ import { CSSProperties, ImgHTMLAttributes, IframeHTMLAttributes, ReactNode } from 'react';
3
+
4
+ /** A cutout's shape and place, in viewport CSS px. */
5
+ type CutoutSpec = {
6
+ type: 'island';
7
+ width: number;
8
+ height: number;
9
+ top: number;
10
+ } | {
11
+ type: 'hole';
12
+ diameter: number;
13
+ /** Distance from the screen's top edge to the hole's top. */
14
+ top: number;
15
+ /** The hole's center as a fraction of the screen width. Defaults to 0.5. */
16
+ x?: number;
17
+ };
18
+ /** `'island'` is the Dynamic Island in proportion to the screen width. */
19
+ type Cutout = 'island' | 'none' | CutoutSpec;
20
+ /** Insets from each screen edge that content should clear, in viewport CSS px, portrait. */
21
+ interface SafeArea {
22
+ top: number;
23
+ right: number;
24
+ bottom: number;
25
+ left: number;
26
+ }
27
+ interface DeviceButton {
28
+ side: 'left' | 'right' | 'top';
29
+ /** Where the button starts, as a fraction of that side's length. */
30
+ start: number;
31
+ /** Button length, as a fraction of that side's length. */
32
+ length: number;
33
+ }
34
+ interface DeviceSpec {
35
+ /**
36
+ * The CSS-pixel viewport the screen shows. A screenshot or page laid out at
37
+ * this size fills the screen exactly; the frame takes its shape from it.
38
+ */
39
+ viewport: {
40
+ width: number;
41
+ height: number;
42
+ };
43
+ /** Bezel thickness, as a fraction of the device's outer width. */
44
+ bezel: number;
45
+ /** Screen corner radius, as a fraction of the screen width. */
46
+ screenRadius: number;
47
+ cutout: Cutout;
48
+ buttons: readonly DeviceButton[];
49
+ /** Hardware insets content should clear. Without it the device draws no status bar. */
50
+ safeArea?: SafeArea;
51
+ statusBar?: StatusBarSpec;
52
+ homeIndicator?: HomeIndicatorSpec;
53
+ }
54
+ /** Where the status bar sits, in viewport CSS px. */
55
+ interface StatusBarSpec {
56
+ /** The time's center, from the screen's left edge. */
57
+ time: number;
58
+ /** The icons' center, from the screen's right edge. */
59
+ icons: number;
60
+ /** The text's vertical center, from the screen's top edge. */
61
+ center: number;
62
+ fontSize: number;
63
+ }
64
+ /** The home indicator or gesture bar, in viewport CSS px. */
65
+ interface HomeIndicatorSpec {
66
+ width: number;
67
+ height: number;
68
+ /** Gap between the bar and the screen's bottom edge. */
69
+ bottom: number;
70
+ }
71
+ /**
72
+ * Proportions follow the screen, not a particular model: the phone is a
73
+ * 390 × 844 iPhone-class screen (iPhone 12 to 14), the size most app
74
+ * screenshots and phone demos are laid out at; the tablet is an 1180 × 820
75
+ * iPad Air in landscape.
76
+ */
77
+ declare const DEVICES: {
78
+ readonly phone: {
79
+ readonly viewport: {
80
+ readonly width: 390;
81
+ readonly height: 844;
82
+ };
83
+ readonly bezel: 0.036;
84
+ readonly screenRadius: number;
85
+ readonly cutout: "island";
86
+ readonly buttons: readonly [{
87
+ readonly side: "left";
88
+ readonly start: 0.175;
89
+ readonly length: 0.045;
90
+ }, {
91
+ readonly side: "left";
92
+ readonly start: 0.255;
93
+ readonly length: 0.085;
94
+ }, {
95
+ readonly side: "left";
96
+ readonly start: 0.36;
97
+ readonly length: 0.085;
98
+ }, {
99
+ readonly side: "right";
100
+ readonly start: 0.29;
101
+ readonly length: 0.14;
102
+ }];
103
+ };
104
+ readonly tablet: {
105
+ readonly viewport: {
106
+ readonly width: 1180;
107
+ readonly height: 820;
108
+ };
109
+ readonly bezel: 0.032;
110
+ readonly screenRadius: number;
111
+ readonly cutout: "none";
112
+ readonly buttons: readonly [{
113
+ readonly side: "top";
114
+ readonly start: 0.84;
115
+ readonly length: 0.06;
116
+ }];
117
+ };
118
+ readonly 'iphone-16-pro': {
119
+ readonly viewport: {
120
+ readonly width: 402;
121
+ readonly height: 874;
122
+ };
123
+ readonly bezel: 0.034;
124
+ readonly screenRadius: number;
125
+ readonly cutout: {
126
+ readonly type: "island";
127
+ readonly width: 125;
128
+ readonly height: 36.67;
129
+ readonly top: 14;
130
+ };
131
+ readonly safeArea: {
132
+ readonly top: 62;
133
+ readonly right: 0;
134
+ readonly bottom: 34;
135
+ readonly left: 0;
136
+ };
137
+ readonly statusBar: {
138
+ readonly time: 74;
139
+ readonly icons: 73;
140
+ readonly center: 33;
141
+ readonly fontSize: 17;
142
+ };
143
+ readonly homeIndicator: {
144
+ readonly width: 134;
145
+ readonly height: 5;
146
+ readonly bottom: 8;
147
+ };
148
+ readonly buttons: readonly [{
149
+ readonly side: "left";
150
+ readonly start: 0.175;
151
+ readonly length: 0.045;
152
+ }, {
153
+ readonly side: "left";
154
+ readonly start: 0.255;
155
+ readonly length: 0.085;
156
+ }, {
157
+ readonly side: "left";
158
+ readonly start: 0.36;
159
+ readonly length: 0.085;
160
+ }, {
161
+ readonly side: "right";
162
+ readonly start: 0.29;
163
+ readonly length: 0.14;
164
+ }, {
165
+ readonly side: "right";
166
+ readonly start: 0.6;
167
+ readonly length: 0.07;
168
+ }];
169
+ };
170
+ readonly 'pixel-9': {
171
+ readonly viewport: {
172
+ readonly width: 412;
173
+ readonly height: 923;
174
+ };
175
+ readonly bezel: 0.034;
176
+ readonly screenRadius: number;
177
+ readonly cutout: {
178
+ readonly type: "hole";
179
+ readonly diameter: 30.5;
180
+ readonly top: 17.5;
181
+ readonly x: 0.5;
182
+ };
183
+ readonly safeArea: {
184
+ readonly top: 50;
185
+ readonly right: 0;
186
+ readonly bottom: 24;
187
+ readonly left: 0;
188
+ };
189
+ readonly statusBar: {
190
+ readonly time: 42;
191
+ readonly icons: 50;
192
+ readonly center: 26;
193
+ readonly fontSize: 14;
194
+ };
195
+ readonly homeIndicator: {
196
+ readonly width: 108;
197
+ readonly height: 4;
198
+ readonly bottom: 10;
199
+ };
200
+ readonly buttons: readonly [{
201
+ readonly side: "right";
202
+ readonly start: 0.22;
203
+ readonly length: 0.07;
204
+ }, {
205
+ readonly side: "right";
206
+ readonly start: 0.33;
207
+ readonly length: 0.14;
208
+ }];
209
+ };
210
+ };
211
+ type DeviceName = keyof typeof DEVICES;
212
+ declare function resolveDevice(device?: DeviceName | DeviceSpec): DeviceSpec;
213
+ interface DeviceGeometry {
214
+ width: number;
215
+ height: number;
216
+ bezel: number;
217
+ radius: number;
218
+ screen: {
219
+ width: number;
220
+ height: number;
221
+ radius: number;
222
+ };
223
+ island?: {
224
+ width: number;
225
+ height: number;
226
+ top: number;
227
+ };
228
+ /** `x` is the hole's center as a fraction of the screen width. */
229
+ hole?: {
230
+ diameter: number;
231
+ top: number;
232
+ x: number;
233
+ };
234
+ /** Present when the spec has one, whatever the cutout. */
235
+ safeArea?: SafeArea;
236
+ buttonDepth: number;
237
+ /** Multiply the viewport by this to fit it to the screen. */
238
+ scale: number;
239
+ }
240
+ /**
241
+ * Every measurement of a device drawn `width` units wide, in the same units.
242
+ * For renderers that can't use the stylesheet, such as React Native.
243
+ */
244
+ declare function deviceGeometry(device: DeviceName | DeviceSpec, width: number, cutout?: Cutout): DeviceGeometry;
245
+ /**
246
+ * The custom properties `styles.css` draws a device from. Lengths are in
247
+ * percent of the device's width and become `cqi` in the stylesheet.
248
+ * `maxHeight` is any CSS length; the width shrinks to keep the shape.
249
+ */
250
+ declare function deviceVars(device?: DeviceName | DeviceSpec, options?: {
251
+ cutout?: Cutout;
252
+ maxHeight?: string;
253
+ }): Record<`--pd-device-${string}`, string>;
254
+
255
+ interface DeviceHTMLOptions {
256
+ /** The screenshot. */
257
+ src: string;
258
+ alt: string;
259
+ device?: DeviceName | DeviceSpec;
260
+ /** Defaults to the device's own cutout. */
261
+ cutout?: Cutout;
262
+ maxHeight?: string;
263
+ className?: string;
264
+ /** The image's pixel size, so the page can reserve its space. */
265
+ width?: number;
266
+ height?: number;
267
+ /** Defaults to `lazy`. */
268
+ loading?: 'lazy' | 'eager';
269
+ }
270
+ /**
271
+ * A screenshot in a device frame, as an HTML string, for pages rendered
272
+ * without React such as Markdown output. Needs `styles.css`.
273
+ */
274
+ declare function deviceHTML(options: DeviceHTMLOptions): string;
275
+
276
+ /** The query parameter a framed page reads its insets from: `top,right,bottom,left` in CSS px. */
277
+ declare const SAFE_AREA_PARAM = "pd-safe-area";
278
+ /**
279
+ * `url` with `pd-safe-area` set to the insets, rounded to whole pixels. The
280
+ * rest of the URL is kept exactly as given, relative or not. URLs that aren't
281
+ * http(s) or relative, such as `data:` and `about:blank`, come back unchanged.
282
+ */
283
+ declare function withSafeArea(url: string, insets: SafeArea): string;
284
+ /** The insets in a query string, or null when the parameter is absent or malformed. */
285
+ declare function readSafeArea(search: string): SafeArea | null;
286
+ /**
287
+ * Sets `--pd-safe-top`, `--pd-safe-right`, `--pd-safe-bottom` and
288
+ * `--pd-safe-left` on `target` from the page URL's `pd-safe-area`. Does
289
+ * nothing when it's absent, so CSS falls back to `env(safe-area-inset-*)`.
290
+ */
291
+ declare function applySafeArea(target?: HTMLElement, search?: string): SafeArea | null;
292
+
293
+ type DeviceStatus = 'loading' | 'ready' | 'failed';
294
+ type SafeAreaMode = 'expose' | 'reserve' | 'none';
295
+ interface DeviceProps {
296
+ /** A preset or your own spec. Defaults to `phone`. */
297
+ device?: DeviceName | DeviceSpec;
298
+ /** Defaults to the device's own cutout. On a device without a safe area
299
+ * (`phone`) it's hidden over an iframe, which would have no way to clear it.
300
+ * `none` hides the drawing; the insets still come from the device. */
301
+ cutout?: Cutout;
302
+ /**
303
+ * How content meets the cutout, on devices with a safe area:
304
+ * `expose` runs content edge to edge and gives it the insets as
305
+ * `--pd-safe-*` (children) or a `pd-safe-area` URL parameter (iframes);
306
+ * `reserve` draws solid bars and lays content out between them;
307
+ * `none` draws no status bar. Defaults to `reserve` for iframes and
308
+ * children and `none` for screenshots.
309
+ */
310
+ safeArea?: SafeAreaMode;
311
+ /** `dark` (default) draws dark text for light content. */
312
+ statusBarStyle?: 'dark' | 'light';
313
+ /** Any CSS length. The width shrinks so the whole device fits. */
314
+ maxHeight?: string;
315
+ className?: string;
316
+ style?: CSSProperties;
317
+ /** A screenshot, drawn at the screen's size. */
318
+ src?: string;
319
+ alt?: string;
320
+ imgProps?: ImgHTMLAttributes<HTMLImageElement>;
321
+ /** A page, laid out at the device's viewport and scaled to the screen. Takes
322
+ * precedence over `src` and `children`. */
323
+ iframe?: string;
324
+ /** Required with `iframe`; the frame's accessible name. */
325
+ title?: string;
326
+ iframeProps?: IframeHTMLAttributes<HTMLIFrameElement>;
327
+ /** Anything else on the screen. */
328
+ children?: ReactNode;
329
+ /** `screen` (default) draws children at the screen's size; `viewport` lays
330
+ * them out at the device's viewport and scales them, like an iframe. */
331
+ fit?: 'screen' | 'viewport';
332
+ /** Lets pointer events reach the screen content. Off by default, so an
333
+ * embedded page never traps a scroll gesture; an off iframe also leaves
334
+ * the tab order. */
335
+ interactive?: boolean;
336
+ /** Drawn over the screen at the screen's size, e.g. a poster until the
337
+ * embedded page has drawn. Stays interactive. */
338
+ overlay?: ReactNode;
339
+ /** Replaces the iframe, laid out at the viewport, once it has failed. */
340
+ fallback?: ReactNode;
341
+ /** Milliseconds the iframe gets to load after it first scrolls into view
342
+ * before it counts as failed. No timeout when omitted. */
343
+ loadTimeout?: number;
344
+ /** Marks the iframe loaded, for pages that report it themselves (say with
345
+ * `postMessage`) because `load` can fire before hydration. */
346
+ ready?: boolean;
347
+ /** Marks the iframe failed and shows `fallback`. */
348
+ failed?: boolean;
349
+ onStatusChange?: (status: DeviceStatus) => void;
350
+ }
351
+ /**
352
+ * A device frame around a screenshot, an embedded page or any content, drawn
353
+ * in CSS from the proportions in `DEVICES`. Give it a width (or `maxHeight`);
354
+ * the height follows from the device's shape.
355
+ */
356
+ declare function Device({ device, cutout, safeArea, statusBarStyle, maxHeight, className, style, src, alt, imgProps, iframe, title, iframeProps, children, fit, interactive, overlay, fallback, loadTimeout, ready, failed, onStatusChange, }: DeviceProps): react.JSX.Element;
357
+
358
+ export { type Cutout, type CutoutSpec, DEVICES, Device, type DeviceButton, type DeviceGeometry, type DeviceHTMLOptions, type DeviceName, type DeviceProps, type DeviceSpec, type DeviceStatus, type HomeIndicatorSpec, SAFE_AREA_PARAM, type SafeArea, type SafeAreaMode, type StatusBarSpec, applySafeArea, deviceGeometry, deviceHTML, deviceVars, readSafeArea, resolveDevice, withSafeArea };