Skip to content

Commit 0209d40

Browse files
Refine Button story code examples (#1402)
1 parent 29aa059 commit 0209d40

File tree

1 file changed

+54
-27
lines changed

1 file changed

+54
-27
lines changed

src/components/button/button.stories.mdx

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import { Story, Canvas, Meta } from '@storybook/addon-docs/blocks';
2+
// The '!!raw-loader!' syntax is a non-standard, Webpack-specific, syntax.
3+
// See: https://github.com/webpack-contrib/raw-loader#examples
4+
// For now, it seems likely Storybook is pretty tied to Webpack, therefore, we are
5+
// okay with the following Webpack-specific raw loader syntax. It's better to leave
6+
// the ESLint rule enabled globally, and only thoughtfully disable as needed (e.g.
7+
// within a Storybook docs page and not within an actual component).
8+
// This can be revisited in the future if Storybook no longer relies on Webpack.
9+
// eslint-disable-next-line @cloudfour/import/no-webpack-loader-syntax
10+
import stylesDemoSource from '!!raw-loader!./demo/styles.twig';
211
import stylesDemo from './demo/styles.twig';
12+
// eslint-disable-next-line @cloudfour/import/no-webpack-loader-syntax
13+
import iconButtonCustomDemoSource from '!!raw-loader!./demo/icon-button-custom-demo.twig';
314
import iconButtonCustomDemo from './demo/icon-button-custom-demo.twig';
415
import button from './button.twig';
516
const iconControlConfig = {
@@ -17,6 +28,22 @@ const iconControlConfig = {
1728
},
1829
defaultValue: '',
1930
};
31+
const buttonStory = (args) => {
32+
// Don't bother with the inline options if they don't exist or are defaults
33+
if (args.content_start_icon === '') {
34+
delete args.content_start_icon;
35+
}
36+
if (args.content_end_icon === '') {
37+
delete args.content_end_icon;
38+
}
39+
if (args.disabled === false) {
40+
delete args.disabled;
41+
}
42+
if (args.type === 'button') {
43+
delete args.type;
44+
}
45+
return button(args);
46+
};
2047

2148
<Meta
2249
title="Components/Button"
@@ -43,10 +70,10 @@ The `c-button` class may be applied to button and link elements. The button appe
4370

4471
<Canvas>
4572
<Story name="Button Element" args={{ label: 'Button', href: false }}>
46-
{(args) => button(args)}
73+
{(args) => buttonStory(args)}
4774
</Story>
4875
<Story name="Link Element" args={{ label: 'Link', tagName: 'a' }}>
49-
{(args) => button(args)}
76+
{(args) => buttonStory(args)}
5077
</Story>
5178
</Canvas>
5279

@@ -55,13 +82,24 @@ The `c-button` class may be applied to button and link elements. The button appe
5582
Some buttons may not require as much emphasis as others. The `c-button--secondary` and `c-button--tertiary` modifiers reflect lesser importance with reduced contrast.
5683

5784
<Canvas>
58-
<Story name="Styles">{(args) => stylesDemo(args)}</Story>
85+
<Story
86+
name="Styles"
87+
parameters={{ docs: { source: { code: stylesDemoSource } } }}
88+
>
89+
{(args) => stylesDemo(args)}
90+
</Story>
5991
</Canvas>
6092

6193
All the above visual styles will appear slightly different within [our dark theme](/docs/design-themes--dark). These versions retain their visual emphasis relative to one another while remaining visually distinct from form inputs.
6294

6395
<Canvas>
64-
<Story name="Styles (Dark)" parameters={{ theme: 't-dark' }}>
96+
<Story
97+
name="Styles (Dark)"
98+
parameters={{
99+
theme: 't-dark',
100+
docs: { source: { code: stylesDemoSource } },
101+
}}
102+
>
65103
{(args) => stylesDemo(args)}
66104
</Story>
67105
</Canvas>
@@ -71,13 +109,6 @@ All the above visual styles will appear slightly different within [our dark them
71109
To add an icon, pass the name of any of the [available icons](/docs/design-icons--page)
72110
to the `content_start_icon` or `content_end_icon` properties.
73111

74-
```twig
75-
{% embed '@cloudfour/components/button/button.twig' with {
76-
label: 'Get notifications'
77-
content_start_icon: 'bell'
78-
} only %}
79-
```
80-
81112
<Canvas>
82113
<Story
83114
name="Icon"
@@ -86,7 +117,7 @@ to the `content_start_icon` or `content_end_icon` properties.
86117
label: 'Get notifications',
87118
}}
88119
>
89-
{(args) => button(args)}
120+
{(args) => buttonStory(args)}
90121
</Story>
91122
</Canvas>
92123

@@ -100,19 +131,15 @@ CSS classes on the `<svg>` element.
100131
The `content_start`/`content_end` blocks override the
101132
`content_start_icon`/`content_end_icon` values.
102133

103-
```twig
104-
{% embed '@cloudfour/components/button/button.twig' %}
105-
{% block content %}Button with a custom icon{% endblock %}
106-
{% block content_end %}
107-
<svg viewBox="0 0 24 24" width="24" height="24" class="c-icon c-icon--inline">
108-
<circle r="8" cx="12" cy="12"></circle>
109-
</svg>
110-
{% endblock %}
111-
{% endembed %}
112-
```
113-
114134
<Canvas>
115-
<Story name="Custom Icon">{(args) => iconButtonCustomDemo(args)}</Story>
135+
<Story
136+
name="Custom Icon"
137+
parameters={{
138+
docs: { source: { code: iconButtonCustomDemoSource } },
139+
}}
140+
>
141+
{(args) => iconButtonCustomDemo(args)}
142+
</Story>
116143
</Canvas>
117144

118145
## Toggling button
@@ -128,7 +155,7 @@ You can override the `content_start_icon` value to a different icon.
128155
aria_pressed: 'true',
129156
}}
130157
>
131-
{(args) => button(args)}
158+
{(args) => buttonStory(args)}
132159
</Story>
133160
<Story
134161
name="Secondary Toggling Button"
@@ -137,7 +164,7 @@ You can override the `content_start_icon` value to a different icon.
137164
aria_pressed: 'true',
138165
}}
139166
>
140-
{(args) => button(args)}
167+
{(args) => buttonStory(args)}
141168
</Story>
142169
<Story
143170
name="Tertiary Toggling Button"
@@ -146,7 +173,7 @@ You can override the `content_start_icon` value to a different icon.
146173
aria_pressed: 'true',
147174
}}
148175
>
149-
{(args) => button(args)}
176+
{(args) => buttonStory(args)}
150177
</Story>
151178
<Story name="Custom Icon Toggling Button" args={{ aria_pressed: 'true' }}>
152179
{(args) => iconButtonCustomDemo(args)}

0 commit comments

Comments
 (0)