diff --git a/.changeset/webframe-navigate-query.md b/.changeset/webframe-navigate-query.md new file mode 100644 index 000000000..e28f04109 --- /dev/null +++ b/.changeset/webframe-navigate-query.md @@ -0,0 +1,6 @@ +--- +"gitbook": patch +"@gitbook/react-contentkit": patch +--- + +Support query params in the `@webframe.navigate` action. diff --git a/packages/gitbook/src/components/DocumentView/Integration/ContentKitWithClientContext.tsx b/packages/gitbook/src/components/DocumentView/Integration/ContentKitWithClientContext.tsx index a00cc9470..04a103be5 100644 --- a/packages/gitbook/src/components/DocumentView/Integration/ContentKitWithClientContext.tsx +++ b/packages/gitbook/src/components/DocumentView/Integration/ContentKitWithClientContext.tsx @@ -12,6 +12,9 @@ import { type GitBookLinker, createLinker } from '@/lib/links'; type ContentKitProps = React.ComponentProps>; +// Query params a webframe may set when navigating: the search/ask widget params (see `useSearch`). +const ALLOWED_NAVIGATE_QUERY_PARAMS = ['q', 'ask', 'scope', 'section']; + /** Serializable inputs to rebuild the tested linker on the client (functions can't cross the RSC boundary). */ export type WebframeLinkerData = Pick< Parameters[0], @@ -62,11 +65,17 @@ export function ContentKitWithClientContext( ? () => ({ visitor: visitorClaims?.visitor ?? null }) : undefined, getPageContext: page ? () => ({ page }) : undefined, - navigate: ({ path, anchor }) => { + navigate: ({ path, anchor, query }) => { // Resolve the requested path relative to the site root so a webframe can navigate // to any section or space within the site (and nowhere outside it). - const suffix = anchor ? `#${anchor}` : ''; - navigateTo(linker.toPathInSite(path) + suffix); + const params = new URLSearchParams( + Object.entries(query ?? {}).filter(([key]) => + ALLOWED_NAVIGATE_QUERY_PARAMS.includes(key) + ) + ).toString(); + const search = params ? `?${params}` : ''; + const hash = anchor ? `#${anchor}` : ''; + navigateTo(linker.toPathInSite(path) + search + hash); }, }), [canAccessVisitorClaims, visitorClaims, page, linker, navigateTo] diff --git a/packages/react-contentkit/src/ElementWebframe.tsx b/packages/react-contentkit/src/ElementWebframe.tsx index 3682d0ab5..57356c843 100644 --- a/packages/react-contentkit/src/ElementWebframe.tsx +++ b/packages/react-contentkit/src/ElementWebframe.tsx @@ -138,6 +138,7 @@ export function ElementWebframe(props: ContentKitClientElementProps void; + navigate?: (target: { path: string; anchor?: string; query?: Record }) => void; }; export interface ContentKitClientContextType {