-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
DocsContainer.tsx
67 lines (61 loc) · 2.04 KB
/
DocsContainer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import type { FC, PropsWithChildren } from 'react';
import React, { useEffect } from 'react';
import { global } from '@storybook/global';
import type { ThemeVars } from '@storybook/theming';
import { ThemeProvider, ensure as ensureTheme } from '@storybook/theming';
import type { Renderer } from '@storybook/types';
import { DocsPageWrapper } from '../components';
import type { DocsContextProps } from './DocsContext';
import { DocsContext } from './DocsContext';
import { SourceContainer } from './SourceContainer';
import { scrollToElement } from './utils';
import { TableOfContents } from '../components/TableOfContents';
const { document, window: globalWindow } = global;
export interface DocsContainerProps<TFramework extends Renderer = Renderer> {
context: DocsContextProps<TFramework>;
theme?: ThemeVars;
}
export const DocsContainer: FC<PropsWithChildren<DocsContainerProps>> = ({
context,
theme,
children,
}) => {
let toc;
try {
const meta = context.resolveOf('meta', ['meta']);
toc = meta.preparedMeta.parameters?.docs?.toc;
} catch (err) {
// No meta, falling back to project annotations
toc = context?.projectAnnotations?.parameters?.docs?.toc;
}
useEffect(() => {
let url;
try {
url = new URL(globalWindow.parent.location.toString());
if (url.hash) {
const element = document.getElementById(url.hash.substring(1));
if (element) {
// Introducing a delay to ensure scrolling works when it's a full refresh.
setTimeout(() => {
scrollToElement(element);
}, 200);
}
}
} catch (err) {
// pass
}
});
return (
<DocsContext.Provider value={context}>
<SourceContainer channel={context.channel}>
<ThemeProvider theme={ensureTheme(theme)}>
<DocsPageWrapper
toc={toc ? <TableOfContents className="sbdocs sbdocs-toc--custom" {...toc} /> : null}
>
{children}
</DocsPageWrapper>
</ThemeProvider>
</SourceContainer>
</DocsContext.Provider>
);
};