Skip to content

Commit b4ede3b

Browse files
docs(icons): update storybook with rem sizing instructions (#19148)
* docs(icons): update storybook with rem sizing instructions * fix(icons): update story format, minor verbiage tweak * fix(icon): reduce relative story down to just an icon --------- Co-authored-by: Taylor Jones <[email protected]> Co-authored-by: Taylor Jones <[email protected]>
1 parent 73209f6 commit b4ede3b

File tree

2 files changed

+108
-10
lines changed

2 files changed

+108
-10
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { ArgTypes, Canvas, Meta } from '@storybook/blocks';
2+
import * as IconsStories from './Icons.stories';
3+
import { stackblitzPrefillConfig } from '../../../previewer/codePreviewer';
4+
5+
<Meta isTemplate />
6+
7+
# Icons
8+
9+
[Source code](https://github.com/carbon-design-system/carbon/tree/main/packages/react/src/components/Icons)
10+
&nbsp;|&nbsp;
11+
[Usage guidelines](https://carbondesignsystem.com/elements/icons/usage/)
12+
13+
## Table of Contents
14+
15+
- [Icons](#Icons)
16+
- [Overview](#overview)
17+
- [Customizing icon size with rem units](#customizing-icon-size-with-rem-units)
18+
- [Feedback](#feedback)
19+
20+
## Overview
21+
22+
<Canvas
23+
of={IconsStories.Default}
24+
additionalActions={[
25+
{
26+
title: 'Open in Stackblitz',
27+
onClick: () => stackblitzPrefillConfig(IconsStories.Default),
28+
},
29+
]}
30+
/>
31+
32+
This guide demonstrates how to make icons scale using `rem` units, without
33+
requiring any changes to the component’s default configuration. In Carbon, icons
34+
accept a `size` prop that can be a number or a string. Numbers are unitless and
35+
interpreted as pixels. This approach is effective for most scenarios, but if
36+
your goal is to improve accessibility or ensure that icons respond to changes in
37+
the browser’s font-size, using a string with `rem` units offers greater
38+
adaptability.
39+
40+
### Customizing icon size with `rem` units
41+
42+
To create scalable, responsive icons, you can pass a `rem` value to the `size`
43+
prop:
44+
45+
```jsx
46+
<Edit size="1rem" />
47+
```
48+
49+
<Canvas
50+
of={IconsStories.WithRelativeSize}
51+
additionalActions={[
52+
{
53+
title: 'Open in Stackblitz',
54+
onClick: () => stackblitzPrefillConfig(IconsStories.WithRelativeSize),
55+
},
56+
]}
57+
/>
58+
59+
Some components use a `renderIcon` prop to display icons internally.
60+
61+
In these cases, you can still pass a responsive size by using an inline function
62+
that returns the icon:
63+
64+
```jsx
65+
<MenuItem renderIcon={(props) => <Edit size="1rem" {...props} />} />
66+
```
67+
68+
## Feedback
69+
70+
Help us improve this component by providing feedback, asking questions on Slack,
71+
or updating this file on
72+
[GitHub](https://github.com/carbon-design-system/carbon/tree/main/packages/react/src/components/Icons/Icons.mdx).
Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
11
/**
2-
* Copyright IBM Corp. 2016, 2023
2+
* Copyright IBM Corp. 2016, 2025
33
*
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
77

88
import './Icons.stories.scss';
99
import React from 'react';
10-
import { Bee, Bicycle, ChevronUp } from '../../../icons';
10+
import { Bee, Edit } from '../../../icons';
11+
import { IconButton } from '../IconButton';
12+
import mdx from './Icons.mdx';
1113

1214
// eslint-disable-next-line storybook/csf-component
1315
export default {
1416
title: 'Elements/Icons',
17+
parameters: {
18+
docs: {
19+
page: mdx,
20+
},
21+
},
22+
decorators: [
23+
(Story, { args }) => {
24+
return (
25+
<section className="demo-icon-example">
26+
<h2>
27+
{args.size} {typeof args.size === 'number' && 'pixel'}{' '}
28+
{args.size === 16 && '(default)'}
29+
{typeof args.size === 'string' &&
30+
args.size.includes('rem') &&
31+
'(responsive)'}
32+
</h2>
33+
<Story />
34+
</section>
35+
);
36+
},
37+
],
1538
};
1639

1740
export const Default = (args) => {
18-
return (
19-
<section className="demo-icon-example">
20-
<h2>
21-
{args.size} pixel {args.size === 16 ? '(default)' : ''}
22-
</h2>
23-
<Bee {...args} />
24-
</section>
25-
);
41+
return <Bee {...args} />;
2642
};
2743

2844
Default.args = {
@@ -35,3 +51,13 @@ Default.argTypes = {
3551
control: { type: 'select' },
3652
},
3753
};
54+
55+
export const WithRelativeSize = (args) => {
56+
return <Edit {...args} />;
57+
};
58+
59+
WithRelativeSize.args = {
60+
size: '1rem',
61+
};
62+
63+
WithRelativeSize.argTypes = { size: { control: 'text' } };

0 commit comments

Comments
 (0)