-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace broadcast channel with storybook channel
- Loading branch information
1 parent
f0706a4
commit f825f53
Showing
16 changed files
with
102 additions
and
159 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
8 changes: 6 additions & 2 deletions
8
examples/storybook-custom-docs-pages/.storybook/page-simple.tsx
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,10 +1,14 @@ | ||
import React from 'react'; | ||
import { CustomPageDef } from '@component-controls/storybook-custom-docs'; | ||
import { CustomPageDef, useStoryId } from '@component-controls/storybook-custom-docs'; | ||
|
||
const CustomPage: React.FC = () => { | ||
const storyId = useStoryId(); | ||
return <div><h1>Simple docs page</h1><p>{storyId}</p></div> | ||
} | ||
const page: CustomPageDef = { | ||
key: 'custom', | ||
title: 'Simple Page', | ||
render: ({ active, storyId }) => active ? <div><h1>Simple docs page</h1><p>{storyId}</p></div> : null, | ||
render: ({ active }) => active ? <CustomPage /> : null, | ||
} | ||
|
||
export default page; |
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
78 changes: 25 additions & 53 deletions
78
misc/storybook-custom-docs/src/components/ManagerContainer.tsx
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,67 +1,39 @@ | ||
import React from 'react'; | ||
import { BroadcastChannel } from 'broadcast-channel'; | ||
import { API } from '@storybook/api'; | ||
|
||
interface ManagerContainerProps { | ||
active?: boolean; | ||
id: string; | ||
api: API; | ||
route: string; | ||
title: string; | ||
} | ||
const activePages: string[] = []; | ||
export const ManagerContainer: React.FC<ManagerContainerProps> = ({ | ||
active, | ||
id, | ||
api, | ||
title, | ||
}) => { | ||
const channel = React.useMemo( | ||
() => | ||
new BroadcastChannel(`attach_docs_page_${title}`, { | ||
type: 'localstorage', | ||
}), | ||
[], | ||
); | ||
|
||
export const ManagerContainer: React.FC<ManagerContainerProps> = props => { | ||
const { active, api, route } = props; | ||
const ATTACH_DOCS_PAGE = `attach_docs_page_${route}`; | ||
const REQUEST_DOCS_PAGE = `request_docs_page_${route}`; | ||
const channel = React.useMemo(() => api.getChannel(), []); | ||
const sendMessage = () => { | ||
const story = api.getCurrentStoryData(); | ||
channel.emit(ATTACH_DOCS_PAGE, { active, storyId: story?.id }); | ||
}; | ||
|
||
React.useEffect(() => { | ||
const iframe = document.getElementById( | ||
'storybook-preview-iframe', | ||
) as HTMLIFrameElement; | ||
const wrapper = document.getElementById('storybook-preview-wrapper'); | ||
if (iframe && iframe.contentDocument) { | ||
const updateDOM = () => { | ||
const story = api.getCurrentStoryData(); | ||
channel.postMessage({ id: id, active, storyId: story?.id }); | ||
const root = iframe?.contentDocument?.getElementById('root'); | ||
if (wrapper && root) { | ||
const pageIndex = activePages.indexOf(id); | ||
if (active) { | ||
if (pageIndex < 0) { | ||
activePages.push(id); | ||
} | ||
root.style.setProperty('display', 'none'); | ||
wrapper.removeAttribute('hidden'); | ||
} else if (pageIndex >= 0) { | ||
activePages.splice(pageIndex, 1); | ||
if (activePages.length === 0) { | ||
root.style.removeProperty('display'); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
if (!iframe.contentDocument.getElementById('root')) { | ||
const saveOnLoad = iframe.onload; | ||
iframe.onload = e => { | ||
updateDOM(); | ||
if (saveOnLoad) { | ||
//@ts-ignore | ||
saveOnLoad(e); | ||
} | ||
}; | ||
} else { | ||
updateDOM(); | ||
const onRequestPage = () => { | ||
sendMessage(); | ||
}; | ||
channel.on(REQUEST_DOCS_PAGE, onRequestPage); | ||
const updateDOM = () => { | ||
sendMessage(); | ||
if (wrapper) { | ||
wrapper.removeAttribute('hidden'); | ||
} | ||
} | ||
}; | ||
updateDOM(); | ||
return () => { | ||
channel.off(REQUEST_DOCS_PAGE, onRequestPage); | ||
}; | ||
}, [active]); | ||
return null; | ||
}; |
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
Oops, something went wrong.