Skip to content

Commit

Permalink
feat: source component props
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Feb 27, 2020
1 parent d8a8231 commit 7dfe0ca
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 60 deletions.
11 changes: 6 additions & 5 deletions blocks/core/src/ActionBar/ActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,19 @@ export interface ActionItem {

export interface ActionBarProps {
actionItems: ActionItem[];
zIndex?: number;
}

export const ActionBar: FunctionComponent<ActionBarProps> = ({
actionItems,
zIndex,
...props
}) => (
<Container zIndex={zIndex} {...props}>
<Container {...props}>
{actionItems.map(({ title, onClick, disabled }, index: number) => (
// eslint-disable-next-line react/no-array-index-key
<ActionButton key={index} onClick={onClick} disabled={disabled}>
<ActionButton
key={`${typeof title === 'string' ? title : 'item'}_${index}`}
onClick={onClick}
disabled={disabled}
>
{title}
</ActionButton>
))}
Expand Down
5 changes: 3 additions & 2 deletions blocks/core/src/Source/Source.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export default {
component: Source,
};

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

const languages: string[] = [
Expand Down Expand Up @@ -49,6 +49,7 @@ simpleSource.story = {
parameters: {
controls: {
language: { type: 'options', options: languages, value: 'jsx' },
dark: { type: 'boolean' },
source: {
type: 'text',
rows: 10,
Expand Down
66 changes: 59 additions & 7 deletions blocks/core/src/Source/Source.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,72 @@
/** @jsx jsx */
/* eslint react/jsx-key: 0 */
import { jsx } from 'theme-ui';
import React from 'react';
import React, { FC, MouseEvent } from 'react';
import { Styled } from 'theme-ui';
import copy from 'copy-to-clipboard';
import Highlight, {
defaultProps,
PrismTheme,
Language,
} from 'prism-react-renderer';
import duotoneDark from 'prism-react-renderer/themes/duotoneDark';
import duotoneLight from 'prism-react-renderer/themes/duotoneLight';

import { BlockContainer } from '../BlockContainer';
import { ActionBar, ActionItem } from '../ActionBar';

export interface SourceProps {
/**
* source code to display
*/
source: string;
/**
* optional theme provided to the component
*/
theme?: PrismTheme;
/**
* code lnguage used, by default "jsx"
*/
language?: Language;
/**
* custom function to render the source code
*/
children?: (props: any) => React.ReactNode;
/**
* additional actions provided to the component
*/
actions?: ActionItem[];
/**
* used to specify a "dark" color theme - applcable only if no custom theme prop is provided
*/
dark?: boolean;
}
export const Source: React.FC<SourceProps> = ({

/**
* Source component used to display source code
*
*/
export const Source: FC<SourceProps> = ({
source = '',
language = 'jsx',
theme,
children,
actions,
dark = false,
}) => {
console.log(source);
const [copied, setCopied] = React.useState(false);

const onCopy = (e: MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
setCopied(true);
copy(source);
window.setTimeout(() => setCopied(false), 1500);
};

const actionsItems = actions || [];

actionsItems.push({ title: copied ? 'copied' : 'copy', onClick: onCopy });

const renderProps =
typeof children === 'function'
? children
Expand All @@ -42,12 +87,19 @@ export const Source: React.FC<SourceProps> = ({
))}
</Styled.pre>
);
const props = { ...defaultProps };
if (theme) {
defaultProps.theme = theme;
props.theme = theme;
} else {
props.theme = dark ? duotoneDark : duotoneLight;
}

return (
<Highlight {...defaultProps} code={source} language={language}>
{renderProps}
</Highlight>
<BlockContainer>
<Highlight {...props} code={source} language={language}>
{renderProps}
</Highlight>
<ActionBar actionItems={actionsItems} />
</BlockContainer>
);
};
31 changes: 8 additions & 23 deletions blocks/core/src/StorySource/StorySource.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, MouseEvent } from 'react';
import React, { FC } from 'react';
import { StoryArguments } from '@component-controls/specification';
import { LoadedComponentControls } from '@component-controls/core';
import { defaultProps, Language, PrismTheme } from 'prism-react-renderer';
Expand All @@ -14,9 +14,6 @@ import palenight from 'prism-react-renderer/themes/palenight';
import shadesOfPurple from 'prism-react-renderer/themes/shadesOfPurple';
import ultramin from 'prism-react-renderer/themes/ultramin';
import vsDark from 'prism-react-renderer/themes/vsDark';
import copy from 'copy-to-clipboard';
import { ActionBar } from '../ActionBar';
import { BlockContainer } from '../BlockContainer';
import { mergeControlValues } from './argument-utils';
import { TaggedSource } from './TaggedSource';
export type ThemeType =
Expand Down Expand Up @@ -98,7 +95,6 @@ export const StorySource: FC<StorySourceProps> = ({
const [showFileSource, setShowFileSource] = React.useState<boolean>(false);

let prismTheme = themes[themeName] || defaultProps.theme;
const [copied, setCopied] = React.useState(false);

const onRotateTheme = () => {
const themeKeys = Object.keys(themes);
Expand All @@ -117,13 +113,6 @@ export const StorySource: FC<StorySourceProps> = ({
);
const onShowFileSource = () => setShowFileSource(!showFileSource);

const onCopy = (e: MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
setCopied(true);
copy(children);
window.setTimeout(() => setCopied(false), 1500);
};

const actions = [];
if (fileSource) {
actions.push({
Expand All @@ -138,8 +127,6 @@ export const StorySource: FC<StorySourceProps> = ({
actions.push({ title: viewStyle, onClick: onMergeValues });
}

actions.push({ title: copied ? 'copied' : 'copy', onClick: onCopy });

let source: string;
if (!showFileSource) {
let code = typeof children === 'string' ? children : '';
Expand All @@ -151,14 +138,12 @@ export const StorySource: FC<StorySourceProps> = ({
source = fileSource || '';
}
return (
<BlockContainer>
<TaggedSource
args={viewStyle === 'tags' && !showFileSource ? args : undefined}
source={source}
theme={prismTheme}
language={language}
/>
<ActionBar actionItems={actions} />
</BlockContainer>
<TaggedSource
args={viewStyle === 'tags' && !showFileSource ? args : undefined}
source={source}
theme={prismTheme}
language={language}
actions={actions}
/>
);
};
26 changes: 7 additions & 19 deletions blocks/core/src/StorySource/TaggedSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import {
} from '@component-controls/specification';
import { Styled } from 'theme-ui';
import { transparentize } from 'polished';
import Highlight, {
defaultProps,
PrismTheme,
Language,
} from 'prism-react-renderer';
import { Source, SourceProps } from '../Source';

interface ArgumentLocations {
name: string;
Expand Down Expand Up @@ -40,17 +36,14 @@ const getArgumentsLocations = (args: StoryArguments): ArgumentLocations[] => {
}, []);
};

export interface TaggedSourceProps {
export type TaggedSourceProps = SourceProps & {
args?: StoryArguments;
source: string;
theme: PrismTheme;
language: Language;
}
};

export const TaggedSource: React.FC<TaggedSourceProps> = ({
args,
theme,
source,
language,
...rest
}) => {
const tags: (ArgumentLocations & { color?: string })[] | undefined = args
? getArgumentsLocations(args)
Expand All @@ -64,12 +57,7 @@ export const TaggedSource: React.FC<TaggedSourceProps> = ({
});
}
return (
<Highlight
{...defaultProps}
theme={theme}
code={source}
language={language}
>
<Source theme={theme} {...rest}>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<Styled.pre
className={`${className}`}
Expand Down Expand Up @@ -136,6 +124,6 @@ export const TaggedSource: React.FC<TaggedSourceProps> = ({
))}
</Styled.pre>
)}
</Highlight>
</Source>
);
};
1 change: 0 additions & 1 deletion integrations/storybook/src/blocks/BlockContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export const useControlsContext = ({
const myStory: Story = myStoryStore && myStoryStore.stories[previewId];
const kindTitle = myStory.kind;
const kind = kindTitle ? myStoryStore.kinds[kindTitle] : undefined;
console.log(kind.component);
const source: string | undefined = myStory ? myStory.source : undefined;
return {
api,
Expand Down
4 changes: 1 addition & 3 deletions integrations/storybook/src/blocks/ImportSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ export type SourceProps = ControlsContextInputProps & {
};

export const ImportSource: FC<SourceProps> = ({ id, name }) => {
const all = useControlsContext({
const { component } = useControlsContext({
id,
name,
});
const { component } = all;
console.log(all);
return (
<ThemeProvider>
<SourceBlock
Expand Down

0 comments on commit 7dfe0ca

Please sign in to comment.