@uniweb/kit 0.1.4 → 0.1.5
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/package.json +2 -2
- package/src/hooks/index.js +1 -0
- package/src/hooks/useRouting.js +119 -0
- package/src/index.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniweb/kit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Standard component library for Uniweb foundations",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"tailwind-merge": "^2.6.0",
|
|
39
|
-
"@uniweb/core": "0.1.
|
|
39
|
+
"@uniweb/core": "0.1.8"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"react": "^18.0.0 || ^19.0.0",
|
package/src/hooks/index.js
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useRouting Hook
|
|
3
|
+
*
|
|
4
|
+
* Provides SSG-safe access to routing functionality.
|
|
5
|
+
*
|
|
6
|
+
* The runtime registers routing components (Link, useNavigate, useLocation, useParams)
|
|
7
|
+
* via the bridge pattern. This hook provides safe access that gracefully handles
|
|
8
|
+
* SSG/SSR contexts where the Router context isn't available.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* function NavItem({ route }) {
|
|
12
|
+
* const { useLocation } = useRouting()
|
|
13
|
+
* const location = useLocation()
|
|
14
|
+
* const isActive = location.pathname === route
|
|
15
|
+
* return <Link to={route} className={isActive ? 'active' : ''}>...</Link>
|
|
16
|
+
* }
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { getUniweb } from '@uniweb/core'
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Default location object for SSG/SSR contexts
|
|
23
|
+
*/
|
|
24
|
+
const DEFAULT_LOCATION = {
|
|
25
|
+
pathname: '/',
|
|
26
|
+
search: '',
|
|
27
|
+
hash: '',
|
|
28
|
+
state: null,
|
|
29
|
+
key: 'default'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Default params object for SSG/SSR contexts
|
|
34
|
+
*/
|
|
35
|
+
const DEFAULT_PARAMS = {}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Get routing utilities with SSG-safe fallbacks
|
|
39
|
+
* @returns {Object} Routing utilities
|
|
40
|
+
*/
|
|
41
|
+
export function useRouting() {
|
|
42
|
+
const uniweb = getUniweb()
|
|
43
|
+
const routing = uniweb?.routingComponents || {}
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
/**
|
|
47
|
+
* SSG-safe useLocation hook
|
|
48
|
+
* Returns current location or defaults during SSG
|
|
49
|
+
* @returns {Object} Location object { pathname, search, hash, state, key }
|
|
50
|
+
*/
|
|
51
|
+
useLocation: () => {
|
|
52
|
+
if (!routing.useLocation) {
|
|
53
|
+
return DEFAULT_LOCATION
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
return routing.useLocation()
|
|
57
|
+
} catch {
|
|
58
|
+
// Router context not available (SSG/SSR)
|
|
59
|
+
return DEFAULT_LOCATION
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* SSG-safe useParams hook
|
|
65
|
+
* Returns route params or empty object during SSG
|
|
66
|
+
* @returns {Object} Params object
|
|
67
|
+
*/
|
|
68
|
+
useParams: () => {
|
|
69
|
+
if (!routing.useParams) {
|
|
70
|
+
return DEFAULT_PARAMS
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
return routing.useParams()
|
|
74
|
+
} catch {
|
|
75
|
+
// Router context not available (SSG/SSR)
|
|
76
|
+
return DEFAULT_PARAMS
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* SSG-safe useNavigate hook
|
|
82
|
+
* Returns navigate function or no-op during SSG
|
|
83
|
+
* @returns {Function} Navigate function
|
|
84
|
+
*/
|
|
85
|
+
useNavigate: () => {
|
|
86
|
+
if (!routing.useNavigate) {
|
|
87
|
+
return () => {} // No-op during SSG
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
return routing.useNavigate()
|
|
91
|
+
} catch {
|
|
92
|
+
// Router context not available (SSG/SSR)
|
|
93
|
+
return () => {}
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Router Link component (or fallback to 'a')
|
|
99
|
+
* Use Kit's Link component instead for most cases
|
|
100
|
+
*/
|
|
101
|
+
Link: routing.Link || 'a',
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Check if routing is available (browser with Router context)
|
|
105
|
+
* @returns {boolean}
|
|
106
|
+
*/
|
|
107
|
+
isRoutingAvailable: () => {
|
|
108
|
+
if (!routing.useLocation) return false
|
|
109
|
+
try {
|
|
110
|
+
routing.useLocation()
|
|
111
|
+
return true
|
|
112
|
+
} catch {
|
|
113
|
+
return false
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export default useRouting
|
package/src/index.js
CHANGED
|
@@ -52,7 +52,7 @@ export { Disclaimer } from './components/Disclaimer/index.js'
|
|
|
52
52
|
// Hooks
|
|
53
53
|
// ============================================================================
|
|
54
54
|
|
|
55
|
-
export { useWebsite } from './hooks/index.js'
|
|
55
|
+
export { useWebsite, useRouting } from './hooks/index.js'
|
|
56
56
|
|
|
57
57
|
// ============================================================================
|
|
58
58
|
// Utilities
|