diff --git a/packages/website/docs/examples.mdx b/packages/website/docs/examples.mdx index fbfde2eb..aa299e9e 100644 --- a/packages/website/docs/examples.mdx +++ b/packages/website/docs/examples.mdx @@ -129,6 +129,64 @@ function CustomHit({ hit }) { --- +## Opening links in new tabs + +By default, DocSearch opens search result links in the current window. If you want results to open in new tabs, you need to use both a custom `hitComponent` and the `navigator` prop to handle both click and keyboard navigation consistently. + +```jsx +// Custom hit component with target="_blank" +function HitWithNewTab({ hit, children }) { + return ( + + {children} + + ); +} + +// Navigator configuration to handle keyboard navigation +const newTabNavigator = { + navigate: ({ itemUrl }) => window.open(itemUrl, '_blank'), + navigateNewTab: ({ itemUrl }) => window.open(itemUrl, '_blank'), + navigateNewWindow: ({ itemUrl }) => window.open(itemUrl, '_blank'), +}; + + +``` + + ( + + {children} + + )} + navigator={{ + navigate: ({ itemUrl }) => window.open(itemUrl, '_blank'), + navigateNewTab: ({ itemUrl }) => window.open(itemUrl, '_blank'), + navigateNewWindow: ({ itemUrl }) => window.open(itemUrl, '_blank'), + }} + insights={true} + translations={{ button: { buttonText: 'open in new tabs (demo)' } }} +/> + +

+ +:::warning +**Note**: Using only `hitComponent` with `target="_blank"` will work for mouse clicks, but keyboard navigation (arrows + Enter) requires the `navigator` prop to consistently open links in new tabs. +::: + +--- + ## Bring-your-own-data shape with `transformItems` Docsearch is not limited to docsearch-like records. use `transformItems` to adapt any record shape into the internal structure docsearch expects. this lets you build search for apps, help centers, changelogs, or any custom content.