Skip to content

Commit a221899

Browse files
authored
Highlight NavLink when on create form (#2066)
* Highlight NavLink when on create form
1 parent 641ebe8 commit a221899

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

app/components/Sidebar.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright Oxide Computer Company
77
*/
88
import cn from 'classnames'
9-
import { NavLink } from 'react-router-dom'
9+
import { NavLink, useLocation } from 'react-router-dom'
1010

1111
import { Action16Icon, Document16Icon } from '@oxide/design-system/icons/react'
1212

@@ -88,20 +88,24 @@ export const NavLinkItem = (props: {
8888
children: React.ReactNode
8989
end?: boolean
9090
disabled?: boolean
91-
}) => (
92-
<li>
93-
<NavLink
94-
to={props.to}
95-
className={({ isActive }) =>
96-
cn(linkStyles, {
97-
'text-accent !bg-accent-secondary hover:!bg-accent-secondary-hover svg:!text-accent-tertiary':
98-
isActive,
99-
'pointer-events-none text-disabled': props.disabled,
100-
})
101-
}
102-
end={props.end}
103-
>
104-
{props.children}
105-
</NavLink>
106-
</li>
107-
)
91+
}) => {
92+
// If the current page is the create form for this NavLinkItem's resource, highlight the NavLink in the sidebar
93+
const currentPathIsCreateForm = useLocation().pathname.startsWith(`${props.to}-new`)
94+
return (
95+
<li>
96+
<NavLink
97+
to={props.to}
98+
className={({ isActive }) =>
99+
cn(linkStyles, {
100+
'text-accent !bg-accent-secondary hover:!bg-accent-secondary-hover svg:!text-accent-tertiary':
101+
isActive || currentPathIsCreateForm,
102+
'pointer-events-none text-disabled': props.disabled,
103+
})
104+
}
105+
end={props.end}
106+
>
107+
{props.children}
108+
</NavLink>
109+
</li>
110+
)
111+
}

0 commit comments

Comments
 (0)