Skip to content

Commit 05b503c

Browse files
authored
Add custom icon documentation to storybook (#1171)
* Init * Change files * Add custom icon documentation to storybook * Fixing linting * Nit fixes * Resolving PR comments * Resolving PR comments
1 parent 87fbc50 commit 05b503c

4 files changed

Lines changed: 160 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Add custom icon documentation to storybook",
4+
"packageName": "@internal/storybook",
5+
"email": "anjulgarg@live.com",
6+
"dependentChangeType": "patch"
7+
}

packages/storybook/.storybook/preview.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export const parameters = {
6767
[
6868
'Styling',
6969
'Theming',
70+
'Icons',
7071
'Localization',
7172
'Accessibility',
7273
'Custom User Data Model',
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
import { mergeStyles, Stack, TextField } from '@fluentui/react';
4+
import React, { useState } from 'react';
5+
6+
const IconGrid = (props: { allIcons: Record<string, JSX.Element>; searchText?: string }): JSX.Element => {
7+
const { allIcons, searchText } = props;
8+
const allIconKeys = Object.keys(allIcons).sort();
9+
const filteredKeys = searchText
10+
? allIconKeys.filter((k) => k.toLowerCase().includes(searchText.toLowerCase()))
11+
: allIconKeys;
12+
13+
return (
14+
<div style={{ display: 'grid', gridTemplateColumns: 'auto auto auto', padding: '1rem 0' }}>
15+
{filteredKeys.map((key, idx) => {
16+
return (
17+
<Stack
18+
key={idx}
19+
horizontal
20+
style={{
21+
padding: '0.5rem 0'
22+
}}
23+
verticalAlign="center"
24+
>
25+
<Stack horizontalAlign="start" className={mergeStyles({ span: { fontSize: '1rem', lineHeight: 0 } })}>
26+
{allIcons[key]}
27+
</Stack>
28+
<Stack
29+
horizontalAlign="start"
30+
style={{
31+
paddingLeft: '0.5rem',
32+
fontSize: '0.75rem',
33+
fontFamily: 'consolas'
34+
}}
35+
>
36+
{key}
37+
</Stack>
38+
</Stack>
39+
);
40+
})}
41+
</div>
42+
);
43+
};
44+
45+
const SearchBar = (props: { onChange: (newValue?: string) => void }): JSX.Element => {
46+
const { onChange } = props;
47+
return (
48+
<Stack style={{ padding: '1rem 0' }} horizontalAlign="start">
49+
<TextField
50+
label="Search Icons:"
51+
underlined
52+
onChange={(e, val) => {
53+
onChange(val);
54+
}}
55+
/>
56+
</Stack>
57+
);
58+
};
59+
60+
export const IconGridWithSearch = (props: { icons: Record<string, JSX.Element> }): JSX.Element => {
61+
const { icons } = props;
62+
const [searchText, setSearchText] = useState<string | undefined>(undefined);
63+
return (
64+
<Stack>
65+
<SearchBar onChange={(val) => setSearchText(val)} />
66+
<IconGrid allIcons={icons} searchText={searchText} />
67+
</Stack>
68+
);
69+
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { DEFAULT_COMPONENT_ICONS, DEFAULT_COMPOSITE_ICONS } from '@azure/communication-react';
2+
import { Meta, Canvas, Source } from '@storybook/addon-docs';
3+
import { IconGridWithSearch } from './IconGrid';
4+
5+
<Meta id="icons" title="Concepts/Icons" />
6+
7+
# Icons
8+
9+
Both the Composites and the Components use a set of default icons from the Fluent UI library. All the icons used in Components and Composites are displayed below with their names.
10+
11+
**All the default icons can be replaced with a custom icon by providing a custom JSX element.**
12+
The method for providing custom icons differs between composites and components. Please read below for more details about each.
13+
14+
## Composites
15+
16+
There is no setup required for rendering icons inside the Composites.
17+
18+
### Custom Icons
19+
20+
All Composites expose a prop `icons` that allows you to override the default icons used inside Composites.
21+
22+
Here is a sample code showcasing how you can override an icon used inside Composites with custom icons.
23+
24+
<Source
25+
language="tsx"
26+
code={`
27+
<CallComposite
28+
adapter={adapter}
29+
icons={{
30+
LocalDeviceSettingsCamera: <p>My Custom Icon</p>
31+
}}
32+
/>
33+
`}
34+
/>
35+
36+
### Default Composite Icons
37+
38+
Below is a list of all the default icons you can override in Composites with their names.
39+
40+
<Canvas withSource="none">
41+
<IconGridWithSearch icons={DEFAULT_COMPOSITE_ICONS} />
42+
</Canvas>
43+
44+
## Components
45+
46+
Before you can render icons in Components, you will need to setup the icons using the `registerIcons` method as shown below.
47+
48+
<Source
49+
language="tsx"
50+
code={`
51+
import { registerIcons } from '@fluentui/react';
52+
import { DEFAULT_COMPONENT_ICONS } from '@azure/communication-react';
53+
//
54+
// If you don't want to provide custom icons, you can register the default ones included with the library.
55+
// This will ensure that all the icons are rendered correctly.
56+
//
57+
registerIcons({ icons: DEFAULT_COMPONENT_ICONS });
58+
`}
59+
/>
60+
61+
### Custom Icons
62+
63+
For overriding a default icon, you can provide a custom react element as shown in the example below.
64+
65+
<Source
66+
language="tsx"
67+
code={`
68+
import { registerIcons } from '@fluentui/react';
69+
import { DEFAULT_COMPONENT_ICONS } from '@azure/communication-react';
70+
const customIcons = {...DEFAULT_COMPONENT_ICONS, {
71+
ControlButtonCameraOff: <p>My Custom Icon</p>
72+
}};
73+
registerIcons({ icons: customIcons });
74+
`}
75+
/>
76+
77+
### Default Component Icons
78+
79+
Below is a list of all the default icons you can override in Components with their names.
80+
81+
<Canvas withSource="none">
82+
<IconGridWithSearch icons={DEFAULT_COMPONENT_ICONS} />
83+
</Canvas>

0 commit comments

Comments
 (0)