Skip to content

Commit

Permalink
Merge pull request #20083 from storybookjs/tom/sb-466-external-docs-e…
Browse files Browse the repository at this point in the history
…xports-from-2

Fix SSR in external docs via `node` exports
  • Loading branch information
ndelangen authored Dec 5, 2022
2 parents 8a076f6 + ee32804 commit b2e0a8a
Show file tree
Hide file tree
Showing 10 changed files with 1,114 additions and 1,217 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test-results
/sandbox
/bench
.verdaccio-cache
.next

# Yarn stuff
/**/.yarn/*
Expand Down
1 change: 1 addition & 0 deletions code/addons/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"license": "MIT",
"exports": {
".": {
"node": "./dist/index.js",
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
Expand Down
1 change: 1 addition & 0 deletions code/addons/docs/preview.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// eslint-disable-next-line import/export
export * from './dist/preview';
17 changes: 9 additions & 8 deletions code/addons/docs/src/DocsRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import ReactDOM from 'react-dom';
import type { Renderer, Parameters, DocsContextProps, DocsRenderFunction } from '@storybook/types';
import { Docs, CodeOrSourceMdx, AnchorMdx, HeadersMdx } from '@storybook/blocks';
import { MDXProvider } from '@mdx-js/react';

// TS doesn't like that we export a component with types that it doesn't know about (TS4203)
export const defaultComponents: Record<string, any> = {
Expand Down Expand Up @@ -31,13 +30,15 @@ export class DocsRenderer<TRenderer extends Renderer> {
...docsParameter?.components,
};

ReactDOM.render(
<MDXProvider components={components}>
<Docs key={Math.random()} context={context} docsParameter={docsParameter} />
</MDXProvider>,
element,
callback
);
import('@mdx-js/react').then(({ MDXProvider }) => {
ReactDOM.render(
<MDXProvider components={components}>
<Docs key={Math.random()} context={context} docsParameter={docsParameter} />
</MDXProvider>,
element,
callback
);
});
};

this.unmount = (element: HTMLElement) => {
Expand Down
1 change: 1 addition & 0 deletions code/addons/essentials/src/docs/preview.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// eslint-disable-next-line import/export
export * from '@storybook/addon-docs/dist/preview';
1 change: 1 addition & 0 deletions code/lib/theming/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"sideEffects": false,
"exports": {
".": {
"node": "./dist/index.js",
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
Expand Down
1 change: 1 addition & 0 deletions code/ui/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"sideEffects": false,
"exports": {
".": {
"node": "./dist/index.js",
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
Expand Down
20 changes: 10 additions & 10 deletions code/ui/components/src/Zoom/ZoomElement.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global MutationObserver */
import type { ReactElement, RefObject } from 'react';
import React, { useEffect, useRef, useState, useMemo, useCallback } from 'react';
import React, { useEffect, useRef, useState, useCallback } from 'react';
import { styled } from '@storybook/theming';
import { browserSupportsCssZoom } from './browserSupportsCssZoom';

Expand Down Expand Up @@ -29,20 +29,20 @@ const useMutationObserver = ({
options: MutationObserverInit;
callback: MutationCallback;
}): void => {
const observer = useMemo(
() =>
new MutationObserver((mutationRecord, mutationObserver) => {
callback(mutationRecord, mutationObserver);
}),
[callback]
);
const observer = useRef<MutationObserver>();

useEffect(() => {
if (!observer.current) {
observer.current = new MutationObserver((mutationRecord, mutationObserver) => {
callback(mutationRecord, mutationObserver);
});
}

if (element?.current) {
observer.observe(element.current, options);
observer.current.observe(element.current, options);
}

return () => observer.disconnect();
return () => observer.current.disconnect();
}, [element, observer, options]);
};

Expand Down
9 changes: 4 additions & 5 deletions test-storybooks/external-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"@storybook/addon-essentials": "7.0.0-alpha.43",
"@storybook/blocks": "7.0.0-alpha.43",
"@storybook/components": "7.0.0-alpha.43",
"@storybook/csf": "0.0.2--canary.53.4879818.0",
"@storybook/preview-web": "7.0.0-alpha.43",
"@storybook/react": "7.0.0-alpha.43",
"@storybook/react-webpack5": "7.0.0-alpha.43",
Expand Down Expand Up @@ -77,8 +76,8 @@
"@storybook/web-components-vite": "portal:../../code/frameworks/web-components-vite",
"@storybook/web-components-webpack5": "portal:../../code/frameworks/web-components-webpack5",
"@storybook/addons": "portal:../../code/lib/addons",
"@storybook/api": "portal:../../code/lib/api",
"@storybook/blocks": "portal:../../code/lib/blocks",
"@storybook/manager-api": "portal:../../code/lib/manager-api",
"@storybook/blocks": "portal:../../code/ui/blocks",
"@storybook/builder-manager": "portal:../../code/lib/builder-manager",
"@storybook/builder-vite": "portal:../../code/lib/builder-vite",
"@storybook/builder-webpack5": "portal:../../code/lib/builder-webpack5",
Expand All @@ -91,7 +90,7 @@
"@storybook/client-api": "portal:../../code/lib/client-api",
"@storybook/client-logger": "portal:../../code/lib/client-logger",
"@storybook/codemod": "portal:../../code/lib/codemod",
"@storybook/components": "portal:../../code/lib/components",
"@storybook/components": "portal:../../code/ui/components",
"@storybook/core-client": "portal:../../code/lib/core-client",
"@storybook/core-common": "portal:../../code/lib/core-common",
"@storybook/core-events": "portal:../../code/lib/core-events",
Expand All @@ -102,7 +101,7 @@
"@storybook/instrumenter": "portal:../../code/lib/instrumenter",
"@storybook/node-logger": "portal:../../code/lib/node-logger",
"@storybook/postinstall": "portal:../../code/lib/postinstall",
"@storybook/preview-web": "portal:../../code/lib/preview-web",
"@storybook/preview-api": "portal:../../code/lib/preview-api",
"@storybook/router": "portal:../../code/lib/router",
"@storybook/source-loader": "portal:../../code/lib/source-loader",
"@storybook/store": "portal:../../code/lib/store",
Expand Down
Loading

0 comments on commit b2e0a8a

Please sign in to comment.