|
5 | 5 | <!-- toc -->
|
6 | 6 |
|
7 | 7 | * [Finding components](#finding-components)
|
| 8 | +* [Loading and exposing components](#loading-and-exposing-components) |
8 | 9 | * [Sections](#sections)
|
9 | 10 | * [Limitations](#limitations)
|
10 | 11 |
|
@@ -41,6 +42,63 @@ module.exports = {
|
41 | 42 |
|
42 | 43 | > **Note:** Use [getComponentPathLine](Configuration.md#getcomponentpathline) option to change a path you see below a component name.
|
43 | 44 |
|
| 45 | +## Loading and exposing components |
| 46 | + |
| 47 | +Styleguidist _loads_ your components and _exposes_ them globally for your examples to consume. |
| 48 | + |
| 49 | +### Identifier |
| 50 | + |
| 51 | +It will try to use the `displayName` of your component as the identifier. If it cannot understand a `displayName` (for example if it is dynamically generated), it will fall back to something it can understand. |
| 52 | + |
| 53 | +In each of the following cases, the global identifier will be `Component`. |
| 54 | + |
| 55 | +| Path | Code | Styleguidist understands | |
| 56 | +| ---- | ---- | ------------------------ | |
| 57 | +| /whatever.js | `export default function Component() { ... }` | displayName | |
| 58 | +| /whatever.js | `export default function SomeName() { ... }`<br>`SomeName.displayName = 'Component';` | displayName | |
| 59 | +| /whatever.js | `export default function Component() { ... }`<br>`Component.displayName = dynamicNamer();` | displayName at declaration |
| 60 | +| /component.js | `const name = 'SomeName';`<br>`const componentMap = {`<br>`[name]: function() { ... }`<br>`};`<br>`export default componentMap[name];` | File name | |
| 61 | +| /component/index.js | `const name = 'SomeName';`<br>`const componentMap = {`<br>`[name]: function() { ... }`<br>`};`<br>`export default componentMap[name];` | Folder name | |
| 62 | + |
| 63 | + |
| 64 | +### Default vs named exports |
| 65 | + |
| 66 | +Stylegudist will use an ECMAScript module’s `default` export or CommonJS `module.exports` if they are defined. |
| 67 | + |
| 68 | +```javascript |
| 69 | +// /component.js |
| 70 | +export default function Component() { ... } |
| 71 | +// will be exposed globally as Component |
| 72 | + |
| 73 | +// /component.js |
| 74 | +function Component() { ... } |
| 75 | +module.exports = Component; |
| 76 | +// will be exposed globally as Component |
| 77 | +``` |
| 78 | + |
| 79 | +If you use only named exports, Styleguidist will expose named exports from modules as follows... |
| 80 | + |
| 81 | +If there is only one named export, it will expose that. |
| 82 | + |
| 83 | +```javascript |
| 84 | +// /component.js |
| 85 | +export function Component() { ... } |
| 86 | +// will be exposed globally as Component |
| 87 | +``` |
| 88 | + |
| 89 | +If there are several named exports, it will expose the named export which has the same name as the understood identifier. |
| 90 | + |
| 91 | +```javascript |
| 92 | +// /component.js |
| 93 | +export function someUtil() { ... } |
| 94 | +// will not be exposed |
| 95 | + |
| 96 | +export function Component() { ... } |
| 97 | +// will be exposed globally as Component |
| 98 | +``` |
| 99 | + |
| 100 | +If you export several React components as named exports from a single module, Styleguidist is likely to behave unreliably. If it cannot understand which named export to expose, you may not be able to access that export. |
| 101 | + |
44 | 102 | ## Sections
|
45 | 103 |
|
46 | 104 | Group components into sections or add extra Markdown documents to your style guide.
|
|
0 commit comments