diff --git a/addons/docs/package.json b/addons/docs/package.json index db90e2f29e7c..3a8a9d80a7d9 100644 --- a/addons/docs/package.json +++ b/addons/docs/package.json @@ -49,6 +49,7 @@ "@storybook/addons": "5.3.0-rc.12", "@storybook/api": "5.3.0-rc.12", "@storybook/components": "5.3.0-rc.12", + "@storybook/core-events": "5.3.0-rc.12", "@storybook/csf": "0.0.1", "@storybook/postinstall": "5.3.0-rc.12", "@storybook/source-loader": "5.3.0-rc.12", diff --git a/addons/docs/src/blocks/DocsContainer.tsx b/addons/docs/src/blocks/DocsContainer.tsx index cedf8e18f4de..bc536a2361c2 100644 --- a/addons/docs/src/blocks/DocsContainer.tsx +++ b/addons/docs/src/blocks/DocsContainer.tsx @@ -29,7 +29,12 @@ export const DocsContainer: FunctionComponent = ({ context, const allComponents = { ...defaultComponents, ...userComponents }; useEffect(() => { - const url = new URL(window.parent.location); + let url; + try { + url = new URL(window.parent.location); + } catch (err) { + return; + } if (url.hash) { const element = document.getElementById(url.hash.substring(1)); if (element) { diff --git a/addons/docs/src/blocks/mdx.tsx b/addons/docs/src/blocks/mdx.tsx index ca5225602199..473ee8a57ee8 100644 --- a/addons/docs/src/blocks/mdx.tsx +++ b/addons/docs/src/blocks/mdx.tsx @@ -1,11 +1,11 @@ import React, { FC, SyntheticEvent } from 'react'; +import addons from '@storybook/addons'; import { Source } from '@storybook/components'; +import { NAVIGATE_URL } from '@storybook/core-events'; import { Code, components } from '@storybook/components/html'; -import { document, window } from 'global'; -import { isNil } from 'lodash'; +import { document } from 'global'; import { styled } from '@storybook/theming'; import { DocsContext, DocsContextProps } from './DocsContext'; -import { scrollToElement } from './utils'; // Hacky utility for asserting identifiers in MDX Story elements export const assertIsFn = (val: any) => { @@ -48,11 +48,8 @@ export const CodeOrSourceMdx: FC = ({ className, children, ); }; -function generateHrefWithHash(hash: string): string { - const url = new URL(window.parent.location); - const href = `${url.origin}/${url.search}#${hash}`; - - return href; +function navigate(url: string) { + addons.getChannel().emit(NAVIGATE_URL, url); } // @ts-ignore @@ -65,14 +62,12 @@ interface AnchorInPageProps { const AnchorInPage: FC = ({ hash, children }) => ( { - event.preventDefault(); - - const hashValue = hash.substring(1); - const element = document.getElementById(hashValue); - if (!isNil(element)) { - window.parent.history.replaceState(null, '', generateHrefWithHash(hashValue)); - scrollToElement(element); + const id = hash.substring(1); + const element = document.getElementById(id); + if (element) { + navigate(hash); } }} > @@ -88,7 +83,7 @@ interface AnchorMdxProps { export const AnchorMdx: FC = props => { const { href, target, children, ...rest } = props; - if (!isNil(href)) { + if (href) { // Enable scrolling for in-page anchors. if (href.startsWith('#')) { return {children}; @@ -96,11 +91,16 @@ export const AnchorMdx: FC = props => { // Links to other pages of SB should use the base URL of the top level iframe instead of the base URL of the preview iframe. if (target !== '_blank') { - const parentUrl = new URL(window.parent.location.href); - const newHref = `${parentUrl.origin}${href}`; - return ( - + { + event.preventDefault(); + navigate(href); + }} + target={target} + {...rest} + > {children} ); @@ -149,17 +149,19 @@ const HeaderWithOcticonAnchor: FC = ({ }) => { // @ts-ignore const OcticonHeader = OcticonHeaders[as]; + const hash = `#${id}`; return (