Skip to content

Commit

Permalink
feat: added theme, components for PageContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Apr 16, 2020
1 parent 9f2acfa commit ae6f7c1
Show file tree
Hide file tree
Showing 36 changed files with 391 additions and 196 deletions.
9 changes: 5 additions & 4 deletions examples/storybook-6-no-docs/.storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ module.exports = {
},
],
stories: [
//'../../../ui/editors/src/**/*.stories.(js|tsx|mdx)',
//'../../../ui/components/src/**/*.stories.(js|tsx|mdx)',
//'../../../ui/blocks/src/**/*.stories.(js|tsx|mdx)',
'../../stories/src/stories/smart-controls.stories.mdx',
'../../../ui/editors/src/**/*.stories.(js|tsx|mdx)',
'../../../ui/components/src/**/*.stories.(js|tsx|mdx)',
'../../../ui/blocks/src/**/*.stories.(js|tsx|mdx)',
'../../stories/src/**/*.stories.(js|tsx|mdx)',
'../stories/**/*.stories.(js|tsx|mdx)',
],
webpackFinal: async (config, { configType }) => {
return {
Expand Down
26 changes: 3 additions & 23 deletions examples/storybook-6-no-docs/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,17 @@
import React from 'react'
import { addDecorator, addParameters } from '@storybook/react';
import { ThemeProvider, BlockContextProvider } from '@component-controls/storybook';
import { ControlsTable, Title, Subtitle, Story, Playground, Stories, Description, ComponentSource, PropsTable } from '@component-controls/blocks';
import { ThemeProvider, useIsDark } from '@component-controls/storybook';

addDecorator((story, ctx ) => {
const isDark = useIsDark();
return (
<ThemeProvider>
<ThemeProvider dark={isDark}>
{story(ctx)}
</ThemeProvider>
);
})

export const DocsPage = () => {
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>
);
};
const categories = ['Storybook', 'Blocks', 'Editors', 'Components']
addParameters({
docs_xxx: { page: DocsPage },
dependencies: { hideEmpty: true },
options: {
storySort: (a, b) => {
Expand Down
1 change: 0 additions & 1 deletion examples/storybook-6-no-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"@storybook/addon-docs": "next",
"@storybook/addon-storysource": "next",
"@storybook/react": "next",
"@storybook/theming": "next",
"babel-loader": "^8.0.6",
"babel-preset-react-app": "^9.0.0",
"cross-env": "^5.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Meta, PropsTable, Playground, Story, BlockContextProvider, ControlsTable, ComponentSource, StorySource } from '@component-controls/storybook';
import { Button } from '../components/Button';

import { Meta} from '@component-controls/storybook';
import { PropsTable, Playground, Story, ControlsTable, ComponentSource, StorySource } from '@component-controls/blocks';
import { Button } from '../../stories/src/components/Button';

<Meta title="Storybook/MDX" parameters={{component: Button}} />


# Smart controls
<ComponentSource of={Button} title='Component source' />

Expand Down
1 change: 1 addition & 0 deletions examples/storybook-6/.storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
'../../../ui/components/src/**/*.stories.(js|tsx|mdx)',
'../../../ui/blocks/src/**/*.stories.(js|tsx|mdx)',
'../../stories/src/**/*.stories.(js|tsx|mdx)',
'../stories/**/*.stories.(js|tsx|mdx)',
],
addons: [
'@storybook/addon-docs',
Expand Down
25 changes: 3 additions & 22 deletions examples/storybook-6/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
import React from 'react'
import { addDecorator, addParameters } from '@storybook/react';
import { ThemeProvider, BlockContextProvider } from '@component-controls/storybook';
import { ControlsTable, Title, Subtitle, Story, Playground, Stories, Description, ComponentSource, PropsTable } from '@component-controls/blocks';
import { ThemeProvider, useIsDark } from '@component-controls/storybook';

addDecorator((story, ctx ) => {
const isDark = useIsDark();
return (
<ThemeProvider>
<ThemeProvider dark={isDark}>
{story(ctx)}
</ThemeProvider>
);
})

export const DocsPage = () => {
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>
);
};
const categories = ['Storybook', 'Blocks', 'Editors', 'Components']
addParameters({
docs_xxx: { page: DocsPage },
dependencies: { hideEmpty: true },
options: {
storySort: (a, b) => {
Expand Down
1 change: 0 additions & 1 deletion examples/storybook-6/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"@storybook/addon-docs": "next",
"@storybook/addon-storysource": "next",
"@storybook/react": "next",
"@storybook/theming": "next",
"babel-loader": "^8.0.6",
"babel-preset-react-app": "^9.0.0",
"cross-env": "^5.2.0",
Expand Down
45 changes: 45 additions & 0 deletions examples/storybook-6/stories/smart-controls.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Meta, PropsTable, Playground as Preview, Story, BlockContextProvider, ControlsTable, ComponentSource, StorySource } from '@component-controls/storybook';
import { Button } from '../../stories/src/components/Button';

<Meta title="Storybook/MDX" parameters={{component: Button}} />


# Smart controls
<ComponentSource of={Button} title='Component source' />

<PropsTable of={Button} />


<Preview>
<Story name="smart story">
{(props) => (
<Button label="default" {...props}/>
)}
</Story>
</Preview>


# Small story with custom controls

<Preview>
<Story name="small story"
controls={{
text: {type: 'text', value: 'Hello'},
}}
>
{({ text }) => (
<Button label={text}/>
)}
</Story>
</Preview>


<ControlsTable name="small story" />
# Story with no parameters == no smart controls

<Preview>
<Story name="no controls">
<Button label="Hello"/>
<Button label="Second button"/>
</Story>
</Preview>
6 changes: 1 addition & 5 deletions integrations/storybook/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
```diff
- WARNING - THIS ADDON CAN NOT YET BE USED AS SOME OF THE STORYBOOK FEATURES IT REQUIRES ARE NOT YET MERGED
```

## Table of contents

* [Storybook](#storybook)
Expand Down Expand Up @@ -32,7 +28,7 @@

## Storybook

The Storybook integration of the component-controls (aka "Addon Controls") allows you to define and then edit story properties dynamically in the [Storybook](https://storybook.js.org) UI with a set of property editors.
The Storybook integration of component-controls allows you to define and then edit story properties dynamically in the [Storybook](https://storybook.js.org) UI with a set of property editors.


The definitions of the control properties can be found [here](https://github.com/ccontrols/component-controls/blob/master/core/specification/src/types.ts):
Expand Down
2 changes: 0 additions & 2 deletions integrations/storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"@storybook/api": "next",
"@storybook/client-api": "next",
"@storybook/react": "next",
"@storybook/theming": "next",
"@types/node": "^13.7.0",
"babel-loader": "^8.0.6",
"babel-preset-react-app": "^9.0.0"
Expand All @@ -62,7 +61,6 @@
"@storybook/api": "*",
"@storybook/client-api": "*",
"@storybook/react": "*",
"@storybook/theming": "*",
"polished": "*",
"react": "*"
},
Expand Down
4 changes: 2 additions & 2 deletions integrations/storybook/src/blocks/ControlsTable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { FC } from 'react';
import {
ControlsTable as BaseControlsTable,
ControlsTable as BlockControlsTable,
ControlsTableProps,
useStoryContext,
} from '@component-controls/blocks';
Expand All @@ -24,7 +24,7 @@ export const ControlsTable: FC<ControlsTableProps> = ({

return id ? (
<ThemeProvider>
<BaseControlsTable id={id} {...rest} />
<BlockControlsTable id={id} {...rest} />
</ThemeProvider>
) : null;
};
12 changes: 5 additions & 7 deletions integrations/storybook/src/context/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import React from 'react';
import { getLuminance } from 'polished';
import { ThemeContext, Theme } from '@storybook/theming';
import { ThemeProvider as ThemeUIProvider } from '@component-controls/components';
import { BlockContextProvider } from './BlockContext';
import { useIsDark } from './useIsDark';

export const ThemeProvider: React.FC = ({ children }) => {
const { background: { content = '#ffffff' } = {} } = React.useContext<Theme>(
ThemeContext as any,
);
const isDark = useIsDark();
return (
<ThemeUIProvider dark={getLuminance(content) < 0.5}>
{children}
<ThemeUIProvider dark={isDark}>
<BlockContextProvider>{children}</BlockContextProvider>
</ThemeUIProvider>
);
};
1 change: 1 addition & 0 deletions integrations/storybook/src/context/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './BlockContext';
export * from './ThemeProvider';
export * from './useIsDark';
19 changes: 19 additions & 0 deletions integrations/storybook/src/context/useIsDark.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';

const prefersDark = window.matchMedia('(prefers-color-scheme: dark)');

export const useIsDark = (): boolean => {
const [isDark, setIsDark] = React.useState(prefersDark.matches);
React.useEffect(() => {
//inspired from: https://github.com/hipstersmoothie/storybook-dark-mode/blob/46476b4e96a5b310f13e7dccc69e2baa5e477814/src/Tool.tsx#L111
const prefersDarkUpdate = (event: MediaQueryListEvent) => {
setIsDark(event.matches);
};
prefersDark.addListener(prefersDarkUpdate);

return () => {
prefersDark.removeListener(prefersDarkUpdate);
};
});
return isDark;
};
9 changes: 3 additions & 6 deletions integrations/storybook/src/page/PageContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React, { FC } from 'react';
import { getLuminance } from 'polished';
import { ThemeContext, Theme } from '@storybook/theming';
import { addons } from '@storybook/addons';
import { STORY_CHANGED } from '@storybook/core-events';
import {
PageContainer as BlockPageContainer,
PageContainerProps,
} from '@component-controls/blocks';
import { useIsDark } from '../context/useIsDark';

export const PageContextContainer: FC<PageContainerProps> = ({
children,
Expand All @@ -15,10 +14,8 @@ export const PageContextContainer: FC<PageContainerProps> = ({
const [storyId, setStoryId] = React.useState<string | undefined>(
defaultStoryId,
);
const isDark = useIsDark();
const channel = React.useMemo(() => addons.getChannel(), []);
const { background: { content = '#ffffff' } = {} } = React.useContext<Theme>(
ThemeContext as any,
);

React.useEffect(() => {
const onStoryChange = (id: string) => {
Expand All @@ -31,7 +28,7 @@ export const PageContextContainer: FC<PageContainerProps> = ({
};
}, []);
return (
<BlockPageContainer dark={getLuminance(content) < 0.5} storyId={storyId}>
<BlockPageContainer dark={isDark} storyId={storyId}>
{children}
</BlockPageContainer>
);
Expand Down
3 changes: 3 additions & 0 deletions ui/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@component-controls/editors": "^0.6.0",
"@component-controls/specification": "^0.6.0",
"@component-controls/store": "^0.6.0",
"@mdx-js/react": "^1.5.8",
"@storybook/csf": "^0.0.1",
"copy-to-clipboard": "^3.2.1",
"global": "^4.3.2",
Expand All @@ -47,7 +48,9 @@
"theme-ui": "^0.3.1"
},
"devDependencies": {
"@theme-ui/presets": "^0.3.0",
"@types/jest": "^25.1.2",
"@types/mdx-js__react":"^1.5.1",
"@types/theme-ui": "^0.3.0",
"@types/qs": "^6.9.1",
"cross-env": "^5.2.1",
Expand Down
5 changes: 2 additions & 3 deletions ui/blocks/src/BlockContainer/story/StoryBlockContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ export const StoryBlockContainer: FC<StoryBlockContainerProps> = ({
id,
name,
});
const { component, kind, story } = context;
const { story } = context;
const title = getStoryBlockTitle({
kind,
component,
story,
title: userTitle,
});
const block = children(context, rest);
Expand Down
2 changes: 1 addition & 1 deletion ui/blocks/src/ComponentSource/ComponentSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const ComponentSource: FC<ComponentSourceProps> = ({

if (component && component.source) {
allActions.push({
title: showFileSource ? 'import' : 'component',
title: showFileSource ? 'import' : 'source',
onClick: onShowFileSource,
});
}
Expand Down
Loading

0 comments on commit ae6f7c1

Please sign in to comment.