@limetry/ui 1.2.63 → 1.2.70
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/dist/index.cjs +12 -5
- package/dist/index.js +12 -5
- package/package.json +16 -2
- package/src/components/drawer-menu.types.ts +12 -0
- package/src/components/site-header.tsx +100 -0
- package/src/components/site-header.types.ts +47 -0
- package/src/components/site-header.web.tsx +209 -0
- package/src/components/theme-toggle.tsx +37 -0
- package/src/components/theme-toggle.web.tsx +77 -0
- package/src/global.css +1 -2
- package/src/navigation/types.test.ts +24 -0
- package/src/navigation/types.ts +21 -5
package/dist/index.cjs
CHANGED
|
@@ -375,7 +375,9 @@ function StatCard({
|
|
|
375
375
|
|
|
376
376
|
// src/navigation/types.ts
|
|
377
377
|
function normalizePathname(pathname) {
|
|
378
|
-
const
|
|
378
|
+
const withoutOrigin = pathname.replace(/^[a-z][a-z\d+\-.]*:\/\/[^/]+/i, "");
|
|
379
|
+
const withoutQuery = withoutOrigin.split(/[?#]/, 1)[0] ?? "";
|
|
380
|
+
const normalized = withoutQuery.replace(/\/\([^/]+\)/g, "");
|
|
379
381
|
return normalized.length > 0 ? normalized : "/";
|
|
380
382
|
}
|
|
381
383
|
function isNavItemActive(pathname, href) {
|
|
@@ -396,14 +398,20 @@ function findActiveNavHref(pathname, items) {
|
|
|
396
398
|
let bestHref = null;
|
|
397
399
|
let bestLength = -1;
|
|
398
400
|
let bestDepth = -1;
|
|
401
|
+
let bestAbsolute = false;
|
|
399
402
|
const walk = (list, depth) => {
|
|
400
403
|
for (const item of list) {
|
|
401
404
|
if (isNavItemActive(pathname, item.href)) {
|
|
402
405
|
const length = normalizePathname(item.href).length;
|
|
403
|
-
|
|
406
|
+
const absolute = /^https?:\/\//i.test(item.href);
|
|
407
|
+
const betterLength = length > bestLength;
|
|
408
|
+
const betterDepth = length === bestLength && depth > bestDepth;
|
|
409
|
+
const preferRelative = length === bestLength && depth === bestDepth && bestAbsolute && !absolute;
|
|
410
|
+
if (betterLength || betterDepth || preferRelative) {
|
|
404
411
|
bestLength = length;
|
|
405
412
|
bestDepth = depth;
|
|
406
413
|
bestHref = item.href;
|
|
414
|
+
bestAbsolute = absolute;
|
|
407
415
|
}
|
|
408
416
|
}
|
|
409
417
|
if (item.children) {
|
|
@@ -418,8 +426,7 @@ function isItemHighlighted(pathname, item, activeHref) {
|
|
|
418
426
|
if (activeHref === null) {
|
|
419
427
|
return false;
|
|
420
428
|
}
|
|
421
|
-
|
|
422
|
-
if (!matches) {
|
|
429
|
+
if (item.href !== activeHref) {
|
|
423
430
|
return false;
|
|
424
431
|
}
|
|
425
432
|
const hasActiveChild = item.children?.some((child) => isNavItemOrChildActive(pathname, child)) ?? false;
|
|
@@ -430,7 +437,7 @@ function isHighlightedNavItem(pathname, href, items) {
|
|
|
430
437
|
if (activeHref === null) {
|
|
431
438
|
return false;
|
|
432
439
|
}
|
|
433
|
-
return
|
|
440
|
+
return href === activeHref;
|
|
434
441
|
}
|
|
435
442
|
// Annotate the CommonJS export names for ESM import in node:
|
|
436
443
|
0 && (module.exports = {
|
package/dist/index.js
CHANGED
|
@@ -333,7 +333,9 @@ function StatCard({
|
|
|
333
333
|
|
|
334
334
|
// src/navigation/types.ts
|
|
335
335
|
function normalizePathname(pathname) {
|
|
336
|
-
const
|
|
336
|
+
const withoutOrigin = pathname.replace(/^[a-z][a-z\d+\-.]*:\/\/[^/]+/i, "");
|
|
337
|
+
const withoutQuery = withoutOrigin.split(/[?#]/, 1)[0] ?? "";
|
|
338
|
+
const normalized = withoutQuery.replace(/\/\([^/]+\)/g, "");
|
|
337
339
|
return normalized.length > 0 ? normalized : "/";
|
|
338
340
|
}
|
|
339
341
|
function isNavItemActive(pathname, href) {
|
|
@@ -354,14 +356,20 @@ function findActiveNavHref(pathname, items) {
|
|
|
354
356
|
let bestHref = null;
|
|
355
357
|
let bestLength = -1;
|
|
356
358
|
let bestDepth = -1;
|
|
359
|
+
let bestAbsolute = false;
|
|
357
360
|
const walk = (list, depth) => {
|
|
358
361
|
for (const item of list) {
|
|
359
362
|
if (isNavItemActive(pathname, item.href)) {
|
|
360
363
|
const length = normalizePathname(item.href).length;
|
|
361
|
-
|
|
364
|
+
const absolute = /^https?:\/\//i.test(item.href);
|
|
365
|
+
const betterLength = length > bestLength;
|
|
366
|
+
const betterDepth = length === bestLength && depth > bestDepth;
|
|
367
|
+
const preferRelative = length === bestLength && depth === bestDepth && bestAbsolute && !absolute;
|
|
368
|
+
if (betterLength || betterDepth || preferRelative) {
|
|
362
369
|
bestLength = length;
|
|
363
370
|
bestDepth = depth;
|
|
364
371
|
bestHref = item.href;
|
|
372
|
+
bestAbsolute = absolute;
|
|
365
373
|
}
|
|
366
374
|
}
|
|
367
375
|
if (item.children) {
|
|
@@ -376,8 +384,7 @@ function isItemHighlighted(pathname, item, activeHref) {
|
|
|
376
384
|
if (activeHref === null) {
|
|
377
385
|
return false;
|
|
378
386
|
}
|
|
379
|
-
|
|
380
|
-
if (!matches) {
|
|
387
|
+
if (item.href !== activeHref) {
|
|
381
388
|
return false;
|
|
382
389
|
}
|
|
383
390
|
const hasActiveChild = item.children?.some((child) => isNavItemOrChildActive(pathname, child)) ?? false;
|
|
@@ -388,7 +395,7 @@ function isHighlightedNavItem(pathname, href, items) {
|
|
|
388
395
|
if (activeHref === null) {
|
|
389
396
|
return false;
|
|
390
397
|
}
|
|
391
|
-
return
|
|
398
|
+
return href === activeHref;
|
|
392
399
|
}
|
|
393
400
|
export {
|
|
394
401
|
AlertBanner,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@limetry/ui",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.70",
|
|
4
4
|
"description": "Shared design system for Limetry — components, icons, and design tokens.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,6 +21,20 @@
|
|
|
21
21
|
"import": "./src/components/drawer-menu.web.tsx",
|
|
22
22
|
"default": "./src/components/drawer-menu.web.tsx"
|
|
23
23
|
},
|
|
24
|
+
"./site-header": {
|
|
25
|
+
"types": "./src/components/site-header.web.tsx",
|
|
26
|
+
"react-native": "./src/components/site-header.tsx",
|
|
27
|
+
"browser": "./src/components/site-header.web.tsx",
|
|
28
|
+
"import": "./src/components/site-header.web.tsx",
|
|
29
|
+
"default": "./src/components/site-header.web.tsx"
|
|
30
|
+
},
|
|
31
|
+
"./theme-toggle": {
|
|
32
|
+
"types": "./src/components/theme-toggle.web.tsx",
|
|
33
|
+
"react-native": "./src/components/theme-toggle.tsx",
|
|
34
|
+
"browser": "./src/components/theme-toggle.web.tsx",
|
|
35
|
+
"import": "./src/components/theme-toggle.web.tsx",
|
|
36
|
+
"default": "./src/components/theme-toggle.web.tsx"
|
|
37
|
+
},
|
|
24
38
|
"./tailwind": "./tailwind.config.ts",
|
|
25
39
|
"./global.css": "./src/global.css"
|
|
26
40
|
},
|
|
@@ -62,7 +76,7 @@
|
|
|
62
76
|
"react-native-css-interop": "^0.2.6",
|
|
63
77
|
"react-native-reanimated": "~3.17.5",
|
|
64
78
|
"react-native-svg": "15.15.5",
|
|
65
|
-
"tailwindcss": "^3.
|
|
79
|
+
"tailwindcss": "^4.3.3",
|
|
66
80
|
"tsup": "^8.5.0",
|
|
67
81
|
"typescript": "^5.8.2",
|
|
68
82
|
"vitest": "4.1.11"
|
|
@@ -36,6 +36,18 @@ export type DrawerLinkProps = {
|
|
|
36
36
|
* Optional Tailwind / NativeWind class names for the link root.
|
|
37
37
|
*/
|
|
38
38
|
className?: string
|
|
39
|
+
/**
|
|
40
|
+
* Optional browser target for header links.
|
|
41
|
+
*/
|
|
42
|
+
target?: string
|
|
43
|
+
/**
|
|
44
|
+
* Optional browser relationship for header links.
|
|
45
|
+
*/
|
|
46
|
+
rel?: string
|
|
47
|
+
/**
|
|
48
|
+
* Optional current-page marker for header links.
|
|
49
|
+
*/
|
|
50
|
+
ariaCurrent?: "page"
|
|
39
51
|
}
|
|
40
52
|
|
|
41
53
|
/**
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared native site header with the same slots as the web implementation.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { useState } from "react"
|
|
6
|
+
import { Pressable, View } from "react-native"
|
|
7
|
+
|
|
8
|
+
import { cn } from "../lib/cn"
|
|
9
|
+
import { DrawerMenu } from "./drawer-menu"
|
|
10
|
+
import { MenuIcon, XIcon } from "./icons"
|
|
11
|
+
import type { SiteHeaderProps } from "./site-header.types"
|
|
12
|
+
|
|
13
|
+
export type { SiteHeaderLinkProps, SiteHeaderProps } from "./site-header.types"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Native implementation of the shared site header contract.
|
|
17
|
+
*
|
|
18
|
+
* @param props - Brand, navigation, host link adapter, and action slots.
|
|
19
|
+
* @returns Native header and mobile drawer.
|
|
20
|
+
*/
|
|
21
|
+
export function SiteHeader({
|
|
22
|
+
brand,
|
|
23
|
+
brandHref,
|
|
24
|
+
items,
|
|
25
|
+
pathname,
|
|
26
|
+
LinkComponent,
|
|
27
|
+
desktopActions,
|
|
28
|
+
mobileActions,
|
|
29
|
+
drawerFooter,
|
|
30
|
+
renderIcon,
|
|
31
|
+
drawerClassName,
|
|
32
|
+
menuOpen,
|
|
33
|
+
onMenuOpenChange,
|
|
34
|
+
onNavigate,
|
|
35
|
+
className,
|
|
36
|
+
topOffset,
|
|
37
|
+
}: SiteHeaderProps): React.JSX.Element {
|
|
38
|
+
const [internalMenuOpen, setInternalMenuOpen] = useState(false)
|
|
39
|
+
const isMenuOpen = menuOpen ?? internalMenuOpen
|
|
40
|
+
const updateMenuOpen = (open: boolean): void => {
|
|
41
|
+
setInternalMenuOpen(open)
|
|
42
|
+
onMenuOpenChange?.(open)
|
|
43
|
+
}
|
|
44
|
+
const NavigationLink: SiteHeaderProps["LinkComponent"] = ({
|
|
45
|
+
onPress,
|
|
46
|
+
...props
|
|
47
|
+
}) => (
|
|
48
|
+
<LinkComponent
|
|
49
|
+
{...props}
|
|
50
|
+
onPress={() => {
|
|
51
|
+
onPress?.()
|
|
52
|
+
onNavigate?.()
|
|
53
|
+
}}
|
|
54
|
+
/>
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<>
|
|
59
|
+
<View className={cn(
|
|
60
|
+
"z-20 border-b border-slate-200 bg-white dark:border-slate-800 dark:bg-slate-900",
|
|
61
|
+
className,
|
|
62
|
+
)}>
|
|
63
|
+
<View className="h-16 flex-row items-center justify-between px-4">
|
|
64
|
+
<LinkComponent
|
|
65
|
+
href={brandHref}
|
|
66
|
+
className="shrink-0 flex-row items-center"
|
|
67
|
+
onPress={onNavigate}
|
|
68
|
+
>
|
|
69
|
+
{brand}
|
|
70
|
+
</LinkComponent>
|
|
71
|
+
<View className="flex-row items-center gap-1">
|
|
72
|
+
{desktopActions}
|
|
73
|
+
{mobileActions}
|
|
74
|
+
<Pressable
|
|
75
|
+
accessibilityLabel={isMenuOpen ? "Close navigation menu" : "Open navigation menu"}
|
|
76
|
+
accessibilityRole="button"
|
|
77
|
+
className="rounded-lg p-2 active:opacity-80"
|
|
78
|
+
onPress={() => {
|
|
79
|
+
updateMenuOpen(!isMenuOpen)
|
|
80
|
+
}}
|
|
81
|
+
>
|
|
82
|
+
{isMenuOpen ? <XIcon className="text-slate-600 dark:text-slate-400" /> : <MenuIcon className="text-slate-600 dark:text-slate-400" />}
|
|
83
|
+
</Pressable>
|
|
84
|
+
</View>
|
|
85
|
+
</View>
|
|
86
|
+
</View>
|
|
87
|
+
<DrawerMenu
|
|
88
|
+
open={isMenuOpen}
|
|
89
|
+
onOpenChange={updateMenuOpen}
|
|
90
|
+
items={items}
|
|
91
|
+
pathname={pathname}
|
|
92
|
+
LinkComponent={NavigationLink}
|
|
93
|
+
renderIcon={renderIcon}
|
|
94
|
+
footer={drawerFooter}
|
|
95
|
+
topOffset={topOffset}
|
|
96
|
+
className={drawerClassName}
|
|
97
|
+
/>
|
|
98
|
+
</>
|
|
99
|
+
)
|
|
100
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ComponentType, ReactNode } from "react"
|
|
2
|
+
|
|
3
|
+
import type { NavItem } from "../navigation/types"
|
|
4
|
+
import type { DrawerLinkProps } from "./drawer-menu.types"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Link contract used by the shared site header and mobile drawer.
|
|
8
|
+
*/
|
|
9
|
+
export type SiteHeaderLinkProps = DrawerLinkProps
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Shared web and native site-header inputs.
|
|
13
|
+
*/
|
|
14
|
+
export type SiteHeaderProps = {
|
|
15
|
+
/** Brand content rendered at the leading edge of the header. */
|
|
16
|
+
brand: ReactNode
|
|
17
|
+
/** Destination used when the brand is selected. */
|
|
18
|
+
brandHref: string
|
|
19
|
+
/** Top-level links shown in desktop navigation and the mobile drawer. */
|
|
20
|
+
items: NavItem[]
|
|
21
|
+
/** Current pathname used for active-link styling. */
|
|
22
|
+
pathname: string
|
|
23
|
+
/** Host router adapter for internal and external links. */
|
|
24
|
+
LinkComponent: ComponentType<SiteHeaderLinkProps>
|
|
25
|
+
/** Whether top-level links render in the desktop header. */
|
|
26
|
+
showDesktopNav?: boolean
|
|
27
|
+
/** Optional controls shown on desktop to the right of navigation. */
|
|
28
|
+
desktopActions?: ReactNode
|
|
29
|
+
/** Optional controls shown on mobile before the menu button. */
|
|
30
|
+
mobileActions?: ReactNode
|
|
31
|
+
/** Optional content pinned below the mobile navigation list. */
|
|
32
|
+
drawerFooter?: ReactNode
|
|
33
|
+
/** Optional controlled state for the mobile navigation drawer. */
|
|
34
|
+
menuOpen?: boolean
|
|
35
|
+
/** Called when the mobile navigation drawer changes state. */
|
|
36
|
+
onMenuOpenChange?: (open: boolean) => void
|
|
37
|
+
/** Called when a header or drawer link is selected. */
|
|
38
|
+
onNavigate?: () => void
|
|
39
|
+
/** Optional icon renderer for drawer navigation rows. */
|
|
40
|
+
renderIcon?: (icon: string, active: boolean) => ReactNode
|
|
41
|
+
/** Optional extra classes for the header element. */
|
|
42
|
+
className?: string
|
|
43
|
+
/** Optional extra classes for the drawer panel. */
|
|
44
|
+
drawerClassName?: string
|
|
45
|
+
/** Offset between the header and the drawer. */
|
|
46
|
+
topOffset?: number
|
|
47
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Shared DOM site header for the OSS and enterprise web frontends.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ComponentType, JSX } from "react"
|
|
8
|
+
import { useCallback, useEffect, useMemo, useState } from "react"
|
|
9
|
+
|
|
10
|
+
import { cn } from "../lib/cn"
|
|
11
|
+
import { isNavItemActive } from "../navigation/types"
|
|
12
|
+
import { DrawerMenu } from "./drawer-menu.web"
|
|
13
|
+
import type { SiteHeaderLinkProps, SiteHeaderProps } from "./site-header.types"
|
|
14
|
+
|
|
15
|
+
export type { SiteHeaderLinkProps, SiteHeaderProps } from "./site-header.types"
|
|
16
|
+
|
|
17
|
+
function isExternalHref(href: string): boolean {
|
|
18
|
+
return /^https?:\/\//i.test(href)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isDesktopItemActive(pathname: string, href: string): boolean {
|
|
22
|
+
if (isExternalHref(href)) {
|
|
23
|
+
return false
|
|
24
|
+
}
|
|
25
|
+
if (href.startsWith("/docs")) {
|
|
26
|
+
return pathname.startsWith("/docs")
|
|
27
|
+
}
|
|
28
|
+
return isNavItemActive(pathname, href)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function externalLinkProps(href: string): Pick<SiteHeaderLinkProps, "rel" | "target"> {
|
|
32
|
+
if (!isExternalHref(href)) {
|
|
33
|
+
return {}
|
|
34
|
+
}
|
|
35
|
+
return { rel: "noopener noreferrer", target: "_blank" }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type HeaderNavLinkProps = SiteHeaderLinkProps & {
|
|
39
|
+
LinkComponent: ComponentType<SiteHeaderLinkProps>
|
|
40
|
+
onHeaderNavigate?: () => void
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Stable adapter so drawer and desktop links do not remount when the header re-renders.
|
|
45
|
+
*/
|
|
46
|
+
function HeaderNavLink({
|
|
47
|
+
LinkComponent,
|
|
48
|
+
onHeaderNavigate,
|
|
49
|
+
onClick,
|
|
50
|
+
...props
|
|
51
|
+
}: HeaderNavLinkProps): JSX.Element {
|
|
52
|
+
return (
|
|
53
|
+
<LinkComponent
|
|
54
|
+
{...props}
|
|
55
|
+
onClick={() => {
|
|
56
|
+
onClick?.()
|
|
57
|
+
onHeaderNavigate?.()
|
|
58
|
+
}}
|
|
59
|
+
/>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Shared sticky header and mobile navigation drawer.
|
|
65
|
+
*
|
|
66
|
+
* @param props - Brand, navigation, host link adapter, and action slots.
|
|
67
|
+
* @returns Site header and mobile drawer.
|
|
68
|
+
*/
|
|
69
|
+
export function SiteHeader({
|
|
70
|
+
brand,
|
|
71
|
+
brandHref,
|
|
72
|
+
items,
|
|
73
|
+
pathname,
|
|
74
|
+
LinkComponent,
|
|
75
|
+
showDesktopNav = true,
|
|
76
|
+
desktopActions,
|
|
77
|
+
mobileActions,
|
|
78
|
+
drawerFooter,
|
|
79
|
+
menuOpen: controlledMenuOpen,
|
|
80
|
+
onMenuOpenChange,
|
|
81
|
+
onNavigate,
|
|
82
|
+
renderIcon,
|
|
83
|
+
className,
|
|
84
|
+
drawerClassName,
|
|
85
|
+
topOffset = 64,
|
|
86
|
+
}: SiteHeaderProps): JSX.Element {
|
|
87
|
+
const [internalMenuOpen, setInternalMenuOpen] = useState(false)
|
|
88
|
+
const menuOpen = controlledMenuOpen ?? internalMenuOpen
|
|
89
|
+
const setMenuOpen = useCallback(
|
|
90
|
+
(open: boolean): void => {
|
|
91
|
+
if (controlledMenuOpen === undefined) {
|
|
92
|
+
setInternalMenuOpen(open)
|
|
93
|
+
}
|
|
94
|
+
onMenuOpenChange?.(open)
|
|
95
|
+
},
|
|
96
|
+
[controlledMenuOpen, onMenuOpenChange],
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
const NavLink = useMemo((): ComponentType<SiteHeaderLinkProps> => {
|
|
100
|
+
function BoundHeaderNavLink(props: SiteHeaderLinkProps): JSX.Element {
|
|
101
|
+
return (
|
|
102
|
+
<HeaderNavLink
|
|
103
|
+
{...props}
|
|
104
|
+
LinkComponent={LinkComponent}
|
|
105
|
+
onHeaderNavigate={onNavigate}
|
|
106
|
+
/>
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
return BoundHeaderNavLink
|
|
110
|
+
}, [LinkComponent, onNavigate])
|
|
111
|
+
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
setInternalMenuOpen(false)
|
|
114
|
+
onMenuOpenChange?.(false)
|
|
115
|
+
}, [pathname, onMenuOpenChange])
|
|
116
|
+
|
|
117
|
+
return (
|
|
118
|
+
<>
|
|
119
|
+
<header
|
|
120
|
+
className={cn(
|
|
121
|
+
"sticky top-0 z-50 border-b border-border/60 bg-background/95 backdrop-blur-xl",
|
|
122
|
+
className,
|
|
123
|
+
)}
|
|
124
|
+
>
|
|
125
|
+
<div className="mx-auto flex h-16 max-w-7xl items-center justify-between gap-4 px-4 sm:px-6 lg:px-8">
|
|
126
|
+
<div className="flex min-w-0 items-center gap-6 md:gap-8">
|
|
127
|
+
<NavLink href={brandHref} className="flex shrink-0 items-center">
|
|
128
|
+
{brand}
|
|
129
|
+
</NavLink>
|
|
130
|
+
|
|
131
|
+
{showDesktopNav ? (
|
|
132
|
+
<nav className="hidden min-w-0 items-center gap-4 md:flex md:gap-5 xl:gap-6">
|
|
133
|
+
{items.map((item) => {
|
|
134
|
+
const active = isDesktopItemActive(pathname, item.href)
|
|
135
|
+
return (
|
|
136
|
+
<NavLink
|
|
137
|
+
key={item.href}
|
|
138
|
+
href={item.href}
|
|
139
|
+
{...externalLinkProps(item.href)}
|
|
140
|
+
onClick={() => {
|
|
141
|
+
setMenuOpen(false)
|
|
142
|
+
}}
|
|
143
|
+
ariaCurrent={active ? "page" : undefined}
|
|
144
|
+
className={cn(
|
|
145
|
+
"whitespace-nowrap text-sm transition-colors hover:text-foreground",
|
|
146
|
+
active
|
|
147
|
+
? "font-semibold text-emerald-600 dark:text-emerald-400"
|
|
148
|
+
: "font-medium text-muted-foreground",
|
|
149
|
+
)}
|
|
150
|
+
>
|
|
151
|
+
{item.label}
|
|
152
|
+
</NavLink>
|
|
153
|
+
)
|
|
154
|
+
})}
|
|
155
|
+
</nav>
|
|
156
|
+
) : null}
|
|
157
|
+
</div>
|
|
158
|
+
|
|
159
|
+
<div className="hidden shrink-0 flex-nowrap items-center gap-2 md:flex">
|
|
160
|
+
{desktopActions}
|
|
161
|
+
</div>
|
|
162
|
+
|
|
163
|
+
<div className="flex items-center gap-1 md:hidden">
|
|
164
|
+
{mobileActions}
|
|
165
|
+
<button
|
|
166
|
+
type="button"
|
|
167
|
+
className="flex items-center justify-center rounded-lg p-2 text-muted-foreground hover:bg-muted hover:text-foreground"
|
|
168
|
+
onClick={() => {
|
|
169
|
+
setMenuOpen(!menuOpen)
|
|
170
|
+
}}
|
|
171
|
+
aria-label={menuOpen ? "Close navigation" : "Open navigation"}
|
|
172
|
+
aria-expanded={menuOpen}
|
|
173
|
+
>
|
|
174
|
+
{menuOpen ? <CloseIcon /> : <MenuIcon />}
|
|
175
|
+
</button>
|
|
176
|
+
</div>
|
|
177
|
+
</div>
|
|
178
|
+
</header>
|
|
179
|
+
|
|
180
|
+
<DrawerMenu
|
|
181
|
+
open={menuOpen}
|
|
182
|
+
onOpenChange={setMenuOpen}
|
|
183
|
+
items={items}
|
|
184
|
+
pathname={pathname}
|
|
185
|
+
LinkComponent={NavLink}
|
|
186
|
+
renderIcon={renderIcon}
|
|
187
|
+
footer={drawerFooter}
|
|
188
|
+
topOffset={topOffset}
|
|
189
|
+
className={drawerClassName}
|
|
190
|
+
/>
|
|
191
|
+
</>
|
|
192
|
+
)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function MenuIcon(): JSX.Element {
|
|
196
|
+
return (
|
|
197
|
+
<svg className="size-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
|
|
198
|
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16M4 18h16" />
|
|
199
|
+
</svg>
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function CloseIcon(): JSX.Element {
|
|
204
|
+
return (
|
|
205
|
+
<svg className="size-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
|
|
206
|
+
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
|
207
|
+
</svg>
|
|
208
|
+
)
|
|
209
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Controlled sun/moon toggle for native consumers.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Moon, Sun } from "lucide-react-native"
|
|
6
|
+
import { Pressable } from "react-native"
|
|
7
|
+
|
|
8
|
+
import { cn } from "../lib/cn"
|
|
9
|
+
import type { ThemeToggleProps } from "./theme-toggle.web"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Renders the shared theme toggle contract for native consumers.
|
|
13
|
+
*
|
|
14
|
+
* @param props - Resolved theme, toggle callback, and optional classes.
|
|
15
|
+
* @returns Theme toggle button.
|
|
16
|
+
*/
|
|
17
|
+
export function ThemeToggle({
|
|
18
|
+
resolvedTheme,
|
|
19
|
+
onToggle,
|
|
20
|
+
className,
|
|
21
|
+
}: ThemeToggleProps): React.JSX.Element {
|
|
22
|
+
const isDark = resolvedTheme === "dark"
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<Pressable
|
|
26
|
+
accessibilityLabel={isDark ? "Switch to light mode" : "Switch to dark mode"}
|
|
27
|
+
accessibilityRole="button"
|
|
28
|
+
className={cn(
|
|
29
|
+
"h-10 w-10 items-center justify-center rounded-xl border border-slate-200 bg-white active:opacity-80 dark:border-slate-700 dark:bg-slate-900",
|
|
30
|
+
className,
|
|
31
|
+
)}
|
|
32
|
+
onPress={onToggle}
|
|
33
|
+
>
|
|
34
|
+
{isDark ? <Sun color="#94a3b8" size={18} strokeWidth={2} /> : <Moon color="#64748b" size={18} strokeWidth={2} />}
|
|
35
|
+
</Pressable>
|
|
36
|
+
)
|
|
37
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Controlled sun/moon toggle used by every Limetry web header.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { JSX } from "react"
|
|
8
|
+
|
|
9
|
+
export type ThemeToggleProps = {
|
|
10
|
+
/** Current resolved theme. */
|
|
11
|
+
resolvedTheme: "light" | "dark"
|
|
12
|
+
/** Switches to the opposite resolved theme. */
|
|
13
|
+
onToggle: () => void
|
|
14
|
+
/** Optional classes for the control. */
|
|
15
|
+
className?: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Renders the shared OSS-style light/dark theme control.
|
|
20
|
+
*
|
|
21
|
+
* @param props - Resolved theme, toggle callback, and optional classes.
|
|
22
|
+
* @returns Theme toggle button.
|
|
23
|
+
*/
|
|
24
|
+
export function ThemeToggle({
|
|
25
|
+
resolvedTheme,
|
|
26
|
+
onToggle,
|
|
27
|
+
className = "",
|
|
28
|
+
}: ThemeToggleProps): JSX.Element {
|
|
29
|
+
const isDark = resolvedTheme === "dark"
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<button
|
|
33
|
+
type="button"
|
|
34
|
+
aria-label={isDark ? "Switch to light mode" : "Switch to dark mode"}
|
|
35
|
+
onClick={onToggle}
|
|
36
|
+
className={`flex h-9 w-9 items-center justify-center rounded-xl border border-border bg-card text-muted-foreground transition-colors hover:bg-muted hover:text-foreground ${className}`}
|
|
37
|
+
>
|
|
38
|
+
<SunIcon className="h-[1.1rem] w-[1.1rem] dark:hidden" />
|
|
39
|
+
<MoonIcon className="hidden h-[1.1rem] w-[1.1rem] dark:block" />
|
|
40
|
+
</button>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function SunIcon({ className }: { className?: string }): JSX.Element {
|
|
45
|
+
return (
|
|
46
|
+
<svg
|
|
47
|
+
className={className}
|
|
48
|
+
viewBox="0 0 24 24"
|
|
49
|
+
fill="none"
|
|
50
|
+
stroke="currentColor"
|
|
51
|
+
strokeWidth={2}
|
|
52
|
+
strokeLinecap="round"
|
|
53
|
+
strokeLinejoin="round"
|
|
54
|
+
aria-hidden="true"
|
|
55
|
+
>
|
|
56
|
+
<circle cx="12" cy="12" r="4" />
|
|
57
|
+
<path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41" />
|
|
58
|
+
</svg>
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function MoonIcon({ className }: { className?: string }): JSX.Element {
|
|
63
|
+
return (
|
|
64
|
+
<svg
|
|
65
|
+
className={className}
|
|
66
|
+
viewBox="0 0 24 24"
|
|
67
|
+
fill="none"
|
|
68
|
+
stroke="currentColor"
|
|
69
|
+
strokeWidth={2}
|
|
70
|
+
strokeLinecap="round"
|
|
71
|
+
strokeLinejoin="round"
|
|
72
|
+
aria-hidden="true"
|
|
73
|
+
>
|
|
74
|
+
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
|
|
75
|
+
</svg>
|
|
76
|
+
)
|
|
77
|
+
}
|
package/src/global.css
CHANGED
|
@@ -69,4 +69,28 @@ describe("Navigation Types & Drawer Highlight", () => {
|
|
|
69
69
|
expect(isItemHighlighted("/blog", blog, blogActiveHref)).toBe(true)
|
|
70
70
|
expect(isItemHighlighted("/blog", home, blogActiveHref)).toBe(false)
|
|
71
71
|
})
|
|
72
|
+
|
|
73
|
+
it("matches absolute cloud URLs against the current pathname", () => {
|
|
74
|
+
const cloudHome = { href: "https://app.limetry.com/", label: "Cloud" }
|
|
75
|
+
const cloudPricing = { href: "https://app.limetry.com/pricing", label: "Pricing" }
|
|
76
|
+
|
|
77
|
+
expect(findActiveNavHref("/pricing?interval=monthly", [cloudHome, cloudPricing])).toBe(
|
|
78
|
+
cloudPricing.href,
|
|
79
|
+
)
|
|
80
|
+
expect(isNavItemOrChildActive("/dashboard", cloudHome)).toBe(false)
|
|
81
|
+
expect(isNavItemOrChildActive("/pricing", cloudPricing)).toBe(true)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it("does not highlight a cross-origin root next to Home on /", () => {
|
|
85
|
+
const home = { href: "/", label: "Home" }
|
|
86
|
+
const limetryOrg = { href: "https://limetry.org", label: "Limetry.org" }
|
|
87
|
+
const pathname = "/"
|
|
88
|
+
|
|
89
|
+
for (const items of [[home, limetryOrg], [limetryOrg, home]]) {
|
|
90
|
+
const activeHref = findActiveNavHref(pathname, items)
|
|
91
|
+
expect(activeHref).toBe("/")
|
|
92
|
+
expect(isItemHighlighted(pathname, home, activeHref)).toBe(true)
|
|
93
|
+
expect(isItemHighlighted(pathname, limetryOrg, activeHref)).toBe(false)
|
|
94
|
+
}
|
|
95
|
+
})
|
|
72
96
|
})
|
package/src/navigation/types.ts
CHANGED
|
@@ -38,7 +38,9 @@ export type NavItem = {
|
|
|
38
38
|
* @returns Normalized path beginning with `/`, or `"/"` when empty after stripping.
|
|
39
39
|
*/
|
|
40
40
|
export function normalizePathname(pathname: string): string {
|
|
41
|
-
const
|
|
41
|
+
const withoutOrigin = pathname.replace(/^[a-z][a-z\d+\-.]*:\/\/[^/]+/i, "")
|
|
42
|
+
const withoutQuery = withoutOrigin.split(/[?#]/, 1)[0] ?? ""
|
|
43
|
+
const normalized = withoutQuery.replace(/\/\([^/]+\)/g, "")
|
|
42
44
|
return normalized.length > 0 ? normalized : "/"
|
|
43
45
|
}
|
|
44
46
|
|
|
@@ -87,15 +89,25 @@ export function findActiveNavHref(pathname: string, items: NavItem[]): string |
|
|
|
87
89
|
let bestHref: string | null = null
|
|
88
90
|
let bestLength = -1
|
|
89
91
|
let bestDepth = -1
|
|
92
|
+
let bestAbsolute = false
|
|
90
93
|
|
|
91
94
|
const walk = (list: NavItem[], depth: number): void => {
|
|
92
95
|
for (const item of list) {
|
|
93
96
|
if (isNavItemActive(pathname, item.href)) {
|
|
94
97
|
const length = normalizePathname(item.href).length
|
|
95
|
-
|
|
98
|
+
const absolute = /^https?:\/\//i.test(item.href)
|
|
99
|
+
const betterLength = length > bestLength
|
|
100
|
+
const betterDepth = length === bestLength && depth > bestDepth
|
|
101
|
+
const preferRelative =
|
|
102
|
+
length === bestLength
|
|
103
|
+
&& depth === bestDepth
|
|
104
|
+
&& bestAbsolute
|
|
105
|
+
&& !absolute
|
|
106
|
+
if (betterLength || betterDepth || preferRelative) {
|
|
96
107
|
bestLength = length
|
|
97
108
|
bestDepth = depth
|
|
98
109
|
bestHref = item.href
|
|
110
|
+
bestAbsolute = absolute
|
|
99
111
|
}
|
|
100
112
|
}
|
|
101
113
|
if (item.children) {
|
|
@@ -126,8 +138,12 @@ export function isItemHighlighted(
|
|
|
126
138
|
if (activeHref === null) {
|
|
127
139
|
return false
|
|
128
140
|
}
|
|
129
|
-
|
|
130
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Compare the winning href literally so cross-origin roots (for example
|
|
143
|
+
* `https://limetry.org` next to Home `/`) do not both light up after
|
|
144
|
+
* {@link normalizePathname} strips the origin to `/`.
|
|
145
|
+
*/
|
|
146
|
+
if (item.href !== activeHref) {
|
|
131
147
|
return false
|
|
132
148
|
}
|
|
133
149
|
const hasActiveChild =
|
|
@@ -148,5 +164,5 @@ export function isHighlightedNavItem(pathname: string, href: string, items: NavI
|
|
|
148
164
|
if (activeHref === null) {
|
|
149
165
|
return false
|
|
150
166
|
}
|
|
151
|
-
return
|
|
167
|
+
return href === activeHref
|
|
152
168
|
}
|