@sigmela/router 0.3.6 → 0.4.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/CHANGELOG.md +23 -0
- package/LICENSE +2 -1
- package/README.md +108 -479
- package/dist/Router.d.ts +190 -0
- package/dist/Router.d.ts.map +1 -0
- package/dist/components/Modal.d.ts +56 -0
- package/dist/components/Modal.d.ts.map +1 -0
- package/dist/components/OverlayContentScope.d.ts +8 -0
- package/dist/components/OverlayContentScope.d.ts.map +1 -0
- package/dist/components/RouterCloseButton.d.ts +9 -0
- package/dist/components/RouterCloseButton.d.ts.map +1 -0
- package/dist/components/Sheet.d.ts +53 -0
- package/dist/components/Sheet.d.ts.map +1 -0
- package/dist/components/SheetSurface.d.ts +27 -0
- package/dist/components/SheetSurface.d.ts.map +1 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/pageScrollLock.d.ts +3 -0
- package/dist/components/pageScrollLock.d.ts.map +1 -0
- package/dist/components/sheetDragEngine.d.ts +27 -0
- package/dist/components/sheetDragEngine.d.ts.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3649 -0
- package/dist/index.js.map +1 -0
- package/dist/navigationHistory.d.ts +17 -0
- package/dist/navigationHistory.d.ts.map +1 -0
- package/dist/router.css +971 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/id.d.ts +2 -0
- package/dist/utils/id.d.ts.map +1 -0
- package/package.json +69 -174
- package/lib/module/Drawer/Drawer.js +0 -250
- package/lib/module/Drawer/DrawerContext.js +0 -4
- package/lib/module/Drawer/DrawerIcon.web.js +0 -47
- package/lib/module/Drawer/RenderDrawer.native.js +0 -248
- package/lib/module/Drawer/RenderDrawer.web.js +0 -197
- package/lib/module/Drawer/useDrawer.js +0 -11
- package/lib/module/Navigation.js +0 -79
- package/lib/module/NavigationStack.js +0 -254
- package/lib/module/Router.js +0 -2171
- package/lib/module/RouterContext.js +0 -33
- package/lib/module/ScreenStack/ScreenStack.native.js +0 -3
- package/lib/module/ScreenStack/ScreenStack.web.js +0 -561
- package/lib/module/ScreenStack/ScreenStackContext.js +0 -21
- package/lib/module/ScreenStack/animationHelpers.js +0 -87
- package/lib/module/ScreenStack/index.js +0 -3
- package/lib/module/ScreenStackItem/ScreenStackItem.js +0 -70
- package/lib/module/ScreenStackItem/ScreenStackItem.types.js +0 -3
- package/lib/module/ScreenStackItem/ScreenStackItem.web.js +0 -171
- package/lib/module/ScreenStackItem/index.js +0 -3
- package/lib/module/ScreenStackSheetItem/ScreenStackSheetItem.native.js +0 -70
- package/lib/module/ScreenStackSheetItem/ScreenStackSheetItem.web.js +0 -5
- package/lib/module/ScreenStackSheetItem/index.js +0 -3
- package/lib/module/SplitView/RenderSplitView.native.js +0 -101
- package/lib/module/SplitView/RenderSplitView.web.js +0 -107
- package/lib/module/SplitView/SplitView.js +0 -97
- package/lib/module/SplitView/SplitViewContext.js +0 -4
- package/lib/module/SplitView/index.js +0 -5
- package/lib/module/SplitView/useSplitView.js +0 -11
- package/lib/module/StackRenderer.js +0 -42
- package/lib/module/TabBar/RenderTabBar.native.js +0 -418
- package/lib/module/TabBar/RenderTabBar.web.js +0 -218
- package/lib/module/TabBar/TabBar.js +0 -208
- package/lib/module/TabBar/TabBarContext.js +0 -4
- package/lib/module/TabBar/TabIcon.web.js +0 -47
- package/lib/module/TabBar/index.js +0 -6
- package/lib/module/TabBar/useTabBar.js +0 -11
- package/lib/module/TabBar/useTabBarHeight.js +0 -22
- package/lib/module/createController.js +0 -5
- package/lib/module/index.js +0 -15
- package/lib/module/navigationNode.js +0 -3
- package/lib/module/package.json +0 -1
- package/lib/module/styles.css +0 -1111
- package/lib/module/types.js +0 -13
- package/lib/typescript/package.json +0 -1
- package/lib/typescript/src/Drawer/Drawer.d.ts +0 -100
- package/lib/typescript/src/Drawer/DrawerContext.d.ts +0 -3
- package/lib/typescript/src/Drawer/DrawerIcon.web.d.ts +0 -7
- package/lib/typescript/src/Drawer/RenderDrawer.native.d.ts +0 -8
- package/lib/typescript/src/Drawer/RenderDrawer.web.d.ts +0 -8
- package/lib/typescript/src/Drawer/useDrawer.d.ts +0 -2
- package/lib/typescript/src/Navigation.d.ts +0 -17
- package/lib/typescript/src/NavigationStack.d.ts +0 -46
- package/lib/typescript/src/Router.d.ts +0 -232
- package/lib/typescript/src/RouterContext.d.ts +0 -18
- package/lib/typescript/src/ScreenStack/ScreenStack.native.d.ts +0 -2
- package/lib/typescript/src/ScreenStack/ScreenStack.web.d.ts +0 -9
- package/lib/typescript/src/ScreenStack/ScreenStackContext.d.ts +0 -31
- package/lib/typescript/src/ScreenStack/animationHelpers.d.ts +0 -6
- package/lib/typescript/src/ScreenStack/index.d.ts +0 -2
- package/lib/typescript/src/ScreenStackItem/ScreenStackItem.d.ts +0 -3
- package/lib/typescript/src/ScreenStackItem/ScreenStackItem.types.d.ts +0 -14
- package/lib/typescript/src/ScreenStackItem/ScreenStackItem.web.d.ts +0 -3
- package/lib/typescript/src/ScreenStackItem/index.d.ts +0 -3
- package/lib/typescript/src/ScreenStackSheetItem/ScreenStackSheetItem.native.d.ts +0 -11
- package/lib/typescript/src/ScreenStackSheetItem/ScreenStackSheetItem.web.d.ts +0 -2
- package/lib/typescript/src/ScreenStackSheetItem/index.d.ts +0 -2
- package/lib/typescript/src/SplitView/RenderSplitView.native.d.ts +0 -19
- package/lib/typescript/src/SplitView/RenderSplitView.web.d.ts +0 -8
- package/lib/typescript/src/SplitView/SplitView.d.ts +0 -33
- package/lib/typescript/src/SplitView/SplitViewContext.d.ts +0 -3
- package/lib/typescript/src/SplitView/index.d.ts +0 -5
- package/lib/typescript/src/SplitView/useSplitView.d.ts +0 -2
- package/lib/typescript/src/StackRenderer.d.ts +0 -8
- package/lib/typescript/src/TabBar/RenderTabBar.native.d.ts +0 -8
- package/lib/typescript/src/TabBar/RenderTabBar.web.d.ts +0 -8
- package/lib/typescript/src/TabBar/TabBar.d.ts +0 -112
- package/lib/typescript/src/TabBar/TabBarContext.d.ts +0 -3
- package/lib/typescript/src/TabBar/TabIcon.web.d.ts +0 -7
- package/lib/typescript/src/TabBar/index.d.ts +0 -5
- package/lib/typescript/src/TabBar/useTabBar.d.ts +0 -2
- package/lib/typescript/src/TabBar/useTabBarHeight.d.ts +0 -18
- package/lib/typescript/src/createController.d.ts +0 -15
- package/lib/typescript/src/index.d.ts +0 -21
- package/lib/typescript/src/navigationNode.d.ts +0 -41
- package/lib/typescript/src/types.d.ts +0 -140
|
@@ -1,254 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
import { nanoid } from 'nanoid/non-secure';
|
|
4
|
-
import { match as pathMatchFactory } from 'path-to-regexp';
|
|
5
|
-
import qs from 'query-string';
|
|
6
|
-
import React from 'react';
|
|
7
|
-
import { StackRenderer } from "./StackRenderer.js";
|
|
8
|
-
export class NavigationStack {
|
|
9
|
-
routes = [];
|
|
10
|
-
children = [];
|
|
11
|
-
debugEnabled = false;
|
|
12
|
-
_cachedRenderer = null;
|
|
13
|
-
constructor(idOrOptions, maybeOptions, maybeDebug) {
|
|
14
|
-
if (typeof idOrOptions === 'string') {
|
|
15
|
-
this.stackId = idOrOptions;
|
|
16
|
-
this.defaultOptions = maybeOptions;
|
|
17
|
-
this.debugEnabled = maybeDebug ?? false;
|
|
18
|
-
this.provider = maybeOptions?.provider;
|
|
19
|
-
} else if (typeof idOrOptions === 'object') {
|
|
20
|
-
this.stackId = `stack-${nanoid()}`;
|
|
21
|
-
this.defaultOptions = idOrOptions;
|
|
22
|
-
this.debugEnabled = idOrOptions.debug ?? false;
|
|
23
|
-
this.provider = idOrOptions?.provider;
|
|
24
|
-
} else {
|
|
25
|
-
this.stackId = `stack-${nanoid()}`;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
log(message, data) {
|
|
29
|
-
if (this.debugEnabled) {
|
|
30
|
-
if (data !== undefined) {
|
|
31
|
-
console.log(`[NavigationStack] ${message}`, data);
|
|
32
|
-
} else {
|
|
33
|
-
console.log(`[NavigationStack] ${message}`);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
getId() {
|
|
38
|
-
return this.stackId;
|
|
39
|
-
}
|
|
40
|
-
addScreen(pathPattern, mixedComponent, options) {
|
|
41
|
-
const {
|
|
42
|
-
component,
|
|
43
|
-
controller,
|
|
44
|
-
childNode
|
|
45
|
-
} = this.extractComponent(mixedComponent);
|
|
46
|
-
const parsed = qs.parseUrl(pathPattern);
|
|
47
|
-
const pathnamePattern = parsed.url || '/';
|
|
48
|
-
const rawQuery = parsed.query || {};
|
|
49
|
-
const isWildcardPath = pathnamePattern === '*';
|
|
50
|
-
const queryPattern = this.buildQueryPattern(rawQuery);
|
|
51
|
-
const baseSpecificity = this.computeBaseSpecificity(pathnamePattern, isWildcardPath, queryPattern);
|
|
52
|
-
const routeId = `${this.stackId}-route-${this.routes.length}`;
|
|
53
|
-
const pathMatcher = !isWildcardPath ? pathMatchFactory(pathnamePattern) : null;
|
|
54
|
-
const matchPath = p => {
|
|
55
|
-
if (isWildcardPath) {
|
|
56
|
-
return {
|
|
57
|
-
params: {}
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
const result = pathMatcher ? pathMatcher(p) : null;
|
|
61
|
-
return result ? {
|
|
62
|
-
params: result.params ?? {}
|
|
63
|
-
} : false;
|
|
64
|
-
};
|
|
65
|
-
const builtRoute = {
|
|
66
|
-
routeId,
|
|
67
|
-
path: pathPattern,
|
|
68
|
-
pathnamePattern,
|
|
69
|
-
isWildcardPath,
|
|
70
|
-
queryPattern,
|
|
71
|
-
baseSpecificity,
|
|
72
|
-
matchPath,
|
|
73
|
-
component,
|
|
74
|
-
controller,
|
|
75
|
-
options,
|
|
76
|
-
childNode
|
|
77
|
-
};
|
|
78
|
-
this.routes.push(builtRoute);
|
|
79
|
-
this.log('addRoute', {
|
|
80
|
-
stackId: this.stackId,
|
|
81
|
-
routeId,
|
|
82
|
-
path: pathPattern,
|
|
83
|
-
pathnamePattern,
|
|
84
|
-
isWildcardPath,
|
|
85
|
-
queryPattern,
|
|
86
|
-
baseSpecificity
|
|
87
|
-
});
|
|
88
|
-
return this;
|
|
89
|
-
}
|
|
90
|
-
addModal(path, mixedComponent, options) {
|
|
91
|
-
return this.addScreen(path, mixedComponent, {
|
|
92
|
-
syncWithUrl: false,
|
|
93
|
-
...options,
|
|
94
|
-
stackPresentation: 'modal'
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
addSheet(path, mixedComponent, options) {
|
|
98
|
-
return this.addScreen(path, mixedComponent, {
|
|
99
|
-
...options,
|
|
100
|
-
stackPresentation: 'sheet'
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
addStack(prefixOrStack, maybeStack) {
|
|
104
|
-
const hasExplicitPrefix = typeof prefixOrStack === 'string';
|
|
105
|
-
const prefixRaw = hasExplicitPrefix ? prefixOrStack : '';
|
|
106
|
-
const stack = hasExplicitPrefix ? maybeStack : prefixOrStack;
|
|
107
|
-
if (!stack) {
|
|
108
|
-
throw new Error('NavigationStack.addStack: child stack is required');
|
|
109
|
-
}
|
|
110
|
-
const prefix = this.normalizePrefix(prefixRaw ?? '');
|
|
111
|
-
this.children.push({
|
|
112
|
-
prefix,
|
|
113
|
-
node: stack
|
|
114
|
-
});
|
|
115
|
-
this.log('addStack', {
|
|
116
|
-
stackId: this.stackId,
|
|
117
|
-
childId: stack.getId(),
|
|
118
|
-
prefix
|
|
119
|
-
});
|
|
120
|
-
return this;
|
|
121
|
-
}
|
|
122
|
-
getChildren() {
|
|
123
|
-
return this.children.slice();
|
|
124
|
-
}
|
|
125
|
-
getRoutes() {
|
|
126
|
-
return this.routes.slice();
|
|
127
|
-
}
|
|
128
|
-
getFirstRoute() {
|
|
129
|
-
return this.routes[0];
|
|
130
|
-
}
|
|
131
|
-
getDefaultOptions() {
|
|
132
|
-
return this.defaultOptions;
|
|
133
|
-
}
|
|
134
|
-
getNodeRoutes() {
|
|
135
|
-
return this.getRoutes();
|
|
136
|
-
}
|
|
137
|
-
getNodeChildren() {
|
|
138
|
-
return this.children.slice();
|
|
139
|
-
}
|
|
140
|
-
getRenderer() {
|
|
141
|
-
if (this._cachedRenderer) {
|
|
142
|
-
return this._cachedRenderer;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// eslint-disable-next-line consistent-this
|
|
146
|
-
const stackInstance = this;
|
|
147
|
-
const stackId = stackInstance.getId();
|
|
148
|
-
const provider = this.provider;
|
|
149
|
-
const renderer = function NavigationStackRenderer(props) {
|
|
150
|
-
const stackElement = /*#__PURE__*/React.createElement(StackRenderer, {
|
|
151
|
-
stackId: stackId,
|
|
152
|
-
appearance: props.appearance
|
|
153
|
-
});
|
|
154
|
-
if (provider) {
|
|
155
|
-
return /*#__PURE__*/React.createElement(provider, null, stackElement);
|
|
156
|
-
}
|
|
157
|
-
return stackElement;
|
|
158
|
-
};
|
|
159
|
-
this._cachedRenderer = renderer;
|
|
160
|
-
return renderer;
|
|
161
|
-
}
|
|
162
|
-
seed() {
|
|
163
|
-
const first = this.getFirstRoute();
|
|
164
|
-
if (!first) return null;
|
|
165
|
-
return {
|
|
166
|
-
routeId: first.routeId,
|
|
167
|
-
params: {},
|
|
168
|
-
path: first.path,
|
|
169
|
-
stackId: this.stackId
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
extractComponent(component) {
|
|
173
|
-
if (this.isNavigationNode(component)) {
|
|
174
|
-
return {
|
|
175
|
-
controller: undefined,
|
|
176
|
-
component: component.getRenderer(),
|
|
177
|
-
childNode: component
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
const componentWithController = component;
|
|
181
|
-
if (componentWithController?.component) {
|
|
182
|
-
return {
|
|
183
|
-
controller: componentWithController.controller,
|
|
184
|
-
component: componentWithController.component,
|
|
185
|
-
childNode: componentWithController.childNode
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
return {
|
|
189
|
-
component: component,
|
|
190
|
-
controller: undefined,
|
|
191
|
-
childNode: undefined
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
isNavigationNode(obj) {
|
|
195
|
-
return obj && typeof obj === 'object' && typeof obj.getNodeRoutes === 'function' && typeof obj.getNodeChildren === 'function' && typeof obj.getRenderer === 'function';
|
|
196
|
-
}
|
|
197
|
-
buildQueryPattern(rawQuery) {
|
|
198
|
-
const patternEntries = [];
|
|
199
|
-
for (const [key, value] of Object.entries(rawQuery)) {
|
|
200
|
-
if (typeof value !== 'string') {
|
|
201
|
-
continue;
|
|
202
|
-
}
|
|
203
|
-
if (value.startsWith(':') && value.length > 1) {
|
|
204
|
-
patternEntries.push([key, {
|
|
205
|
-
type: 'param',
|
|
206
|
-
name: value.slice(1)
|
|
207
|
-
}]);
|
|
208
|
-
} else {
|
|
209
|
-
patternEntries.push([key, {
|
|
210
|
-
type: 'const',
|
|
211
|
-
value
|
|
212
|
-
}]);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
if (!patternEntries.length) return null;
|
|
216
|
-
return Object.fromEntries(patternEntries);
|
|
217
|
-
}
|
|
218
|
-
computeBaseSpecificity(pathnamePattern, isWildcardPath, queryPattern) {
|
|
219
|
-
let pathScore = 0;
|
|
220
|
-
if (!isWildcardPath) {
|
|
221
|
-
const segments = pathnamePattern.split('/').filter(Boolean);
|
|
222
|
-
for (const seg of segments) {
|
|
223
|
-
if (seg.startsWith(':')) {
|
|
224
|
-
pathScore += 1;
|
|
225
|
-
} else {
|
|
226
|
-
pathScore += 2;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
} else {
|
|
230
|
-
pathScore = 0;
|
|
231
|
-
}
|
|
232
|
-
let queryScore = 0;
|
|
233
|
-
if (queryPattern) {
|
|
234
|
-
for (const token of Object.values(queryPattern)) {
|
|
235
|
-
if (token.type === 'const') {
|
|
236
|
-
queryScore += 2;
|
|
237
|
-
} else {
|
|
238
|
-
queryScore += 1;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
return pathScore + queryScore;
|
|
243
|
-
}
|
|
244
|
-
normalizePrefix(input) {
|
|
245
|
-
if (!input || input === '/') {
|
|
246
|
-
return '';
|
|
247
|
-
}
|
|
248
|
-
let normalized = input.startsWith('/') ? input : `/${input}`;
|
|
249
|
-
if (normalized.length > 1 && normalized.endsWith('/')) {
|
|
250
|
-
normalized = normalized.slice(0, -1);
|
|
251
|
-
}
|
|
252
|
-
return normalized;
|
|
253
|
-
}
|
|
254
|
-
}
|