Skip to content

Commit

Permalink
fix: only use window.parent and fall back to window to prevent manage…
Browse files Browse the repository at this point in the history
…r from throwing
  • Loading branch information
JeremyRH committed Jun 25, 2024
1 parent 3d24276 commit cc48522
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions src/createStore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// This provides shared state and the ability to subscribe to changes
// between the manager and preview iframes.
// Attempted to use Storybook's `addons.getChannel()` but it doesn't emit
// across iframes.

type Callback<T> = (newValue: T) => any;

interface Store<T> {
Expand Down Expand Up @@ -48,12 +53,12 @@ function newKeyStore<T>(): KeyStore<T> {
}

export function createStore<T>(): KeyStore<T> {
const getStore = (managerWindow: any) =>
(managerWindow._addon_code_editor_store ||= newKeyStore());
try {
return ((window.top as any)._addon_code_editor_store ||= newKeyStore());
// This will throw in the manager if the storybook site is in an iframe.
return getStore(window.parent);
} catch {
// Storybook sites can be embedded in iframes. Using window.top will fail in that case.
// Try window.parent as a fallback. This can break if Storybook changes how previews are rendered.
// TODO: Use Storybook Channels to communicate between the manager and preview.
return ((window.parent as any)._addon_code_editor_store ||= newKeyStore());
return getStore(window);
}
}

0 comments on commit cc48522

Please sign in to comment.