Skip to content

Commit

Permalink
feat: source stories
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Feb 19, 2020
1 parent 5834ec3 commit 6942eda
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 13 deletions.
76 changes: 76 additions & 0 deletions core/editors/src/blocks/Source/Source.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React from 'react';

import { Source, SourceProps, themes } from './Source';

export default {
title: 'Components/Source',
component: Source,
};

export const simpleSource = ({ language, children, theme }: SourceProps) => {
return (
<Source language={language} theme={theme}>
{children}
</Source>
);
};

const languages: string[] = [
'markup',
'bash',
'clike',
'c',
'cpp',
'css',
'javascript',
'jsx',
'coffeescript',
'actionscript',
'css-extr',
'diff',
'git',
'go',
'graphql',
'handlebars',
'json',
'less',
'makefile',
'markdown',
'objectivec',
'ocaml',
'python',
'reason',
'sass',
'scss',
'sql',
'stylus',
'tsx',
'typescript',
'wasm',
'yaml',
];

simpleSource.story = {
parameters: {
controls: {
language: { type: 'options', options: languages, value: 'jsx' },
children: {
type: 'text',
rows: 10,
value: `
export const sample = () => {
const [state, setState] = React.useState(false);
return (
<BooleanEditor
name="prop"
onChange={(name, newVal) => setState(newVal)}
prop={{ type: ControlTypes.BOOLEAN, value: state }}
/>
);
};
`,
},
theme: { type: 'options', options: Object.keys(themes) },
},
},
};
44 changes: 33 additions & 11 deletions core/editors/src/blocks/Source/Source.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ import copy from 'copy-to-clipboard';
import { ActionBar } from '../../components/ActionBar/ActionBar';
import { BlockContainer } from '../BlockContainer/BlockContainer';

const themes: {
[key: string]: PrismTheme;
export type ThemeType =
| 'nightowl-light'
| 'nightowl'
| 'github'
| 'vs-dark'
| 'oceanic-next'
| 'palenight'
| 'ultramin'
| 'duotone-light'
| 'duotone-dark'
| 'dracula'
| 'shades-of-purple';
export const themes: {
[key in ThemeType]: PrismTheme;
} = {
'nightowl-light': nightOwlLight,
nightowl,
Expand All @@ -39,21 +51,30 @@ const themes: {
export interface SourceProps {
children?: string;
language?: Language;
/**
* prism theme passed from parent
* if the theme is selected in the parent, the Source
* componnet will not have a thmme selection option
*/
theme?: ThemeType;
}

export const Source: FC<SourceProps> = ({
children = '',
language = 'jsx',
theme: parentTheme,
}) => {
const [themeName, setThemeName] = React.useState<string>('nightowl-light');
const [themeName, setThemeName] = React.useState<ThemeType>(
parentTheme || 'nightowl-light',
);
let prismTheme = themes[themeName] || defaultProps.theme;
const [copied, setCopied] = React.useState(false);

const onRotateTheme = () => {
const themeKeys = Object.keys(themes);
const themeIdx = themeKeys.indexOf(themeName);
const newIdx = themeIdx >= themeKeys.length - 1 ? 0 : themeIdx + 1;
setThemeName(themeKeys[newIdx]);
setThemeName(themeKeys[newIdx] as ThemeType);
};

const onCopy = (e: MouseEvent<HTMLButtonElement>) => {
Expand All @@ -62,12 +83,18 @@ export const Source: FC<SourceProps> = ({
copy(children);
window.setTimeout(() => setCopied(false), 1500);
};

const actions = [];
if (parentTheme === undefined) {
actions.push({ title: themeName, onClick: onRotateTheme });
}
actions.push({ title: copied ? 'Copied' : 'Copy', onClick: onCopy });
return (
<BlockContainer>
<Highlight
{...defaultProps}
theme={prismTheme}
code={typeof children === 'string' ? children.trim() : ''}
code={typeof children === 'string' ? children : ''}
language={language}
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
Expand All @@ -88,12 +115,7 @@ export const Source: FC<SourceProps> = ({
</Styled.pre>
)}
</Highlight>
<ActionBar
actionItems={[
{ title: themeName, onClick: onRotateTheme },
{ title: copied ? 'Copied' : 'Copy', onClick: onCopy },
]}
/>
<ActionBar actionItems={actions} />
</BlockContainer>
);
};
8 changes: 7 additions & 1 deletion core/editors/src/editors/OptionsEditor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ export const normalizeOptions = (
entries = options.reduce((acc: NormalizedOptions, o) => {
return [...acc, findLabelOption(null, o)];
}, []) as NormalizedOptions;
} else {
} else if (typeof options === 'object') {
entries = Object.keys(options).reduce(
(acc: NormalizedOptions, key: string) => {
return [...acc, findLabelOption(key, options[key])];
},
[],
);
} else {
console.error('invalid options parameter passed to controls');
return {
entries: [],
selected: [],
};
}
const selected = entries
.filter(option => {
Expand Down
2 changes: 1 addition & 1 deletion integrations/storybook/src/blocks/ControlsEditorsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const ControlsEditorsTableBlock: FC<ControlsEditorsTableProps> = ({
}
}
};
if (!story || controls.disable) {
if (!story || !controls || controls.disable) {
return null;
}

Expand Down

0 comments on commit 6942eda

Please sign in to comment.