-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(storylite): use context instead of virtual modules
- Loading branch information
Showing
23 changed files
with
231 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from 'react' | ||
|
||
import { StoryLiteAppConfig, StoryModulesMap } from '..' | ||
|
||
export type StoryliteDataContextType = { | ||
config?: StoryLiteAppConfig | ||
stories: StoryModulesMap | ||
} | ||
|
||
export const StoryliteDataContext = React.createContext<StoryliteDataContextType | undefined>( | ||
undefined, | ||
) | ||
|
||
export const useStoryliteDataContext = () => { | ||
const context = React.useContext(StoryliteDataContext) | ||
if (!context) { | ||
throw new Error('useStoryliteDataContext must be used within a StoryliteDataProvider') | ||
} | ||
|
||
return context | ||
} | ||
|
||
export const useStoryliteData = (): Required<StoryliteDataContextType> => { | ||
const { config, stories } = useStoryliteDataContext() | ||
|
||
if (!config) { | ||
throw new Error( | ||
'useStoryliteData must be used within a StoryliteDataProvider. config is undefined.', | ||
) | ||
} | ||
|
||
return { config, stories } | ||
} | ||
|
||
export const useStoryliteConfig = (): StoryLiteAppConfig => { | ||
const { config } = useStoryliteData() | ||
|
||
return config | ||
} | ||
|
||
export const useStoryliteStories = (): StoryModulesMap => { | ||
const { stories } = useStoryliteData() | ||
|
||
return stories | ||
} | ||
|
||
const defaultConfig: StoryLiteAppConfig = { | ||
title: 'Storylite', | ||
defaultStory: 'index', | ||
stylesheets: [], | ||
} | ||
|
||
export const StoryliteDataProvider = ({ | ||
config, | ||
stories, | ||
children, | ||
}: { | ||
config?: StoryLiteAppConfig | ||
stories: StoryModulesMap | ||
children: React.ReactNode | ||
}) => { | ||
const mergedConfig: StoryLiteAppConfig = { ...defaultConfig, ...config } | ||
|
||
return ( | ||
<StoryliteDataContext.Provider value={{ config: mergedConfig, stories }}> | ||
{children} | ||
</StoryliteDataContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import './styles/index.css' | ||
|
||
import StoryLiteRouter from './router' | ||
import StoryLiteApp from './app' | ||
|
||
export * from './types' | ||
|
||
export { StoryLiteRouter } | ||
export { StoryLiteApp } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import userConfig from 'storylite-user-config' | ||
|
||
import { Story } from '@/components/Story' | ||
import { useStoryliteConfig } from '@/context/StoriesDataContext' | ||
|
||
import SandboxLayout from '../../layouts/SandboxLayout' | ||
|
||
export default function StoryPage() { | ||
return <Story story={userConfig.defaultStory || 'index'} exportName={'Main'} /> | ||
const config = useStoryliteConfig() | ||
|
||
return <Story story={config.defaultStory || 'index'} exportName={'Main'} /> | ||
} | ||
|
||
export const Layout = SandboxLayout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import removeTestIdPlugin from './removeTestIdPlugin' | ||
import storylitePlugin from './storylitePlugin' | ||
import removeTestIdPlugin from './vite-plugin-remove-testid' | ||
import storylitePlugin from './vite-plugin-storylite' | ||
|
||
export { removeTestIdPlugin, storylitePlugin } |
Oops, something went wrong.