Is Node Active
Checks if a slug is part of the currently active route.
Returns true if the given slug is equal to or an ancestor of currentSlug. Useful for highlighting active and expanded items in sidebars.
import { isNodeActive } from 'slugtree'
isNodeActive(['guides'], ['guides', 'routing']) // true — ancestor
isNodeActive(['guides', 'routing'], ['guides', 'routing']) // true — exact match
isNodeActive(['api'], ['guides', 'routing']) // false — different branchParameters
| Parameter | Type | Description |
|---|---|---|
slug | string | string[] | The slug of the node to test. |
currentSlug | string | string[] | The slug of the currently active page. |
Return value
Returns boolean. Returns false if either argument is empty.
Usage
Typical sidebar usage — apply an active style to both the current page and any expanded folder ancestors:
import { isNodeActive } from 'slugtree'
function NavItem({
node,
currentSlug
}: {
node: { slug: string[]; href: string; title: string }
currentSlug: string[]
}) {
const active = isNodeActive(node.slug, currentSlug)
return (
<a href={node.href} data-active={active}>
{node.title}
</a>
)
}To test specifically whether a descendant of a folder is active (excluding the folder itself), use isNodeChildrenActive(). For client components, use useIsNodeActive().