@storybook/addon-docs 6.5.9 → 7.0.0-alpha.10

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 (163) hide show
  1. package/blocks.d.ts +1 -1
  2. package/dist/cjs/blocks/index.js +5 -359
  3. package/dist/cjs/index.js +1 -7
  4. package/dist/cjs/manager.js +9 -15
  5. package/dist/cjs/preset.js +121 -193
  6. package/dist/cjs/preview.js +9 -87
  7. package/dist/cjs/shared.js +5 -5
  8. package/dist/esm/blocks/index.js +1 -24
  9. package/dist/esm/manager.js +9 -14
  10. package/dist/esm/preset.js +114 -179
  11. package/dist/esm/preview.js +7 -61
  12. package/dist/esm/shared.js +5 -5
  13. package/dist/types/blocks/index.d.ts +1 -0
  14. package/dist/{ts3.9 → types}/index.d.ts +0 -0
  15. package/dist/{ts3.9 → types}/manager.d.ts +0 -0
  16. package/dist/{ts3.9 → types}/preset.d.ts +2 -1
  17. package/dist/types/preview.d.ts +1 -0
  18. package/dist/{ts3.9 → types}/shared.d.ts +0 -0
  19. package/package.json +18 -25
  20. package/dist/cjs/blocks/Anchor.js +0 -26
  21. package/dist/cjs/blocks/ArgsTable.js +0 -400
  22. package/dist/cjs/blocks/Canvas.js +0 -144
  23. package/dist/cjs/blocks/Description.js +0 -153
  24. package/dist/cjs/blocks/DocsContainer.js +0 -155
  25. package/dist/cjs/blocks/DocsContext.js +0 -25
  26. package/dist/cjs/blocks/DocsPage.js +0 -32
  27. package/dist/cjs/blocks/DocsStory.js +0 -78
  28. package/dist/cjs/blocks/Heading.js +0 -35
  29. package/dist/cjs/blocks/Meta.js +0 -72
  30. package/dist/cjs/blocks/Preview.js +0 -29
  31. package/dist/cjs/blocks/Primary.js +0 -67
  32. package/dist/cjs/blocks/Props.js +0 -35
  33. package/dist/cjs/blocks/Source.js +0 -197
  34. package/dist/cjs/blocks/SourceContainer.js +0 -116
  35. package/dist/cjs/blocks/Stories.js +0 -82
  36. package/dist/cjs/blocks/Story.js +0 -263
  37. package/dist/cjs/blocks/Subheading.js +0 -35
  38. package/dist/cjs/blocks/Subtitle.js +0 -59
  39. package/dist/cjs/blocks/Title.js +0 -68
  40. package/dist/cjs/blocks/Wrapper.js +0 -21
  41. package/dist/cjs/blocks/enhanceSource.js +0 -129
  42. package/dist/cjs/blocks/mdx.js +0 -245
  43. package/dist/cjs/blocks/types.js +0 -10
  44. package/dist/cjs/blocks/useStory.js +0 -108
  45. package/dist/cjs/blocks/utils.js +0 -61
  46. package/dist/esm/blocks/Anchor.js +0 -11
  47. package/dist/esm/blocks/ArgsTable.js +0 -341
  48. package/dist/esm/blocks/Canvas.js +0 -95
  49. package/dist/esm/blocks/Description.js +0 -113
  50. package/dist/esm/blocks/DocsContainer.js +0 -106
  51. package/dist/esm/blocks/DocsContext.js +0 -16
  52. package/dist/esm/blocks/DocsPage.js +0 -13
  53. package/dist/esm/blocks/DocsStory.js +0 -55
  54. package/dist/esm/blocks/Heading.js +0 -19
  55. package/dist/esm/blocks/Meta.js +0 -35
  56. package/dist/esm/blocks/Preview.js +0 -14
  57. package/dist/esm/blocks/Primary.js +0 -29
  58. package/dist/esm/blocks/Props.js +0 -19
  59. package/dist/esm/blocks/Source.js +0 -150
  60. package/dist/esm/blocks/SourceContainer.js +0 -77
  61. package/dist/esm/blocks/Stories.js +0 -42
  62. package/dist/esm/blocks/Story.js +0 -207
  63. package/dist/esm/blocks/Subheading.js +0 -19
  64. package/dist/esm/blocks/Subtitle.js +0 -23
  65. package/dist/esm/blocks/Title.js +0 -25
  66. package/dist/esm/blocks/Wrapper.js +0 -9
  67. package/dist/esm/blocks/enhanceSource.js +0 -111
  68. package/dist/esm/blocks/mdx.js +0 -207
  69. package/dist/esm/blocks/types.js +0 -2
  70. package/dist/esm/blocks/useStory.js +0 -84
  71. package/dist/esm/blocks/utils.js +0 -43
  72. package/dist/modern/blocks/Anchor.js +0 -8
  73. package/dist/modern/blocks/ArgsTable.js +0 -292
  74. package/dist/modern/blocks/Canvas.js +0 -83
  75. package/dist/modern/blocks/Description.js +0 -107
  76. package/dist/modern/blocks/DocsContainer.js +0 -98
  77. package/dist/modern/blocks/DocsContext.js +0 -16
  78. package/dist/modern/blocks/DocsPage.js +0 -11
  79. package/dist/modern/blocks/DocsStory.js +0 -48
  80. package/dist/modern/blocks/Heading.js +0 -17
  81. package/dist/modern/blocks/Meta.js +0 -31
  82. package/dist/modern/blocks/Preview.js +0 -9
  83. package/dist/modern/blocks/Primary.js +0 -23
  84. package/dist/modern/blocks/Props.js +0 -14
  85. package/dist/modern/blocks/Source.js +0 -154
  86. package/dist/modern/blocks/SourceContainer.js +0 -44
  87. package/dist/modern/blocks/Stories.js +0 -34
  88. package/dist/modern/blocks/Story.js +0 -168
  89. package/dist/modern/blocks/Subheading.js +0 -17
  90. package/dist/modern/blocks/Subtitle.js +0 -23
  91. package/dist/modern/blocks/Title.js +0 -24
  92. package/dist/modern/blocks/Wrapper.js +0 -8
  93. package/dist/modern/blocks/enhanceSource.js +0 -89
  94. package/dist/modern/blocks/index.js +0 -24
  95. package/dist/modern/blocks/mdx.js +0 -204
  96. package/dist/modern/blocks/types.js +0 -2
  97. package/dist/modern/blocks/useStory.js +0 -28
  98. package/dist/modern/blocks/utils.js +0 -29
  99. package/dist/modern/index.js +0 -1
  100. package/dist/modern/manager.js +0 -16
  101. package/dist/modern/preset.js +0 -130
  102. package/dist/modern/preview.js +0 -6
  103. package/dist/modern/shared.js +0 -11
  104. package/dist/ts3.4/blocks/Anchor.d.ts +0 -6
  105. package/dist/ts3.4/blocks/ArgsTable.d.ts +0 -33
  106. package/dist/ts3.4/blocks/Canvas.d.ts +0 -9
  107. package/dist/ts3.4/blocks/Description.d.ts +0 -20
  108. package/dist/ts3.4/blocks/DocsContainer.d.ts +0 -7
  109. package/dist/ts3.4/blocks/DocsContext.d.ts +0 -5
  110. package/dist/ts3.4/blocks/DocsPage.d.ts +0 -2
  111. package/dist/ts3.4/blocks/DocsStory.d.ts +0 -3
  112. package/dist/ts3.4/blocks/Heading.d.ts +0 -6
  113. package/dist/ts3.4/blocks/Meta.d.ts +0 -9
  114. package/dist/ts3.4/blocks/Preview.d.ts +0 -3
  115. package/dist/ts3.4/blocks/Primary.d.ts +0 -6
  116. package/dist/ts3.4/blocks/Props.d.ts +0 -3
  117. package/dist/ts3.4/blocks/Source.d.ts +0 -38
  118. package/dist/ts3.4/blocks/SourceContainer.d.ts +0 -14
  119. package/dist/ts3.4/blocks/Stories.d.ts +0 -7
  120. package/dist/ts3.4/blocks/Story.d.ts +0 -28
  121. package/dist/ts3.4/blocks/Subheading.d.ts +0 -3
  122. package/dist/ts3.4/blocks/Subtitle.d.ts +0 -6
  123. package/dist/ts3.4/blocks/Title.d.ts +0 -8
  124. package/dist/ts3.4/blocks/Wrapper.d.ts +0 -2
  125. package/dist/ts3.4/blocks/enhanceSource.d.ts +0 -3
  126. package/dist/ts3.4/blocks/index.d.ts +0 -24
  127. package/dist/ts3.4/blocks/mdx.d.ts +0 -20
  128. package/dist/ts3.4/blocks/types.d.ts +0 -13
  129. package/dist/ts3.4/blocks/useStory.d.ts +0 -5
  130. package/dist/ts3.4/blocks/utils.d.ts +0 -3
  131. package/dist/ts3.4/index.d.ts +0 -1
  132. package/dist/ts3.4/manager.d.ts +0 -1
  133. package/dist/ts3.4/preset.d.ts +0 -11
  134. package/dist/ts3.4/preview.d.ts +0 -6
  135. package/dist/ts3.4/shared.d.ts +0 -24
  136. package/dist/ts3.9/blocks/Anchor.d.ts +0 -6
  137. package/dist/ts3.9/blocks/ArgsTable.d.ts +0 -33
  138. package/dist/ts3.9/blocks/Canvas.d.ts +0 -9
  139. package/dist/ts3.9/blocks/Description.d.ts +0 -20
  140. package/dist/ts3.9/blocks/DocsContainer.d.ts +0 -7
  141. package/dist/ts3.9/blocks/DocsContext.d.ts +0 -5
  142. package/dist/ts3.9/blocks/DocsPage.d.ts +0 -2
  143. package/dist/ts3.9/blocks/DocsStory.d.ts +0 -3
  144. package/dist/ts3.9/blocks/Heading.d.ts +0 -6
  145. package/dist/ts3.9/blocks/Meta.d.ts +0 -9
  146. package/dist/ts3.9/blocks/Preview.d.ts +0 -3
  147. package/dist/ts3.9/blocks/Primary.d.ts +0 -6
  148. package/dist/ts3.9/blocks/Props.d.ts +0 -3
  149. package/dist/ts3.9/blocks/Source.d.ts +0 -38
  150. package/dist/ts3.9/blocks/SourceContainer.d.ts +0 -14
  151. package/dist/ts3.9/blocks/Stories.d.ts +0 -7
  152. package/dist/ts3.9/blocks/Story.d.ts +0 -28
  153. package/dist/ts3.9/blocks/Subheading.d.ts +0 -3
  154. package/dist/ts3.9/blocks/Subtitle.d.ts +0 -6
  155. package/dist/ts3.9/blocks/Title.d.ts +0 -8
  156. package/dist/ts3.9/blocks/Wrapper.d.ts +0 -2
  157. package/dist/ts3.9/blocks/enhanceSource.d.ts +0 -3
  158. package/dist/ts3.9/blocks/index.d.ts +0 -24
  159. package/dist/ts3.9/blocks/mdx.d.ts +0 -20
  160. package/dist/ts3.9/blocks/types.d.ts +0 -13
  161. package/dist/ts3.9/blocks/useStory.d.ts +0 -5
  162. package/dist/ts3.9/blocks/utils.d.ts +0 -3
  163. package/dist/ts3.9/preview.d.ts +0 -6
@@ -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,48 +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
- var _docs$description;
28
-
29
- description = (_docs$description = docs.description) === null || _docs$description === void 0 ? void 0 : _docs$description.story;
30
-
31
- if (!description) {
32
- description = docs.storyDescription;
33
- if (description) warnStoryDescription();
34
- }
35
- }
36
-
37
- const subheading = expanded && name;
38
- return /*#__PURE__*/React.createElement(Anchor, {
39
- storyId: id
40
- }, subheading && /*#__PURE__*/React.createElement(Subheading, null, subheading), description && /*#__PURE__*/React.createElement(Description, {
41
- markdown: description
42
- }), /*#__PURE__*/React.createElement(Canvas, {
43
- withToolbar: withToolbar
44
- }, /*#__PURE__*/React.createElement(Story, {
45
- id: id,
46
- parameters: parameters
47
- })));
48
- };
@@ -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 || 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,154 +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 => {
19
- var _story$parameters$doc, _story$parameters$doc2;
20
-
21
- return (_story$parameters$doc = story.parameters.docs) === null || _story$parameters$doc === void 0 ? void 0 : (_story$parameters$doc2 = _story$parameters$doc.source) === null || _story$parameters$doc2 === void 0 ? void 0 : _story$parameters$doc2.state;
22
- }).filter(Boolean);
23
- if (states.length === 0) return SourceState.CLOSED; // FIXME: handling multiple stories is a pain
24
-
25
- return states[0];
26
- };
27
-
28
- const getStorySource = (storyId, sourceContext) => {
29
- const {
30
- sources
31
- } = sourceContext; // source rendering is async so source is unavailable at the start of the render cycle,
32
- // so we fail gracefully here without warning
33
-
34
- return (sources === null || sources === void 0 ? void 0 : sources[storyId]) || {
35
- code: '',
36
- format: false
37
- };
38
- };
39
-
40
- const getSnippet = (snippet, story) => {
41
- var _parameters$docs, _parameters$docs$sour, _parameters$docs2, _parameters$docs2$sou, _enhanced$docs, _enhanced$docs$source;
42
-
43
- if (!story) {
44
- return snippet;
45
- }
46
-
47
- const {
48
- parameters
49
- } = story; // eslint-disable-next-line no-underscore-dangle
50
-
51
- const isArgsStory = parameters.__isArgsStory;
52
- const type = ((_parameters$docs = parameters.docs) === null || _parameters$docs === void 0 ? void 0 : (_parameters$docs$sour = _parameters$docs.source) === null || _parameters$docs$sour === void 0 ? void 0 : _parameters$docs$sour.type) || SourceType.AUTO; // if user has hard-coded the snippet, that takes precedence
53
-
54
- const userCode = (_parameters$docs2 = parameters.docs) === null || _parameters$docs2 === void 0 ? void 0 : (_parameters$docs2$sou = _parameters$docs2.source) === null || _parameters$docs2$sou === void 0 ? void 0 : _parameters$docs2$sou.code;
55
-
56
- if (userCode !== undefined) {
57
- return userCode;
58
- } // if user has explicitly set this as dynamic, use snippet
59
-
60
-
61
- if (type === SourceType.DYNAMIC) {
62
- var _parameters$docs3, _parameters$docs3$tra;
63
-
64
- return ((_parameters$docs3 = parameters.docs) === null || _parameters$docs3 === void 0 ? void 0 : (_parameters$docs3$tra = _parameters$docs3.transformSource) === null || _parameters$docs3$tra === void 0 ? void 0 : _parameters$docs3$tra.call(_parameters$docs3, snippet, story)) || snippet;
65
- } // if this is an args story and there's a snippet
66
-
67
-
68
- if (type === SourceType.AUTO && snippet && isArgsStory) {
69
- var _parameters$docs4, _parameters$docs4$tra;
70
-
71
- return ((_parameters$docs4 = parameters.docs) === null || _parameters$docs4 === void 0 ? void 0 : (_parameters$docs4$tra = _parameters$docs4.transformSource) === null || _parameters$docs4$tra === void 0 ? void 0 : _parameters$docs4$tra.call(_parameters$docs4, snippet, story)) || snippet;
72
- } // otherwise, use the source code logic
73
-
74
-
75
- const enhanced = enhanceSource(story) || parameters;
76
- return (enhanced === null || enhanced === void 0 ? void 0 : (_enhanced$docs = enhanced.docs) === null || _enhanced$docs === void 0 ? void 0 : (_enhanced$docs$source = _enhanced$docs.source) === null || _enhanced$docs$source === void 0 ? void 0 : _enhanced$docs$source.code) || '';
77
- };
78
-
79
- export const getSourceProps = (props, docsContext, sourceContext) => {
80
- const {
81
- id: currentId,
82
- storyById
83
- } = docsContext;
84
- const {
85
- parameters
86
- } = storyById(currentId);
87
- const codeProps = props;
88
- const singleProps = props;
89
- const multiProps = props;
90
- let source = codeProps.code; // prefer user-specified code
91
-
92
- let {
93
- format
94
- } = codeProps; // prefer user-specified code
95
-
96
- const targetIds = multiProps.ids || [singleProps.id || currentId];
97
- const storyIds = targetIds.map(targetId => targetId === CURRENT_SELECTION ? currentId : targetId);
98
- const stories = useStories(storyIds, docsContext);
99
-
100
- if (!stories.every(Boolean)) {
101
- return {
102
- error: SourceError.SOURCE_UNAVAILABLE,
103
- state: SourceState.NONE
104
- };
105
- }
106
-
107
- if (!source) {
108
- // just take the format from the first story, given how they're all concatinated together...
109
- // TODO: we should consider sending an event with all the sources separately, instead of concatenating them here
110
- ({
111
- format
112
- } = getStorySource(storyIds[0], sourceContext));
113
- source = storyIds.map((storyId, idx) => {
114
- const {
115
- code: storySource
116
- } = getStorySource(storyId, sourceContext);
117
- const storyObj = stories[idx];
118
- return getSnippet(storySource, storyObj);
119
- }).join('\n\n');
120
- }
121
-
122
- const state = getSourceState(stories);
123
- const {
124
- docs: docsParameters = {}
125
- } = parameters;
126
- const {
127
- source: sourceParameters = {}
128
- } = docsParameters;
129
- const {
130
- language: docsLanguage = null
131
- } = sourceParameters;
132
- return source ? {
133
- code: source,
134
- state,
135
- format,
136
- language: props.language || docsLanguage || 'jsx',
137
- dark: props.dark || false
138
- } : {
139
- error: SourceError.SOURCE_UNAVAILABLE,
140
- state
141
- };
142
- };
143
- /**
144
- * Story source doc block renders source code if provided,
145
- * or the source for a story if `storyId` is provided, or
146
- * the source for the current story if nothing is provided.
147
- */
148
-
149
- export const Source = props => {
150
- const sourceContext = useContext(SourceContext);
151
- const docsContext = useContext(DocsContext);
152
- const sourceProps = getSourceProps(props, docsContext, sourceContext);
153
- return /*#__PURE__*/React.createElement(PureSource, sourceProps);
154
- };
@@ -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,34 +0,0 @@
1
- function _extends() { _extends = Object.assign || 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 => {
16
- var _story$parameters, _story$parameters$doc;
17
-
18
- return !((_story$parameters = story.parameters) !== null && _story$parameters !== void 0 && (_story$parameters$doc = _story$parameters.docs) !== null && _story$parameters$doc !== void 0 && _story$parameters$doc.disable);
19
- });
20
- if (!includePrimary) stories = stories.slice(1);
21
-
22
- if (!stories || stories.length === 0) {
23
- return null;
24
- }
25
-
26
- return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Heading, null, title), stories.map(story => story && /*#__PURE__*/React.createElement(DocsStory, _extends({
27
- key: story.id
28
- }, story, {
29
- expanded: true
30
- }))));
31
- };
32
- Stories.defaultProps = {
33
- title: 'Stories'
34
- };
@@ -1,168 +0,0 @@
1
- import React, { useContext, useRef, useEffect, useState } from 'react';
2
- import { MDXProvider } from '@mdx-js/react';
3
- import global from 'global';
4
- import { resetComponents, Story as PureStory, StorySkeleton } from '@storybook/components';
5
- import { toId, storyNameFromExport } from '@storybook/csf';
6
- import { addons } from '@storybook/addons';
7
- import Events from '@storybook/core-events';
8
- import { CURRENT_SELECTION } from './types';
9
- import { DocsContext } from './DocsContext';
10
- import { useStory } from './useStory';
11
- export const storyBlockIdFromId = storyId => `story--${storyId}`;
12
- export const lookupStoryId = (storyName, {
13
- mdxStoryNameToKey,
14
- mdxComponentAnnotations
15
- }) => toId(mdxComponentAnnotations.id || mdxComponentAnnotations.title, storyNameFromExport(mdxStoryNameToKey[storyName]));
16
- export const getStoryId = (props, context) => {
17
- const {
18
- id
19
- } = props;
20
- const {
21
- name
22
- } = props;
23
- const inputId = id === CURRENT_SELECTION ? context.id : id;
24
- return inputId || lookupStoryId(name, context);
25
- };
26
- export const getStoryProps = ({
27
- height,
28
- inline
29
- }, story, context, onStoryFnCalled) => {
30
- const {
31
- name: storyName,
32
- parameters
33
- } = story;
34
- const {
35
- docs = {}
36
- } = parameters;
37
-
38
- if (docs.disable) {
39
- return null;
40
- } // prefer block props, then story parameters defined by the framework-specific settings and optionally overridden by users
41
-
42
-
43
- const {
44
- inlineStories = false,
45
- iframeHeight = 100,
46
- prepareForInline
47
- } = docs;
48
- const storyIsInline = typeof inline === 'boolean' ? inline : inlineStories;
49
-
50
- if (storyIsInline && !prepareForInline) {
51
- throw new Error(`Story '${storyName}' is set to render inline, but no 'prepareForInline' function is implemented in your docs configuration!`);
52
- }
53
-
54
- const boundStoryFn = () => {
55
- const storyResult = story.unboundStoryFn(Object.assign({}, context.getStoryContext(story), {
56
- loaded: {},
57
- abortSignal: undefined,
58
- canvasElement: undefined
59
- })); // We need to wait until the bound story function has actually been called before we
60
- // consider the story rendered. Certain frameworks (i.e. angular) don't actually render
61
- // the component in the very first react render cycle, and so we can't just wait until the
62
- // `PureStory` component has been rendered to consider the underlying story "rendered".
63
-
64
- onStoryFnCalled();
65
- return storyResult;
66
- };
67
-
68
- return Object.assign({
69
- inline: storyIsInline,
70
- id: story.id,
71
- height: height || (storyIsInline ? undefined : iframeHeight),
72
- title: storyName
73
- }, storyIsInline && {
74
- parameters,
75
- storyFn: () => prepareForInline(boundStoryFn, context.getStoryContext(story))
76
- });
77
- };
78
-
79
- function makeGate() {
80
- let open;
81
- const gate = new Promise(r => {
82
- open = r;
83
- });
84
- return [gate, open];
85
- }
86
-
87
- const Story = props => {
88
- const context = useContext(DocsContext);
89
- const channel = addons.getChannel();
90
- const storyRef = useRef();
91
- const storyId = getStoryId(props, context);
92
- const story = useStory(storyId, context);
93
- const [showLoader, setShowLoader] = useState(true);
94
- useEffect(() => {
95
- let cleanup;
96
-
97
- if (story && storyRef.current) {
98
- const element = storyRef.current;
99
- cleanup = context.renderStoryToElement(story, element);
100
- setShowLoader(false);
101
- }
102
-
103
- return () => cleanup && cleanup();
104
- }, [story]);
105
- const [storyFnRan, onStoryFnRan] = makeGate();
106
- const [rendered, onRendered] = makeGate();
107
- useEffect(onRendered);
108
-
109
- if (!story) {
110
- return /*#__PURE__*/React.createElement(StorySkeleton, null);
111
- }
112
-
113
- const storyProps = getStoryProps(props, story, context, onStoryFnRan);
114
-
115
- if (!storyProps) {
116
- return null;
117
- }
118
-
119
- if (storyProps.inline) {
120
- var _global$FEATURES;
121
-
122
- // If we are rendering a old-style inline Story via `PureStory` below, we want to emit
123
- // the `STORY_RENDERED` event when it renders. The modern mode below calls out to
124
- // `Preview.renderStoryToDom()` which itself emits the event.
125
- if (!(global !== null && global !== void 0 && (_global$FEATURES = global.FEATURES) !== null && _global$FEATURES !== void 0 && _global$FEATURES.modernInlineRender)) {
126
- // We need to wait for two things before we can consider the story rendered:
127
- // (a) React's `useEffect` hook needs to fire. This is needed for React stories, as
128
- // decorators of the form `<A><B/></A>` will not actually execute `B` in the first
129
- // call to the story function.
130
- // (b) The story function needs to actually have been called.
131
- // Certain frameworks (i.e.angular) don't actually render the component in the very first
132
- // React render cycle, so we need to wait for the framework to actually do that
133
- Promise.all([storyFnRan, rendered]).then(() => {
134
- channel.emit(Events.STORY_RENDERED, storyId);
135
- });
136
- } else {
137
- // We do this so React doesn't complain when we replace the span in a secondary render
138
- const htmlContents = `<span></span>`; // FIXME: height/style/etc. lifted from PureStory
139
-
140
- const {
141
- height
142
- } = storyProps;
143
- return /*#__PURE__*/React.createElement("div", {
144
- id: storyBlockIdFromId(story.id)
145
- }, /*#__PURE__*/React.createElement(MDXProvider, {
146
- components: resetComponents
147
- }, height ? /*#__PURE__*/React.createElement("style", null, `#story--${story.id} { min-height: ${height}; transform: translateZ(0); overflow: auto }`) : null, showLoader && /*#__PURE__*/React.createElement(StorySkeleton, null), /*#__PURE__*/React.createElement("div", {
148
- ref: storyRef,
149
- "data-name": story.name,
150
- dangerouslySetInnerHTML: {
151
- __html: htmlContents
152
- }
153
- })));
154
- }
155
- }
156
-
157
- return /*#__PURE__*/React.createElement("div", {
158
- id: storyBlockIdFromId(story.id)
159
- }, /*#__PURE__*/React.createElement(MDXProvider, {
160
- components: resetComponents
161
- }, /*#__PURE__*/React.createElement(PureStory, storyProps)));
162
- };
163
-
164
- Story.defaultProps = {
165
- children: null,
166
- name: null
167
- };
168
- export { Story };