Skip to content

Commit

Permalink
feat: add story config block
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed May 9, 2020
1 parent 848feb3 commit 6f69648
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 0 deletions.
27 changes: 27 additions & 0 deletions ui/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [<ins>PropsTable</ins>](#inspropstableins)
- [<ins>Stories</ins>](#insstoriesins)
- [<ins>Story</ins>](#insstoryins)
- [<ins>StoryConfig</ins>](#insstoryconfigins)
- [<ins>StorySource</ins>](#insstorysourceins)
- [<ins>Subtitle</ins>](#inssubtitleins)
- [<ins>Title</ins>](#institleins)
Expand Down Expand Up @@ -307,6 +308,32 @@ _Story [source code](https:/github.com/ccontrols/component-controls/tree/master/
| `collapsible` | _boolean_ | if false, will nothave a collapsible frame. |
| `sxStyle` | _SystemStyleObject_ | theme-ui styling object for Block Box |

## <ins>StoryConfig</ins>

Displays the configuration object of a story.

_StoryConfig [source code](https:/github.com/ccontrols/component-controls/tree/master/ui/blocks/src/StoryConfig/StoryConfig.tsx)_

### properties

| Name | Type | Description |
| ------------- | ------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | _string_ | id of the story optional id to be used for the block if no id is provided, one will be calculated automatically from the title. |
| `name` | _string_ | alternatively you can use the name of a story to load from an external file |
| `title` | _string_ | optional section title for the block. |
| `description` | _string_ | optional markdown description. |
| `collapsible` | _boolean_ | if false, will nothave a collapsible frame. |
| `sxStyle` | _SystemStyleObject_ | theme-ui styling object for Block Box |
| `actions` | _ActionItem\[]_ | optional actions provided to the component |
| `plain` | _boolean_ | if plain, skip the border and spacing around the children |
| `children` | _ReactNode_ | source code to be displayed. |
| `theme` | _PrismTheme_ | optional \`PrismTheme\` theme provided to the component. Themes can be imported from \`prism-react-renderer/themes\`. |
| `language` | _Language_ | source lnguage used, by default "jsx". |
| `renderFn` | _(props: RenderProps, other: { theme: PrismTheme; }) => ReactNode_ | custom function to render the source code. |
| `dark` | _boolean_ | used to specify a "dark" color theme - applcable only if no custom theme prop is provided. if dark: true, duotoneDark theme is used. if dark: false, duotoneLight theme is used. |
| `style` | _any_ | css styles for the container. |
| `as` | _any_ | syntax container as element. Can be used as \`div\` or \`span\`. |

## <ins>StorySource</ins>

Display source code of a story.
Expand Down
9 changes: 9 additions & 0 deletions ui/blocks/src/Playground/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
StoryBlockContainer,
StoryBlockContainerProps,
} from '../BlockContainer';
import { StoryConfig } from '../StoryConfig';
import { StorySource } from '../StorySource';

const IconButton = (props: ButtonProps) => (
Expand Down Expand Up @@ -76,6 +77,14 @@ export const Playground: FC<PlaygroundProps> = ({
<StorySource dark={isDark} sxStyle={{ mt: 0, mb: 0 }} id={storyId} />
),
});
userActions.push({
title: 'config',
id: 'config',
'aria-label': 'display story configuration object',
panel: (
<StoryConfig dark={isDark} sxStyle={{ mt: 0, mb: 0 }} id={storyId} />
),
});
}
const panels: ActionItems = getSortedPanels(userActions);

Expand Down
80 changes: 80 additions & 0 deletions ui/blocks/src/StoryConfig/StoryConfig.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import React from 'react';
import { ActionItem } from '@component-controls/components';
import { PrismTheme } from 'prism-react-renderer';
import dracula from 'prism-react-renderer/themes/dracula';
import duotoneDark from 'prism-react-renderer/themes/duotoneDark';
import duotoneLight from 'prism-react-renderer/themes/duotoneLight';
import github from 'prism-react-renderer/themes/github';
import nightOwl from 'prism-react-renderer/themes/nightOwl';
import nightOwlLight from 'prism-react-renderer/themes/nightOwlLight';
import oceanicNext from 'prism-react-renderer/themes/oceanicNext';
import palenight from 'prism-react-renderer/themes/palenight';
import shadesOfPurple from 'prism-react-renderer/themes/shadesOfPurple';

import { StoryConfig } from './StoryConfig';
import { MockContext } from '../test/MockContext';
export default {
title: 'Blocks/StoryConfig',
component: StoryConfig,
};

export const overview = () => (
<MockContext storyId="id-of-story">
<StoryConfig id="." />
</MockContext>
);

export const theme = () => (
<MockContext storyId="blocks-core-story-plain--controls">
<StoryConfig id="." theme={shadesOfPurple} />
</MockContext>
);

const themes: {
[key: string]: PrismTheme;
} = {
dracula,
duotoneDark,
duotoneLight,
github,
nightOwl,
nightOwlLight,
oceanicNext,
palenight,
shadesOfPurple,
};
export const themeSelector = () => {
const [theme, setTheme] = React.useState('dracula');
const themeAction: ActionItem = {
title: theme,
onClick: () => {
const themeNames = Object.keys(themes);
const selected = themeNames.indexOf(theme);
const nextTheme = selected < themeNames.length - 1 ? selected + 1 : 0;
setTheme(themeNames[nextTheme]);
},
};
return (
<MockContext storyId="blocks-core-story-plain--controls">
<StoryConfig id="." actions={[themeAction]} theme={themes[theme]} />
</MockContext>
);
};

export const customTitle = () => (
<MockContext storyId="id-of-story">
<StoryConfig title="Story configuration" />
</MockContext>
);

export const notCollapsible = () => (
<MockContext>
<StoryConfig title="." collapsible={false} />
</MockContext>
);

export const noMargin = () => (
<MockContext storyId="id-of-story">
<StoryConfig sxStyle={{ mt: 0, mb: 0 }} />
</MockContext>
);
59 changes: 59 additions & 0 deletions ui/blocks/src/StoryConfig/StoryConfig.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/** @jsx jsx */
/* eslint react/jsx-key: 0 */
import { jsx } from 'theme-ui';
import { FC, useState, useContext } from 'react';
import {
ThemeContext,
Source,
SourceProps,
ActionItem,
} from '@component-controls/components';
import { repositoryActions } from '../utils/repositoryActions';
import {
StoryBlockContainer,
StoryBlockContainerProps,
} from '../BlockContainer/story';

export type StoryConfigProps = StoryBlockContainerProps & SourceProps;

/**
* Displays the configuration object of a story.
*/
export const StoryConfig: FC<StoryConfigProps> = props => {
const [showFileSource, setShowFileSource] = useState<boolean>(false);

return (
<StoryBlockContainer {...props}>
{(context, sourceProps: SourceProps) => {
const onShowFileSource = () => setShowFileSource(!showFileSource);

const { story, kind, kindPackage } = context;
const { dark } = useContext(ThemeContext);
const allActions: ActionItem[] = [];
const repositoryItems = repositoryActions(kindPackage);
if (repositoryItems) {
allActions.push.apply(allActions, repositoryItems);
}
if (kind?.source) {
allActions.push({
title: showFileSource ? 'story code' : 'file code',
onClick: onShowFileSource,
});
}
const { loc, renderFn, source, arguments: storyArgs, ...other } =
story || {};
return Object.keys(other).length ? (
<Source
dark={dark}
language="json"
{...sourceProps}
actions={allActions}
>
{JSON.stringify(other, null, 2)}
</Source>
) : null;
}}
</StoryBlockContainer>
);
};
1 change: 1 addition & 0 deletions ui/blocks/src/StoryConfig/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './StoryConfig';
1 change: 1 addition & 0 deletions ui/blocks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from './Playground';
export * from './PropsTable';
export * from './Stories';
export * from './Story';
export * from './StoryConfig';
export * from './StorySource';
export * from './Subtitle';
export * from './Title';
Expand Down

0 comments on commit 6f69648

Please sign in to comment.