@storybook/addon-docs 7.0.0-alpha.5 → 7.0.0-alpha.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/README.md +14 -77
  2. package/angular/index.js +1 -1
  3. package/dist/DocsRenderer-MHI45CTG.mjs +1 -0
  4. package/dist/chunk-HKSD7XQF.mjs +1 -0
  5. package/dist/chunk-WMY67HY3.mjs +1 -0
  6. package/dist/index.d.ts +11 -0
  7. package/dist/index.js +1 -0
  8. package/dist/index.mjs +1 -0
  9. package/dist/preset.d.ts +5 -0
  10. package/dist/preset.js +7 -0
  11. package/dist/preset.mjs +7 -0
  12. package/dist/preview.d.ts +3 -0
  13. package/dist/preview.js +1 -0
  14. package/dist/preview.mjs +1 -0
  15. package/jest-transform-mdx.js +4 -3
  16. package/jest.config.js +7 -0
  17. package/package.json +63 -36
  18. package/preset.js +1 -1
  19. package/preview.js +1 -1
  20. package/web-components/README.md +1 -0
  21. package/LICENSE +0 -21
  22. package/blocks.d.ts +0 -2
  23. package/blocks.js +0 -14
  24. package/dist/cjs/blocks/Anchor.js +0 -23
  25. package/dist/cjs/blocks/ArgsTable.js +0 -329
  26. package/dist/cjs/blocks/Canvas.js +0 -109
  27. package/dist/cjs/blocks/Description.js +0 -126
  28. package/dist/cjs/blocks/DocsContainer.js +0 -127
  29. package/dist/cjs/blocks/DocsContext.js +0 -25
  30. package/dist/cjs/blocks/DocsPage.js +0 -30
  31. package/dist/cjs/blocks/DocsStory.js +0 -68
  32. package/dist/cjs/blocks/Heading.js +0 -31
  33. package/dist/cjs/blocks/Meta.js +0 -50
  34. package/dist/cjs/blocks/Preview.js +0 -23
  35. package/dist/cjs/blocks/Primary.js +0 -39
  36. package/dist/cjs/blocks/Props.js +0 -29
  37. package/dist/cjs/blocks/Source.js +0 -179
  38. package/dist/cjs/blocks/SourceContainer.js +0 -67
  39. package/dist/cjs/blocks/Stories.js +0 -51
  40. package/dist/cjs/blocks/Story.js +0 -143
  41. package/dist/cjs/blocks/Subheading.js +0 -31
  42. package/dist/cjs/blocks/Subtitle.js +0 -39
  43. package/dist/cjs/blocks/Title.js +0 -44
  44. package/dist/cjs/blocks/Wrapper.js +0 -20
  45. package/dist/cjs/blocks/enhanceSource.js +0 -92
  46. package/dist/cjs/blocks/index.js +0 -366
  47. package/dist/cjs/blocks/mdx.js +0 -234
  48. package/dist/cjs/blocks/types.js +0 -10
  49. package/dist/cjs/blocks/useStory.js +0 -37
  50. package/dist/cjs/blocks/utils.js +0 -40
  51. package/dist/cjs/index.js +0 -18
  52. package/dist/cjs/manager.js +0 -20
  53. package/dist/cjs/preset.js +0 -162
  54. package/dist/cjs/preview.js +0 -18
  55. package/dist/cjs/shared.js +0 -22
  56. package/dist/esm/blocks/Anchor.js +0 -8
  57. package/dist/esm/blocks/ArgsTable.js +0 -292
  58. package/dist/esm/blocks/Canvas.js +0 -83
  59. package/dist/esm/blocks/Description.js +0 -107
  60. package/dist/esm/blocks/DocsContainer.js +0 -98
  61. package/dist/esm/blocks/DocsContext.js +0 -16
  62. package/dist/esm/blocks/DocsPage.js +0 -11
  63. package/dist/esm/blocks/DocsStory.js +0 -46
  64. package/dist/esm/blocks/Heading.js +0 -17
  65. package/dist/esm/blocks/Meta.js +0 -31
  66. package/dist/esm/blocks/Preview.js +0 -9
  67. package/dist/esm/blocks/Primary.js +0 -23
  68. package/dist/esm/blocks/Props.js +0 -14
  69. package/dist/esm/blocks/Source.js +0 -144
  70. package/dist/esm/blocks/SourceContainer.js +0 -44
  71. package/dist/esm/blocks/Stories.js +0 -30
  72. package/dist/esm/blocks/Story.js +0 -114
  73. package/dist/esm/blocks/Subheading.js +0 -17
  74. package/dist/esm/blocks/Subtitle.js +0 -23
  75. package/dist/esm/blocks/Title.js +0 -24
  76. package/dist/esm/blocks/Wrapper.js +0 -8
  77. package/dist/esm/blocks/enhanceSource.js +0 -87
  78. package/dist/esm/blocks/index.js +0 -24
  79. package/dist/esm/blocks/mdx.js +0 -203
  80. package/dist/esm/blocks/types.js +0 -2
  81. package/dist/esm/blocks/useStory.js +0 -27
  82. package/dist/esm/blocks/utils.js +0 -29
  83. package/dist/esm/index.js +0 -1
  84. package/dist/esm/manager.js +0 -16
  85. package/dist/esm/preset.js +0 -132
  86. package/dist/esm/preview.js +0 -6
  87. package/dist/esm/shared.js +0 -11
  88. package/dist/types/blocks/Anchor.d.ts +0 -6
  89. package/dist/types/blocks/ArgsTable.d.ts +0 -33
  90. package/dist/types/blocks/Canvas.d.ts +0 -9
  91. package/dist/types/blocks/Description.d.ts +0 -20
  92. package/dist/types/blocks/DocsContainer.d.ts +0 -7
  93. package/dist/types/blocks/DocsContext.d.ts +0 -5
  94. package/dist/types/blocks/DocsPage.d.ts +0 -2
  95. package/dist/types/blocks/DocsStory.d.ts +0 -3
  96. package/dist/types/blocks/Heading.d.ts +0 -6
  97. package/dist/types/blocks/Meta.d.ts +0 -9
  98. package/dist/types/blocks/Preview.d.ts +0 -3
  99. package/dist/types/blocks/Primary.d.ts +0 -6
  100. package/dist/types/blocks/Props.d.ts +0 -3
  101. package/dist/types/blocks/Source.d.ts +0 -38
  102. package/dist/types/blocks/SourceContainer.d.ts +0 -14
  103. package/dist/types/blocks/Stories.d.ts +0 -7
  104. package/dist/types/blocks/Story.d.ts +0 -28
  105. package/dist/types/blocks/Subheading.d.ts +0 -3
  106. package/dist/types/blocks/Subtitle.d.ts +0 -6
  107. package/dist/types/blocks/Title.d.ts +0 -8
  108. package/dist/types/blocks/Wrapper.d.ts +0 -2
  109. package/dist/types/blocks/enhanceSource.d.ts +0 -3
  110. package/dist/types/blocks/index.d.ts +0 -24
  111. package/dist/types/blocks/mdx.d.ts +0 -20
  112. package/dist/types/blocks/types.d.ts +0 -13
  113. package/dist/types/blocks/useStory.d.ts +0 -5
  114. package/dist/types/blocks/utils.d.ts +0 -3
  115. package/dist/types/index.d.ts +0 -1
  116. package/dist/types/manager.d.ts +0 -1
  117. package/dist/types/preset.d.ts +0 -12
  118. package/dist/types/preview.d.ts +0 -6
  119. package/dist/types/shared.d.ts +0 -24
  120. package/manager.js +0 -1
  121. package/mdx-compiler-plugin.js +0 -1
  122. package/register.js +0 -6
@@ -1,107 +0,0 @@
1
- import React, { useContext } from 'react';
2
- import { Description } from '@storybook/components';
3
- import { str } from '@storybook/docs-tools';
4
- import { DocsContext } from './DocsContext';
5
- import { CURRENT_SELECTION } from './types';
6
- export let DescriptionType;
7
-
8
- (function (DescriptionType) {
9
- DescriptionType["INFO"] = "info";
10
- DescriptionType["NOTES"] = "notes";
11
- DescriptionType["DOCGEN"] = "docgen";
12
- DescriptionType["LEGACY_5_2"] = "legacy-5.2";
13
- DescriptionType["AUTO"] = "auto";
14
- })(DescriptionType || (DescriptionType = {}));
15
-
16
- const getNotes = notes => notes && (typeof notes === 'string' ? notes : str(notes.markdown) || str(notes.text));
17
-
18
- const getInfo = info => info && (typeof info === 'string' ? info : str(info.text));
19
-
20
- const noDescription = component => null;
21
-
22
- export const getDescriptionProps = ({
23
- of,
24
- type,
25
- markdown,
26
- children
27
- }, {
28
- id,
29
- storyById
30
- }) => {
31
- const {
32
- component,
33
- parameters
34
- } = storyById(id);
35
-
36
- if (children || markdown) {
37
- return {
38
- markdown: children || markdown
39
- };
40
- }
41
-
42
- const {
43
- notes,
44
- info,
45
- docs
46
- } = parameters;
47
- const {
48
- extractComponentDescription = noDescription,
49
- description
50
- } = docs || {};
51
- const target = of === CURRENT_SELECTION ? component : of; // override component description
52
-
53
- const componentDescriptionParameter = description?.component;
54
-
55
- if (componentDescriptionParameter) {
56
- return {
57
- markdown: componentDescriptionParameter
58
- };
59
- }
60
-
61
- switch (type) {
62
- case DescriptionType.INFO:
63
- return {
64
- markdown: getInfo(info)
65
- };
66
-
67
- case DescriptionType.NOTES:
68
- return {
69
- markdown: getNotes(notes)
70
- };
71
- // FIXME: remove in 6.0
72
-
73
- case DescriptionType.LEGACY_5_2:
74
- return {
75
- markdown: `
76
- ${getNotes(notes) || getInfo(info) || ''}
77
-
78
- ${extractComponentDescription(target) || ''}
79
- `.trim()
80
- };
81
-
82
- case DescriptionType.DOCGEN:
83
- case DescriptionType.AUTO:
84
- default:
85
- return {
86
- markdown: extractComponentDescription(target, Object.assign({
87
- component
88
- }, parameters))
89
- };
90
- }
91
- };
92
-
93
- const DescriptionContainer = props => {
94
- const context = useContext(DocsContext);
95
- const {
96
- markdown
97
- } = getDescriptionProps(props, context);
98
- return markdown ? /*#__PURE__*/React.createElement(Description, {
99
- markdown: markdown
100
- }) : null;
101
- }; // since we are in the docs blocks, assume default description if for primary component story
102
-
103
-
104
- DescriptionContainer.defaultProps = {
105
- of: '.'
106
- };
107
- export { DescriptionContainer as Description };
@@ -1,98 +0,0 @@
1
- import React, { useEffect } from 'react';
2
- import global from 'global';
3
- import deprecate from 'util-deprecate';
4
- import dedent from 'ts-dedent';
5
- import { MDXProvider } from '@mdx-js/react';
6
- import { ThemeProvider, ensure as ensureTheme } from '@storybook/theming';
7
- import { DocsWrapper, DocsContent, components as htmlComponents } from '@storybook/components';
8
- import { DocsContext } from './DocsContext';
9
- import { anchorBlockIdFromId } from './Anchor';
10
- import { storyBlockIdFromId } from './Story';
11
- import { SourceContainer } from './SourceContainer';
12
- import { CodeOrSourceMdx, AnchorMdx, HeadersMdx } from './mdx';
13
- import { scrollToElement } from './utils';
14
- const {
15
- document,
16
- window: globalWindow
17
- } = global;
18
- const defaultComponents = Object.assign({}, htmlComponents, {
19
- code: CodeOrSourceMdx,
20
- a: AnchorMdx
21
- }, HeadersMdx);
22
- const warnOptionsTheme = deprecate(() => {}, dedent`
23
- Deprecated parameter: options.theme => docs.theme
24
-
25
- https://github.com/storybookjs/storybook/blob/next/addons/docs/docs/theming.md#storybook-theming
26
- `);
27
- export const DocsContainer = ({
28
- context,
29
- children
30
- }) => {
31
- const {
32
- id: storyId,
33
- storyById
34
- } = context;
35
- const {
36
- parameters: {
37
- options = {},
38
- docs = {}
39
- }
40
- } = storyById(storyId);
41
- let themeVars = docs.theme;
42
-
43
- if (!themeVars && options.theme) {
44
- warnOptionsTheme();
45
- themeVars = options.theme;
46
- }
47
-
48
- const theme = ensureTheme(themeVars);
49
- const allComponents = Object.assign({}, defaultComponents, docs.components);
50
- useEffect(() => {
51
- let url;
52
-
53
- try {
54
- url = new URL(globalWindow.parent.location);
55
- } catch (err) {
56
- return;
57
- }
58
-
59
- if (url.hash) {
60
- const element = document.getElementById(url.hash.substring(1));
61
-
62
- if (element) {
63
- // Introducing a delay to ensure scrolling works when it's a full refresh.
64
- setTimeout(() => {
65
- scrollToElement(element);
66
- }, 200);
67
- }
68
- } else {
69
- const element = document.getElementById(anchorBlockIdFromId(storyId)) || document.getElementById(storyBlockIdFromId(storyId));
70
-
71
- if (element) {
72
- const allStories = element.parentElement.querySelectorAll('[id|="anchor-"]');
73
- let scrollTarget = element;
74
-
75
- if (allStories && allStories[0] === element) {
76
- // Include content above first story
77
- scrollTarget = document.getElementById('docs-root');
78
- } // Introducing a delay to ensure scrolling works when it's a full refresh.
79
-
80
-
81
- setTimeout(() => {
82
- scrollToElement(scrollTarget, 'start');
83
- }, 200);
84
- }
85
- }
86
- }, [storyId]);
87
- return /*#__PURE__*/React.createElement(DocsContext.Provider, {
88
- value: context
89
- }, /*#__PURE__*/React.createElement(SourceContainer, null, /*#__PURE__*/React.createElement(ThemeProvider, {
90
- theme: theme
91
- }, /*#__PURE__*/React.createElement(MDXProvider, {
92
- components: allComponents
93
- }, /*#__PURE__*/React.createElement(DocsWrapper, {
94
- className: "sbdocs sbdocs-wrapper"
95
- }, /*#__PURE__*/React.createElement(DocsContent, {
96
- className: "sbdocs sbdocs-content"
97
- }, children))))));
98
- };
@@ -1,16 +0,0 @@
1
- import { createContext } from 'react';
2
- import { window as globalWindow } from 'global';
3
-
4
- // We add DocsContext to window. The reason is that in case DocsContext.ts is
5
- // imported multiple times (maybe once directly, and another time from a minified bundle)
6
- // we will have multiple DocsContext definitions - leading to lost context in
7
- // the React component tree.
8
- // This was specifically a problem with the Vite builder.
9
-
10
- /* eslint-disable no-underscore-dangle */
11
- if (globalWindow && globalWindow.__DOCS_CONTEXT__ === undefined) {
12
- globalWindow.__DOCS_CONTEXT__ = /*#__PURE__*/createContext({});
13
- globalWindow.__DOCS_CONTEXT__.displayName = 'DocsContext';
14
- }
15
-
16
- export const DocsContext = globalWindow ? globalWindow.__DOCS_CONTEXT__ : /*#__PURE__*/createContext({});
@@ -1,11 +0,0 @@
1
- import React from 'react';
2
- import { Title } from './Title';
3
- import { Subtitle } from './Subtitle';
4
- import { Description } from './Description';
5
- import { Primary } from './Primary';
6
- import { PRIMARY_STORY } from './types';
7
- import { ArgsTable } from './ArgsTable';
8
- import { Stories } from './Stories';
9
- export const DocsPage = () => /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Title, null), /*#__PURE__*/React.createElement(Subtitle, null), /*#__PURE__*/React.createElement(Description, null), /*#__PURE__*/React.createElement(Primary, null), /*#__PURE__*/React.createElement(ArgsTable, {
10
- story: PRIMARY_STORY
11
- }), /*#__PURE__*/React.createElement(Stories, null));
@@ -1,46 +0,0 @@
1
- import React from 'react';
2
- import deprecate from 'util-deprecate';
3
- import dedent from 'ts-dedent';
4
- import { Subheading } from './Subheading';
5
- import { Anchor } from './Anchor';
6
- import { Description } from './Description';
7
- import { Story } from './Story';
8
- import { Canvas } from './Canvas';
9
- const warnStoryDescription = deprecate(() => {}, dedent`
10
- Deprecated parameter: docs.storyDescription => docs.description.story
11
-
12
- https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#docs-description-parameter
13
- `);
14
- export const DocsStory = ({
15
- id,
16
- name,
17
- expanded = true,
18
- withToolbar = false,
19
- parameters = {}
20
- }) => {
21
- let description;
22
- const {
23
- docs
24
- } = parameters;
25
-
26
- if (expanded && docs) {
27
- description = docs.description?.story;
28
-
29
- if (!description) {
30
- description = docs.storyDescription;
31
- if (description) warnStoryDescription();
32
- }
33
- }
34
-
35
- const subheading = expanded && name;
36
- return /*#__PURE__*/React.createElement(Anchor, {
37
- storyId: id
38
- }, subheading && /*#__PURE__*/React.createElement(Subheading, null, subheading), description && /*#__PURE__*/React.createElement(Description, {
39
- markdown: description
40
- }), /*#__PURE__*/React.createElement(Canvas, {
41
- withToolbar: withToolbar
42
- }, /*#__PURE__*/React.createElement(Story, {
43
- id: id,
44
- parameters: parameters
45
- })));
46
- };
@@ -1,17 +0,0 @@
1
- import React from 'react';
2
- import { H2 } from '@storybook/components';
3
- import { HeaderMdx } from './mdx';
4
- export const Heading = ({
5
- children,
6
- disableAnchor
7
- }) => {
8
- if (disableAnchor || typeof children !== 'string') {
9
- return /*#__PURE__*/React.createElement(H2, null, children);
10
- }
11
-
12
- const tagID = children.toLowerCase().replace(/[^a-z0-9]/gi, '-');
13
- return /*#__PURE__*/React.createElement(HeaderMdx, {
14
- as: "h2",
15
- id: tagID
16
- }, children);
17
- };
@@ -1,31 +0,0 @@
1
- import React, { useContext } from 'react';
2
- import global from 'global';
3
- import { Anchor } from './Anchor';
4
- import { DocsContext } from './DocsContext';
5
- const {
6
- document
7
- } = global;
8
-
9
- function getFirstStoryId(docsContext) {
10
- const stories = docsContext.componentStories();
11
- return stories.length > 0 ? stories[0].id : null;
12
- }
13
-
14
- function renderAnchor() {
15
- const context = useContext(DocsContext);
16
- const anchorId = getFirstStoryId(context) || context.id;
17
- return /*#__PURE__*/React.createElement(Anchor, {
18
- storyId: anchorId
19
- });
20
- }
21
- /**
22
- * This component is used to declare component metadata in docs
23
- * and gets transformed into a default export underneath the hood.
24
- */
25
-
26
-
27
- export const Meta = () => {
28
- const params = new URL(document.location).searchParams;
29
- const isDocs = params.get('viewMode') === 'docs';
30
- return isDocs ? renderAnchor() : null;
31
- };
@@ -1,9 +0,0 @@
1
- import React from 'react';
2
- import deprecate from 'util-deprecate';
3
- import dedent from 'ts-dedent';
4
- import { Canvas } from './Canvas';
5
- export const Preview = deprecate(props => /*#__PURE__*/React.createElement(Canvas, props), dedent`
6
- Preview doc block has been renamed to Canvas.
7
-
8
- https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#previewprops-renamed
9
- `);
@@ -1,23 +0,0 @@
1
- function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
-
3
- import React, { useContext } from 'react';
4
- import { DocsContext } from './DocsContext';
5
- import { DocsStory } from './DocsStory';
6
- export const Primary = ({
7
- name
8
- }) => {
9
- const {
10
- componentStories: getComponentStories
11
- } = useContext(DocsContext);
12
- const componentStories = getComponentStories();
13
- let story;
14
-
15
- if (componentStories) {
16
- story = name ? componentStories.find(s => s.name === name) : componentStories[0];
17
- }
18
-
19
- return story ? /*#__PURE__*/React.createElement(DocsStory, _extends({}, story, {
20
- expanded: false,
21
- withToolbar: true
22
- })) : null;
23
- };
@@ -1,14 +0,0 @@
1
- import React from 'react';
2
- import deprecate from 'util-deprecate';
3
- import dedent from 'ts-dedent';
4
- import { ArgsTable } from './ArgsTable';
5
- import { CURRENT_SELECTION } from './types';
6
- export const Props = deprecate(props => /*#__PURE__*/React.createElement(ArgsTable, props), dedent`
7
- Props doc block has been renamed to ArgsTable.
8
-
9
- https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#previewprops-renamed
10
- `); // @ts-ignore
11
-
12
- Props.defaultProps = {
13
- of: CURRENT_SELECTION
14
- };
@@ -1,144 +0,0 @@
1
- import React, { useContext } from 'react';
2
- import { Source as PureSource, SourceError } from '@storybook/components';
3
- import { DocsContext } from './DocsContext';
4
- import { SourceContext } from './SourceContainer';
5
- import { CURRENT_SELECTION } from './types';
6
- import { SourceType } from '../shared';
7
- import { enhanceSource } from './enhanceSource';
8
- import { useStories } from './useStory';
9
- export let SourceState;
10
-
11
- (function (SourceState) {
12
- SourceState["OPEN"] = "open";
13
- SourceState["CLOSED"] = "closed";
14
- SourceState["NONE"] = "none";
15
- })(SourceState || (SourceState = {}));
16
-
17
- const getSourceState = stories => {
18
- const states = stories.map(story => story.parameters.docs?.source?.state).filter(Boolean);
19
- if (states.length === 0) return SourceState.CLOSED; // FIXME: handling multiple stories is a pain
20
-
21
- return states[0];
22
- };
23
-
24
- const getStorySource = (storyId, sourceContext) => {
25
- const {
26
- sources
27
- } = sourceContext; // source rendering is async so source is unavailable at the start of the render cycle,
28
- // so we fail gracefully here without warning
29
-
30
- return sources?.[storyId] || {
31
- code: '',
32
- format: false
33
- };
34
- };
35
-
36
- const getSnippet = (snippet, story) => {
37
- if (!story) {
38
- return snippet;
39
- }
40
-
41
- const {
42
- parameters
43
- } = story; // eslint-disable-next-line no-underscore-dangle
44
-
45
- const isArgsStory = parameters.__isArgsStory;
46
- const type = parameters.docs?.source?.type || SourceType.AUTO; // if user has hard-coded the snippet, that takes precedence
47
-
48
- const userCode = parameters.docs?.source?.code;
49
-
50
- if (userCode !== undefined) {
51
- return userCode;
52
- } // if user has explicitly set this as dynamic, use snippet
53
-
54
-
55
- if (type === SourceType.DYNAMIC) {
56
- return parameters.docs?.transformSource?.(snippet, story) || snippet;
57
- } // if this is an args story and there's a snippet
58
-
59
-
60
- if (type === SourceType.AUTO && snippet && isArgsStory) {
61
- return parameters.docs?.transformSource?.(snippet, story) || snippet;
62
- } // otherwise, use the source code logic
63
-
64
-
65
- const enhanced = enhanceSource(story) || parameters;
66
- return enhanced?.docs?.source?.code || '';
67
- };
68
-
69
- export const getSourceProps = (props, docsContext, sourceContext) => {
70
- const {
71
- id: currentId,
72
- storyById
73
- } = docsContext;
74
- const {
75
- parameters
76
- } = storyById(currentId);
77
- const codeProps = props;
78
- const singleProps = props;
79
- const multiProps = props;
80
- let source = codeProps.code; // prefer user-specified code
81
-
82
- let {
83
- format
84
- } = codeProps; // prefer user-specified code
85
-
86
- const targetIds = multiProps.ids || [singleProps.id || currentId];
87
- const storyIds = targetIds.map(targetId => targetId === CURRENT_SELECTION ? currentId : targetId);
88
- const stories = useStories(storyIds, docsContext);
89
-
90
- if (!stories.every(Boolean)) {
91
- return {
92
- error: SourceError.SOURCE_UNAVAILABLE,
93
- state: SourceState.NONE
94
- };
95
- }
96
-
97
- if (!source) {
98
- // just take the format from the first story, given how they're all concatinated together...
99
- // TODO: we should consider sending an event with all the sources separately, instead of concatenating them here
100
- ({
101
- format
102
- } = getStorySource(storyIds[0], sourceContext));
103
- source = storyIds.map((storyId, idx) => {
104
- const {
105
- code: storySource
106
- } = getStorySource(storyId, sourceContext);
107
- const storyObj = stories[idx];
108
- return getSnippet(storySource, storyObj);
109
- }).join('\n\n');
110
- }
111
-
112
- const state = getSourceState(stories);
113
- const {
114
- docs: docsParameters = {}
115
- } = parameters;
116
- const {
117
- source: sourceParameters = {}
118
- } = docsParameters;
119
- const {
120
- language: docsLanguage = null
121
- } = sourceParameters;
122
- return source ? {
123
- code: source,
124
- state,
125
- format,
126
- language: props.language || docsLanguage || 'jsx',
127
- dark: props.dark || false
128
- } : {
129
- error: SourceError.SOURCE_UNAVAILABLE,
130
- state
131
- };
132
- };
133
- /**
134
- * Story source doc block renders source code if provided,
135
- * or the source for a story if `storyId` is provided, or
136
- * the source for the current story if nothing is provided.
137
- */
138
-
139
- export const Source = props => {
140
- const sourceContext = useContext(SourceContext);
141
- const docsContext = useContext(DocsContext);
142
- const sourceProps = getSourceProps(props, docsContext, sourceContext);
143
- return /*#__PURE__*/React.createElement(PureSource, sourceProps);
144
- };
@@ -1,44 +0,0 @@
1
- import React, { createContext, useEffect, useState } from 'react';
2
- import deepEqual from 'fast-deep-equal';
3
- import { addons } from '@storybook/addons';
4
- import { SNIPPET_RENDERED } from '../shared';
5
- export const SourceContext = /*#__PURE__*/createContext({
6
- sources: {}
7
- });
8
- export const SourceContainer = ({
9
- children
10
- }) => {
11
- const [sources, setSources] = useState({});
12
- const channel = addons.getChannel();
13
- useEffect(() => {
14
- const handleSnippetRendered = (id, newSource, format = false) => {
15
- // optimization: if the source is the same, ignore the incoming event
16
- if (sources[id] && sources[id].code === newSource) {
17
- return;
18
- }
19
-
20
- setSources(current => {
21
- const newSources = Object.assign({}, current, {
22
- [id]: {
23
- code: newSource,
24
- format
25
- }
26
- });
27
-
28
- if (!deepEqual(current, newSources)) {
29
- return newSources;
30
- }
31
-
32
- return current;
33
- });
34
- };
35
-
36
- channel.on(SNIPPET_RENDERED, handleSnippetRendered);
37
- return () => channel.off(SNIPPET_RENDERED, handleSnippetRendered);
38
- }, []);
39
- return /*#__PURE__*/React.createElement(SourceContext.Provider, {
40
- value: {
41
- sources
42
- }
43
- }, children);
44
- };
@@ -1,30 +0,0 @@
1
- function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
-
3
- import React, { useContext } from 'react';
4
- import { DocsContext } from './DocsContext';
5
- import { DocsStory } from './DocsStory';
6
- import { Heading } from './Heading';
7
- export const Stories = ({
8
- title,
9
- includePrimary = false
10
- }) => {
11
- const {
12
- componentStories
13
- } = useContext(DocsContext);
14
- let stories = componentStories();
15
- stories = stories.filter(story => !story.parameters?.docs?.disable);
16
- if (!includePrimary) stories = stories.slice(1);
17
-
18
- if (!stories || stories.length === 0) {
19
- return null;
20
- }
21
-
22
- return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Heading, null, title), stories.map(story => story && /*#__PURE__*/React.createElement(DocsStory, _extends({
23
- key: story.id
24
- }, story, {
25
- expanded: true
26
- }))));
27
- };
28
- Stories.defaultProps = {
29
- title: 'Stories'
30
- };