@loidolt/theme-capacitor 0.0.0-stage → 0.12.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/dist/system.js ADDED
@@ -0,0 +1,107 @@
1
+ import { listen, nativePlatform, quietly } from './platform.js';
2
+ const frame = () => new Promise((resolve) => typeof requestAnimationFrame === 'function' ? requestAnimationFrame(() => resolve()) : resolve());
3
+ /**
4
+ * Lifts the native splash screen once the page is ready to be seen: the theme applied, the fonts
5
+ * in, and a frame painted — so the splash gives way to the app rather than to a flash of
6
+ * unstyled or wrong-theme content. Never rejects.
7
+ */
8
+ export async function hideSplashScreen(options) {
9
+ if (!nativePlatform())
10
+ return;
11
+ const { SplashScreen, until, waitForFonts = true, timeout = 3000 } = options;
12
+ const reduced = typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
13
+ const fadeOutDuration = reduced ? 0 : (options.fadeOutDuration ?? 200);
14
+ const waits = [];
15
+ if (until)
16
+ waits.push(until.catch(() => { }));
17
+ if (waitForFonts && typeof document !== 'undefined' && document.fonts) {
18
+ waits.push(document.fonts.ready.catch(() => { }));
19
+ }
20
+ let timer;
21
+ await Promise.race([
22
+ Promise.all(waits),
23
+ new Promise((resolve) => {
24
+ timer = setTimeout(resolve, timeout);
25
+ }),
26
+ ]);
27
+ clearTimeout(timer);
28
+ // Two frames: the first is where the restored theme is laid out, the second is on screen.
29
+ await frame();
30
+ await frame();
31
+ try {
32
+ await SplashScreen.hide({ fadeOutDuration });
33
+ }
34
+ catch {
35
+ /* a splash that is already gone is fine */
36
+ }
37
+ }
38
+ /**
39
+ * Makes the page's text follow the system text size — iOS Dynamic Type and Android's font scale —
40
+ * which a web view otherwise ignores, so people who need larger text get it in the app as they
41
+ * do everywhere else (WCAG 1.4.4). Re-reads on resume when given `App`. Returns the teardown.
42
+ *
43
+ * On iOS the zoom arrives as `-webkit-text-size-adjust`, which scales text after every `min()`
44
+ * and `clamp()` in the stylesheet has been resolved, so the factor is also written to `<html>` as
45
+ * `--ldt-text-zoom`. Components that cap their text's growth — `TabBar`'s labels, the Topbar
46
+ * brand, the `PageHeader` title — read it to keep the cap. Android's web view zooms text its own
47
+ * way and gets no variable.
48
+ */
49
+ export function followSystemTextSize(options) {
50
+ const platform = nativePlatform();
51
+ if (!platform)
52
+ return () => { };
53
+ const { TextZoom, App } = options;
54
+ const root = document.documentElement;
55
+ let last;
56
+ let stopped = false;
57
+ const apply = () => quietly(TextZoom.getPreferred().then(async ({ value }) => {
58
+ if (!Number.isFinite(value) || value <= 0 || value === last)
59
+ return;
60
+ last = value;
61
+ await TextZoom.set({ value });
62
+ if (platform === 'ios' && !stopped)
63
+ root.style.setProperty('--ldt-text-zoom', String(value));
64
+ }));
65
+ apply();
66
+ const stopListening = App ? listen(App.addListener('resume', apply)) : () => { };
67
+ return () => {
68
+ stopped = true;
69
+ stopListening();
70
+ root.style.removeProperty('--ldt-text-zoom');
71
+ };
72
+ }
73
+ /**
74
+ * Routes `navigator.clipboard.writeText` through `@capacitor/clipboard`. Android's web view
75
+ * refuses the web Clipboard API without a permission prompt it cannot show, so every copy button
76
+ * — `CodeBlock`'s included — would fail silently there. Returns the teardown, which restores the
77
+ * original.
78
+ */
79
+ export function bridgeClipboard(options) {
80
+ if (!nativePlatform() || typeof navigator === 'undefined')
81
+ return () => { };
82
+ const { Clipboard } = options;
83
+ const writeText = (text) => Clipboard.write({ string: text });
84
+ const existing = navigator.clipboard;
85
+ if (existing) {
86
+ const own = Object.getOwnPropertyDescriptor(existing, 'writeText');
87
+ Object.defineProperty(existing, 'writeText', {
88
+ configurable: true,
89
+ writable: true,
90
+ value: writeText,
91
+ });
92
+ return () => {
93
+ if (own)
94
+ Object.defineProperty(existing, 'writeText', own);
95
+ else
96
+ delete existing.writeText;
97
+ };
98
+ }
99
+ // No Clipboard API at all (an insecure origin): provide just enough of one.
100
+ Object.defineProperty(navigator, 'clipboard', {
101
+ configurable: true,
102
+ value: { writeText },
103
+ });
104
+ return () => {
105
+ delete navigator.clipboard;
106
+ };
107
+ }
@@ -0,0 +1,33 @@
1
+ import type { Theme } from '@loidolt/theme-svelte';
2
+ import type { PreferencesPlugin, Teardown } from './types.js';
3
+ export interface PersistThemeOptions {
4
+ /** `Preferences` from `@capacitor/preferences`. */
5
+ Preferences: PreferencesPlugin;
6
+ /** Key for the colour scheme. Defaults to `createTheme`'s own storage key. */
7
+ key?: string;
8
+ /** Key for the preset, when the theme has presets. Defaults to `createTheme`'s own. */
9
+ presetKey?: string;
10
+ }
11
+ export interface PersistedTheme {
12
+ /**
13
+ * Settles once the stored choice has been read and applied — await it before hiding the splash
14
+ * screen, so the first frame anyone sees is already in their theme.
15
+ */
16
+ readonly ready: Promise<void>;
17
+ destroy: Teardown;
18
+ }
19
+ /**
20
+ * Backs a `createTheme` store with native storage. iOS may evict a web view's `localStorage`
21
+ * under storage pressure, which would quietly reset everyone's dark mode; `Preferences` lives in
22
+ * `UserDefaults` and `SharedPreferences` and does not. The theme still writes `localStorage` too,
23
+ * so `themeScript()` paints the first frame from it as usual — this restores and mirrors.
24
+ *
25
+ * ```ts
26
+ * import { Preferences } from '@capacitor/preferences';
27
+ * export const theme = createTheme({ presets: ['loidolt', 'soft'] });
28
+ * persistTheme(theme, { Preferences });
29
+ * ```
30
+ *
31
+ * A no-op outside a native shell (`ready` resolves at once).
32
+ */
33
+ export declare function persistTheme(theme: Theme, options: PersistThemeOptions): PersistedTheme;
@@ -0,0 +1,59 @@
1
+ import { nativePlatform, quietly } from './platform.js';
2
+ const PREFERENCES = ['light', 'dark', 'system'];
3
+ /**
4
+ * Backs a `createTheme` store with native storage. iOS may evict a web view's `localStorage`
5
+ * under storage pressure, which would quietly reset everyone's dark mode; `Preferences` lives in
6
+ * `UserDefaults` and `SharedPreferences` and does not. The theme still writes `localStorage` too,
7
+ * so `themeScript()` paints the first frame from it as usual — this restores and mirrors.
8
+ *
9
+ * ```ts
10
+ * import { Preferences } from '@capacitor/preferences';
11
+ * export const theme = createTheme({ presets: ['loidolt', 'soft'] });
12
+ * persistTheme(theme, { Preferences });
13
+ * ```
14
+ *
15
+ * A no-op outside a native shell (`ready` resolves at once).
16
+ */
17
+ export function persistTheme(theme, options) {
18
+ if (!nativePlatform())
19
+ return { ready: Promise.resolve(), destroy: () => { } };
20
+ const { Preferences, key = 'loidolt-theme', presetKey = 'loidolt-preset' } = options;
21
+ const hasPresets = theme.presets.length > 0;
22
+ const read = (name) => Preferences.get({ key: name }).then(({ value }) => value, () => null);
23
+ // Nothing is written until the stored value has been read, or the default the theme started
24
+ // with would overwrite the choice it was about to restore.
25
+ let hydrated = $state(false);
26
+ let stopped = false;
27
+ const ready = Promise.all([read(key), hasPresets ? read(presetKey) : null]).then(([preference, preset]) => {
28
+ if (stopped)
29
+ return;
30
+ if (preference !== null && PREFERENCES.includes(preference)) {
31
+ if (theme.preference !== preference)
32
+ theme.preference = preference;
33
+ }
34
+ if (preset !== null && theme.presets.includes(preset) && theme.preset !== preset) {
35
+ theme.preset = preset;
36
+ }
37
+ hydrated = true;
38
+ });
39
+ const stop = $effect.root(() => {
40
+ $effect(() => {
41
+ const preference = theme.preference;
42
+ if (hydrated)
43
+ quietly(Preferences.set({ key, value: preference }));
44
+ });
45
+ $effect(() => {
46
+ const preset = theme.preset;
47
+ if (hydrated && hasPresets && preset !== undefined) {
48
+ quietly(Preferences.set({ key: presetKey, value: preset }));
49
+ }
50
+ });
51
+ });
52
+ return {
53
+ ready,
54
+ destroy() {
55
+ stopped = true;
56
+ stop();
57
+ },
58
+ };
59
+ }
@@ -0,0 +1,207 @@
1
+ /** What `addListener` resolves to. */
2
+ export interface PluginListenerHandle {
3
+ remove(): Promise<void>;
4
+ }
5
+ /** `@capacitor/app`, for the Android back button. */
6
+ export interface BackButtonPlugin {
7
+ addListener(eventName: 'backButton', listener: (event: {
8
+ canGoBack: boolean;
9
+ }) => void): Promise<PluginListenerHandle>;
10
+ exitApp(): Promise<void>;
11
+ minimizeApp(): Promise<void>;
12
+ }
13
+ /** `@capacitor/app`, for foreground and background. */
14
+ export interface AppStatePlugin {
15
+ addListener(eventName: 'appStateChange', listener: (state: {
16
+ isActive: boolean;
17
+ }) => void): Promise<PluginListenerHandle>;
18
+ }
19
+ /** `@capacitor/app`, for links that open the app. */
20
+ export interface AppUrlPlugin {
21
+ addListener(eventName: 'appUrlOpen', listener: (event: {
22
+ url: string;
23
+ }) => void): Promise<PluginListenerHandle>;
24
+ }
25
+ /** `@capacitor/app`, for re-reading settings when the app comes back. */
26
+ export interface AppResumePlugin {
27
+ addListener(eventName: 'resume', listener: () => void): Promise<PluginListenerHandle>;
28
+ }
29
+ /** `@capacitor/keyboard`. */
30
+ export interface KeyboardPlugin {
31
+ addListener(eventName: 'keyboardWillShow', listener: (info: {
32
+ keyboardHeight: number;
33
+ }) => void): Promise<PluginListenerHandle>;
34
+ addListener(eventName: 'keyboardDidShow', listener: () => void): Promise<PluginListenerHandle>;
35
+ addListener(eventName: 'keyboardWillHide', listener: () => void): Promise<PluginListenerHandle>;
36
+ getResizeMode(): Promise<{
37
+ mode: string;
38
+ }>;
39
+ }
40
+ /** `@capacitor/keyboard`, for matching the keyboard to the colour scheme (iOS). */
41
+ export interface KeyboardStylePlugin {
42
+ setStyle(options: {
43
+ style: 'DARK' | 'LIGHT' | 'DEFAULT';
44
+ }): Promise<void>;
45
+ }
46
+ /** `@capacitor/haptics`. */
47
+ export interface HapticsPlugin {
48
+ impact(options?: {
49
+ style: 'HEAVY' | 'MEDIUM' | 'LIGHT';
50
+ }): Promise<void>;
51
+ notification(options?: {
52
+ type: 'SUCCESS' | 'WARNING' | 'ERROR';
53
+ }): Promise<void>;
54
+ selectionStart(): Promise<void>;
55
+ selectionChanged(): Promise<void>;
56
+ selectionEnd(): Promise<void>;
57
+ }
58
+ /** `@capacitor/preferences`. */
59
+ export interface PreferencesPlugin {
60
+ get(options: {
61
+ key: string;
62
+ }): Promise<{
63
+ value: string | null;
64
+ }>;
65
+ set(options: {
66
+ key: string;
67
+ value: string;
68
+ }): Promise<void>;
69
+ }
70
+ /** `@capacitor/splash-screen`. */
71
+ export interface SplashScreenPlugin {
72
+ hide(options?: {
73
+ fadeOutDuration?: number;
74
+ }): Promise<void>;
75
+ }
76
+ /** `@capacitor/network`. */
77
+ export interface NetworkPlugin {
78
+ getStatus(): Promise<{
79
+ connected: boolean;
80
+ connectionType: string;
81
+ }>;
82
+ addListener(eventName: 'networkStatusChange', listener: (status: {
83
+ connected: boolean;
84
+ connectionType: string;
85
+ }) => void): Promise<PluginListenerHandle>;
86
+ }
87
+ /** `@capacitor/browser`. */
88
+ export interface BrowserPlugin {
89
+ open(options: {
90
+ url: string;
91
+ toolbarColor?: string;
92
+ presentationStyle?: 'fullscreen' | 'popover';
93
+ }): Promise<void>;
94
+ }
95
+ /** `@capacitor/status-bar` — only for Android without edge-to-edge, where it paints the bar. */
96
+ export interface StatusBarPlugin {
97
+ setStyle(options: {
98
+ style: 'DARK' | 'LIGHT' | 'DEFAULT';
99
+ }): Promise<void>;
100
+ setBackgroundColor(options: {
101
+ color: string;
102
+ }): Promise<void>;
103
+ }
104
+ /** The `SystemBars` plugin bundled with `@capacitor/core` 8. */
105
+ export interface SystemBarsPlugin {
106
+ setStyle(options: {
107
+ style: 'DARK' | 'LIGHT' | 'DEFAULT';
108
+ bar?: 'StatusBar' | 'NavigationBar';
109
+ }): Promise<void>;
110
+ }
111
+ /** `@capacitor/text-zoom`. */
112
+ export interface TextZoomPlugin {
113
+ getPreferred(): Promise<{
114
+ value: number;
115
+ }>;
116
+ set(options: {
117
+ value: number;
118
+ }): Promise<void>;
119
+ }
120
+ /** `@capacitor/clipboard`. */
121
+ export interface ClipboardPlugin {
122
+ write(options: {
123
+ string?: string;
124
+ }): Promise<void>;
125
+ }
126
+ /** `@capacitor/share`. */
127
+ export interface SharePlugin {
128
+ canShare(): Promise<{
129
+ value: boolean;
130
+ }>;
131
+ share(options: {
132
+ title?: string;
133
+ text?: string;
134
+ url?: string;
135
+ files?: string[];
136
+ dialogTitle?: string;
137
+ }): Promise<{
138
+ activityType?: string;
139
+ }>;
140
+ }
141
+ /**
142
+ * `@capacitor/filesystem`, for the files these adapters write. `directory` is one of the
143
+ * plugin's `Directory` values (`'CACHE'`, `'DOCUMENTS'`, …); natively, `data` is base64.
144
+ */
145
+ export interface FilesystemPlugin {
146
+ writeFile(options: {
147
+ path: string;
148
+ data: string | Blob;
149
+ directory?: string;
150
+ recursive?: boolean;
151
+ }): Promise<{
152
+ uri: string;
153
+ }>;
154
+ rmdir(options: {
155
+ path: string;
156
+ directory?: string;
157
+ recursive?: boolean;
158
+ }): Promise<void>;
159
+ deleteFile(options: {
160
+ path: string;
161
+ directory?: string;
162
+ }): Promise<void>;
163
+ getUri(options: {
164
+ path: string;
165
+ directory: string;
166
+ }): Promise<{
167
+ uri: string;
168
+ }>;
169
+ stat(options: {
170
+ path: string;
171
+ directory?: string;
172
+ }): Promise<{
173
+ uri: string;
174
+ }>;
175
+ checkPermissions(): Promise<{
176
+ publicStorage: string;
177
+ }>;
178
+ requestPermissions(): Promise<{
179
+ publicStorage: string;
180
+ }>;
181
+ }
182
+ /** A file `FilePickerPlugin.pickFiles` returns. */
183
+ export interface PickedNativeFile {
184
+ name: string;
185
+ mimeType: string;
186
+ size: number;
187
+ /** Milliseconds since the epoch. */
188
+ modifiedAt?: number;
189
+ /** Android and iOS: the file's path on the device. */
190
+ path?: string;
191
+ /** A URL the web view can load the file from. */
192
+ webPath?: string;
193
+ /** Web only: the file itself. */
194
+ blob?: Blob;
195
+ }
196
+ /** `@capawesome/capacitor-file-picker`. */
197
+ export interface FilePickerPlugin {
198
+ pickFiles(options?: {
199
+ types?: string[];
200
+ limit?: number;
201
+ readData?: boolean;
202
+ }): Promise<{
203
+ files: PickedNativeFile[];
204
+ }>;
205
+ }
206
+ /** Undoes whatever the adapter installed. Safe to call twice. */
207
+ export type Teardown = () => void;
package/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ /*
2
+ * The slices of the Capacitor plugin APIs these adapters call. This package never imports a
3
+ * plugin itself: you pass the one you installed (`import { App } from '@capacitor/app'`), so an
4
+ * app pays only for the plugins it uses and a missing one is a type error rather than a runtime
5
+ * crash. The real plugin objects satisfy these shapes; so does a test double.
6
+ */
7
+ export {};
package/package.json CHANGED
@@ -1,6 +1,135 @@
1
1
  {
2
2
  "name": "@loidolt/theme-capacitor",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.12.0",
4
+ "description": "Capacitor adapters that carry the Loidolt theme into native iOS and Android apps",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "svelte": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "svelte": "./dist/index.js",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "./config": {
16
+ "types": "./dist/config/index.d.ts",
17
+ "default": "./dist/config/index.js"
18
+ },
19
+ "./styles.css": "./dist/styles.css",
20
+ "./package.json": "./package.json"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "sideEffects": [
26
+ "**/*.css"
27
+ ],
28
+ "scripts": {
29
+ "build": "svelte-package -i src/lib -o dist && cp src/styles.css dist/styles.css",
30
+ "prepack": "npm run build",
31
+ "typecheck": "svelte-check --tsconfig ./tsconfig.json",
32
+ "test": "vitest run && vitest run --config vitest.config.ssr.ts",
33
+ "test:coverage": "vitest run --coverage",
34
+ "clean": "rimraf dist"
35
+ },
36
+ "dependencies": {
37
+ "@loidolt/theme-svelte": "^0.12.0",
38
+ "@loidolt/theme-tokens": "^0.12.0"
39
+ },
40
+ "peerDependencies": {
41
+ "@capacitor/app": "^8.0.0",
42
+ "@capacitor/browser": "^8.0.0",
43
+ "@capacitor/clipboard": "^8.0.0",
44
+ "@capacitor/core": "^8.0.0",
45
+ "@capacitor/filesystem": "^8.0.0",
46
+ "@capacitor/haptics": "^8.0.0",
47
+ "@capacitor/keyboard": "^8.0.0",
48
+ "@capacitor/network": "^8.0.0",
49
+ "@capacitor/preferences": "^8.0.0",
50
+ "@capacitor/share": "^8.0.0",
51
+ "@capacitor/splash-screen": "^8.0.0",
52
+ "@capacitor/status-bar": "^8.0.0",
53
+ "@capacitor/text-zoom": "^8.0.0",
54
+ "@capawesome/capacitor-file-picker": "^8.1.0",
55
+ "svelte": "^5.33.0"
56
+ },
57
+ "peerDependenciesMeta": {
58
+ "@capacitor/app": {
59
+ "optional": true
60
+ },
61
+ "@capacitor/browser": {
62
+ "optional": true
63
+ },
64
+ "@capacitor/clipboard": {
65
+ "optional": true
66
+ },
67
+ "@capacitor/filesystem": {
68
+ "optional": true
69
+ },
70
+ "@capacitor/haptics": {
71
+ "optional": true
72
+ },
73
+ "@capacitor/keyboard": {
74
+ "optional": true
75
+ },
76
+ "@capacitor/network": {
77
+ "optional": true
78
+ },
79
+ "@capacitor/preferences": {
80
+ "optional": true
81
+ },
82
+ "@capacitor/share": {
83
+ "optional": true
84
+ },
85
+ "@capacitor/splash-screen": {
86
+ "optional": true
87
+ },
88
+ "@capacitor/status-bar": {
89
+ "optional": true
90
+ },
91
+ "@capacitor/text-zoom": {
92
+ "optional": true
93
+ },
94
+ "@capawesome/capacitor-file-picker": {
95
+ "optional": true
96
+ }
97
+ },
98
+ "devDependencies": {
99
+ "@capacitor/app": "^8.1.2",
100
+ "@capacitor/browser": "^8.0.5",
101
+ "@capacitor/clipboard": "^8.0.1",
102
+ "@capacitor/core": "^8.5.3",
103
+ "@capacitor/filesystem": "^8.1.5",
104
+ "@capacitor/haptics": "^8.0.2",
105
+ "@capacitor/keyboard": "^8.0.6",
106
+ "@capacitor/network": "^8.0.1",
107
+ "@capacitor/preferences": "^8.0.1",
108
+ "@capacitor/share": "^8.0.3",
109
+ "@capacitor/splash-screen": "^8.0.2",
110
+ "@capacitor/status-bar": "^8.0.4",
111
+ "@capacitor/text-zoom": "^8.0.1",
112
+ "@capawesome/capacitor-file-picker": "^8.1.1",
113
+ "@sveltejs/package": "^2.5.8",
114
+ "@sveltejs/vite-plugin-svelte": "^7.0.0",
115
+ "@testing-library/jest-dom": "^6.8.0",
116
+ "@testing-library/svelte": "^5.2.8",
117
+ "@types/node": "^24.3.0",
118
+ "@vitest/coverage-v8": "^4.1.11",
119
+ "jsdom": "^27.0.0",
120
+ "rimraf": "^6.0.1",
121
+ "svelte": "^5.56.10",
122
+ "svelte-check": "^4.3.2",
123
+ "typescript": "^6.0.3",
124
+ "vitest": "^4.0.0"
125
+ },
126
+ "publishConfig": {
127
+ "access": "public",
128
+ "provenance": true
129
+ },
130
+ "repository": {
131
+ "type": "git",
132
+ "url": "git+https://github.com/loidolt/loidolt-theme.git",
133
+ "directory": "packages/capacitor"
134
+ }
135
+ }