Title: Expose a link's active and pending state as values, not only DOM attributes · Issue #803 · voidzero-dev/void · GitHub
Open Graph Title: Expose a link's active and pending state as values, not only DOM attributes · Issue #803 · voidzero-dev/void
X Title: Expose a link's active and pending state as values, not only DOM attributes · Issue #803 · voidzero-dev/void
Description: Version void 1.8.0, @void/react 1.8.0 Problem Link already works out three things about itself: current: it points to this page, so it gets aria-current="page" and data-void-active. active: with activeMatch="prefix", it points to this pa...
Open Graph Description: Version void 1.8.0, @void/react 1.8.0 Problem Link already works out three things about itself: current: it points to this page, so it gets aria-current="page" and data-void-active. active: with ac...
X Description: Version void 1.8.0, @void/react 1.8.0 Problem Link already works out three things about itself: current: it points to this page, so it gets aria-current="page" and data-void-active. activ...
Opengraph URL: https://github.com/voidzero-dev/void/issues/803
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Expose a link's active and pending state as values, not only DOM attributes","articleBody":"### Version\n\n`void` 1.8.0, `@void/react` 1.8.0\n\n### Problem\n\n`Link` already works out three things about itself:\n\n- **current**: it points to this page, so it gets `aria-current=\"page\"` and `data-void-active`.\n- **active**: with `activeMatch=\"prefix\"`, it points to this page or a parent of it, so it gets `data-void-active`.\n- **pending**: a visit to its destination is loading, so it gets `data-void-pending`.\n\nThat's enough to style the `\u003ca>` itself with CSS. But apps can only read that state as DOM attributes, so anything else has to repeat the matching:\n\n1. **A wrapper that takes the state as a prop.** shadcn's `SidebarMenuButton` renders the link through `render={\u003cLink />}` and styles itself, its icon and its tooltip from an `isActive` prop. `data-void-active` lands on the link, not on a prop.\n2. **Code that isn't a link.** A page header that shows the label of the active sidebar link, a breadcrumb, or a tab bar's sliding indicator all need the active link as a value.\n3. **Content inside the link.** Swapping an icon for a spinner while the link is pending, or a filled icon for an outline one while it's active, needs a CSS selector on the parent for each case.\n\nSo apps write their own check, and it drifts from `Link`'s rules:\n\n```tsx\nimport { Link, useNavigation, useRouter } from '@void/react';\n\n// Ignores the query and doesn't decode the path the way Link does.\nfunction isActive(path: string, href: string, home: string) {\n return path === href || (href !== home && path.startsWith(`${href}/`));\n}\n\nfunction SidebarItem({ href, label, home }: { href: string; label: string; home: string }) {\n const { path } = useRouter();\n const navigation = useNavigation();\n // Also compares the path only, so it misses the query, the method and the hash.\n const pending = navigation.state === 'loading' && navigation.location?.pathname === href;\n return (\n \u003cSidebarMenuButton isActive={isActive(path, href, home)} render={\u003cLink href={href} />}>\n {pending ? \u003cSpinner /> : \u003cIcon />}\n {label}\n \u003c/SidebarMenuButton>\n );\n}\n\nfunction PageTitle({ links, home }: { links: { href: string; label: string }[]; home: string }) {\n const { path } = useRouter();\n return \u003ch1>{links.find((link) => isActive(path, link.href, home))?.label}\u003c/h1>;\n}\n```\n\n### Proposal\n\n**1. A hook that takes the same values as `Link`.**\n\n```ts\nconst { current, active, pending } = useLinkStatus(href, { method, data, activeMatch });\n```\n\n- `current` is `true` when `Link` would set `aria-current`.\n- `active` is `true` when it would set `data-void-active`.\n- `pending` is `true` when it would set `data-void-pending`.\n\nThe options keep `Link`'s prop names, so the same values go to both. The result would be a `LinkStatus`, named like the `FormStatus` that `useForm()`'s state is built on, with the same `pending`.\n\nEvery adapter's `Link` already passes its router, navigation and resolved link to `linkActiveAttributes()` and `linkPendingAttributes()` from `void/pages-client`. The hook would be a thin wrapper around those two, with no new matching rules:\n\n```ts\nexport function useLinkStatus(href: string, { method, data, activeMatch }: LinkStatusOptions = {}) {\n const router = useContext(RouterContext);\n const navigation = useContext(NavigationContext);\n const link = useMemo(() => resolveLink({ href, method, data }), [href, method, data]);\n return useMemo(() => {\n const active = linkActiveAttributes(router, link, activeMatch);\n return {\n current: 'aria-current' in active,\n active: 'data-void-active' in active,\n pending: 'data-void-pending' in linkPendingAttributes(router, navigation, link),\n };\n }, [router, navigation, link, activeMatch]);\n}\n```\n\nThe example above becomes:\n\n```tsx\nfunction SidebarItem({ href, label }: { href: string; label: string }) {\n const { active, pending } = useLinkStatus(href, { activeMatch: 'prefix' });\n return (\n \u003cSidebarMenuButton isActive={active} render={\u003cLink href={href} activeMatch=\"prefix\" />}>\n {pending ? \u003cSpinner /> : \u003cIcon />}\n {label}\n \u003c/SidebarMenuButton>\n );\n}\n```\n\n**2. A render prop on `Link`'s children** that receives the same object, for content inside the link without the hook's repeated `href`:\n\n```tsx\n\u003cLink href=\"/admin/users\" activeMatch=\"prefix\">\n {({ active, pending }) => (pending ? \u003cSpinner /> : active ? \u003cUsersFilledIcon /> : \u003cUsersIcon />)}\n\u003c/Link>\n```\n\n**3. A plain function for matching several hrefs at once**, since hooks can't be called in a loop, as `PageTitle` above needs. It would take the router, so it works in any adapter:\n\n```tsx\nimport { isLinkActive } from 'void/pages-client';\n\nfunction PageTitle({ links }: { links: { href: string; label: string }[] }) {\n const router = useRouter();\n return \u003ch1>{links.find((link) => isLinkActive(router, link.href, { activeMatch: 'prefix' }))?.label}\u003c/h1>;\n}\n```\n\nThat one is the existing `linkActiveAttributes()` with a boolean result and an `href` instead of a resolved link. `isLinkPending(router, navigation, href)` would match it.\n\nVue, Solid and Svelte would each get (1) and (2) in their own form: a computed ref, an accessor, a store, and a scoped slot or snippet.\n\n### Elsewhere\n\n- **TanStack Router:** `useMatchRoute()` matches any route by `to`, with `fuzzy` for nested routes and `pending: true` for the location being loaded. `Link` passes `{ isActive, isTransitioning }` to its children, and `createLink()` lets a custom component be the link and receive `activeProps`.\n- **React Router:** `useMatch()` matches a pattern, and `NavLink` passes `{ isActive, isPending, isTransitioning }` to `className`, `style` and `children`.\n- **Vue Router:** `useLink()` returns `isActive` and `isExactActive`, the same split as `active` and `current` here.\n- **Next.js:** `useLinkStatus()` returns `{ pending }` inside a `Link`'s children. There's no active state, so apps compare `usePathname()` by hand.\n\n### Scope\n\n(1) covers wrappers and content inside the link; (3) covers matching a list of links; (2) is a convenience over (1). I'd start with (1) and (3), which share all their logic with `Link`, and add (2) if you want it. I'm happy to open a PR, with tests that check each value agrees with `Link`'s attributes for exact, prefix, query, method and pending cases.\n\nFollows up on #676.\n","author":{"url":"https://github.com/karthickeyan-dev","@type":"Person","name":"karthickeyan-dev"},"datePublished":"2026-11-12T10:02:38Z","url":"https://github.com/voidzero-dev/void/issues/803"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d1cd47f1-37a6-cdce-67d2-1911f4945865 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 95EC:C4D3F:13E9BB:1BC6E9:6AFB8BA2 |
| html-safe-nonce | 71f4d755af58d1af6479e9494b80a08562bca9fe67444d51755830cb41a3573a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NUVDOkM0RDNGOjEzRTlCQjoxQkM2RTk6NkFGQjhCQTIiLCJ2aXNpdG9yX2lkIjoiNzI3MTEzOTE4ODc2ODczODIxMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 29ac1ed2d7ced54be61584ffbae7bfc90e240f530be437704353208fe147fe10 |
| hovercard-subject-tag | issue:6347381022 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/voidzero-dev/void/803/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c73a3945bb9b17716e5c09bd59052a3e8e7f2af2a83026c9aecf26cb1d7df787/voidzero-dev/void/issues/803 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c73a3945bb9b17716e5c09bd59052a3e8e7f2af2a83026c9aecf26cb1d7df787/voidzero-dev/void/issues/803 |
| og:image:alt | Version void 1.8.0, @void/react 1.8.0 Problem Link already works out three things about itself: current: it points to this page, so it gets aria-current="page" and data-void-active. active: with ac... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | karthickeyan-dev |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9694464143465782ecae2a97650b2a2b0708d5c31c74f41ee86da57e315e2735 |
| turbo-cache-control | no-preview |
| go-import | github.com/voidzero-dev/void git https://github.com/voidzero-dev/void.git |
| octolytics-dimension-user_id | 149750581 |
| octolytics-dimension-user_login | voidzero-dev |
| octolytics-dimension-repository_id | 1164437080 |
| octolytics-dimension-repository_nwo | voidzero-dev/void |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1164437080 |
| octolytics-dimension-repository_network_root_nwo | voidzero-dev/void |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 05d967182208691fc7f2e6af52e82fa7fabf6bd5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width