capacitor-device-profile 0.0.0-stage → 0.1.2
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/CHANGELOG.md +23 -0
- package/CONTRIBUTING.md +52 -0
- package/LICENSE +21 -0
- package/README.md +83 -2
- package/SECURITY.md +26 -0
- package/dist/cjs/haptics.cjs +37 -0
- package/dist/cjs/haptics.d.cts +36 -0
- package/dist/cjs/haptics.d.cts.map +1 -0
- package/dist/cjs/index.cjs +24 -0
- package/dist/cjs/index.d.cts +6 -0
- package/dist/cjs/index.d.cts.map +1 -0
- package/dist/cjs/lifecycle.cjs +85 -0
- package/dist/cjs/lifecycle.d.cts +48 -0
- package/dist/cjs/lifecycle.d.cts.map +1 -0
- package/dist/cjs/orientation.cjs +16 -0
- package/dist/cjs/orientation.d.cts +21 -0
- package/dist/cjs/orientation.d.cts.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/profile.cjs +86 -0
- package/dist/cjs/profile.d.cts +71 -0
- package/dist/cjs/profile.d.cts.map +1 -0
- package/dist/cjs/react.cjs +61 -0
- package/dist/cjs/react.d.cts +16 -0
- package/dist/cjs/react.d.cts.map +1 -0
- package/dist/cjs/safeArea.cjs +99 -0
- package/dist/cjs/safeArea.d.cts +33 -0
- package/dist/cjs/safeArea.d.cts.map +1 -0
- package/dist/esm/haptics.d.ts +36 -0
- package/dist/esm/haptics.d.ts.map +1 -0
- package/dist/esm/haptics.js +34 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/lifecycle.d.ts +48 -0
- package/dist/esm/lifecycle.d.ts.map +1 -0
- package/dist/esm/lifecycle.js +82 -0
- package/dist/esm/orientation.d.ts +21 -0
- package/dist/esm/orientation.d.ts.map +1 -0
- package/dist/esm/orientation.js +13 -0
- package/dist/esm/profile.d.ts +71 -0
- package/dist/esm/profile.d.ts.map +1 -0
- package/dist/esm/profile.js +77 -0
- package/dist/esm/react.d.ts +16 -0
- package/dist/esm/react.d.ts.map +1 -0
- package/dist/esm/react.js +56 -0
- package/dist/esm/safeArea.d.ts +33 -0
- package/dist/esm/safeArea.d.ts.map +1 -0
- package/dist/esm/safeArea.js +93 -0
- package/docs/API.md +123 -0
- package/docs/ARCHITECTURE.md +49 -0
- package/docs/contributing.md +26 -0
- package/package.json +143 -4
- package/src/haptics.ts +63 -0
- package/src/index.ts +47 -0
- package/src/lifecycle.ts +130 -0
- package/src/orientation.ts +39 -0
- package/src/profile.ts +126 -0
- package/src/react.tsx +80 -0
- package/src/safeArea.ts +114 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Device profile: a pure classification of the current viewport into the categories a game's layout
|
|
3
|
+
* actually branches on. Every input is injectable so the classification unit-tests without a browser
|
|
4
|
+
* and so a native shell can feed it platform facts (Capacitor `Device.getInfo()`).
|
|
5
|
+
*/
|
|
6
|
+
/** Short edge (CSS px) at which a frame counts as expanded. Phones stay below ~480. */
|
|
7
|
+
export const EXPANDED_MIN_SHORT_EDGE = 700;
|
|
8
|
+
/** Short edge (CSS px) from which a touch screen is tablet-sized rather than a phone. */
|
|
9
|
+
export const TABLET_MIN_SHORT_EDGE = 600;
|
|
10
|
+
/**
|
|
11
|
+
* Long/short aspect at or below which a screen is near-square. A phone in portrait is ~2.1, a 4:3
|
|
12
|
+
* tablet 1.33, an unfolded OnePlus Open ~1.08.
|
|
13
|
+
*/
|
|
14
|
+
export const NEAR_SQUARE_MAX_ASPECT = 1.3;
|
|
15
|
+
export function classifyOrientation(width, height) {
|
|
16
|
+
return height > width ? 'portrait' : 'landscape';
|
|
17
|
+
}
|
|
18
|
+
/** True for an unfolded book-style foldable: near-square and tablet-sized, on a touch screen. */
|
|
19
|
+
export function isFoldableOpen(facts) {
|
|
20
|
+
if (!facts.touch)
|
|
21
|
+
return false;
|
|
22
|
+
const shortEdge = Math.min(facts.width, facts.height);
|
|
23
|
+
const longEdge = Math.max(facts.width, facts.height);
|
|
24
|
+
if (shortEdge <= 0)
|
|
25
|
+
return false;
|
|
26
|
+
return longEdge / shortEdge <= NEAR_SQUARE_MAX_ASPECT && shortEdge >= TABLET_MIN_SHORT_EDGE;
|
|
27
|
+
}
|
|
28
|
+
export function classifyFormFactor(facts) {
|
|
29
|
+
const shortEdge = Math.min(facts.width, facts.height);
|
|
30
|
+
if (facts.platform === 'ios' && /ipad/i.test(facts.model))
|
|
31
|
+
return 'tablet';
|
|
32
|
+
if (isFoldableOpen(facts))
|
|
33
|
+
return 'foldable-open';
|
|
34
|
+
if (!facts.touch && facts.platform === 'web')
|
|
35
|
+
return 'desktop';
|
|
36
|
+
if (facts.platform === 'electron')
|
|
37
|
+
return 'desktop';
|
|
38
|
+
return shortEdge >= TABLET_MIN_SHORT_EDGE ? 'tablet' : 'phone';
|
|
39
|
+
}
|
|
40
|
+
export function classifyFrame(facts) {
|
|
41
|
+
if (facts.platform === 'ios')
|
|
42
|
+
return /ipad/i.test(facts.model) ? 'expanded' : 'compact';
|
|
43
|
+
return Math.min(facts.width, facts.height) >= EXPANDED_MIN_SHORT_EDGE ? 'expanded' : 'compact';
|
|
44
|
+
}
|
|
45
|
+
export function classifyDevice(facts) {
|
|
46
|
+
const shortEdge = Math.min(facts.width, facts.height);
|
|
47
|
+
const longEdge = Math.max(facts.width, facts.height);
|
|
48
|
+
const native = facts.platform === 'android' || facts.platform === 'ios';
|
|
49
|
+
return {
|
|
50
|
+
frame: classifyFrame(facts),
|
|
51
|
+
formFactor: classifyFormFactor(facts),
|
|
52
|
+
orientation: classifyOrientation(facts.width, facts.height),
|
|
53
|
+
platform: facts.platform,
|
|
54
|
+
native,
|
|
55
|
+
touch: facts.touch || native,
|
|
56
|
+
shortEdge,
|
|
57
|
+
longEdge,
|
|
58
|
+
aspect: shortEdge > 0 ? longEdge / shortEdge : 1,
|
|
59
|
+
devicePixelRatio: facts.devicePixelRatio,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Reads viewport facts from a window (`null` outside a browser gives a 1280x720 desktop default).
|
|
64
|
+
* Platform facts default to `web` until a native shell says otherwise.
|
|
65
|
+
*/
|
|
66
|
+
export function readViewportFacts(win = typeof window === 'undefined' ? null : window, platform = { platform: 'web', model: '' }) {
|
|
67
|
+
if (!win) {
|
|
68
|
+
return { width: 1280, height: 720, devicePixelRatio: 1, touch: false, ...platform };
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
width: win.innerWidth,
|
|
72
|
+
height: win.innerHeight,
|
|
73
|
+
devicePixelRatio: win.devicePixelRatio ?? 1,
|
|
74
|
+
touch: (win.navigator?.maxTouchPoints ?? 0) > 0 || 'ontouchstart' in win,
|
|
75
|
+
...platform,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type DeviceProfile, type ViewportFacts } from './profile.js';
|
|
2
|
+
import { type SafeAreaInsets } from './safeArea.js';
|
|
3
|
+
type PlatformFacts = Pick<ViewportFacts, 'platform' | 'model'>;
|
|
4
|
+
export interface UseDeviceProfileOptions {
|
|
5
|
+
/** Platform facts known synchronously, e.g. `{ platform: Capacitor.getPlatform(), model: '' }`. */
|
|
6
|
+
platform?: PlatformFacts;
|
|
7
|
+
/** Async platform lookup, e.g. `() => Device.getInfo()` from `@capacitor/device`. */
|
|
8
|
+
loadPlatform?: () => Promise<PlatformFacts>;
|
|
9
|
+
}
|
|
10
|
+
/** Subscribes to every event that changes viewport geometry, including fold posture changes. */
|
|
11
|
+
export declare function subscribeViewportGeometry(callback: () => void): () => void;
|
|
12
|
+
export declare function useDeviceProfile(options?: UseDeviceProfileOptions): DeviceProfile;
|
|
13
|
+
/** Live safe-area insets; also publishes `--safe-*` CSS variables on the root element. */
|
|
14
|
+
export declare function useSafeAreaInsets(): SafeAreaInsets;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=react.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../../src/react.tsx"],"names":[],"mappings":"AAMA,OAAO,EAEL,KAAK,aAAa,EAElB,KAAK,aAAa,EACnB,MAAM,cAAc,CAAA;AACrB,OAAO,EAAsB,KAAK,cAAc,EAA8B,MAAM,eAAe,CAAA;AAEnG,KAAK,aAAa,GAAG,IAAI,CAAC,aAAa,EAAE,UAAU,GAAG,OAAO,CAAC,CAAA;AAE9D,MAAM,WAAW,uBAAuB;IACtC,mGAAmG;IACnG,QAAQ,CAAC,EAAE,aAAa,CAAA;IACxB,qFAAqF;IACrF,YAAY,CAAC,EAAE,MAAM,OAAO,CAAC,aAAa,CAAC,CAAA;CAC5C;AAID,gGAAgG;AAChG,wBAAgB,yBAAyB,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI,CAe1E;AAED,wBAAgB,gBAAgB,CAAC,OAAO,GAAE,uBAA4B,GAAG,aAAa,CA2BrF;AAED,0FAA0F;AAC1F,wBAAgB,iBAAiB,IAAI,cAAc,CAMlD"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React bindings: a live device profile and live safe-area insets. Both re-read on resize,
|
|
3
|
+
* orientation change, visual-viewport resize and fold posture change, so a foldable switches layout
|
|
4
|
+
* when it is folded or unfolded without a reload.
|
|
5
|
+
*/
|
|
6
|
+
import { useEffect, useState } from 'react';
|
|
7
|
+
import { classifyDevice, readViewportFacts, } from './profile.js';
|
|
8
|
+
import { readSafeAreaInsets, watchSafeArea, ZERO_INSETS } from './safeArea.js';
|
|
9
|
+
const WEB = { platform: 'web', model: '' };
|
|
10
|
+
/** Subscribes to every event that changes viewport geometry, including fold posture changes. */
|
|
11
|
+
export function subscribeViewportGeometry(callback) {
|
|
12
|
+
const fold = typeof window.matchMedia === 'function'
|
|
13
|
+
? window.matchMedia('(horizontal-viewport-segments: 2), (vertical-viewport-segments: 2)')
|
|
14
|
+
: null;
|
|
15
|
+
window.addEventListener('resize', callback);
|
|
16
|
+
window.addEventListener('orientationchange', callback);
|
|
17
|
+
window.visualViewport?.addEventListener('resize', callback);
|
|
18
|
+
fold?.addEventListener('change', callback);
|
|
19
|
+
return () => {
|
|
20
|
+
window.removeEventListener('resize', callback);
|
|
21
|
+
window.removeEventListener('orientationchange', callback);
|
|
22
|
+
window.visualViewport?.removeEventListener('resize', callback);
|
|
23
|
+
fold?.removeEventListener('change', callback);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function useDeviceProfile(options = {}) {
|
|
27
|
+
const [platform, setPlatform] = useState(options.platform ?? WEB);
|
|
28
|
+
const [profile, setProfile] = useState(() => classifyDevice(readViewportFacts(typeof window === 'undefined' ? null : window, platform)));
|
|
29
|
+
const loadPlatform = options.loadPlatform;
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (!loadPlatform)
|
|
32
|
+
return;
|
|
33
|
+
let cancelled = false;
|
|
34
|
+
loadPlatform()
|
|
35
|
+
.then((info) => {
|
|
36
|
+
if (!cancelled)
|
|
37
|
+
setPlatform({ platform: info.platform, model: info.model });
|
|
38
|
+
})
|
|
39
|
+
.catch(() => undefined);
|
|
40
|
+
return () => {
|
|
41
|
+
cancelled = true;
|
|
42
|
+
};
|
|
43
|
+
}, [loadPlatform]);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
const update = () => setProfile(classifyDevice(readViewportFacts(window, platform)));
|
|
46
|
+
update();
|
|
47
|
+
return subscribeViewportGeometry(update);
|
|
48
|
+
}, [platform]);
|
|
49
|
+
return profile;
|
|
50
|
+
}
|
|
51
|
+
/** Live safe-area insets; also publishes `--safe-*` CSS variables on the root element. */
|
|
52
|
+
export function useSafeAreaInsets() {
|
|
53
|
+
const [insets, setInsets] = useState(() => typeof document === 'undefined' ? { ...ZERO_INSETS } : readSafeAreaInsets(document));
|
|
54
|
+
useEffect(() => watchSafeArea({ onChange: setInsets }), []);
|
|
55
|
+
return insets;
|
|
56
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe-area insets as numbers and as CSS variables.
|
|
3
|
+
*
|
|
4
|
+
* `env(safe-area-inset-*)` already works inside CSS, but JavaScript layout (canvas HUDs, joystick
|
|
5
|
+
* placement) needs the numbers, and some Android WebViews do not refresh `env()` consumers after a
|
|
6
|
+
* fold, an orientation change or a cut-out change. `watchSafeArea` measures the insets through a
|
|
7
|
+
* hidden probe element and republishes them as `--safe-top/right/bottom/left` px variables on every
|
|
8
|
+
* resize, orientation change, visual-viewport resize and fold posture change.
|
|
9
|
+
*/
|
|
10
|
+
export interface SafeAreaInsets {
|
|
11
|
+
top: number;
|
|
12
|
+
right: number;
|
|
13
|
+
bottom: number;
|
|
14
|
+
left: number;
|
|
15
|
+
}
|
|
16
|
+
export declare const ZERO_INSETS: Readonly<SafeAreaInsets>;
|
|
17
|
+
export interface SafeAreaWatchOptions {
|
|
18
|
+
/** Element that receives the CSS variables. Defaults to `document.documentElement`. */
|
|
19
|
+
target?: HTMLElement;
|
|
20
|
+
/** CSS variable prefix. Defaults to `--safe-` (so `--safe-top` ...). */
|
|
21
|
+
prefix?: string;
|
|
22
|
+
/** Called with fresh insets after every measurement. */
|
|
23
|
+
onChange?: (insets: SafeAreaInsets) => void;
|
|
24
|
+
}
|
|
25
|
+
/** Measures the current safe-area insets through a temporary probe element. */
|
|
26
|
+
export declare function readSafeAreaInsets(doc?: Document): SafeAreaInsets;
|
|
27
|
+
export declare function applySafeAreaVariables(insets: SafeAreaInsets, target: HTMLElement, prefix?: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* Measures the insets now and again after every geometry change. Returns a disposer that removes all
|
|
30
|
+
* listeners. Measurement runs on the next animation frame so a fold reports post-layout values.
|
|
31
|
+
*/
|
|
32
|
+
export declare function watchSafeArea(options?: SafeAreaWatchOptions): () => void;
|
|
33
|
+
//# sourceMappingURL=safeArea.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safeArea.d.ts","sourceRoot":"","sources":["../../src/safeArea.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,WAAW,cAAc;IAC7B,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;CACb;AAED,eAAO,MAAM,WAAW,EAAE,QAAQ,CAAC,cAAc,CAK/C,CAAA;AAIF,MAAM,WAAW,oBAAoB;IACnC,uFAAuF;IACvF,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB,wEAAwE;IACxE,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,wDAAwD;IACxD,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,IAAI,CAAA;CAC5C;AAOD,+EAA+E;AAC/E,wBAAgB,kBAAkB,CAAC,GAAG,GAAE,QAAmB,GAAG,cAAc,CAwB3E;AAED,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,cAAc,EACtB,MAAM,EAAE,WAAW,EACnB,MAAM,SAAY,GACjB,IAAI,CAEN;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,OAAO,GAAE,oBAAyB,GAAG,MAAM,IAAI,CA4B5E"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe-area insets as numbers and as CSS variables.
|
|
3
|
+
*
|
|
4
|
+
* `env(safe-area-inset-*)` already works inside CSS, but JavaScript layout (canvas HUDs, joystick
|
|
5
|
+
* placement) needs the numbers, and some Android WebViews do not refresh `env()` consumers after a
|
|
6
|
+
* fold, an orientation change or a cut-out change. `watchSafeArea` measures the insets through a
|
|
7
|
+
* hidden probe element and republishes them as `--safe-top/right/bottom/left` px variables on every
|
|
8
|
+
* resize, orientation change, visual-viewport resize and fold posture change.
|
|
9
|
+
*/
|
|
10
|
+
export const ZERO_INSETS = Object.freeze({
|
|
11
|
+
top: 0,
|
|
12
|
+
right: 0,
|
|
13
|
+
bottom: 0,
|
|
14
|
+
left: 0,
|
|
15
|
+
});
|
|
16
|
+
const SIDES = ['top', 'right', 'bottom', 'left'];
|
|
17
|
+
function parsePx(value) {
|
|
18
|
+
const parsed = Number.parseFloat(value);
|
|
19
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
20
|
+
}
|
|
21
|
+
/** Measures the current safe-area insets through a temporary probe element. */
|
|
22
|
+
export function readSafeAreaInsets(doc = document) {
|
|
23
|
+
const view = doc.defaultView;
|
|
24
|
+
if (!view || !doc.body)
|
|
25
|
+
return { ...ZERO_INSETS };
|
|
26
|
+
const probe = doc.createElement('div');
|
|
27
|
+
probe.setAttribute('aria-hidden', 'true');
|
|
28
|
+
probe.style.cssText = [
|
|
29
|
+
'position:fixed',
|
|
30
|
+
'visibility:hidden',
|
|
31
|
+
'pointer-events:none',
|
|
32
|
+
'padding-top:env(safe-area-inset-top,0px)',
|
|
33
|
+
'padding-right:env(safe-area-inset-right,0px)',
|
|
34
|
+
'padding-bottom:env(safe-area-inset-bottom,0px)',
|
|
35
|
+
'padding-left:env(safe-area-inset-left,0px)',
|
|
36
|
+
].join(';');
|
|
37
|
+
doc.body.appendChild(probe);
|
|
38
|
+
const style = view.getComputedStyle(probe);
|
|
39
|
+
const insets = {
|
|
40
|
+
top: parsePx(style.paddingTop),
|
|
41
|
+
right: parsePx(style.paddingRight),
|
|
42
|
+
bottom: parsePx(style.paddingBottom),
|
|
43
|
+
left: parsePx(style.paddingLeft),
|
|
44
|
+
};
|
|
45
|
+
probe.remove();
|
|
46
|
+
return insets;
|
|
47
|
+
}
|
|
48
|
+
export function applySafeAreaVariables(insets, target, prefix = '--safe-') {
|
|
49
|
+
for (const side of SIDES)
|
|
50
|
+
target.style.setProperty(`${prefix}${side}`, `${insets[side]}px`);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Measures the insets now and again after every geometry change. Returns a disposer that removes all
|
|
54
|
+
* listeners. Measurement runs on the next animation frame so a fold reports post-layout values.
|
|
55
|
+
*/
|
|
56
|
+
export function watchSafeArea(options = {}) {
|
|
57
|
+
if (typeof window === 'undefined' || typeof document === 'undefined')
|
|
58
|
+
return () => undefined;
|
|
59
|
+
const target = options.target ?? document.documentElement;
|
|
60
|
+
const prefix = options.prefix ?? '--safe-';
|
|
61
|
+
let frame = 0;
|
|
62
|
+
const measure = () => {
|
|
63
|
+
frame = 0;
|
|
64
|
+
const insets = readSafeAreaInsets(document);
|
|
65
|
+
applySafeAreaVariables(insets, target, prefix);
|
|
66
|
+
options.onChange?.(insets);
|
|
67
|
+
};
|
|
68
|
+
const schedule = () => {
|
|
69
|
+
if (frame !== 0)
|
|
70
|
+
return;
|
|
71
|
+
frame = window.requestAnimationFrame(measure);
|
|
72
|
+
};
|
|
73
|
+
measure();
|
|
74
|
+
window.addEventListener('resize', schedule);
|
|
75
|
+
window.addEventListener('orientationchange', schedule);
|
|
76
|
+
window.visualViewport?.addEventListener('resize', schedule);
|
|
77
|
+
const fold = foldPostureMedia(window);
|
|
78
|
+
fold?.addEventListener('change', schedule);
|
|
79
|
+
return () => {
|
|
80
|
+
if (frame !== 0)
|
|
81
|
+
window.cancelAnimationFrame(frame);
|
|
82
|
+
window.removeEventListener('resize', schedule);
|
|
83
|
+
window.removeEventListener('orientationchange', schedule);
|
|
84
|
+
window.visualViewport?.removeEventListener('resize', schedule);
|
|
85
|
+
fold?.removeEventListener('change', schedule);
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** Media query that flips when a foldable changes posture; `null` where unsupported. */
|
|
89
|
+
function foldPostureMedia(win) {
|
|
90
|
+
if (typeof win.matchMedia !== 'function')
|
|
91
|
+
return null;
|
|
92
|
+
return win.matchMedia('(horizontal-viewport-segments: 2), (vertical-viewport-segments: 2)');
|
|
93
|
+
}
|
package/docs/API.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: API reference
|
|
3
|
+
description: Every export of capacitor-device-profile with signatures and the exact classification rules.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
All exports come from `capacitor-device-profile` unless stated; the React bindings come from
|
|
7
|
+
`capacitor-device-profile/react`.
|
|
8
|
+
|
|
9
|
+
## Device profile
|
|
10
|
+
|
|
11
|
+
### `classifyDevice(facts: ViewportFacts): DeviceProfile`
|
|
12
|
+
|
|
13
|
+
Pure classification. `ViewportFacts` is `{ width, height, devicePixelRatio, touch, platform, model }`
|
|
14
|
+
with sizes in CSS pixels and `platform` one of `web`, `android`, `ios`, `electron`. The result:
|
|
15
|
+
|
|
16
|
+
| Field | Meaning |
|
|
17
|
+
| --- | --- |
|
|
18
|
+
| `formFactor` | `phone`, `foldable-open`, `tablet` or `desktop` |
|
|
19
|
+
| `frame` | `compact` (a phone-sized layout) or `expanded` (room for side panels) |
|
|
20
|
+
| `orientation` | `portrait` when height exceeds width, otherwise `landscape` |
|
|
21
|
+
| `shortEdge`, `longEdge`, `aspect` | Edges in CSS px; `aspect` is long over short (`1` when square or empty) |
|
|
22
|
+
| `native` | True on `android` and `ios` |
|
|
23
|
+
| `touch` | `facts.touch`, or true on any native platform |
|
|
24
|
+
| `platform`, `devicePixelRatio` | Passed through |
|
|
25
|
+
|
|
26
|
+
### Rules
|
|
27
|
+
|
|
28
|
+
Applied in this order by `classifyFormFactor`:
|
|
29
|
+
|
|
30
|
+
1. iOS with an iPad model string is a `tablet`.
|
|
31
|
+
2. `isFoldableOpen(facts)` is `foldable-open`.
|
|
32
|
+
3. A non-touch web page, or any `electron` shell, is a `desktop`.
|
|
33
|
+
4. Otherwise a short edge of at least `TABLET_MIN_SHORT_EDGE` (600) is a `tablet`, else a `phone`.
|
|
34
|
+
|
|
35
|
+
`isFoldableOpen` requires touch, a long/short aspect of at most `NEAR_SQUARE_MAX_ASPECT` (1.3) and a
|
|
36
|
+
short edge of at least 600. An unfolded OnePlus Open is 821 by 765 CSS px (aspect 1.07). A phone in
|
|
37
|
+
portrait is about 2.1; a 4:3 tablet is 1.33.
|
|
38
|
+
|
|
39
|
+
`classifyFrame` is `expanded` from a short edge of `EXPANDED_MIN_SHORT_EDGE` (700), except on iOS where
|
|
40
|
+
only an iPad is expanded.
|
|
41
|
+
|
|
42
|
+
### `readViewportFacts(win?, platform?)`
|
|
43
|
+
|
|
44
|
+
Reads `innerWidth`, `innerHeight`, `devicePixelRatio` and touch support from a window. `platform`
|
|
45
|
+
defaults to `{ platform: 'web', model: '' }`; a native shell supplies the real values (for example
|
|
46
|
+
from `Device.getInfo()`). Passing `null`, or running without a `window`, returns a 1280 by 720
|
|
47
|
+
non-touch desktop default.
|
|
48
|
+
|
|
49
|
+
## Orientation
|
|
50
|
+
|
|
51
|
+
### `decideOrientation(profile, preferred): OrientationDecision`
|
|
52
|
+
|
|
53
|
+
`preferred` is `portrait`, `landscape` or `any`. Returns `{ allowed, promptRotate, target }`. Tablets,
|
|
54
|
+
desktops, unfolded foldables, non-touch devices and `any` accept both orientations and never prompt.
|
|
55
|
+
A phone that is not in the preferred orientation gets `promptRotate: true` and the orientation to
|
|
56
|
+
ask for in `target`. The decision drives a hint only: the package never locks or forces rotation.
|
|
57
|
+
|
|
58
|
+
## Safe area
|
|
59
|
+
|
|
60
|
+
### `readSafeAreaInsets(doc?): SafeAreaInsets`
|
|
61
|
+
|
|
62
|
+
Measures `env(safe-area-inset-*)` through a hidden, fixed-position probe element and returns
|
|
63
|
+
`{ top, right, bottom, left }` in px. Returns `ZERO_INSETS` values without a document body.
|
|
64
|
+
|
|
65
|
+
### `applySafeAreaVariables(insets, target, prefix?)`
|
|
66
|
+
|
|
67
|
+
Writes `--safe-top`, `--safe-right`, `--safe-bottom` and `--safe-left` (px) on `target`. The default
|
|
68
|
+
prefix is `--safe-`.
|
|
69
|
+
|
|
70
|
+
### `watchSafeArea(options?): () => void`
|
|
71
|
+
|
|
72
|
+
Measures now and again, on the next animation frame, after every `resize`, `orientationchange`,
|
|
73
|
+
visual-viewport `resize` and fold-posture media-query change. Options: `target` (default
|
|
74
|
+
`document.documentElement`), `prefix`, `onChange(insets)`. Returns a disposer that removes every
|
|
75
|
+
listener; outside a browser it returns a no-op.
|
|
76
|
+
|
|
77
|
+
## App lifecycle
|
|
78
|
+
|
|
79
|
+
### `createAppLifecycle(options): AppLifecycle`
|
|
80
|
+
|
|
81
|
+
Options: `native`, `app` (Capacitor's `App` plugin), `onPause`, `onResume`, and `window` / `document`
|
|
82
|
+
overrides for the web path.
|
|
83
|
+
|
|
84
|
+
- Native: pause and resume follow `appStateChange`; the Android `backButton` runs the back stack.
|
|
85
|
+
- Web: pause and resume follow `visibilitychange`, `pagehide` and `pageshow`.
|
|
86
|
+
|
|
87
|
+
The returned object has `state` (`active` or `background`), `pushBackHandler(handler)` (returns a
|
|
88
|
+
remover), `handleBack()` and `dispose()`. Handlers run newest first; a handler returns `true` to
|
|
89
|
+
consume the press. An unconsumed native back press calls `app.minimizeApp()` instead of exiting.
|
|
90
|
+
`dispose()` removes every listener, including the native listener handles, and stops further
|
|
91
|
+
callbacks.
|
|
92
|
+
|
|
93
|
+
## Haptics
|
|
94
|
+
|
|
95
|
+
### `createHaptics(options): Haptics`
|
|
96
|
+
|
|
97
|
+
Options: `native`, `plugin` (Capacitor's `Haptics` plugin), `enabled` (default `true`). Methods:
|
|
98
|
+
`impact(strength?)` with `light`, `medium` or `heavy`; `notify(kind)` with `success`, `warning` or
|
|
99
|
+
`error`; `selection()`; `setEnabled(boolean)` and the `enabled` getter. Off the native platform, or
|
|
100
|
+
while disabled, every call does nothing. A rejected plugin call is swallowed.
|
|
101
|
+
|
|
102
|
+
## React bindings
|
|
103
|
+
|
|
104
|
+
### `useDeviceProfile(options?): DeviceProfile`
|
|
105
|
+
|
|
106
|
+
Returns the live profile and updates on every geometry event, so folding or unfolding re-classifies
|
|
107
|
+
without a reload. Options: `platform` (facts known synchronously) and `loadPlatform` (async lookup such
|
|
108
|
+
as `() => Device.getInfo()`; the profile re-classifies when it resolves).
|
|
109
|
+
|
|
110
|
+
### `useSafeAreaInsets(): SafeAreaInsets`
|
|
111
|
+
|
|
112
|
+
Live insets from `watchSafeArea`; also publishes the `--safe-*` variables on the root element.
|
|
113
|
+
|
|
114
|
+
### `subscribeViewportGeometry(callback): () => void`
|
|
115
|
+
|
|
116
|
+
Subscribes to `resize`, `orientationchange`, visual-viewport `resize` and the fold-posture media query.
|
|
117
|
+
Returns the unsubscribe function.
|
|
118
|
+
|
|
119
|
+
## Capacitor compatibility
|
|
120
|
+
|
|
121
|
+
`AppPluginLike`, `HapticsPluginLike` and `PluginListenerHandleLike` are the structural subsets of the
|
|
122
|
+
Capacitor 8 plugins this package uses. A type test keeps the real `@capacitor/app` and
|
|
123
|
+
`@capacitor/haptics` plugin types assignable to them.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Architecture
|
|
3
|
+
description: Module boundaries, invariants and intentional limits of capacitor-device-profile.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Modules
|
|
7
|
+
|
|
8
|
+
| Module | Responsibility |
|
|
9
|
+
| --- | --- |
|
|
10
|
+
| `profile` | Pure viewport classification. No DOM access except in `readViewportFacts`. |
|
|
11
|
+
| `orientation` | Pure policy over a profile. Never touches the screen. |
|
|
12
|
+
| `safeArea` | Measures insets and publishes CSS variables. The only module that creates DOM nodes. |
|
|
13
|
+
| `lifecycle` | Pause/resume and the back-button stack over an injected `App` plugin or page events. |
|
|
14
|
+
| `haptics` | A guarded facade over an injected `Haptics` plugin. |
|
|
15
|
+
| `react` | Thin hooks over the modules above. The only module that imports React. |
|
|
16
|
+
|
|
17
|
+
## Invariants
|
|
18
|
+
|
|
19
|
+
1. **No runtime dependencies, no Capacitor import.** Plugins are passed in and described by `*Like`
|
|
20
|
+
structural interfaces, so the package pins no Capacitor version and every module unit-tests with a
|
|
21
|
+
fake. A type test keeps the real plugin types assignable.
|
|
22
|
+
2. **Classification is a pure function of facts.** Everything environmental (window, platform,
|
|
23
|
+
model) is read once into `ViewportFacts` and injected, so a thresholds change is a unit test.
|
|
24
|
+
3. **Never force-rotate.** `decideOrientation` produces a hint. Tablets and unfolded foldables are
|
|
25
|
+
never nagged.
|
|
26
|
+
4. **A foldable is its own class.** An unfolded book-style foldable is touch-primary, tablet-sized and
|
|
27
|
+
near-square, so it gets `foldable-open` instead of being stretched from a phone or squashed from a
|
|
28
|
+
tablet. The thresholds come from a measured device and move only with a measurement.
|
|
29
|
+
5. **Geometry changes re-classify live.** Fold posture, resize, orientation change and visual-viewport
|
|
30
|
+
resize all feed the same subscription, so no reload is needed after a fold.
|
|
31
|
+
6. **Failures never reach a frame loop.** A rejected haptics or `minimizeApp` call is swallowed.
|
|
32
|
+
7. **Everything disposes.** Each subscription returns a disposer, and `dispose()` on the lifecycle also
|
|
33
|
+
removes the native listener handles.
|
|
34
|
+
|
|
35
|
+
## Dual build
|
|
36
|
+
|
|
37
|
+
`tsc` emits ESM with declarations, and CommonJS with its own declarations, from two tsconfigs, both
|
|
38
|
+
with `moduleResolution: bundler`. `scripts/build.mjs` renames the CommonJS output to `.cjs` and
|
|
39
|
+
`.d.cts`, rewrites relative specifiers to match, and writes a `{ "type": "commonjs" }` manifest into
|
|
40
|
+
`dist/cjs`, so Node and TypeScript each see the correct module format. `publint` and
|
|
41
|
+
`@arethetypeswrong/cli` gate the result, and `scripts/verify-package.mjs` plus `scripts/consumer-smoke.mjs`
|
|
42
|
+
install the packed tarball into an empty project against npmjs only.
|
|
43
|
+
|
|
44
|
+
## Intentional limits
|
|
45
|
+
|
|
46
|
+
- No orientation locking and no screen-wake or status-bar control.
|
|
47
|
+
- No Capacitor plugin wrappers beyond the structural subsets above.
|
|
48
|
+
- Thresholds are CSS-pixel based; they do not read the Window Segments API geometry beyond using its
|
|
49
|
+
media query as a change signal.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Contributing
|
|
3
|
+
description: Set up capacitor-device-profile, validate a change, and contribute through the protected workflow.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## Local workflow
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
mise install
|
|
10
|
+
pnpm install --frozen-lockfile
|
|
11
|
+
pnpm verify
|
|
12
|
+
pnpm docs:build
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`pnpm verify` is the library gate: Biome, Markdown linting, strict TypeScript 7, coverage, the dual
|
|
16
|
+
ESM/CommonJS build, `publint`, Are The Types Wrong, a packed-tarball content check and a clean-consumer
|
|
17
|
+
install against npmjs. `pnpm docs:build` validates and renders the Sourcey site.
|
|
18
|
+
|
|
19
|
+
Branch from `main`, make a focused Conventional Commit, open an upstream pull request, and keep the
|
|
20
|
+
branch current by merging `main` into it when necessary. The protected path uses automated checks
|
|
21
|
+
rather than a routine human approval; merge commits preserve the constituent history. Do not
|
|
22
|
+
hand-edit versions or `CHANGELOG.md`: Release Please owns them.
|
|
23
|
+
|
|
24
|
+
Read the repository [contribution guide](https://github.com/jbcom/capacitor-device-profile/blob/main/CONTRIBUTING.md)
|
|
25
|
+
and [agent instructions](https://github.com/jbcom/capacitor-device-profile/blob/main/AGENTS.md) before changing public
|
|
26
|
+
APIs.
|
package/package.json
CHANGED
|
@@ -1,6 +1,145 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "capacitor-device-profile",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Mobile and foldable runtime over Capacitor: device profile (phone, foldable-open, tablet, desktop) that re-classifies live on fold and unfold, orientation policy, live safe-area insets, app lifecycle with a back-button stack, and a haptics facade.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Jon Bogaty <jon@jonbogaty.com>",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public",
|
|
10
|
+
"provenance": true
|
|
11
|
+
},
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"main": "./dist/cjs/index.cjs",
|
|
14
|
+
"module": "./dist/esm/index.js",
|
|
15
|
+
"types": "./dist/esm/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": {
|
|
19
|
+
"types": "./dist/esm/index.d.ts",
|
|
20
|
+
"default": "./dist/esm/index.js"
|
|
21
|
+
},
|
|
22
|
+
"require": {
|
|
23
|
+
"types": "./dist/cjs/index.d.cts",
|
|
24
|
+
"default": "./dist/cjs/index.cjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"./react": {
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./dist/esm/react.d.ts",
|
|
30
|
+
"default": "./dist/esm/react.js"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./dist/cjs/react.d.cts",
|
|
34
|
+
"default": "./dist/cjs/react.cjs"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"typesVersions": {
|
|
40
|
+
"*": {
|
|
41
|
+
"react": [
|
|
42
|
+
"./dist/esm/react.d.ts"
|
|
43
|
+
],
|
|
44
|
+
"*": [
|
|
45
|
+
"./dist/esm/index.d.ts"
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"files": [
|
|
50
|
+
"CHANGELOG.md",
|
|
51
|
+
"CONTRIBUTING.md",
|
|
52
|
+
"SECURITY.md",
|
|
53
|
+
"dist",
|
|
54
|
+
"docs/API.md",
|
|
55
|
+
"docs/ARCHITECTURE.md",
|
|
56
|
+
"src"
|
|
57
|
+
],
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "node scripts/build.mjs",
|
|
60
|
+
"clean": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\"",
|
|
61
|
+
"prepack": "node scripts/build.mjs",
|
|
62
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
63
|
+
"test": "vitest run",
|
|
64
|
+
"test:watch": "vitest",
|
|
65
|
+
"coverage": "vitest run --coverage",
|
|
66
|
+
"lint": "biome check .",
|
|
67
|
+
"lint:fix": "biome check --write .",
|
|
68
|
+
"lint:docs": "markdownlint-cli2 README.md CONTRIBUTING.md SECURITY.md CODE_OF_CONDUCT.md CHANGELOG.md AGENTS.md \"docs/**/*.md\" \"!docs/node_modules/**\" \"!docs/dist/**\" .github/PULL_REQUEST_TEMPLATE.md",
|
|
69
|
+
"package:check": "publint && attw --pack . && node scripts/verify-package.mjs",
|
|
70
|
+
"smoke:consumer": "node scripts/consumer-smoke.mjs",
|
|
71
|
+
"verify": "pnpm run lint && pnpm run lint:docs && pnpm run typecheck && pnpm run coverage && pnpm run build && pnpm run package:check && pnpm run smoke:consumer",
|
|
72
|
+
"docs:dev": "pnpm --filter capacitor-device-profile-docs dev",
|
|
73
|
+
"docs:build": "pnpm --filter capacitor-device-profile-docs build",
|
|
74
|
+
"docs:check": "pnpm --filter capacitor-device-profile-docs check",
|
|
75
|
+
"prepare": "simple-git-hooks"
|
|
76
|
+
},
|
|
77
|
+
"peerDependencies": {
|
|
78
|
+
"react": "^18 || ^19"
|
|
79
|
+
},
|
|
80
|
+
"peerDependenciesMeta": {
|
|
81
|
+
"react": {
|
|
82
|
+
"optional": true
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
"devDependencies": {
|
|
86
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
87
|
+
"@biomejs/biome": "2.5.15",
|
|
88
|
+
"@capacitor/app": "^8.1.2",
|
|
89
|
+
"@capacitor/core": "^8.5.3",
|
|
90
|
+
"@capacitor/device": "^8.0.3",
|
|
91
|
+
"@capacitor/haptics": "^8.0.2",
|
|
92
|
+
"@commitlint/cli": "^21.2.3",
|
|
93
|
+
"@commitlint/config-conventional": "^21.2.3",
|
|
94
|
+
"@testing-library/react": "^16.3.3",
|
|
95
|
+
"@types/node": "^24.13.3",
|
|
96
|
+
"@types/react": "^19.3.0",
|
|
97
|
+
"@types/react-dom": "^19.3.0",
|
|
98
|
+
"@vitest/coverage-v8": "^5.0.3",
|
|
99
|
+
"jsdom": "^30.1.2",
|
|
100
|
+
"lint-staged": "^17.6.0",
|
|
101
|
+
"markdownlint-cli2": "^0.23.3",
|
|
102
|
+
"publint": "^0.3.25",
|
|
103
|
+
"react": "^19.3.0",
|
|
104
|
+
"react-dom": "^19.3.0",
|
|
105
|
+
"simple-git-hooks": "^2.14.0",
|
|
106
|
+
"typescript": "^7.0.2",
|
|
107
|
+
"vitest": "^5.0.3"
|
|
108
|
+
},
|
|
109
|
+
"simple-git-hooks": {
|
|
110
|
+
"pre-commit": "pnpm exec lint-staged",
|
|
111
|
+
"commit-msg": "pnpm exec commitlint --edit $1"
|
|
112
|
+
},
|
|
113
|
+
"lint-staged": {
|
|
114
|
+
"*.{ts,tsx,js,mjs,cjs,json}": [
|
|
115
|
+
"biome check --write --no-errors-on-unmatched"
|
|
116
|
+
],
|
|
117
|
+
"*.md": [
|
|
118
|
+
"markdownlint-cli2"
|
|
119
|
+
]
|
|
120
|
+
},
|
|
121
|
+
"repository": {
|
|
122
|
+
"type": "git",
|
|
123
|
+
"url": "git+https://github.com/jbcom/capacitor-device-profile.git"
|
|
124
|
+
},
|
|
125
|
+
"packageManager": "pnpm@12.10.1",
|
|
126
|
+
"bugs": {
|
|
127
|
+
"url": "https://github.com/jbcom/capacitor-device-profile/issues"
|
|
128
|
+
},
|
|
129
|
+
"homepage": "https://github.com/jbcom/capacitor-device-profile#readme",
|
|
130
|
+
"engines": {
|
|
131
|
+
"node": ">=24"
|
|
132
|
+
},
|
|
133
|
+
"keywords": [
|
|
134
|
+
"capacitor",
|
|
135
|
+
"device-profile",
|
|
136
|
+
"form-factor",
|
|
137
|
+
"foldable",
|
|
138
|
+
"safe-area",
|
|
139
|
+
"haptics",
|
|
140
|
+
"app-lifecycle",
|
|
141
|
+
"orientation",
|
|
142
|
+
"react",
|
|
143
|
+
"game-dev"
|
|
144
|
+
]
|
|
145
|
+
}
|