ink-framework 0.0.0-stage → 0.1.0-alpha.10

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,74 @@
1
+ import { Activity, createElement, useMemo, type ComponentType, type ReactNode } from "react";
2
+ import { back, LayoutContext, NavigationStack, RouteContext, type Destination, type Entry } from "./navigation";
3
+
4
+ export type FileRoute = { name: string; path: string; component: ComponentType };
5
+ export type FileLayout = { name: string; layout?: ComponentType; pages: FileRoute[]; children: FileLayout[] };
6
+
7
+ function paths(group: FileLayout): string[] {
8
+ return [...group.pages.map(page => page.path), ...group.children.flatMap(paths)];
9
+ }
10
+
11
+ function LayoutBranch({ group, entries, active, selectTab, root = false }: {
12
+ group: FileLayout;
13
+ entries: Entry[];
14
+ active: Entry;
15
+ selectTab: (path: string) => void;
16
+ root?: boolean;
17
+ }) {
18
+ const groupPaths = useMemo(() => new Set(paths(group)), [group]);
19
+ const local = entries.filter(entry => groupPaths.has(entry.path) && !entry.unavailable && !entry.page);
20
+ const last = local.at(-1);
21
+ if (!root && !last) return null;
22
+ const renderPage = (route: FileRoute, entry: Entry) => (
23
+ <Activity key={entry.key} mode={entry.key === active.key ? "visible" : "hidden"}>
24
+ <RouteContext.Provider value={entry.params}><route.component /></RouteContext.Provider>
25
+ </Activity>
26
+ );
27
+ const pages = new Map(group.pages.map(route => {
28
+ const entry = local.filter(entry => entry.path === route.path).at(-1);
29
+ return [route.name, {
30
+ path: route.path,
31
+ page: <RouteContext.Provider value={entry?.params ?? {}}><route.component /></RouteContext.Provider>,
32
+ }];
33
+ }));
34
+ const fallback = root && entries.filter(entry => entry.page || entry.unavailable).map(entry => (
35
+ <Activity key={entry.key} mode={entry.key === active.key ? "visible" : "hidden"}>
36
+ <RouteContext.Provider value={entry.params}>
37
+ {entry.page ?? createElement("Screen", { title: "Page unavailable" },
38
+ createElement("Text", null, "This notification links to a page that is no longer available."),
39
+ createElement("Button", { onPress: back }, "Go back"))}
40
+ </RouteContext.Provider>
41
+ </Activity>
42
+ ));
43
+ const content = <>
44
+ {group.pages.flatMap(route => local.filter(entry => entry.path === route.path).map(entry => renderPage(route, entry)))}
45
+ {group.children.map(child => <LayoutBranch key={child.name} group={child} entries={entries} active={active} selectTab={selectTab} />)}
46
+ {fallback}
47
+ </>;
48
+ const ordinaryPages = (tabPaths: readonly string[]) => <>
49
+ {group.pages.flatMap(route => local.filter(entry =>
50
+ entry.path === route.path && entry.key !== 0 && entry.tabGroup !== group && !tabPaths.includes(entry.path)).map(entry => renderPage(route, entry)))}
51
+ {fallback}
52
+ </>;
53
+ const value = {
54
+ content, pages, group, ordinaryPages, selectTab,
55
+ // The implicit startup entry belongs to its tabs before registration commits.
56
+ tabPath: local.find(entry => entry.tabGroup === group || entry.key === 0)?.path,
57
+ directories: group.children.map(child => child.name),
58
+ activePath: root && (active.page || active.unavailable) ? active.path : last?.path ?? active.path,
59
+ };
60
+ return <Activity mode={root || groupPaths.has(active.path) && !active.page && !active.unavailable ? "visible" : "hidden"}>
61
+ <LayoutContext.Provider value={value}>
62
+ <RouteContext.Provider value={last?.params ?? active.params}>
63
+ {group.layout ? createElement(group.layout) : content}
64
+ </RouteContext.Provider>
65
+ </LayoutContext.Provider>
66
+ </Activity>;
67
+ }
68
+
69
+ export function FileNavigator({ tree, initialDestination }: { tree: FileLayout; initialDestination?: Destination }) {
70
+ const routes = useMemo(() => new Set(paths(tree)), [tree]);
71
+ return <NavigationStack routes={routes} initialDestination={initialDestination} renderEntries={(entries, selectTab) => (
72
+ <LayoutBranch group={tree} entries={entries} active={entries[entries.length - 1]} selectTab={selectTab} root />
73
+ )} />;
74
+ }
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Hugeicons
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.
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none"><path d="M2.5 13.8333L6 17.5L7.02402 16.4272M16.5 6.5L10.437 12.8517" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"/><path d="M7.5 13.8333L11 17.5L21.5 6.5" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"/></svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none"><path d="M5 14L8.5 17.5L19 6.5" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"/></svg>
@@ -0,0 +1,4 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
2
+ <path d="M21.544 11.045C21.848 11.4713 22 11.6845 22 12C22 12.3155 21.848 12.5287 21.544 12.955C20.1779 14.8706 16.6892 19 12 19C7.31078 19 3.8221 14.8706 2.45604 12.955C2.15201 12.5287 2 12.3155 2 12C2 11.6845 2.15201 11.4713 2.45604 11.045C3.8221 9.12944 7.31078 5 12 5C16.6892 5 20.1779 9.12944 21.544 11.045Z"/>
3
+ <path d="M15 12C15 10.3431 13.6569 9 12 9C10.3431 9 9 10.3431 9 12C9 13.6569 10.3431 15 12 15C13.6569 15 15 13.6569 15 12Z"/>
4
+ </svg>