Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@storybook/addon-cssresources - Hide Code #9627

Merged
merged 4 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions addons/cssresources/README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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