Skip to content

Commit

Permalink
@storybook/addon-cssresources - Hide Code (#9627)
Browse files Browse the repository at this point in the history
@storybook/addon-cssresources - Hide Code
  • Loading branch information
ndelangen committed Jan 29, 2020
2 parents 544091d + fc6d8d1 commit 9c62740
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
12 changes: 6 additions & 6 deletions addons/cssresources/README.md
Expand Up @@ -20,8 +20,8 @@ Add following content to it:

```js
module.exports = {
addons: ['@storybook/addon-cssresources/register']
}
addons: ['@storybook/addon-cssresources/register'],
};
```

## Usage
Expand All @@ -34,17 +34,17 @@ import { withCssResources } from '@storybook/addon-cssresources';
export default {
title: 'CssResources',
parameters: {
cssresources: [{
cssresources: [
{
id: `bluetheme`,
code: `<style>body { background-color: lightblue; }</style>`,
picked: false,
hideCode: false, // Defaults to false, this enables you to hide the code snippet and only displays the style selector
},
],
},
decorators: [withCssResources],
};

export const defaultView = () => (
<div />
);
export const defaultView = () => <div />;
```
1 change: 1 addition & 0 deletions addons/cssresources/src/CssResource.ts
Expand Up @@ -2,4 +2,5 @@ export interface CssResource {
id: string;
code: string;
picked: boolean;
hideCode: boolean;
}
27 changes: 27 additions & 0 deletions addons/cssresources/src/css-resource-panel.test.js
Expand Up @@ -300,5 +300,32 @@ describe('CSSResourcePanel', () => {
node.instance().onStoryChange('fake-story-id');
expect(node.find(SyntaxHighlighter).length).toEqual(1);
});

it('should not render code for items /w the `hideCode` flag', () => {
const apiGetParameters = jest.fn(() => [
{
id: 'local-fake-id-1',
code: 'local-fake-code-1',
picked: true,
hideCode: true,
},
{
id: 'local-fake-id-2',
code: 'local-fake-code-2',
picked: false,
},
]);

const node = shallowNode({
...defaultProps,
api: {
...defaultProps.api,
getParameters: apiGetParameters,
},
});

node.instance().onStoryChange('fake-story-id');
expect(node.find(SyntaxHighlighter).length).toEqual(1);
});
});
});
6 changes: 3 additions & 3 deletions addons/cssresources/src/css-resource-panel.tsx
Expand Up @@ -112,16 +112,16 @@ export class CssResourcePanel extends Component<Props, State> {
return (
<div>
{list &&
list.map(({ id, code, picked }) => (
list.map(({ id, code, picked, hideCode = false }) => (
<div key={id} style={{ padding: 10 }}>
<label>
<input type="checkbox" checked={picked} onChange={this.onChange} id={id} />
<span>#{id}</span>
</label>
{code && code.length < maxLimitToUseSyntaxHighlighter && (
{code && !hideCode && code.length < maxLimitToUseSyntaxHighlighter && (
<SyntaxHighlighter language="html">{code}</SyntaxHighlighter>
)}
{code && code.length >= maxLimitToUseSyntaxHighlighter && (
{code && !hideCode && code.length >= maxLimitToUseSyntaxHighlighter && (
<Placeholder>
<Spaced row={1}>
<PlainCode>{code.substring(0, maxLimitToUseSyntaxHighlighter)} ...</PlainCode>
Expand Down

0 comments on commit 9c62740

Please sign in to comment.