11import type { Preview } from '@storybook/react-vite' ;
2+ import {
3+ createMemoryHistory ,
4+ createRootRoute ,
5+ createRouter ,
6+ RouterProvider ,
7+ } from '@tanstack/react-router' ;
28import { useDarkMode } from '@vueless/storybook-dark-mode' ;
3- import { StrictMode , useEffect } from 'react' ;
4- import { ReactNode } from 'react' ;
9+ import type { ReactNode } from 'react' ;
10+ import { createContext , StrictMode , useContext , useEffect , useMemo } from 'react' ;
511import { useTranslation } from 'react-i18next' ;
612import { StoryContext } from 'storybook/internal/csf' ;
713
@@ -15,6 +21,33 @@ import {
1521import i18nGlobal from '../src/lib/i18n/index' ;
1622import { Providers } from '../src/providers' ;
1723
24+ const StorybookStorySlotContext = createContext < ReactNode > ( null ) ;
25+
26+ function StorybookStorySlotRoot ( ) {
27+ const slot = useContext ( StorybookStorySlotContext ) ;
28+ return < > { slot } </ > ;
29+ }
30+
31+ /** Minimal router so components using useRouter() work in Storybook. */
32+ function StorybookTanStackRouter ( { children } : { children : ReactNode } ) {
33+ const router = useMemo ( ( ) => {
34+ const rootRoute = createRootRoute ( {
35+ component : StorybookStorySlotRoot ,
36+ } ) ;
37+ const routeTree = rootRoute . addChildren ( [ ] ) ;
38+ return createRouter ( {
39+ routeTree,
40+ history : createMemoryHistory ( { initialEntries : [ '/' ] } ) ,
41+ } ) ;
42+ } , [ ] ) ;
43+
44+ return (
45+ < StorybookStorySlotContext . Provider value = { children } >
46+ < RouterProvider router = { router } />
47+ </ StorybookStorySlotContext . Provider >
48+ ) ;
49+ }
50+
1851const DocumentationWrapper = ( {
1952 children,
2053 context,
@@ -77,12 +110,14 @@ const preview: Preview = {
77110 return (
78111 < Providers forcedTheme = { isDarkMode ? 'dark' : 'light' } >
79112 < StrictMode >
80- < DocumentationWrapper context = { context } >
81- { /* Calling as a function to avoid errors. Learn more at:
82- * https://github.com/storybookjs/storybook/issues/15223#issuecomment-1092837912
83- */ }
84- { story ( context ) }
85- </ DocumentationWrapper >
113+ < StorybookTanStackRouter >
114+ < DocumentationWrapper context = { context } >
115+ { /* Calling as a function to avoid errors. Learn more at:
116+ * https://github.com/storybookjs/storybook/issues/15223#issuecomment-1092837912
117+ */ }
118+ { story ( context ) }
119+ </ DocumentationWrapper >
120+ </ StorybookTanStackRouter >
86121 </ StrictMode >
87122 </ Providers >
88123 ) ;
0 commit comments