Files
Alaister YoungandAlaister Young d6e4089b1a feat(common): add FrameworkProvider and remove next imports (#53905)
Introduces a lightweight `FrameworkProvider` React context in
`packages/common` (`Link`, `Image`, `pathname`, `asPath`, `isReady`,
`params`, `push`), wires it up in the consuming apps, and removes the
`next` dependency from `packages/common`.

**Added:**

- `FrameworkProvider`, `useFramework`, `FrameworkLink`, and
`FrameworkImage` in `packages/common/framework.tsx`, with plain HTML
defaults (`DefaultFrameworkLink` / `DefaultFrameworkImage`).
`useFramework().push` always exists and falls back to
`window.location.assign`
- App-level framework providers wiring `next/link`, `next/image`, and
router state in `www` (App + Pages routers), `docs`, `design-system`,
`learn`, and `ui-library`, and native TanStack Router bindings in
`studio` (`StudioFrameworkProvider`)
- `StudioFrameworkProvider` reuses the common defaults (its image only
applies the basePath) and reports `isReady` once the first route load
commits, so the first page view carries the final route after any
redirect
- Unit tests for `StudioFrameworkProvider` (`useParams`,
`FrameworkLink`, `FrameworkImage`, query/hash-only navigation,
`isReady`) and for `PageTelemetry` route-change sends and the
`TelemetryTagManager` consent gate

**Changed:**

- `common/hooks/useParams.ts`: read params from `useFramework()` instead
of `next/compat/router`
- `common/telemetry.tsx`: replace `next/script` in `TelemetryTagManager`
with `useEffect` script injection, and read route state from
`useFramework()` in `PageTelemetry`
- `common/MetaFavicons/app-router.ts`: drop type-only `Metadata` import
from `next`
- Moved `MetaFaviconsPagesRouter` to
`apps/www/components/MetaFaviconsPagesRouter.tsx` (its sole consumer)
- Studio: `toRoutePattern` and `applyBasePath` now live once in
`lib/internal-url.ts`, shared by the provider and the `compat/next`
shims
- `PageTelemetry` throws outside production when no `FrameworkProvider`
is mounted, since it would otherwise send only the landing pageview.
`useFramework()` itself still falls back to the defaults
- `ui-library`: renamed its code-sample framework context to
`FrameworkPreferenceProvider` / `useFrameworkPreference` so
`FrameworkProvider` only means the `common` one

**Removed:**

- `learn`'s unused copy of that context (`useCourse` had no callers)

- `next` from `packages/common/package.json` peerDependencies

App Router adapters set `asPath` to the pathname only, same as the
`usePathname`-based telemetry on master (reading search params at the
root would opt static pages out of static rendering).

## To test

- Run `packages/common` tests and typecheck, and `apps/studio`
`StudioFrameworkProvider` / `route-render` / `telemetry` tests
- Navigate between routes in Studio, Docs, and WWW and confirm page
telemetry fires once per navigation (and does not duplicate on hash-only
changes)
- Load Studio at `/` (which redirects) and confirm the first page event
has the final route
- Verify URL params (`useParams`) continue resolving route and search
params in Studio

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-25 20:13:22 +08:00

83 lines
2.6 KiB
TypeScript

'use client'
import { useEffect, useRef, type ReactNode } from 'react'
import { cn, Tabs, TabsContent, TabsList, TabsTrigger } from 'ui'
import { frameworkTitles } from '@/config/docs'
import { useFrameworkPreference } from '@/context/framework-preference-context'
export function FrameworkQuickstart({ children }: { children: ReactNode }) {
const { framework, setFramework } = useFrameworkPreference()
const tabStripRef = useRef<HTMLDivElement>(null)
const activeTabRef = useRef<HTMLButtonElement>(null)
useEffect(() => {
const strip = tabStripRef.current
const tab = activeTabRef.current
if (!strip || !tab) return
const stripBounds = strip.getBoundingClientRect()
const tabBounds = tab.getBoundingClientRect()
if (tabBounds.left < stripBounds.left) {
strip.scrollLeft += tabBounds.left - stripBounds.left
} else if (tabBounds.right > stripBounds.right) {
strip.scrollLeft += tabBounds.right - stripBounds.right
}
}, [framework])
return (
<Tabs value={framework} onValueChange={setFramework} className="w-full">
<div ref={tabStripRef} className="overflow-x-auto">
<TabsList aria-label="Framework setup guides" className="w-max min-w-full gap-0">
{Object.entries(frameworkTitles).map(([value, label]) => (
<TabsTrigger
key={value}
ref={value === framework ? activeTabRef : undefined}
value={value}
className="shrink-0 px-3 py-3 text-xs font-medium data-[state=active]:border-brand"
>
{label}
</TabsTrigger>
))}
</TabsList>
</div>
{children}
</Tabs>
)
}
export function FrameworkQuickstartTab({
value,
children,
}: {
value: string
children: ReactNode
}) {
return (
<TabsContent value={value} className="mt-8 [&>h2]:sr-only">
{children}
</TabsContent>
)
}
export function QuickstartStep({ number, children }: { number: string; children: ReactNode }) {
return (
<section
className={cn(
'grid grid-cols-[1.75rem_minmax(0,1fr)] gap-4 border-t py-6',
number === '1' && 'border-t-0 pt-0'
)}
>
<span
aria-hidden="true"
className="flex size-7 items-center justify-center rounded-full border font-mono text-xs text-foreground-lighter"
>
{number}
</span>
<div className="min-w-0 [&_h3]:m-0 [&_h3]:font-sans [&_h3]:text-sm [&_h3]:font-semibold [&_p]:mt-2! [&_p]:text-sm [&_p]:leading-6 [&_pre]:mt-4 [&_pre]:mb-0 [&_pre]:rounded-md [&_pre_code]:text-xs">
{children}
</div>
</section>
)
}