Skip to content

Commit

Permalink
feat: initial controls docs page
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Apr 2, 2020
1 parent 46f28a4 commit 02c2368
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 3 deletions.
7 changes: 6 additions & 1 deletion integrations/storybook/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { config } from 'docz-rollup';

export default config({
input: ['./src/index.ts', './src/preset.ts', './src/config.ts'],
input: [
'./src/index.ts',
'./src/preset.ts',
'./src/config.ts',
'./src/register.tsx',
],
});
2 changes: 1 addition & 1 deletion integrations/storybook/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { addDecorator } from '@storybook/client-api';
import { makeDecorator } from '@storybook/addons';
import { controlsMessages } from './context/BroadcastMessage';
// import './context/RenderDocsPages';

addDecorator(
makeDecorator({
name: 'componnet-controls',
parameterName: 'controls',
wrapper: (storyFn, context) => {
console.log(controlsMessages);
const values = controlsMessages && controlsMessages[context.id];
//@ts-ignore
return values ? storyFn(values, context) : storyFn(context);
Expand Down
15 changes: 15 additions & 0 deletions integrations/storybook/src/context/RenderDocsPages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { BroadcastChannel } from 'broadcast-channel';
import { DocsPage } from '../page/DocsPage';

const ATTACH_DOCS_PAGE = 'attach_docs_page';
const channel = new BroadcastChannel(ATTACH_DOCS_PAGE);

channel.onmessage = (message: { id: string; active: boolean }) => {
console.log('will attach');
ReactDOM.render(
<DocsPage active={message.active} />,
document.getElementById(message.id),
);
};
39 changes: 39 additions & 0 deletions integrations/storybook/src/page/DocsPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, { FC } from 'react';

import {
ControlsTable,
Title,
Subtitle,
Story,
Playground,
Stories,
Description,
ComponentSource,
PropsTable,
} from '@component-controls/blocks';
import { ThemeProvider, BlockContextProvider } from '../context';

interface DocsPageProps {
active?: boolean;
}
export const DocsPage: FC<DocsPageProps> = ({ active }) => {
if (!active) {
return null;
}
return (
<ThemeProvider>
<BlockContextProvider>
<Title />
<Subtitle />
<Description />
<ComponentSource id="." title="Component" />
<Playground openTab="source" title=".">
<Story id="." />
</Playground>
<ControlsTable id="." />
<PropsTable of="." />
<Stories dark={true} />
</BlockContextProvider>
</ThemeProvider>
);
};
2 changes: 2 additions & 0 deletions integrations/storybook/src/page/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ADDON_ID = 'storybook/page';
export const PANEL_ID = `${ADDON_ID}/panel`;
7 changes: 6 additions & 1 deletion integrations/storybook/src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
module.exports = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
config: (entry: any[] = [], options: any = {}) => {
const result = [...entry];
result.push(require.resolve('./config'));
return result;
},
managerEntries: (entry: any[] = [], options: any = {}) => {
const result = [...entry];
result.push(require.resolve('./register'));
return result;
},
};
34 changes: 34 additions & 0 deletions integrations/storybook/src/register.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable react/display-name */
import React from 'react';
import { addons, types } from '@storybook/addons';
import { ADDON_ID, PANEL_ID } from './page/constants';

interface AddonPanelProps {
active?: boolean;
id?: string;
}

const AddonPanel: React.FC<AddonPanelProps> = ({ active, id }) => {
const channel = React.useMemo(
() => new BroadcastChannel('attach_docs_page'),
[],
);
React.useEffect(() => {
console.log('will post message');
channel.postMessage({ id: id, active });
}, [active]);
return <div id={id} />;
};
addons.register(ADDON_ID, () => {
const title = 'Page';
const key = title.toLowerCase();
addons.add(PANEL_ID, {
type: types.TAB,
title,
route: ({ storyId }) => `/${key}/${storyId}`,
match: ({ viewMode }) => viewMode === key,
render: ({ active }) => (
<AddonPanel active={active} id={`controls-docs-page-${key}`} />
),
});
});

0 comments on commit 02c2368

Please sign in to comment.