Skip to content

feat(nextjs): functional App Router adapter (replaces stub)#30

Open
Priyans-hu wants to merge 3 commits into
mainfrom
feat/nextjs-adapter
Open

feat(nextjs): functional App Router adapter (replaces stub)#30
Priyans-hu wants to merge 3 commits into
mainfrom
feat/nextjs-adapter

Conversation

@Priyans-hu
Copy link
Copy Markdown
Owner

The Next.js adapter in src/adapters/nextjs/index.ts previously threw "not yet implemented", despite README + keywords + comparison table claiming Next.js support. This PR ships a real adapter that mirrors the React Router adapter's shape.

What's included

  • useNextNavigate() — wraps next/navigation useRouter for command actions (supports replace and scroll options)
  • useNextPrefetch() / usePrefetchOnHover() — hover prefetch via router.prefetch(href)
  • useNextCommandRoutes(routes, options?) / <NextCommandRoutes routes={...} /> — registers build-time-scanned routes as commands; each command's action calls router.push(href)
  • next declared as an optional peer dep (alongside react/react-dom) — not bundled
  • New entry points: cmdk-engine/nextjs (alias cmdk-engine/adapters/nextjs)

How to use

'use client'
import { useNextCommandRoutes } from 'cmdk-engine/nextjs'
import sitemap from '@/generated/command-routes.json'

export function CommandRoutes() {
  useNextCommandRoutes(sitemap)
  return null
}

The hook accepts the Sitemap produced by cmdk-engine scan, a SitemapRoute[], hand-authored NextRouteLike[], or any { routes: [...] }-shaped object. Options: prefetchOnMount, defaultGroup, transform.

Tests

+17 tests under tests/adapters/nextjs.test.tsx mocking next/navigation. Total moves 196 → 213. Lint, typecheck, test, and build all pass.

Verification

bun install
bun run lint && bun run typecheck && bun run test && bun run build
ls dist/adapters/nextjs/index.js   # ~4.3KB ESM

Migration

None — adapter was non-functional before.

Priyans-hu added 3 commits May 4, 2026 01:05
Replace the stub that threw "not yet implemented" with a working
adapter mirroring the React Router adapter shape.

- useNextNavigate(): wraps next/navigation useRouter for command actions
- useNextPrefetch(): exposes router.prefetch
- useNextCommandRoutes(routes): registers a sitemap as command items
  with router.push wired into each command's action
- <NextCommandRoutes routes={...} />: HOC variant
- usePrefetchOnHover(): factory for hover-prefetch handlers
- Accepts NextRouteLike[], Sitemap, or { routes: [...] } as input
- Optional prefetchOnMount, defaultGroup, transform options

next is added as an optional peer dep alongside react/react-dom.
Both cmdk-engine/adapters/nextjs and cmdk-engine/nextjs entry points
are exported from package.json. Bundle is ~4.2KB ESM.
Mock next/navigation useRouter to test:
- useNextNavigate calls router.push (and router.replace when replace:true)
- useNextPrefetch calls router.prefetch
- useNextCommandRoutes registers commands derived from input
- prefetchOnMount calls prefetch for every route on mount
- transform option overrides derived defaults
- defaultGroup falls in when no group is set
- <NextCommandRoutes /> HOC delegates to the hook
- usePrefetchOnHover returns a handler factory

17 new tests, total moves 196 -> 213.
- Add a "Next.js (App Router) Integration" section between
  "React Router Integration" and "RBAC".
- Update package entry point table and adapter quick-reference.
- Add changeset entry under .changeset/nextjs-adapter.md as a
  minor bump for cmdk-engine.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant