@uniweb/kit 0.1.8 → 0.1.9
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/useActiveRoute.js +26 -34
- package/src/hooks/useWebsite.js +10 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniweb/kit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"description": "Standard component library for Uniweb foundations",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"tailwind-merge": "^2.6.0",
|
|
40
|
-
"@uniweb/core": "0.1.
|
|
40
|
+
"@uniweb/core": "0.1.14"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
43
|
"react": "^18.0.0 || ^19.0.0",
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* useActiveRoute Hook
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Hook for active route detection in navigation components.
|
|
5
|
+
* All route comparison logic is delegated to Website class methods
|
|
6
|
+
* to ensure a single source of truth for route normalization and matching.
|
|
6
7
|
*
|
|
7
8
|
* @example
|
|
8
9
|
* function NavItem({ page }) {
|
|
@@ -13,25 +14,18 @@
|
|
|
13
14
|
* href={page.getNavigableRoute()}
|
|
14
15
|
* className={isActiveOrAncestor(page) ? 'active' : ''}
|
|
15
16
|
* >
|
|
16
|
-
* {page.
|
|
17
|
+
* {page.getLabel()}
|
|
17
18
|
* </Link>
|
|
18
19
|
* )
|
|
19
20
|
* }
|
|
20
21
|
*/
|
|
21
22
|
|
|
22
23
|
import { useRouting } from './useRouting.js'
|
|
24
|
+
import { useWebsite } from './useWebsite.js'
|
|
23
25
|
|
|
24
26
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* @returns {string}
|
|
28
|
-
*/
|
|
29
|
-
function normalizeRoute(route) {
|
|
30
|
-
return (route || '').replace(/^\//, '').replace(/\/$/, '')
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Hook for active route detection with SSG-safe fallbacks.
|
|
27
|
+
* Hook for active route detection.
|
|
28
|
+
* Delegates all logic to Website class for consistency.
|
|
35
29
|
*
|
|
36
30
|
* @returns {Object} Route utilities
|
|
37
31
|
* @property {string} route - Current normalized route (e.g., 'docs/getting-started')
|
|
@@ -41,17 +35,18 @@ function normalizeRoute(route) {
|
|
|
41
35
|
*/
|
|
42
36
|
export function useActiveRoute() {
|
|
43
37
|
const { useLocation } = useRouting()
|
|
38
|
+
const { website } = useWebsite()
|
|
44
39
|
const location = useLocation()
|
|
45
40
|
|
|
46
|
-
const
|
|
47
|
-
const rootSegment =
|
|
41
|
+
const currentRoute = website.normalizeRoute(location?.pathname)
|
|
42
|
+
const rootSegment = currentRoute.split('/')[0]
|
|
48
43
|
|
|
49
44
|
return {
|
|
50
45
|
/**
|
|
51
46
|
* Current normalized route (no leading/trailing slashes)
|
|
52
47
|
* @type {string}
|
|
53
48
|
*/
|
|
54
|
-
route,
|
|
49
|
+
route: currentRoute,
|
|
55
50
|
|
|
56
51
|
/**
|
|
57
52
|
* First segment of the current route
|
|
@@ -62,34 +57,31 @@ export function useActiveRoute() {
|
|
|
62
57
|
|
|
63
58
|
/**
|
|
64
59
|
* Check if a page is the current active page (exact match)
|
|
60
|
+
* Delegates to Website.isRouteActive() for consistent comparison.
|
|
65
61
|
*
|
|
66
|
-
* @param {Object}
|
|
62
|
+
* @param {Object|string} pageOrRoute - Page object or route string
|
|
67
63
|
* @returns {boolean}
|
|
68
64
|
*/
|
|
69
|
-
isActive: (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return normalizeRoute(page.route) === route
|
|
65
|
+
isActive: (pageOrRoute) => {
|
|
66
|
+
const targetRoute = typeof pageOrRoute === 'string'
|
|
67
|
+
? pageOrRoute
|
|
68
|
+
: pageOrRoute.route
|
|
69
|
+
return website.isRouteActive(targetRoute, currentRoute)
|
|
75
70
|
},
|
|
76
71
|
|
|
77
72
|
/**
|
|
78
73
|
* Check if a page or any of its descendants is active
|
|
79
|
-
* Useful for highlighting parent nav items when child is active
|
|
74
|
+
* Useful for highlighting parent nav items when child is active.
|
|
75
|
+
* Delegates to Website.isRouteActiveOrAncestor() for consistent logic.
|
|
80
76
|
*
|
|
81
|
-
* @param {Object}
|
|
77
|
+
* @param {Object|string} pageOrRoute - Page object or route string
|
|
82
78
|
* @returns {boolean}
|
|
83
79
|
*/
|
|
84
|
-
isActiveOrAncestor: (
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const pageRoute = normalizeRoute(page.route)
|
|
90
|
-
if (pageRoute === route) return true
|
|
91
|
-
if (pageRoute === '') return true // Root is ancestor of all
|
|
92
|
-
return route.startsWith(pageRoute + '/')
|
|
80
|
+
isActiveOrAncestor: (pageOrRoute) => {
|
|
81
|
+
const targetRoute = typeof pageOrRoute === 'string'
|
|
82
|
+
? pageOrRoute
|
|
83
|
+
: pageOrRoute.route
|
|
84
|
+
return website.isRouteActiveOrAncestor(targetRoute, currentRoute)
|
|
93
85
|
},
|
|
94
86
|
}
|
|
95
87
|
}
|
package/src/hooks/useWebsite.js
CHANGED
|
@@ -14,25 +14,23 @@
|
|
|
14
14
|
import { getUniweb } from '@uniweb/core'
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
* Get the current website instance and utilities
|
|
17
|
+
* Get the current website instance and utilities.
|
|
18
|
+
* This hook assumes the runtime is properly initialized.
|
|
19
|
+
*
|
|
18
20
|
* @returns {Object} Website utilities
|
|
21
|
+
* @throws {Error} If called before runtime initialization
|
|
19
22
|
*/
|
|
20
23
|
export function useWebsite() {
|
|
21
24
|
const uniweb = getUniweb()
|
|
25
|
+
const website = uniweb?.activeWebsite
|
|
22
26
|
|
|
23
|
-
if (!
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
makeHref: (href) => href,
|
|
29
|
-
getLanguage: () => 'en',
|
|
30
|
-
getLanguages: () => []
|
|
31
|
-
}
|
|
27
|
+
if (!website) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
'[Kit] useWebsite() called before runtime initialization. ' +
|
|
30
|
+
'Components must be rendered within a properly initialized Uniweb runtime.'
|
|
31
|
+
)
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
const website = uniweb.activeWebsite
|
|
35
|
-
|
|
36
34
|
return {
|
|
37
35
|
/**
|
|
38
36
|
* The active Website instance
|