@bromscandium/router 1.0.0 → 1.0.2

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/src/hooks.ts CHANGED
@@ -1,180 +1,180 @@
1
- /**
2
- * Router hooks for components.
3
- * @module
4
- */
5
-
6
- import { computed, ComputedRef, Ref } from '@bromscandium/core';
7
- import { getRouter, Router, RouteLocation } from './router.js';
8
-
9
- /**
10
- * Returns the router instance.
11
- * Must be called within a component where a router has been created.
12
- *
13
- * @returns The router instance
14
- * @throws Error if called outside of router context
15
- *
16
- * @example
17
- * ```ts
18
- * function MyComponent() {
19
- * const router = useRouter();
20
- *
21
- * function handleClick() {
22
- * router.push('/dashboard');
23
- * }
24
- *
25
- * return <button onClick={handleClick}>Go to Dashboard</button>;
26
- * }
27
- * ```
28
- */
29
- export function useRouter(): Router {
30
- const router = getRouter();
31
- if (!router) {
32
- throw new Error(
33
- 'useRouter() called outside of router context. ' +
34
- 'Make sure you have created a router with createRouter() and mounted it.'
35
- );
36
- }
37
- return router;
38
- }
39
-
40
- /**
41
- * Returns a reactive reference to the current route location.
42
- *
43
- * @returns A ref containing the current RouteLocation
44
- *
45
- * @example
46
- * ```ts
47
- * function Breadcrumb() {
48
- * const route = useRoute();
49
- *
50
- * return <span>Current path: {route.value.path}</span>;
51
- * }
52
- * ```
53
- */
54
- export function useRoute(): Ref<RouteLocation> {
55
- const router = useRouter();
56
- return router.currentRoute;
57
- }
58
-
59
- /**
60
- * Returns a computed ref of the current route parameters.
61
- *
62
- * @returns A computed ref containing route params
63
- *
64
- * @example
65
- * ```ts
66
- * // For route /users/:id
67
- * function UserProfile() {
68
- * const params = useParams();
69
- *
70
- * return <div>User ID: {params.value.id}</div>;
71
- * }
72
- * ```
73
- */
74
- export function useParams(): ComputedRef<Record<string, string>> {
75
- const route = useRoute();
76
- return computed(() => route.value.params);
77
- }
78
-
79
- /**
80
- * Returns a computed ref of the current query string parameters.
81
- *
82
- * @returns A computed ref containing query params
83
- *
84
- * @example
85
- * ```ts
86
- * // For URL /search?q=hello
87
- * function SearchResults() {
88
- * const query = useQuery();
89
- *
90
- * return <div>Searching for: {query.value.q}</div>;
91
- * }
92
- * ```
93
- */
94
- export function useQuery(): ComputedRef<Record<string, string>> {
95
- const route = useRoute();
96
- return computed(() => route.value.query);
97
- }
98
-
99
- /**
100
- * Returns a computed ref of the merged metadata from all matched routes.
101
- *
102
- * @returns A computed ref containing route metadata
103
- *
104
- * @example
105
- * ```ts
106
- * function PageTitle() {
107
- * const meta = useRouteMeta();
108
- *
109
- * return <title>{meta.value.title || 'My App'}</title>;
110
- * }
111
- * ```
112
- */
113
- export function useRouteMeta(): ComputedRef<Record<string, any>> {
114
- const route = useRoute();
115
- return computed(() => route.value.meta);
116
- }
117
-
118
- /**
119
- * Returns navigation helper functions from the router.
120
- *
121
- * @returns An object with push, replace, back, forward, and go methods
122
- *
123
- * @example
124
- * ```ts
125
- * function Navigation() {
126
- * const { push, back } = useNavigate();
127
- *
128
- * return (
129
- * <>
130
- * <button onClick={back}>Back</button>
131
- * <button onClick={() => push('/home')}>Home</button>
132
- * </>
133
- * );
134
- * }
135
- * ```
136
- */
137
- export function useNavigate() {
138
- const router = useRouter();
139
-
140
- return {
141
- push: router.push,
142
- replace: router.replace,
143
- back: router.back,
144
- forward: router.forward,
145
- go: router.go,
146
- };
147
- }
148
-
149
- /**
150
- * Returns a computed boolean indicating if the current route matches a path.
151
- *
152
- * @param path - A string path or RegExp to match against
153
- * @returns A computed ref that is true when the route matches
154
- *
155
- * @example
156
- * ```ts
157
- * function NavLink({ to, children }) {
158
- * const isActive = useRouteMatch(to);
159
- *
160
- * return (
161
- * <a href={to} className={isActive.value ? 'active' : ''}>
162
- * {children}
163
- * </a>
164
- * );
165
- * }
166
- * ```
167
- */
168
- export function useRouteMatch(path: string | RegExp): ComputedRef<boolean> {
169
- const route = useRoute();
170
-
171
- return computed(() => {
172
- const currentPath = route.value.path;
173
-
174
- if (typeof path === 'string') {
175
- return currentPath === path || currentPath.startsWith(path + '/');
176
- }
177
-
178
- return path.test(currentPath);
179
- });
180
- }
1
+ /**
2
+ * Router hooks for components.
3
+ * @module
4
+ */
5
+
6
+ import { computed, ComputedRef, Ref } from '@bromscandium/core';
7
+ import { getRouter, Router, RouteLocation } from './router.js';
8
+
9
+ /**
10
+ * Returns the router instance.
11
+ * Must be called within a component where a router has been created.
12
+ *
13
+ * @returns The router instance
14
+ * @throws Error if called outside of router context
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * function MyComponent() {
19
+ * const router = useRouter();
20
+ *
21
+ * function handleClick() {
22
+ * router.push('/dashboard');
23
+ * }
24
+ *
25
+ * return <button onClick={handleClick}>Go to Dashboard</button>;
26
+ * }
27
+ * ```
28
+ */
29
+ export function useRouter(): Router {
30
+ const router = getRouter();
31
+ if (!router) {
32
+ throw new Error(
33
+ 'useRouter() called outside of router context. ' +
34
+ 'Make sure you have created a router with createRouter() and mounted it.'
35
+ );
36
+ }
37
+ return router;
38
+ }
39
+
40
+ /**
41
+ * Returns a reactive reference to the current route location.
42
+ *
43
+ * @returns A ref containing the current RouteLocation
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * function Breadcrumb() {
48
+ * const route = useRoute();
49
+ *
50
+ * return <span>Current path: {route.value.path}</span>;
51
+ * }
52
+ * ```
53
+ */
54
+ export function useRoute(): Ref<RouteLocation> {
55
+ const router = useRouter();
56
+ return router.currentRoute;
57
+ }
58
+
59
+ /**
60
+ * Returns a computed ref of the current route parameters.
61
+ *
62
+ * @returns A computed ref containing route params
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * // For route /users/:id
67
+ * function UserProfile() {
68
+ * const params = useParams();
69
+ *
70
+ * return <div>User ID: {params.value.id}</div>;
71
+ * }
72
+ * ```
73
+ */
74
+ export function useParams(): ComputedRef<Record<string, string>> {
75
+ const route = useRoute();
76
+ return computed(() => route.value.params);
77
+ }
78
+
79
+ /**
80
+ * Returns a computed ref of the current query string parameters.
81
+ *
82
+ * @returns A computed ref containing query params
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * // For URL /search?q=hello
87
+ * function SearchResults() {
88
+ * const query = useQuery();
89
+ *
90
+ * return <div>Searching for: {query.value.q}</div>;
91
+ * }
92
+ * ```
93
+ */
94
+ export function useQuery(): ComputedRef<Record<string, string>> {
95
+ const route = useRoute();
96
+ return computed(() => route.value.query);
97
+ }
98
+
99
+ /**
100
+ * Returns a computed ref of the merged metadata from all matched routes.
101
+ *
102
+ * @returns A computed ref containing route metadata
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * function PageTitle() {
107
+ * const meta = useRouteMeta();
108
+ *
109
+ * return <title>{meta.value.title || 'My App'}</title>;
110
+ * }
111
+ * ```
112
+ */
113
+ export function useRouteMeta(): ComputedRef<Record<string, any>> {
114
+ const route = useRoute();
115
+ return computed(() => route.value.meta);
116
+ }
117
+
118
+ /**
119
+ * Returns navigation helper functions from the router.
120
+ *
121
+ * @returns An object with push, replace, back, forward, and go methods
122
+ *
123
+ * @example
124
+ * ```ts
125
+ * function Navigation() {
126
+ * const { push, back } = useNavigate();
127
+ *
128
+ * return (
129
+ * <>
130
+ * <button onClick={back}>Back</button>
131
+ * <button onClick={() => push('/home')}>Home</button>
132
+ * </>
133
+ * );
134
+ * }
135
+ * ```
136
+ */
137
+ export function useNavigate() {
138
+ const router = useRouter();
139
+
140
+ return {
141
+ push: router.push,
142
+ replace: router.replace,
143
+ back: router.back,
144
+ forward: router.forward,
145
+ go: router.go,
146
+ };
147
+ }
148
+
149
+ /**
150
+ * Returns a computed boolean indicating if the current route matches a path.
151
+ *
152
+ * @param path - A string path or RegExp to match against
153
+ * @returns A computed ref that is true when the route matches
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * function NavLink({ to, children }) {
158
+ * const isActive = useRouteMatch(to);
159
+ *
160
+ * return (
161
+ * <a href={to} className={isActive.value ? 'active' : ''}>
162
+ * {children}
163
+ * </a>
164
+ * );
165
+ * }
166
+ * ```
167
+ */
168
+ export function useRouteMatch(path: string | RegExp): ComputedRef<boolean> {
169
+ const route = useRoute();
170
+
171
+ return computed(() => {
172
+ const currentPath = route.value.path;
173
+
174
+ if (typeof path === 'string') {
175
+ return currentPath === path || currentPath.startsWith(path + '/');
176
+ }
177
+
178
+ return path.test(currentPath);
179
+ });
180
+ }
package/src/index.ts CHANGED
@@ -1,31 +1,31 @@
1
- // Router package exports
2
-
3
- export {
4
- createRouter,
5
- getRouter,
6
- setRouter,
7
- type Router,
8
- type Route,
9
- type RouteLocation,
10
- type MatchedRoute,
11
- type NavigationTarget,
12
- type NavigationGuard,
13
- type NavigationHook,
14
- } from './router.js';
15
-
16
- export {
17
- useRouter,
18
- useRoute,
19
- useParams,
20
- useQuery,
21
- useRouteMeta,
22
- useNavigate,
23
- useRouteMatch,
24
- } from './hooks.js';
25
-
26
- export {
27
- Link,
28
- RouterView,
29
- Redirect,
30
- Navigate,
31
- } from './components.js';
1
+ // Router package exports
2
+
3
+ export {
4
+ createRouter,
5
+ getRouter,
6
+ setRouter,
7
+ type Router,
8
+ type Route,
9
+ type RouteLocation,
10
+ type MatchedRoute,
11
+ type NavigationTarget,
12
+ type NavigationGuard,
13
+ type NavigationHook,
14
+ } from './router.js';
15
+
16
+ export {
17
+ useRouter,
18
+ useRoute,
19
+ useParams,
20
+ useQuery,
21
+ useRouteMeta,
22
+ useNavigate,
23
+ useRouteMatch,
24
+ } from './hooks.js';
25
+
26
+ export {
27
+ Link,
28
+ RouterView,
29
+ Redirect,
30
+ Navigate,
31
+ } from './components.js';