Skip to content

Commit b4392ad

Browse files
authored
docs: add docs tab in storybook (#61)
1 parent 98f7936 commit b4392ad

File tree

6 files changed

+356
-8
lines changed

6 files changed

+356
-8
lines changed

Diff for: .storybook/docs.tsx

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import React from 'react';
2+
3+
import {
4+
Description,
5+
DocsContext,
6+
Subtitle,
7+
Title,
8+
} from '@storybook/addon-docs/';
9+
10+
const readmeCache: Record<string, {default: string}> = {};
11+
12+
function importAllReadme(ctx: __WebpackModuleApi.RequireContext) {
13+
const path = ctx.id.split(' ')[0].replace('./', '') + '/';
14+
ctx.keys().forEach((key) => {
15+
const dirPath = key.replace(/^\.\//, path).replace(/\/readme\.md$/i, '');
16+
readmeCache[dirPath] = ctx(key);
17+
});
18+
}
19+
20+
importAllReadme(require.context('../src/components', true, /readme\.md$/i));
21+
22+
export const Docs = () => {
23+
const context = React.useContext(DocsContext);
24+
const fileName = context?.parameters?.fileName;
25+
const kind = context.kind;
26+
let isComponent = false;
27+
if (kind && /Components\//.test(kind)) {
28+
isComponent = true;
29+
}
30+
31+
let dirPath;
32+
if (isComponent && fileName) {
33+
const pathArr = fileName.split('/');
34+
dirPath = pathArr.slice(1, pathArr.length - 2).join('/');
35+
}
36+
37+
let sourceBadgeContent;
38+
if (dirPath) {
39+
sourceBadgeContent = (
40+
<a
41+
href={`https://github.com/gravity-ui/components/tree/main/${dirPath}`}
42+
target="_blank"
43+
rel="noopener noreferrer"
44+
>
45+
<img src="https://storage.yandexcloud.net/cloud-www-assets/constructor/storybook/icons/github-badge.svg" />
46+
</a>
47+
);
48+
}
49+
50+
let readmeContent;
51+
if (dirPath && readmeCache[dirPath]) {
52+
readmeContent = (
53+
<div
54+
className="yfm yfm_only-light"
55+
dangerouslySetInnerHTML={{__html: readmeCache[dirPath].default}}
56+
/>
57+
);
58+
}
59+
60+
return (
61+
<>
62+
{/* <Title /> */}
63+
{sourceBadgeContent}
64+
{/* <Subtitle /> */}
65+
{/* <Description /> */}
66+
{readmeContent}
67+
</>
68+
);
69+
};

Diff for: .storybook/main.ts

+18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
import type {StorybookConfig} from '@storybook/core-common';
22

3+
const {join} = require('path');
4+
5+
36
const config: StorybookConfig = {
47
stories: ['../src/**/*.stories.@(ts|tsx)'],
58
addons: [
69
'@storybook/preset-scss',
710
{name: '@storybook/addon-essentials', options: {backgrounds: false}},
811
'./theme-addon/register.tsx',
912
],
13+
14+
webpackFinal: (storybookBaseConfig) => {
15+
storybookBaseConfig?.module?.rules.push({
16+
test: /\.md$/,
17+
include: [join(__dirname, '../src')],
18+
use: [{loader: 'markdown-loader'}],
19+
});
20+
21+
// to turn fileName in context.parameters into path form number in production bundle
22+
if (storybookBaseConfig?.optimization) {
23+
storybookBaseConfig.optimization.moduleIds = 'named';
24+
}
25+
26+
return storybookBaseConfig;
27+
},
1028
};
1129

1230
module.exports = config;

Diff for: .storybook/preview.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import './styles.scss';
12
import '@gravity-ui/uikit/styles/styles.css';
23

34
import React from 'react';
@@ -7,6 +8,7 @@ import {ThemeProvider, MobileProvider, Lang, configure as uiKitConfigure} from '
78
import {configure} from '../src';
89
import {withMobile} from './decorators/withMobile';
910
import {withLang} from './decorators/withLang';
11+
import {Docs} from './docs';
1012

1113
configure({
1214
lang: Lang.En,
@@ -31,7 +33,7 @@ export const decorators = [withMobile, withLang, withContextProvider];
3133

3234
export const parameters = {
3335
docs: {
34-
theme: 'light',
36+
page: Docs,
3537
},
3638
jsx: {showFunctions: true},
3739
viewport: {

Diff for: .storybook/styles.scss

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import '~@doc-tools/transform/dist/css/yfm.css';

0 commit comments

Comments
 (0)