Skip to content

Commit 7acdbbf

Browse files
committed
feat: add the CIconSvg component to allow adding custom SVG icons
1 parent 4f68c51 commit 7acdbbf

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/CIconSvg.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { cloneVNode, defineComponent } from 'vue'
2+
3+
const CIconSvg = defineComponent({
4+
name: 'CIconSvg',
5+
props: {
6+
/**
7+
* Use for replacing default CIconSvg component classes. Prop is overriding the 'size' prop.
8+
*/
9+
customClassName: [String, Array, Object],
10+
/**
11+
* The height attribute defines the vertical length of an icon.
12+
*/
13+
height: Number,
14+
/**
15+
* Size of the icon. Available sizes: 'sm', 'lg', 'xl', 'xxl', '3xl...9xl', 'custom', 'custom-size'.
16+
*/
17+
size: {
18+
type: String,
19+
validator: (value: string) => {
20+
return [
21+
'custom',
22+
'custom-size',
23+
'sm',
24+
'lg',
25+
'xl',
26+
'xxl',
27+
'3xl',
28+
'4xl',
29+
'5xl',
30+
'6xl',
31+
'7xl',
32+
'8xl',
33+
'9xl',
34+
].includes(value)
35+
},
36+
},
37+
/**
38+
* Title tag content.
39+
*/
40+
title: String,
41+
/**
42+
* The width attribute defines the horizontal length of an icon.
43+
*/
44+
width: Number,
45+
},
46+
setup(props, { attrs, slots }) {
47+
return () =>
48+
slots.default &&
49+
slots.default().map((slot) =>
50+
cloneVNode(slot, {
51+
'aria-hidden': true,
52+
class: [
53+
props.customClassName || [
54+
'icon',
55+
{
56+
[`icon-${props.size}`]: props.size,
57+
[`icon-custom-size`]: props.height || props.width,
58+
},
59+
attrs.class,
60+
],
61+
],
62+
height: props.height,
63+
focusable: 'false',
64+
role: 'img',
65+
width: props.width,
66+
...attrs,
67+
}),
68+
)
69+
},
70+
})
71+
export { CIconSvg }

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import { CIcon } from './CIcon'
2-
export { CIcon }
2+
import { CIconSvg } from './CIconSvg'
3+
export { CIcon, CIconSvg }
34
export default CIcon

0 commit comments

Comments
 (0)