@fabrika/ui 0.9.0-dev.42 → 0.9.0-dev.44
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/README.md +3 -0
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/router.tsx +65 -24
- package/types/src/index.d.ts +1 -1
- package/types/src/router.d.ts +10 -2
package/README.md
CHANGED
|
@@ -21,5 +21,8 @@ const { Router, useRoute, href } = createRouter({
|
|
|
21
21
|
- `styles.css` — the theme tokens plus every component rule; the one file to import.
|
|
22
22
|
- `theme.css` — the tokens alone (`--fui-*`), light on `:root`, dark on `:root[data-theme='dark']`.
|
|
23
23
|
- Every plain `<a href>` inside `<Router>` navigates in-app; add `data-router="native"` to opt a link out.
|
|
24
|
+
- A list keeps its filters in the URL: `href`, `navigate` and `<Link>` take `query` (`{ stage: 'won', owner: ['a', 'b'] }`),
|
|
25
|
+
and `useSearchParams()` reads it. A navigation that changes only the query keeps the scroll position and the focus
|
|
26
|
+
(a page link that should start at the top changes the path, or scrolls itself).
|
|
24
27
|
- `@fabrika/ui/challenge`: `<Challenge url={challengeUrl(api.visitors.register)} onToken={…} />`, the ALTCHA widget of
|
|
25
28
|
a public procedure's challenge. It needs the optional peer `altcha`, and fills the form's `challengeToken` field.
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -42,10 +42,12 @@ export { PageHeader, type PageHeaderProps } from './PageHeader'
|
|
|
42
42
|
export {
|
|
43
43
|
type CreatedRouter,
|
|
44
44
|
createRouter,
|
|
45
|
+
type HrefOptions,
|
|
45
46
|
type LinkProps,
|
|
46
47
|
type NavigateOptions,
|
|
47
48
|
type ParamsArgs,
|
|
48
49
|
type ParamsOf,
|
|
50
|
+
type QueryInit,
|
|
49
51
|
type RouteMatch,
|
|
50
52
|
type RouterOptions,
|
|
51
53
|
type RouterProps,
|
package/src/router.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ComponentProps, type ReactNode, useEffect, useSyncExternalStore } from 'react'
|
|
1
|
+
import { type ComponentProps, type ReactNode, useEffect, useMemo, useSyncExternalStore } from 'react'
|
|
2
2
|
|
|
3
3
|
type ParamNames<P extends string> = P extends `${string}:${infer Name}/${infer Rest}` ? Name | ParamNames<`/${Rest}`>
|
|
4
4
|
: P extends `${string}:${infer Name}` ? Name
|
|
@@ -24,13 +24,20 @@ export interface RouterOptions {
|
|
|
24
24
|
readonly base?: string
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
/** A URL query: an `undefined` value leaves its key out, an array repeats the key once per value. */
|
|
28
|
+
export type QueryInit = Readonly<Record<string, string | readonly string[] | undefined>>
|
|
29
|
+
|
|
30
|
+
export interface HrefOptions {
|
|
31
|
+
readonly query?: QueryInit
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface NavigateOptions extends HrefOptions {
|
|
28
35
|
readonly replace?: boolean
|
|
29
36
|
}
|
|
30
37
|
|
|
31
38
|
export type LinkProps<R extends RouteTable, Name extends RouteName<R>> =
|
|
32
39
|
& Readonly<Omit<ComponentProps<'a'>, 'href'>>
|
|
33
|
-
& { readonly to: Name }
|
|
40
|
+
& { readonly to: Name; readonly query?: QueryInit }
|
|
34
41
|
& ([ParamNames<R[Name]>] extends [never] ? { readonly params?: undefined } : { readonly params: ParamsOf<R[Name]> })
|
|
35
42
|
|
|
36
43
|
export interface RouterProps {
|
|
@@ -41,7 +48,9 @@ export interface CreatedRouter<R extends RouteTable> {
|
|
|
41
48
|
readonly Router: (props: RouterProps) => ReactNode
|
|
42
49
|
readonly Link: <Name extends RouteName<R>>(props: LinkProps<R, Name>) => ReactNode
|
|
43
50
|
readonly useRoute: () => RouteMatch<R> | null
|
|
44
|
-
|
|
51
|
+
/** The current URL's query; a new instance when the query changes. Read it, and change it through `navigate`. */
|
|
52
|
+
readonly useSearchParams: () => URLSearchParams
|
|
53
|
+
readonly href: <Name extends RouteName<R>>(name: Name, ...args: [...ParamsArgs<R[Name]>, options?: HrefOptions]) => string
|
|
45
54
|
readonly navigate: <Name extends RouteName<R>>(name: Name, ...args: [...ParamsArgs<R[Name]>, options?: NavigateOptions]) => void
|
|
46
55
|
}
|
|
47
56
|
|
|
@@ -54,7 +63,11 @@ interface CompiledRoute {
|
|
|
54
63
|
|
|
55
64
|
interface LocationSnapshot {
|
|
56
65
|
readonly pathname: string
|
|
57
|
-
|
|
66
|
+
readonly search: string
|
|
67
|
+
/**
|
|
68
|
+
* Set by a navigation this router made to another path, or to the same URL again, so the page scrolls to the top and
|
|
69
|
+
* focus moves to its heading. A navigation that changes only the query (a filter) keeps both.
|
|
70
|
+
*/
|
|
58
71
|
readonly announce: boolean
|
|
59
72
|
}
|
|
60
73
|
|
|
@@ -125,6 +138,16 @@ function isNavigateOptions(value: unknown): value is NavigateOptions {
|
|
|
125
138
|
return typeof value === 'object' && value !== null
|
|
126
139
|
}
|
|
127
140
|
|
|
141
|
+
function searchOf(query: QueryInit | undefined): string {
|
|
142
|
+
const search = new URLSearchParams()
|
|
143
|
+
for (const [name, value] of Object.entries(query ?? {})) {
|
|
144
|
+
if (value === undefined) continue
|
|
145
|
+
for (const item of typeof value === 'string' ? [value] : value) search.append(name, item)
|
|
146
|
+
}
|
|
147
|
+
const text = search.toString()
|
|
148
|
+
return text === '' ? '' : `?${text}`
|
|
149
|
+
}
|
|
150
|
+
|
|
128
151
|
/**
|
|
129
152
|
* A typed History API router over a route table. Every function it answers is typed from `routes`, and `Router` turns
|
|
130
153
|
* every plain same-origin `<a href>` under `base` into an in-app navigation.
|
|
@@ -162,7 +185,7 @@ export function createRouter<const R extends RouteTable>(routes: R, options: Rou
|
|
|
162
185
|
return null
|
|
163
186
|
}
|
|
164
187
|
|
|
165
|
-
function buildHref(name: string, params: unknown): string {
|
|
188
|
+
function buildHref(name: string, params: unknown, query?: QueryInit): string {
|
|
166
189
|
const route = byName.get(name)
|
|
167
190
|
if (route === undefined) throw new Error(`unknown route ${JSON.stringify(name)}`)
|
|
168
191
|
const values = isStringRecord(params) ? params : {}
|
|
@@ -172,7 +195,7 @@ export function createRouter<const R extends RouteTable>(routes: R, options: Rou
|
|
|
172
195
|
if (value === undefined) throw new Error(`route ${name}: missing param ${segment.name}`)
|
|
173
196
|
return encodeURIComponent(value)
|
|
174
197
|
})
|
|
175
|
-
return `${base}/${path.join('/')}`
|
|
198
|
+
return `${base}/${path.join('/')}${searchOf(query)}`
|
|
176
199
|
}
|
|
177
200
|
|
|
178
201
|
function hasParams(name: string): boolean {
|
|
@@ -180,13 +203,13 @@ export function createRouter<const R extends RouteTable>(routes: R, options: Rou
|
|
|
180
203
|
}
|
|
181
204
|
|
|
182
205
|
function getSnapshot(): LocationSnapshot {
|
|
183
|
-
const pathname = window.location
|
|
184
|
-
if (snapshot === undefined || snapshot.pathname !== pathname) snapshot = { pathname, announce: false }
|
|
206
|
+
const { pathname, search } = window.location
|
|
207
|
+
if (snapshot === undefined || snapshot.pathname !== pathname || snapshot.search !== search) snapshot = { pathname, search, announce: false }
|
|
185
208
|
return snapshot
|
|
186
209
|
}
|
|
187
210
|
|
|
188
211
|
function notify(announce: boolean): void {
|
|
189
|
-
snapshot = { pathname: window.location.pathname, announce }
|
|
212
|
+
snapshot = { pathname: window.location.pathname, search: window.location.search, announce }
|
|
190
213
|
for (const listener of listeners) listener()
|
|
191
214
|
}
|
|
192
215
|
|
|
@@ -200,10 +223,16 @@ export function createRouter<const R extends RouteTable>(routes: R, options: Rou
|
|
|
200
223
|
}
|
|
201
224
|
}
|
|
202
225
|
|
|
203
|
-
|
|
226
|
+
/**
|
|
227
|
+
* Moves to `url`. A new path scrolls up and focuses the heading; a new query alone keeps both, so a filter does not
|
|
228
|
+
* jump the page. A click on a link to the page already shown (`reclick`) scrolls up too, as a browser would.
|
|
229
|
+
*/
|
|
230
|
+
function go(url: string, replace: boolean, reclick: boolean): void {
|
|
231
|
+
const { pathname, search } = window.location
|
|
204
232
|
if (replace) window.history.replaceState(null, '', url)
|
|
205
233
|
else window.history.pushState(null, '', url)
|
|
206
|
-
|
|
234
|
+
const samePage = window.location.pathname === pathname && window.location.search === search
|
|
235
|
+
notify(window.location.pathname !== pathname || (reclick && samePage))
|
|
207
236
|
}
|
|
208
237
|
|
|
209
238
|
function useLocation(): LocationSnapshot {
|
|
@@ -234,7 +263,7 @@ export function createRouter<const R extends RouteTable>(routes: R, options: Rou
|
|
|
234
263
|
const url = interceptedUrl(event)
|
|
235
264
|
if (url === undefined) return
|
|
236
265
|
event.preventDefault()
|
|
237
|
-
go(`${url.pathname}${url.search}${url.hash}`, false)
|
|
266
|
+
go(`${url.pathname}${url.search}${url.hash}`, false, true)
|
|
238
267
|
}
|
|
239
268
|
document.addEventListener('click', onClick)
|
|
240
269
|
return () => document.removeEventListener('click', onClick)
|
|
@@ -252,28 +281,40 @@ export function createRouter<const R extends RouteTable>(routes: R, options: Rou
|
|
|
252
281
|
return match(useLocation().pathname)
|
|
253
282
|
}
|
|
254
283
|
|
|
255
|
-
function
|
|
256
|
-
|
|
284
|
+
function useSearchParams(): URLSearchParams {
|
|
285
|
+
const { search } = useLocation()
|
|
286
|
+
// One instance per query, so an effect that depends on it reruns only when the query changes.
|
|
287
|
+
return useMemo(() => new URLSearchParams(search), [search])
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Splits the rest arguments of `href` and `navigate`: the params when the route has any, then the options. */
|
|
291
|
+
function splitArgs(name: string, args: readonly unknown[]): { readonly params: unknown; readonly options: NavigateOptions } {
|
|
292
|
+
const params = hasParams(name) ? args[0] : undefined
|
|
293
|
+
const options = args[hasParams(name) ? 1 : 0]
|
|
294
|
+
return { params, options: isNavigateOptions(options) ? options : {} }
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function href<Name extends RouteName<R>>(name: Name, ...args: [...ParamsArgs<R[Name]>, options?: HrefOptions]): string {
|
|
298
|
+
const { params, options } = splitArgs(name, args)
|
|
299
|
+
return buildHref(name, params, options.query)
|
|
257
300
|
}
|
|
258
301
|
|
|
259
302
|
function navigate<Name extends RouteName<R>>(name: Name, ...args: [...ParamsArgs<R[Name]>, options?: NavigateOptions]): void {
|
|
260
|
-
const
|
|
261
|
-
|
|
262
|
-
const options = list[hasParams(name) ? 1 : 0]
|
|
263
|
-
go(buildHref(name, params), isNavigateOptions(options) && options.replace === true)
|
|
303
|
+
const { params, options } = splitArgs(name, args)
|
|
304
|
+
go(buildHref(name, params, options.query), options.replace === true, false)
|
|
264
305
|
}
|
|
265
306
|
|
|
266
307
|
function Link<Name extends RouteName<R>>(props: LinkProps<R, Name>): ReactNode {
|
|
267
|
-
const { to, params, children, ...rest } = props
|
|
308
|
+
const { to, params, query, children, ...rest } = props
|
|
268
309
|
const location = useLocation()
|
|
269
|
-
|
|
270
|
-
const current = splitPath(location.pathname).join('/') === splitPath(
|
|
310
|
+
// Current by path alone: a navigation item stays current whatever query (filters) the page carries.
|
|
311
|
+
const current = splitPath(location.pathname).join('/') === splitPath(buildHref(to, params)).join('/')
|
|
271
312
|
return (
|
|
272
|
-
<a {...rest} href={
|
|
313
|
+
<a {...rest} href={buildHref(to, params, query)} aria-current={rest['aria-current'] ?? (current ? 'page' : undefined)}>
|
|
273
314
|
{children}
|
|
274
315
|
</a>
|
|
275
316
|
)
|
|
276
317
|
}
|
|
277
318
|
|
|
278
|
-
return { Router, Link, useRoute, href, navigate }
|
|
319
|
+
return { Router, Link, useRoute, useSearchParams, href, navigate }
|
|
279
320
|
}
|
package/types/src/index.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export { FormDialog, type FormDialogProps } from './FormDialog';
|
|
|
12
12
|
export { Checkbox, type CheckboxProps, DateInput, type DateInputProps, DateRange, type DateRangeProps, type DateRangeValue, Input, type InputProps, RadioGroup, type RadioGroupProps, Select, type SelectOption, type SelectProps, Textarea, type TextareaProps, } from './inputs';
|
|
13
13
|
export { type Gap, Inline, type InlineProps, Stack, type StackProps } from './layout';
|
|
14
14
|
export { PageHeader, type PageHeaderProps } from './PageHeader';
|
|
15
|
-
export { type CreatedRouter, createRouter, type LinkProps, type NavigateOptions, type ParamsArgs, type ParamsOf, type RouteMatch, type RouterOptions, type RouterProps, type RouteTable, } from './router';
|
|
15
|
+
export { type CreatedRouter, createRouter, type HrefOptions, type LinkProps, type NavigateOptions, type ParamsArgs, type ParamsOf, type QueryInit, type RouteMatch, type RouterOptions, type RouterProps, type RouteTable, } from './router';
|
|
16
16
|
export { CountBadge, type CountBadgeProps, EmptyState, type EmptyStateProps, ErrorState, type ErrorStateProps, LoadingState, type LoadingStateProps, Spinner, type SpinnerProps, } from './states';
|
|
17
17
|
export { StatusPill, type StatusPillProps } from './StatusPill';
|
|
18
18
|
export { type TabItem, Tabs, type TabsProps } from './Tabs';
|
package/types/src/router.d.ts
CHANGED
|
@@ -19,11 +19,17 @@ export interface RouterOptions {
|
|
|
19
19
|
/** The path the application is mounted under; it is stripped when matching and added by `href`. */
|
|
20
20
|
readonly base?: string;
|
|
21
21
|
}
|
|
22
|
-
|
|
22
|
+
/** A URL query: an `undefined` value leaves its key out, an array repeats the key once per value. */
|
|
23
|
+
export type QueryInit = Readonly<Record<string, string | readonly string[] | undefined>>;
|
|
24
|
+
export interface HrefOptions {
|
|
25
|
+
readonly query?: QueryInit;
|
|
26
|
+
}
|
|
27
|
+
export interface NavigateOptions extends HrefOptions {
|
|
23
28
|
readonly replace?: boolean;
|
|
24
29
|
}
|
|
25
30
|
export type LinkProps<R extends RouteTable, Name extends RouteName<R>> = Readonly<Omit<ComponentProps<'a'>, 'href'>> & {
|
|
26
31
|
readonly to: Name;
|
|
32
|
+
readonly query?: QueryInit;
|
|
27
33
|
} & ([ParamNames<R[Name]>] extends [never] ? {
|
|
28
34
|
readonly params?: undefined;
|
|
29
35
|
} : {
|
|
@@ -36,7 +42,9 @@ export interface CreatedRouter<R extends RouteTable> {
|
|
|
36
42
|
readonly Router: (props: RouterProps) => ReactNode;
|
|
37
43
|
readonly Link: <Name extends RouteName<R>>(props: LinkProps<R, Name>) => ReactNode;
|
|
38
44
|
readonly useRoute: () => RouteMatch<R> | null;
|
|
39
|
-
|
|
45
|
+
/** The current URL's query; a new instance when the query changes. Read it, and change it through `navigate`. */
|
|
46
|
+
readonly useSearchParams: () => URLSearchParams;
|
|
47
|
+
readonly href: <Name extends RouteName<R>>(name: Name, ...args: [...ParamsArgs<R[Name]>, options?: HrefOptions]) => string;
|
|
40
48
|
readonly navigate: <Name extends RouteName<R>>(name: Name, ...args: [...ParamsArgs<R[Name]>, options?: NavigateOptions]) => void;
|
|
41
49
|
}
|
|
42
50
|
/**
|