Skip to content

Commit 6db2efb

Browse files
committed
feat: make header links active
1 parent a99232e commit 6db2efb

File tree

8 files changed

+49
-8
lines changed

8 files changed

+49
-8
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@swc/helpers": "~0.5.2",
3434
"@tabler/icons-react": "^2.42.0",
3535
"@tanstack/react-query": "^5.10.0",
36+
"clsx": "^2.0.0",
3637
"core-js": "^3.33.3",
3738
"create-nx-workspace": "17.1.3",
3839
"dayjs": "^1.11.10",

packages/core/src/lib/ui-header/ui-header.module.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.header {
22
height: rem(56px);
3-
background-color: var(--mantine-color-body);
3+
background-color: light-dark(var(--mantine-color-white), var(--mantine-color-dark-9));
44
padding-left: var(--mantine-spacing-md);
55
padding-right: var(--mantine-spacing-md);
66
}
@@ -26,3 +26,7 @@
2626
background-color: light-dark(var(--mantine-color-gray-0), var(--mantine-color-dark-6));
2727
}
2828
}
29+
.linkActive {
30+
background-color: light-dark(var(--mantine-color-gray-0), var(--mantine-color-dark-6));
31+
color: light-dark(var(--mantine-color-brand-6), var(--mantine-color-brand-4));
32+
}

packages/core/src/lib/ui-header/ui-header.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Anchor, Burger, Group } from '@mantine/core'
22
import { ReactNode } from 'react'
3-
import { Link } from 'react-router-dom'
3+
import cx from 'clsx'
4+
import { Link, useLocation } from 'react-router-dom'
45
import { UiLogo, UiLogoType } from '../ui-logo'
56

67
import classes from './ui-header.module.css'
@@ -20,8 +21,14 @@ export interface UiHeaderLink {
2021
}
2122

2223
export function UiHeader({ base, links = [], logo, logoSmall, opened, profile, toggle }: UiHeaderProps) {
24+
const { pathname } = useLocation()
2325
const items = links.map((link) => (
24-
<Anchor component={Link} key={link.label} to={link.link} className={classes.link}>
26+
<Anchor
27+
component={Link}
28+
key={link.label}
29+
to={link.link}
30+
className={cx(classes.link, { [classes.linkActive]: pathname.startsWith(link.link) })}
31+
>
2532
{link.label}
2633
</Anchor>
2734
))

packages/generators/src/generators/component/files/header/__prefixFileName__-header.module.css.template

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.header {
22
height: rem(56px);
3-
background-color: var(--mantine-color-body);
3+
background-color: light-dark(var(--mantine-color-white), var(--mantine-color-dark-9));
44
padding-left: var(--mantine-spacing-md);
55
padding-right: var(--mantine-spacing-md);
66
}
@@ -26,3 +26,7 @@
2626
background-color: light-dark(var(--mantine-color-gray-0), var(--mantine-color-dark-6));
2727
}
2828
}
29+
.linkActive {
30+
background-color: light-dark(var(--mantine-color-gray-0), var(--mantine-color-dark-6));
31+
color: light-dark(var(--mantine-color-brand-6), var(--mantine-color-brand-4));
32+
}

packages/generators/src/generators/component/files/header/__prefixFileName__-header.tsx.template

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Anchor, Burger, Group } from '@mantine/core'
22
import { ReactNode } from 'react'
3-
import { Link } from 'react-router-dom'
3+
import cx from 'clsx'
4+
import { Link, useLocation } from 'react-router-dom'
45
import { <%= prefix.className %>Logo, <%= prefix.className %>LogoType } from '../<%= prefix.fileName %>-logo'
56

67
import classes from './<%= prefix.fileName %>-header.module.css'
@@ -20,8 +21,14 @@ export interface <%= prefix.className %>HeaderLink {
2021
}
2122

2223
export function <%= prefix.className %>Header({ base, links = [], logo, logoSmall, opened, profile, toggle }: <%= prefix.className %>HeaderProps) {
24+
const { pathname } = useLocation()
2325
const items = links.map((link) => (
24-
<Anchor component={Link} key={link.label} to={link.link} className={classes.link}>
26+
<Anchor
27+
component={Link}
28+
key={link.label}
29+
to={link.link}
30+
className={cx(classes.link, { [classes.linkActive]: pathname.startsWith(link.link) })}
31+
>
2532
{link.label}
2633
</Anchor>
2734
))

packages/generators/src/generators/theme/__snapshots__/theme-generator.spec.ts.snap

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ exports[`theme generator should run successfully 1`] = `
3535
""@mantine/modals": "^7.2.2",",
3636
""@mantine/notifications": "^7.2.2",",
3737
""@tabler/icons-react": "^2.42.0",",
38+
""clsx": "2.0.0",",
3839
""dayjs": "^1.11.10",",
40+
""react-router-dom": "6.20.1",",
3941
""timeago-react": "^3.0.6"",
4042
"},",
4143
""devDependencies": {",
@@ -636,6 +638,13 @@ exports[`theme generator should run successfully 1`] = `
636638
");",
637639
"}",
638640
"}",
641+
".linkActive {",
642+
"background-color: light-dark(",
643+
"var(--mantine-color-gray-0),",
644+
"var(--mantine-color-dark-6)",
645+
");",
646+
"color: light-dark(var(--mantine-color-brand-6), var(--mantine-color-brand-4));",
647+
"}",
639648
],
640649
"isBinary": false,
641650
"path": "./test-target/src/app/ui/ui-header/ui-header.module.css",
@@ -644,7 +653,8 @@ exports[`theme generator should run successfully 1`] = `
644653
"content": [
645654
"import { Anchor, Burger, Group } from '@mantine/core';",
646655
"import { ReactNode } from 'react';",
647-
"import { Link } from 'react-router-dom';",
656+
"import cx from 'clsx';",
657+
"import { Link, useLocation } from 'react-router-dom';",
648658
"import { UiLogo, UiLogoType } from '../ui-logo';",
649659
"import classes from './ui-header.module.css';",
650660
"export interface UiHeaderProps {",
@@ -669,12 +679,15 @@ exports[`theme generator should run successfully 1`] = `
669679
"profile,",
670680
"toggle,",
671681
"}: UiHeaderProps) {",
682+
"const { pathname } = useLocation();",
672683
"const items = links.map((link) => (",
673684
"<Anchor",
674685
"component={Link}",
675686
"key={link.label}",
676687
"to={link.link}",
677-
"className={classes.link}",
688+
"className={cx(classes.link, {",
689+
"[classes.linkActive]: pathname.startsWith(link.link),",
690+
"})}",
678691
">",
679692
"{link.label}",
680693
"</Anchor>",

packages/generators/src/generators/theme/theme-generator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export async function themeGenerator(tree: Tree, options: ThemeGeneratorSchema)
2828
'@mantine/modals': '^7.2.2',
2929
'@mantine/notifications': '^7.2.2',
3030
'@tabler/icons-react': '^2.42.0',
31+
clsx: '2.0.0',
3132
dayjs: '^1.11.10',
33+
'react-router-dom': '6.20.1',
3234
'timeago-react': '^3.0.6',
3335
},
3436
{

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)