ink-framework-overlays 0.0.0-stage → 0.1.0-alpha.13
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 +21 -0
- package/ink-native.json +4 -0
- package/package.json +22 -4
- package/src/index.ts +70 -0
- package/src/internal.ts +21 -0
- package/src/runtime.tsx +15 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vandam Dinh
|
|
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/ink-native.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ink-framework-overlays",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-alpha.13",
|
|
4
|
+
"exports": {
|
|
5
|
+
".": "./src/index.ts",
|
|
6
|
+
"./runtime": "./src/runtime.tsx"
|
|
7
|
+
},
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"ink": "npm:ink-framework@0.1.0-alpha.13",
|
|
10
|
+
"react": "19.2.8"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/No-Scrolling/ink.git",
|
|
16
|
+
"directory": "packages/overlays"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"*.json",
|
|
21
|
+
"*.ts",
|
|
22
|
+
"*.md"
|
|
23
|
+
]
|
|
24
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { createElement, type ComponentType } from "ink/internal/react";
|
|
2
|
+
import type { Snapshot, SnapshotSource } from "ink";
|
|
3
|
+
import { callNative, onNativeMessage } from "ink/native";
|
|
4
|
+
import { overlayComponents } from "./internal";
|
|
5
|
+
|
|
6
|
+
export type OverlayPosition = "top" | "center" | "bottom";
|
|
7
|
+
export type OverlayOptions = { width?: number; height: number; position?: OverlayPosition; offset?: number };
|
|
8
|
+
export type OverlayState = Readonly<{ visible: boolean; id: string | null }>;
|
|
9
|
+
|
|
10
|
+
let snapshot: Snapshot<OverlayState> = { status: "loading" };
|
|
11
|
+
const listeners = new Set<() => void>();
|
|
12
|
+
function publish(data: OverlayState) {
|
|
13
|
+
snapshot = { status: "ready", data: Object.freeze(data) };
|
|
14
|
+
for (const listener of listeners) listener();
|
|
15
|
+
}
|
|
16
|
+
function decode(value: unknown): OverlayState {
|
|
17
|
+
if (!value || typeof value !== "object" || !("visible" in value) || typeof value.visible !== "boolean"
|
|
18
|
+
|| !("id" in value) || (value.id !== null && typeof value.id !== "string")) throw new Error("Invalid overlay state");
|
|
19
|
+
return { visible: value.visible, id: value.id };
|
|
20
|
+
}
|
|
21
|
+
onNativeMessage("overlays-changed", message => { publish(decode(message.data)); });
|
|
22
|
+
async function read(operation: string): Promise<OverlayState> {
|
|
23
|
+
try {
|
|
24
|
+
const data = decode(JSON.parse(await callNative("overlays", operation, {})));
|
|
25
|
+
publish(data);
|
|
26
|
+
return data;
|
|
27
|
+
} catch (error) {
|
|
28
|
+
snapshot = { status: "error", error: error instanceof Error ? error : new Error(String(error)) };
|
|
29
|
+
for (const listener of listeners) listener();
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function defineOverlay<Props extends object>(definition: { id: string; component: ComponentType<Props> }) {
|
|
35
|
+
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/.test(definition.id)) throw new TypeError("Invalid overlay ID");
|
|
36
|
+
if (overlayComponents.has(definition.id)) throw new Error(`Duplicate overlay: ${definition.id}`);
|
|
37
|
+
overlayComponents.set(definition.id, props => createElement(definition.component, props as Props));
|
|
38
|
+
return {
|
|
39
|
+
async show(props: Props, options: OverlayOptions): Promise<void> {
|
|
40
|
+
await callNative("overlays", "show", { id: definition.id, props, options });
|
|
41
|
+
},
|
|
42
|
+
async update(props: Props, options?: OverlayOptions): Promise<void> {
|
|
43
|
+
await callNative("overlays", "update", { id: definition.id, props, options });
|
|
44
|
+
},
|
|
45
|
+
async hide(): Promise<void> {
|
|
46
|
+
await callNative("overlays", "hide", { id: definition.id });
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const overlays: SnapshotSource<OverlayState> & {
|
|
52
|
+
isAllowed(): Promise<boolean>;
|
|
53
|
+
requestPermission(): Promise<void>;
|
|
54
|
+
getState(): Promise<OverlayState>;
|
|
55
|
+
} = {
|
|
56
|
+
isAllowed: async () => (await callNative("overlays", "permission", {})) === "true",
|
|
57
|
+
async requestPermission(): Promise<void> {
|
|
58
|
+
await callNative("overlays", "request-permission", {}, { timeoutMs: 900_000 });
|
|
59
|
+
},
|
|
60
|
+
getState: () => read("state"),
|
|
61
|
+
getSnapshot: () => snapshot,
|
|
62
|
+
subscribe(listener) {
|
|
63
|
+
listeners.add(listener);
|
|
64
|
+
if (listeners.size === 1) void read("watch").catch(() => {});
|
|
65
|
+
return () => {
|
|
66
|
+
listeners.delete(listener);
|
|
67
|
+
if (listeners.size === 0) void callNative("overlays", "unwatch", {}).catch(() => {});
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
};
|
package/src/internal.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ReactNode } from "ink/internal/react";
|
|
2
|
+
import { onNativeMessage } from "ink/native";
|
|
3
|
+
|
|
4
|
+
export const overlayComponents = new Map<string, (props: object) => ReactNode>();
|
|
5
|
+
let content: { id: string; props: object } | null = null;
|
|
6
|
+
const listeners = new Set<() => void>();
|
|
7
|
+
onNativeMessage("overlay-content", message => {
|
|
8
|
+
if (typeof message.id !== "string" || typeof message.props !== "object" || message.props === null || Array.isArray(message.props)) {
|
|
9
|
+
throw new TypeError("Invalid overlay content");
|
|
10
|
+
}
|
|
11
|
+
content = { id: message.id, props: message.props };
|
|
12
|
+
for (const listener of listeners) listener();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const overlayContent = {
|
|
16
|
+
getSnapshot: () => content,
|
|
17
|
+
subscribe(listener: () => void) {
|
|
18
|
+
listeners.add(listener);
|
|
19
|
+
return () => { listeners.delete(listener); };
|
|
20
|
+
},
|
|
21
|
+
};
|
package/src/runtime.tsx
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createElement, useSyncExternalStore } from "ink/internal/react";
|
|
2
|
+
import { render } from "ink/renderer";
|
|
3
|
+
import { overlayComponents, overlayContent } from "./internal";
|
|
4
|
+
|
|
5
|
+
function OverlayRoot() {
|
|
6
|
+
const content = useSyncExternalStore(overlayContent.subscribe, overlayContent.getSnapshot);
|
|
7
|
+
if (!content) return null;
|
|
8
|
+
const component = overlayComponents.get(content.id);
|
|
9
|
+
if (!component) throw new Error(`Overlay is not registered in the overlay entry point: ${content.id}`);
|
|
10
|
+
return createElement(component, { ...content.props, key: content.id });
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function startOverlays() {
|
|
14
|
+
render(<OverlayRoot />);
|
|
15
|
+
}
|
package/README.md
DELETED