Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 71cae21

Browse files
Merge pull request #50 from chakra-ui/feat/icons-extends
feat: icons extends
2 parents b14aac0 + 311ac35 commit 71cae21

File tree

9 files changed

+155
-5
lines changed

9 files changed

+155
-5
lines changed

.changeset/ten-pandas-run.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
'@chakra-ui/c-icon': minor
3+
'@chakra-ui/c-accordion': patch
4+
'@chakra-ui/c-alert': patch
5+
'@chakra-ui/c-badge': patch
6+
'@chakra-ui/c-button': patch
7+
'@chakra-ui/c-close-button': patch
8+
'@chakra-ui/c-color-mode': patch
9+
'@chakra-ui/c-flex': patch
10+
'@chakra-ui/c-modal': patch
11+
'@chakra-ui/c-popper': patch
12+
'@chakra-ui/c-portal': patch
13+
'@chakra-ui/c-reset': patch
14+
'@chakra-ui/c-spinner': patch
15+
'@chakra-ui/c-theme-provider': patch
16+
'@chakra-ui/c-visually-hidden': patch
17+
'@chakra-ui/vue-next': patch
18+
'@chakra-ui/nuxt-next': patch
19+
'@chakra-ui/vue-system': patch
20+
'@chakra-ui/vue-test-utils': patch
21+
'@chakra-ui/vue-theme': patch
22+
'@chakra-ui/vue-theme-tools': patch
23+
'@chakra-ui/vue-utils': patch
24+
'@chakra-ui/vue-auto-import': patch
25+
'@chakra-ui/vue-docs': patch
26+
---
27+
28+
Add option to extend icons with custom icons

.changeset/wild-chairs-applaud.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@chakra-ui/c-icon': minor
3+
---
4+
5+
Add option to extend icons

packages/c-icon/README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,56 @@ A component to display icons in the browser
88
yarn add @chakra-ui/c-icon
99
# or
1010
npm i @chakra-ui/c-icon
11-
```
11+
```
12+
13+
### Adding custom icons
14+
All Chakra icons are registered in the Chakra plugin under the `icons.extend` key. So you
15+
can extend this object to add your own icons. Here are the steps:
16+
17+
- Export the icon's `svg` from Figma, Sketch, etc.
18+
- Use a tool like [SvgOmg](https://svgomg.firebaseapp.com) to reduce the size
19+
and minify the markup.
20+
- Add the icon to the theme object.
21+
22+
> Add the `fill=currentColor` attribute to the `path` or `g` so that when you
23+
> this `<Icon color="gray.200"/>`, it works correctly.
24+
25+
<br />
26+
27+
```js
28+
// Step 1: Each icon should be stored as an object of `path` and `viewBox`
29+
const customIcons = {
30+
icon1: {
31+
path: `<path fill="currentColor" d="..." />`,
32+
// If the icon's viewBox is `0 0 24 24`, you can ignore `viewBox`
33+
viewBox: "0 0 40 40",
34+
},
35+
icon2: {
36+
path: `
37+
<g fill="currentColor">
38+
<path d="..."/>
39+
</g>
40+
`
41+
}
42+
};
43+
44+
// Step 2: Add the custom icon to the Chakra plugin
45+
const app = createApp(App)
46+
.use(ChakraUIVuePlugin, {
47+
icons: {
48+
// ...
49+
extend: customIcons
50+
}
51+
})
52+
```
53+
54+
You can now consume your custom icons in your template like this:
55+
56+
```vue
57+
<template>
58+
<c-icon name="icon1" color="yellow.600" />
59+
<c-icon name="icon2" color="green.300" />
60+
</template>
61+
```
62+
63+
> You can access the full merged icons object under `this.$chakra.icons` in your Vue components.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<div>
3+
<c-icon size="10" name="discord" />
4+
</div>
5+
</template>

packages/c-icon/src/icon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const CIcon = defineComponent({
4646
color: 'currentColor',
4747
innerHTML: icon.value.path,
4848
focusable: false,
49-
viewBox: fallbackIcon.viewBox,
49+
viewBox: icon.value.viewBox || fallbackIcon.viewBox,
5050
}))
5151

5252
return () =>

packages/c-icon/tests/__snapshots__/c-icon.test.ts.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`should render custom icon properly 1`] = `
4+
<DocumentFragment>
5+
<svg
6+
class="chakra-icon css-stgirh"
7+
focusable="false"
8+
viewBox="0 0 496 512"
9+
>
10+
<path
11+
d="M297.216 243.2c0 15.616-11.52 28.416-26.112 28.416-14.336 0-26.112-12.8-26.112-28.416s11.52-28.416 26.112-28.416c14.592 0 26.112 12.8 26.112 28.416zm-119.552-28.416c-14.592 0-26.112 12.8-26.112 28.416s11.776 28.416 26.112 28.416c14.592 0 26.112-12.8 26.112-28.416.256-15.616-11.52-28.416-26.112-28.416zM448 52.736V512c-64.494-56.994-43.868-38.128-118.784-107.776l13.568 47.36H52.48C23.552 451.584 0 428.032 0 398.848V52.736C0 23.552 23.552 0 52.48 0h343.04C424.448 0 448 23.552 448 52.736zm-72.96 242.688c0-82.432-36.864-149.248-36.864-149.248-36.864-27.648-71.936-26.88-71.936-26.88l-3.584 4.096c43.52 13.312 63.744 32.512 63.744 32.512-60.811-33.329-132.244-33.335-191.232-7.424-9.472 4.352-15.104 7.424-15.104 7.424s21.248-20.224 67.328-33.536l-2.56-3.072s-35.072-.768-71.936 26.88c0 0-36.864 66.816-36.864 149.248 0 0 21.504 37.12 78.08 38.912 0 0 9.472-11.52 17.152-21.248-32.512-9.728-44.8-30.208-44.8-30.208 3.766 2.636 9.976 6.053 10.496 6.4 43.21 24.198 104.588 32.126 159.744 8.96 8.96-3.328 18.944-8.192 29.44-15.104 0 0-12.8 20.992-46.336 30.464 7.68 9.728 16.896 20.736 16.896 20.736 56.576-1.792 78.336-38.912 78.336-38.912z"
12+
fa-key="3"
13+
fill="currentColor"
14+
/>
15+
</svg>
16+
</DocumentFragment>
17+
`;
18+
319
exports[`should render properly 1`] = `
420
<DocumentFragment>
521
<svg

packages/c-icon/tests/c-icon.test.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
1-
import { render } from '../../test-utils/src'
21
import { CIcon } from '../src'
2+
import { render, testA11y } from '../../test-utils/src'
3+
import { provide } from 'vue'
4+
5+
const renderComponent = (props?: any) => {
6+
const base = {
7+
components: {
8+
CIcon,
9+
},
10+
template: '<CIcon name="star" />',
11+
...props,
12+
}
13+
return render(base)
14+
}
315

416
it('should render properly', () => {
5-
const { asFragment } = render(CIcon)
17+
const { asFragment } = renderComponent()
18+
expect(asFragment()).toMatchSnapshot()
19+
})
20+
21+
it('should have no a11y violations', async () => {
22+
await testA11y(renderComponent())
23+
})
24+
25+
it('should render custom icon properly', async () => {
26+
const icons = {
27+
discord: {
28+
path:
29+
'<path fill="currentColor" d="M297.216 243.2c0 15.616-11.52 28.416-26.112 28.416-14.336 0-26.112-12.8-26.112-28.416s11.52-28.416 26.112-28.416c14.592 0 26.112 12.8 26.112 28.416zm-119.552-28.416c-14.592 0-26.112 12.8-26.112 28.416s11.776 28.416 26.112 28.416c14.592 0 26.112-12.8 26.112-28.416.256-15.616-11.52-28.416-26.112-28.416zM448 52.736V512c-64.494-56.994-43.868-38.128-118.784-107.776l13.568 47.36H52.48C23.552 451.584 0 428.032 0 398.848V52.736C0 23.552 23.552 0 52.48 0h343.04C424.448 0 448 23.552 448 52.736zm-72.96 242.688c0-82.432-36.864-149.248-36.864-149.248-36.864-27.648-71.936-26.88-71.936-26.88l-3.584 4.096c43.52 13.312 63.744 32.512 63.744 32.512-60.811-33.329-132.244-33.335-191.232-7.424-9.472 4.352-15.104 7.424-15.104 7.424s21.248-20.224 67.328-33.536l-2.56-3.072s-35.072-.768-71.936 26.88c0 0-36.864 66.816-36.864 149.248 0 0 21.504 37.12 78.08 38.912 0 0 9.472-11.52 17.152-21.248-32.512-9.728-44.8-30.208-44.8-30.208 3.766 2.636 9.976 6.053 10.496 6.4 43.21 24.198 104.588 32.126 159.744 8.96 8.96-3.328 18.944-8.192 29.44-15.104 0 0-12.8 20.992-46.336 30.464 7.68 9.728 16.896 20.736 16.896 20.736 56.576-1.792 78.336-38.912 78.336-38.912z" fa-key="3" fill="currentColor"></path>',
30+
viewBox: '0 0 496 512',
31+
},
32+
}
33+
34+
const { asFragment } = renderComponent({
35+
template: '<c-icon size="10" name="discord" />',
36+
setup() {
37+
provide('$chakraIcons', icons)
38+
},
39+
})
40+
641
expect(asFragment()).toMatchSnapshot()
742
})

packages/core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const ChakraUIVuePlugin: Plugin = {
3333
})
3434

3535
let libraryIcons = options.icons?.library || {}
36+
let extendedIcons = options.icons?.extend || {}
3637

3738
// Initialize colormode
3839
const colorMode = theme.config?.initialColorMode || 'dark'
@@ -51,6 +52,7 @@ const ChakraUIVuePlugin: Plugin = {
5152
const mergedIcons: MergedIcons = {
5253
...internalIcons,
5354
...libraryIcons,
55+
...extendedIcons,
5456
}
5557
app.provide('$chakraIcons', mergedIcons)
5658
},

playground/src/main.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ const app = createApp(App)
1414
icons: {
1515
library: {
1616
feActivity
17-
}
17+
},
18+
extend: {
19+
discord: {
20+
path:
21+
'<path fill="currentColor" d="M297.216 243.2c0 15.616-11.52 28.416-26.112 28.416-14.336 0-26.112-12.8-26.112-28.416s11.52-28.416 26.112-28.416c14.592 0 26.112 12.8 26.112 28.416zm-119.552-28.416c-14.592 0-26.112 12.8-26.112 28.416s11.776 28.416 26.112 28.416c14.592 0 26.112-12.8 26.112-28.416.256-15.616-11.52-28.416-26.112-28.416zM448 52.736V512c-64.494-56.994-43.868-38.128-118.784-107.776l13.568 47.36H52.48C23.552 451.584 0 428.032 0 398.848V52.736C0 23.552 23.552 0 52.48 0h343.04C424.448 0 448 23.552 448 52.736zm-72.96 242.688c0-82.432-36.864-149.248-36.864-149.248-36.864-27.648-71.936-26.88-71.936-26.88l-3.584 4.096c43.52 13.312 63.744 32.512 63.744 32.512-60.811-33.329-132.244-33.335-191.232-7.424-9.472 4.352-15.104 7.424-15.104 7.424s21.248-20.224 67.328-33.536l-2.56-3.072s-35.072-.768-71.936 26.88c0 0-36.864 66.816-36.864 149.248 0 0 21.504 37.12 78.08 38.912 0 0 9.472-11.52 17.152-21.248-32.512-9.728-44.8-30.208-44.8-30.208 3.766 2.636 9.976 6.053 10.496 6.4 43.21 24.198 104.588 32.126 159.744 8.96 8.96-3.328 18.944-8.192 29.44-15.104 0 0-12.8 20.992-46.336 30.464 7.68 9.728 16.896 20.736 16.896 20.736 56.576-1.792 78.336-38.912 78.336-38.912z" fa-key="3" fill="currentColor"></path>',
22+
viewBox: '0 0 496 512',
23+
},
24+
},
1825
},
1926
extendTheme: extendTheme({
2027
config: {

0 commit comments

Comments
 (0)