@storybook/addon-docs 7.0.0-alpha.6 → 7.0.0-alpha.60

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 (134) hide show
  1. package/README.md +14 -77
  2. package/angular/index.js +1 -1
  3. package/dist/DocsRenderer-FLOC7YSC.mjs +1 -0
  4. package/dist/chunk-HXSBEJGO.mjs +1 -0
  5. package/dist/chunk-MA2MUXQN.mjs +1 -0
  6. package/dist/index.d.ts +10 -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/dist/shims/mdx-react-shim.d.ts +1 -0
  16. package/dist/shims/mdx-react-shim.js +1 -0
  17. package/dist/shims/mdx-react-shim.mjs +1 -0
  18. package/jest-transform-mdx.js +4 -3
  19. package/jest.config.js +7 -0
  20. package/package.json +80 -46
  21. package/preset.js +1 -1
  22. package/preview.js +2 -1
  23. package/web-components/README.md +1 -0
  24. package/LICENSE +0 -21
  25. package/blocks.d.ts +0 -2
  26. package/blocks.js +0 -14
  27. package/dist/cjs/blocks/Anchor.js +0 -23
  28. package/dist/cjs/blocks/ArgsTable.js +0 -329
  29. package/dist/cjs/blocks/Canvas.js +0 -119
  30. package/dist/cjs/blocks/Description.js +0 -126
  31. package/dist/cjs/blocks/DocsContainer.js +0 -134
  32. package/dist/cjs/blocks/DocsContext.js +0 -25
  33. package/dist/cjs/blocks/DocsPage.js +0 -30
  34. package/dist/cjs/blocks/DocsRenderer.js +0 -50
  35. package/dist/cjs/blocks/DocsStory.js +0 -68
  36. package/dist/cjs/blocks/ExternalDocsContainer.js +0 -64
  37. package/dist/cjs/blocks/ExternalPreview.js +0 -99
  38. package/dist/cjs/blocks/Heading.js +0 -31
  39. package/dist/cjs/blocks/Meta.js +0 -64
  40. package/dist/cjs/blocks/Preview.js +0 -23
  41. package/dist/cjs/blocks/Primary.js +0 -39
  42. package/dist/cjs/blocks/Props.js +0 -29
  43. package/dist/cjs/blocks/Source.js +0 -185
  44. package/dist/cjs/blocks/SourceContainer.js +0 -67
  45. package/dist/cjs/blocks/Stories.js +0 -51
  46. package/dist/cjs/blocks/Story.js +0 -152
  47. package/dist/cjs/blocks/Subheading.js +0 -31
  48. package/dist/cjs/blocks/Subtitle.js +0 -39
  49. package/dist/cjs/blocks/Title.js +0 -44
  50. package/dist/cjs/blocks/Wrapper.js +0 -20
  51. package/dist/cjs/blocks/enhanceSource.js +0 -92
  52. package/dist/cjs/blocks/index.js +0 -394
  53. package/dist/cjs/blocks/mdx.js +0 -234
  54. package/dist/cjs/blocks/types.js +0 -10
  55. package/dist/cjs/blocks/useStory.js +0 -44
  56. package/dist/cjs/blocks/utils.js +0 -40
  57. package/dist/cjs/index.js +0 -18
  58. package/dist/cjs/manager.js +0 -20
  59. package/dist/cjs/preset.js +0 -162
  60. package/dist/cjs/preview.js +0 -22
  61. package/dist/cjs/shared.js +0 -22
  62. package/dist/esm/blocks/Anchor.js +0 -8
  63. package/dist/esm/blocks/ArgsTable.js +0 -292
  64. package/dist/esm/blocks/Canvas.js +0 -93
  65. package/dist/esm/blocks/Description.js +0 -107
  66. package/dist/esm/blocks/DocsContainer.js +0 -105
  67. package/dist/esm/blocks/DocsContext.js +0 -16
  68. package/dist/esm/blocks/DocsPage.js +0 -11
  69. package/dist/esm/blocks/DocsRenderer.js +0 -32
  70. package/dist/esm/blocks/DocsStory.js +0 -46
  71. package/dist/esm/blocks/ExternalDocsContainer.js +0 -48
  72. package/dist/esm/blocks/ExternalPreview.js +0 -89
  73. package/dist/esm/blocks/Heading.js +0 -17
  74. package/dist/esm/blocks/Meta.js +0 -45
  75. package/dist/esm/blocks/Preview.js +0 -9
  76. package/dist/esm/blocks/Primary.js +0 -23
  77. package/dist/esm/blocks/Props.js +0 -14
  78. package/dist/esm/blocks/Source.js +0 -150
  79. package/dist/esm/blocks/SourceContainer.js +0 -44
  80. package/dist/esm/blocks/Stories.js +0 -30
  81. package/dist/esm/blocks/Story.js +0 -123
  82. package/dist/esm/blocks/Subheading.js +0 -17
  83. package/dist/esm/blocks/Subtitle.js +0 -23
  84. package/dist/esm/blocks/Title.js +0 -24
  85. package/dist/esm/blocks/Wrapper.js +0 -8
  86. package/dist/esm/blocks/enhanceSource.js +0 -87
  87. package/dist/esm/blocks/index.js +0 -27
  88. package/dist/esm/blocks/mdx.js +0 -203
  89. package/dist/esm/blocks/types.js +0 -2
  90. package/dist/esm/blocks/useStory.js +0 -34
  91. package/dist/esm/blocks/utils.js +0 -29
  92. package/dist/esm/index.js +0 -1
  93. package/dist/esm/manager.js +0 -16
  94. package/dist/esm/preset.js +0 -132
  95. package/dist/esm/preview.js +0 -10
  96. package/dist/esm/shared.js +0 -11
  97. package/dist/types/blocks/Anchor.d.ts +0 -6
  98. package/dist/types/blocks/ArgsTable.d.ts +0 -33
  99. package/dist/types/blocks/Canvas.d.ts +0 -9
  100. package/dist/types/blocks/Description.d.ts +0 -20
  101. package/dist/types/blocks/DocsContainer.d.ts +0 -7
  102. package/dist/types/blocks/DocsContext.d.ts +0 -5
  103. package/dist/types/blocks/DocsPage.d.ts +0 -2
  104. package/dist/types/blocks/DocsRenderer.d.ts +0 -7
  105. package/dist/types/blocks/DocsStory.d.ts +0 -3
  106. package/dist/types/blocks/ExternalDocsContainer.d.ts +0 -4
  107. package/dist/types/blocks/ExternalPreview.d.ts +0 -19
  108. package/dist/types/blocks/Heading.d.ts +0 -6
  109. package/dist/types/blocks/Meta.d.ts +0 -12
  110. package/dist/types/blocks/Preview.d.ts +0 -3
  111. package/dist/types/blocks/Primary.d.ts +0 -6
  112. package/dist/types/blocks/Props.d.ts +0 -3
  113. package/dist/types/blocks/Source.d.ts +0 -38
  114. package/dist/types/blocks/SourceContainer.d.ts +0 -14
  115. package/dist/types/blocks/Stories.d.ts +0 -7
  116. package/dist/types/blocks/Story.d.ts +0 -30
  117. package/dist/types/blocks/Subheading.d.ts +0 -3
  118. package/dist/types/blocks/Subtitle.d.ts +0 -6
  119. package/dist/types/blocks/Title.d.ts +0 -8
  120. package/dist/types/blocks/Wrapper.d.ts +0 -2
  121. package/dist/types/blocks/enhanceSource.d.ts +0 -3
  122. package/dist/types/blocks/index.d.ts +0 -26
  123. package/dist/types/blocks/mdx.d.ts +0 -20
  124. package/dist/types/blocks/types.d.ts +0 -13
  125. package/dist/types/blocks/useStory.d.ts +0 -5
  126. package/dist/types/blocks/utils.d.ts +0 -3
  127. package/dist/types/index.d.ts +0 -1
  128. package/dist/types/manager.d.ts +0 -1
  129. package/dist/types/preset.d.ts +0 -12
  130. package/dist/types/preview.d.ts +0 -5
  131. package/dist/types/shared.d.ts +0 -24
  132. package/manager.js +0 -1
  133. package/mdx-compiler-plugin.js +0 -1
  134. package/register.js +0 -6
@@ -1,40 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getComponentName = void 0;
7
- exports.scrollToElement = scrollToElement;
8
-
9
- /* eslint-disable no-underscore-dangle */
10
- const titleCase = str => str.split('-').map(part => part.charAt(0).toUpperCase() + part.slice(1)).join('');
11
-
12
- const getComponentName = component => {
13
- if (!component) {
14
- return undefined;
15
- }
16
-
17
- if (typeof component === 'string') {
18
- if (component.includes('-')) {
19
- return titleCase(component);
20
- }
21
-
22
- return component;
23
- }
24
-
25
- if (component.__docgenInfo && component.__docgenInfo.displayName) {
26
- return component.__docgenInfo.displayName;
27
- }
28
-
29
- return component.name;
30
- };
31
-
32
- exports.getComponentName = getComponentName;
33
-
34
- function scrollToElement(element, block = 'start') {
35
- element.scrollIntoView({
36
- behavior: 'smooth',
37
- block,
38
- inline: 'nearest'
39
- });
40
- }
package/dist/cjs/index.js DELETED
@@ -1,18 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _blocks = require("./blocks");
8
-
9
- Object.keys(_blocks).forEach(function (key) {
10
- if (key === "default" || key === "__esModule") return;
11
- if (key in exports && exports[key] === _blocks[key]) return;
12
- Object.defineProperty(exports, key, {
13
- enumerable: true,
14
- get: function () {
15
- return _blocks[key];
16
- }
17
- });
18
- });
@@ -1,20 +0,0 @@
1
- "use strict";
2
-
3
- var _addons = require("@storybook/addons");
4
-
5
- var _shared = require("./shared");
6
-
7
- _addons.addons.register(_shared.ADDON_ID, () => {
8
- _addons.addons.add(_shared.PANEL_ID, {
9
- type: _addons.types.TAB,
10
- title: 'Docs',
11
- route: ({
12
- storyId,
13
- refId
14
- }) => refId ? `/docs/${refId}_${storyId}` : `/docs/${storyId}`,
15
- match: ({
16
- viewMode
17
- }) => viewMode === 'docs',
18
- render: () => null
19
- });
20
- });
@@ -1,162 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.storyIndexers = void 0;
7
- exports.webpack = webpack;
8
-
9
- var _fsExtra = _interopRequireDefault(require("fs-extra"));
10
-
11
- var _remarkSlug = _interopRequireDefault(require("remark-slug"));
12
-
13
- var _remarkExternalLinks = _interopRequireDefault(require("remark-external-links"));
14
-
15
- var _global = _interopRequireDefault(require("global"));
16
-
17
- var _nodeLogger = require("@storybook/node-logger");
18
-
19
- var _csfTools = require("@storybook/csf-tools");
20
-
21
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
-
23
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
24
-
25
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
26
-
27
- function createBabelOptions({
28
- babelOptions,
29
- mdxBabelOptions,
30
- configureJSX
31
- }) {
32
- const babelPlugins = (mdxBabelOptions === null || mdxBabelOptions === void 0 ? void 0 : mdxBabelOptions.plugins) || (babelOptions === null || babelOptions === void 0 ? void 0 : babelOptions.plugins) || [];
33
- const jsxPlugin = [require.resolve('@babel/plugin-transform-react-jsx'), {
34
- pragma: 'React.createElement',
35
- pragmaFrag: 'React.Fragment'
36
- }];
37
- const plugins = configureJSX ? [...babelPlugins, jsxPlugin] : babelPlugins;
38
- return Object.assign({
39
- // don't use the root babelrc by default (users can override this in mdxBabelOptions)
40
- babelrc: false,
41
- configFile: false
42
- }, babelOptions, mdxBabelOptions, {
43
- plugins
44
- });
45
- }
46
-
47
- async function webpack(webpackConfig = {}, options) {
48
- var _global$FEATURES, _global$FEATURES2;
49
-
50
- const resolvedBabelLoader = require.resolve('babel-loader');
51
-
52
- const {
53
- module = {}
54
- } = webpackConfig; // it will reuse babel options that are already in use in storybook
55
- // also, these babel options are chained with other presets.
56
-
57
- const {
58
- babelOptions,
59
- mdxBabelOptions,
60
- configureJSX = true,
61
- sourceLoaderOptions = {
62
- injectStoryParameters: true
63
- },
64
- transcludeMarkdown = false
65
- } = options;
66
- const mdxLoaderOptions = {
67
- skipCsf: true,
68
- remarkPlugins: [_remarkSlug.default, _remarkExternalLinks.default]
69
- };
70
- const mdxVersion = (_global$FEATURES = _global.default.FEATURES) !== null && _global$FEATURES !== void 0 && _global$FEATURES.previewMdx2 ? 'MDX2' : 'MDX1';
71
-
72
- _nodeLogger.logger.info(`Addon-docs: using ${mdxVersion}`);
73
-
74
- const mdxLoader = (_global$FEATURES2 = _global.default.FEATURES) !== null && _global$FEATURES2 !== void 0 && _global$FEATURES2.previewMdx2 ? require.resolve('@storybook/mdx2-csf/loader') : require.resolve('@storybook/mdx1-csf/loader'); // set `sourceLoaderOptions` to `null` to disable for manual configuration
75
-
76
- const sourceLoader = sourceLoaderOptions ? [{
77
- test: /\.(stories|story)\.[tj]sx?$/,
78
- loader: require.resolve('@storybook/source-loader'),
79
- options: Object.assign({}, sourceLoaderOptions, {
80
- inspectLocalDependencies: true
81
- }),
82
- enforce: 'pre'
83
- }] : [];
84
- let rules = module.rules || [];
85
-
86
- if (transcludeMarkdown) {
87
- rules = [...rules.filter(rule => {
88
- var _rule$test;
89
-
90
- return ((_rule$test = rule.test) === null || _rule$test === void 0 ? void 0 : _rule$test.toString()) !== '/\\.md$/';
91
- }), {
92
- test: /\.md$/,
93
- use: [{
94
- loader: resolvedBabelLoader,
95
- options: createBabelOptions({
96
- babelOptions,
97
- mdxBabelOptions,
98
- configureJSX
99
- })
100
- }, {
101
- loader: mdxLoader,
102
- options: mdxLoaderOptions
103
- }]
104
- }];
105
- }
106
-
107
- const result = Object.assign({}, webpackConfig, {
108
- module: Object.assign({}, module, {
109
- rules: [...rules, {
110
- test: /(stories|story)\.mdx$/,
111
- use: [{
112
- loader: resolvedBabelLoader,
113
- options: createBabelOptions({
114
- babelOptions,
115
- mdxBabelOptions,
116
- configureJSX
117
- })
118
- }, {
119
- loader: mdxLoader
120
- }]
121
- }, {
122
- test: /\.mdx$/,
123
- exclude: /(stories|story)\.mdx$/,
124
- use: [{
125
- loader: resolvedBabelLoader,
126
- options: createBabelOptions({
127
- babelOptions,
128
- mdxBabelOptions,
129
- configureJSX
130
- })
131
- }, {
132
- loader: mdxLoader,
133
- options: mdxLoaderOptions
134
- }]
135
- }, ...sourceLoader]
136
- })
137
- });
138
- return result;
139
- }
140
-
141
- const storyIndexers = async indexers => {
142
- const mdxIndexer = async (fileName, opts) => {
143
- var _global$FEATURES3;
144
-
145
- let code = (await _fsExtra.default.readFile(fileName, 'utf-8')).toString(); // @ts-ignore
146
-
147
- const {
148
- compile
149
- } = (_global$FEATURES3 = _global.default.FEATURES) !== null && _global$FEATURES3 !== void 0 && _global$FEATURES3.previewMdx2 ? await Promise.resolve().then(() => _interopRequireWildcard(require('@storybook/mdx2-csf'))) : await Promise.resolve().then(() => _interopRequireWildcard(require('@storybook/mdx1-csf')));
150
- code = await compile(code, {});
151
- return (0, _csfTools.loadCsf)(code, Object.assign({}, opts, {
152
- fileName
153
- })).parse();
154
- };
155
-
156
- return [{
157
- test: /(stories|story)\.mdx$/,
158
- indexer: mdxIndexer
159
- }, ...(indexers || [])];
160
- };
161
-
162
- exports.storyIndexers = storyIndexers;
@@ -1,22 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.parameters = void 0;
7
-
8
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
9
-
10
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
11
-
12
- const parameters = {
13
- docs: {
14
- renderer: async () => {
15
- const {
16
- DocsRenderer
17
- } = await Promise.resolve().then(() => _interopRequireWildcard(require('./blocks/DocsRenderer')));
18
- return new DocsRenderer();
19
- }
20
- }
21
- };
22
- exports.parameters = parameters;
@@ -1,22 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.SourceType = exports.SNIPPET_RENDERED = exports.PARAM_KEY = exports.PANEL_ID = exports.ADDON_ID = void 0;
7
- const ADDON_ID = 'storybook/docs';
8
- exports.ADDON_ID = ADDON_ID;
9
- const PANEL_ID = `${ADDON_ID}/panel`;
10
- exports.PANEL_ID = PANEL_ID;
11
- const PARAM_KEY = `docs`;
12
- exports.PARAM_KEY = PARAM_KEY;
13
- const SNIPPET_RENDERED = `${ADDON_ID}/snippet-rendered`;
14
- exports.SNIPPET_RENDERED = SNIPPET_RENDERED;
15
- let SourceType;
16
- exports.SourceType = SourceType;
17
-
18
- (function (SourceType) {
19
- SourceType["AUTO"] = "auto";
20
- SourceType["CODE"] = "code";
21
- SourceType["DYNAMIC"] = "dynamic";
22
- })(SourceType || (exports.SourceType = SourceType = {}));
@@ -1,8 +0,0 @@
1
- import React from 'react';
2
- export const anchorBlockIdFromId = storyId => `anchor--${storyId}`;
3
- export const Anchor = ({
4
- storyId,
5
- children
6
- }) => /*#__PURE__*/React.createElement("div", {
7
- id: anchorBlockIdFromId(storyId)
8
- }, children);
@@ -1,292 +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, useEffect, useState, useCallback } from 'react';
4
- import mapValues from 'lodash/mapValues';
5
- import { ArgsTable as PureArgsTable, ArgsTableError, TabbedArgsTable } from '@storybook/components';
6
- import { addons } from '@storybook/addons';
7
- import { filterArgTypes } from '@storybook/store';
8
- import Events from '@storybook/core-events';
9
- import { DocsContext } from './DocsContext';
10
- import { CURRENT_SELECTION, PRIMARY_STORY } from './types';
11
- import { getComponentName } from './utils';
12
- import { lookupStoryId } from './Story';
13
- import { useStory } from './useStory';
14
-
15
- const getContext = (storyId, context) => {
16
- const story = context.storyById(storyId);
17
-
18
- if (!story) {
19
- throw new Error(`Unknown story: ${storyId}`);
20
- }
21
-
22
- return context.getStoryContext(story);
23
- };
24
-
25
- const useArgs = (storyId, context) => {
26
- const channel = addons.getChannel();
27
- const storyContext = getContext(storyId, context);
28
- const [args, setArgs] = useState(storyContext.args);
29
- useEffect(() => {
30
- const cb = changed => {
31
- if (changed.storyId === storyId) {
32
- setArgs(changed.args);
33
- }
34
- };
35
-
36
- channel.on(Events.STORY_ARGS_UPDATED, cb);
37
- return () => channel.off(Events.STORY_ARGS_UPDATED, cb);
38
- }, [storyId]);
39
- const updateArgs = useCallback(updatedArgs => channel.emit(Events.UPDATE_STORY_ARGS, {
40
- storyId,
41
- updatedArgs
42
- }), [storyId]);
43
- const resetArgs = useCallback(argNames => channel.emit(Events.RESET_STORY_ARGS, {
44
- storyId,
45
- argNames
46
- }), [storyId]);
47
- return [args, updateArgs, resetArgs];
48
- };
49
-
50
- const useGlobals = (storyId, context) => {
51
- const channel = addons.getChannel();
52
- const storyContext = getContext(storyId, context);
53
- const [globals, setGlobals] = useState(storyContext.globals);
54
- useEffect(() => {
55
- const cb = changed => {
56
- setGlobals(changed.globals);
57
- };
58
-
59
- channel.on(Events.GLOBALS_UPDATED, cb);
60
- return () => channel.off(Events.GLOBALS_UPDATED, cb);
61
- }, []);
62
- return [globals];
63
- };
64
-
65
- export const extractComponentArgTypes = (component, {
66
- id,
67
- storyById
68
- }, include, exclude) => {
69
- const {
70
- parameters
71
- } = storyById(id);
72
- const {
73
- extractArgTypes
74
- } = parameters.docs || {};
75
-
76
- if (!extractArgTypes) {
77
- throw new Error(ArgsTableError.ARGS_UNSUPPORTED);
78
- }
79
-
80
- let argTypes = extractArgTypes(component);
81
- argTypes = filterArgTypes(argTypes, include, exclude);
82
- return argTypes;
83
- };
84
-
85
- const isShortcut = value => {
86
- return value && [CURRENT_SELECTION, PRIMARY_STORY].includes(value);
87
- };
88
-
89
- export const getComponent = (props = {}, {
90
- id,
91
- storyById
92
- }) => {
93
- const {
94
- of
95
- } = props;
96
- const {
97
- story
98
- } = props;
99
- const {
100
- component
101
- } = storyById(id);
102
-
103
- if (isShortcut(of) || isShortcut(story)) {
104
- return component || null;
105
- }
106
-
107
- if (!of) {
108
- throw new Error(ArgsTableError.NO_COMPONENT);
109
- }
110
-
111
- return of;
112
- };
113
-
114
- const addComponentTabs = (tabs, components, context, include, exclude, sort) => Object.assign({}, tabs, mapValues(components, comp => ({
115
- rows: extractComponentArgTypes(comp, context, include, exclude),
116
- sort
117
- })));
118
-
119
- export const StoryTable = props => {
120
- const context = useContext(DocsContext);
121
- const {
122
- id: currentId,
123
- componentStories
124
- } = context;
125
- const {
126
- story: storyName,
127
- component,
128
- subcomponents,
129
- showComponent,
130
- include,
131
- exclude,
132
- sort
133
- } = props;
134
-
135
- try {
136
- let storyId;
137
-
138
- switch (storyName) {
139
- case CURRENT_SELECTION:
140
- {
141
- storyId = currentId;
142
- break;
143
- }
144
-
145
- case PRIMARY_STORY:
146
- {
147
- const primaryStory = componentStories()[0];
148
- storyId = primaryStory.id;
149
- break;
150
- }
151
-
152
- default:
153
- {
154
- storyId = lookupStoryId(storyName, context);
155
- }
156
- }
157
-
158
- const story = useStory(storyId, context); // eslint-disable-next-line prefer-const
159
-
160
- let [args, updateArgs, resetArgs] = useArgs(storyId, context);
161
- const [globals] = useGlobals(storyId, context);
162
- if (!story) return /*#__PURE__*/React.createElement(PureArgsTable, {
163
- isLoading: true,
164
- updateArgs: updateArgs,
165
- resetArgs: resetArgs
166
- });
167
- const argTypes = filterArgTypes(story.argTypes, include, exclude);
168
- const mainLabel = getComponentName(component) || 'Story';
169
- let tabs = {
170
- [mainLabel]: {
171
- rows: argTypes,
172
- args,
173
- globals,
174
- updateArgs,
175
- resetArgs
176
- }
177
- }; // Use the dynamically generated component tabs if there are no controls
178
-
179
- const storyHasArgsWithControls = argTypes && Object.values(argTypes).find(v => !!v?.control);
180
-
181
- if (!storyHasArgsWithControls) {
182
- updateArgs = null;
183
- resetArgs = null;
184
- tabs = {};
185
- }
186
-
187
- if (component && (!storyHasArgsWithControls || showComponent)) {
188
- tabs = addComponentTabs(tabs, {
189
- [mainLabel]: component
190
- }, context, include, exclude);
191
- }
192
-
193
- if (subcomponents) {
194
- if (Array.isArray(subcomponents)) {
195
- throw new Error(`Unexpected subcomponents array. Expected an object whose keys are tab labels and whose values are components.`);
196
- }
197
-
198
- tabs = addComponentTabs(tabs, subcomponents, context, include, exclude);
199
- }
200
-
201
- return /*#__PURE__*/React.createElement(TabbedArgsTable, {
202
- tabs: tabs,
203
- sort: sort
204
- });
205
- } catch (err) {
206
- return /*#__PURE__*/React.createElement(PureArgsTable, {
207
- error: err.message
208
- });
209
- }
210
- };
211
- export const ComponentsTable = props => {
212
- const context = useContext(DocsContext);
213
- const {
214
- components,
215
- include,
216
- exclude,
217
- sort
218
- } = props;
219
- const tabs = addComponentTabs({}, components, context, include, exclude);
220
- return /*#__PURE__*/React.createElement(TabbedArgsTable, {
221
- tabs: tabs,
222
- sort: sort
223
- });
224
- };
225
- export const ArgsTable = props => {
226
- const context = useContext(DocsContext);
227
- const {
228
- id,
229
- storyById
230
- } = context;
231
- const {
232
- parameters: {
233
- controls
234
- },
235
- subcomponents
236
- } = storyById(id);
237
- const {
238
- include,
239
- exclude,
240
- components,
241
- sort: sortProp
242
- } = props;
243
- const {
244
- story: storyName
245
- } = props;
246
- const sort = sortProp || controls?.sort;
247
- const main = getComponent(props, context);
248
-
249
- if (storyName) {
250
- return /*#__PURE__*/React.createElement(StoryTable, _extends({}, props, {
251
- component: main,
252
- subcomponents,
253
- sort
254
- }));
255
- }
256
-
257
- if (!components && !subcomponents) {
258
- let mainProps;
259
-
260
- try {
261
- mainProps = {
262
- rows: extractComponentArgTypes(main, context, include, exclude)
263
- };
264
- } catch (err) {
265
- mainProps = {
266
- error: err.message
267
- };
268
- }
269
-
270
- return /*#__PURE__*/React.createElement(PureArgsTable, _extends({}, mainProps, {
271
- sort: sort
272
- }));
273
- }
274
-
275
- if (components) {
276
- return /*#__PURE__*/React.createElement(ComponentsTable, _extends({}, props, {
277
- components,
278
- sort
279
- }));
280
- }
281
-
282
- const mainLabel = getComponentName(main);
283
- return /*#__PURE__*/React.createElement(ComponentsTable, _extends({}, props, {
284
- components: Object.assign({
285
- [mainLabel]: main
286
- }, subcomponents),
287
- sort: sort
288
- }));
289
- };
290
- ArgsTable.defaultProps = {
291
- of: CURRENT_SELECTION
292
- };
@@ -1,93 +0,0 @@
1
- const _excluded = ["withSource", "mdxSource", "children"];
2
-
3
- function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
4
-
5
- import React, { useContext } from 'react';
6
- import { MDXProvider } from '@mdx-js/react';
7
- import { toId, storyNameFromExport } from '@storybook/csf';
8
- import { resetComponents, Preview as PurePreview, PreviewSkeleton } from '@storybook/components';
9
- import { DocsContext } from './DocsContext';
10
- import { SourceContext } from './SourceContainer';
11
- import { getSourceProps, SourceState } from './Source';
12
- import { useStories } from './useStory';
13
- import { CURRENT_SELECTION } from './types';
14
- export { SourceState };
15
-
16
- const getPreviewProps = (_ref, docsContext, sourceContext) => {
17
- let {
18
- withSource,
19
- mdxSource,
20
- children
21
- } = _ref,
22
- props = _objectWithoutPropertiesLoose(_ref, _excluded);
23
-
24
- const {
25
- mdxComponentAnnotations,
26
- mdxStoryNameToKey
27
- } = docsContext;
28
- let sourceState = withSource;
29
- let isLoading = false;
30
-
31
- if (sourceState === SourceState.NONE) {
32
- return {
33
- isLoading,
34
- previewProps: props
35
- };
36
- }
37
-
38
- if (mdxSource) {
39
- return {
40
- isLoading,
41
- previewProps: Object.assign({}, props, {
42
- withSource: getSourceProps({
43
- code: decodeURI(mdxSource)
44
- }, docsContext, sourceContext)
45
- })
46
- };
47
- }
48
-
49
- const childArray = Array.isArray(children) ? children : [children];
50
- const storyChildren = childArray.filter(c => c.props && (c.props.id || c.props.name || c.props.of));
51
- const targetIds = storyChildren.map(({
52
- props: {
53
- id,
54
- of,
55
- name
56
- }
57
- }) => {
58
- if (id) return id;
59
- if (of) return docsContext.storyIdByModuleExport(of);
60
- return toId(mdxComponentAnnotations.id || mdxComponentAnnotations.title, storyNameFromExport(mdxStoryNameToKey[name]));
61
- });
62
- const sourceProps = getSourceProps({
63
- ids: targetIds
64
- }, docsContext, sourceContext);
65
- if (!sourceState) sourceState = sourceProps.state;
66
- const storyIds = targetIds.map(targetId => targetId === CURRENT_SELECTION ? docsContext.id : targetId);
67
- const stories = useStories(storyIds, docsContext);
68
- isLoading = stories.some(s => !s);
69
- return {
70
- isLoading,
71
- previewProps: Object.assign({}, props, {
72
- // pass through columns etc.
73
- withSource: sourceProps,
74
- isExpanded: sourceState === SourceState.OPEN
75
- })
76
- };
77
- };
78
-
79
- export const Canvas = props => {
80
- const docsContext = useContext(DocsContext);
81
- const sourceContext = useContext(SourceContext);
82
- const {
83
- isLoading,
84
- previewProps
85
- } = getPreviewProps(props, docsContext, sourceContext);
86
- const {
87
- children
88
- } = props;
89
- if (isLoading) return /*#__PURE__*/React.createElement(PreviewSkeleton, null);
90
- return /*#__PURE__*/React.createElement(MDXProvider, {
91
- components: resetComponents
92
- }, /*#__PURE__*/React.createElement(PurePreview, previewProps, children));
93
- };