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

Commit bd6d7cf

Browse files
committed
feat: support passing icon as slot in icon-button
1 parent 85f96a8 commit bd6d7cf

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

packages/c-button/examples/as-icon-button.vue

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,23 @@
2222
color-scheme="teal"
2323
/>
2424
<c-icon-button
25-
icon="star"
26-
aria-label="Favourite album"
25+
aria-label="Documentation Icon"
2726
mr="3"
2827
size="lg"
2928
color-scheme="teal"
30-
/>
29+
>
30+
<DocumentationIcon />
31+
</c-icon-button>
3132
</template>
33+
34+
<script lang="ts" setup>
35+
import { chakra } from "../../vue/src"
36+
import { CIconButton } from "../src/icon-button"
37+
38+
import { createIcon } from "../../c-icon/src/"
39+
const DocumentationIcon = createIcon({
40+
name: "Documentation",
41+
viewBox: "0 2.4 24 24",
42+
path: `<path fill="currentColor" d="M6 24.4q-.825 0-1.413-.587T4 22.4v-16q0-.825.588-1.412T6 4.4h12q.825 0 1.413.588T20 6.4v16q0 .825-.588 1.413T18 24.4H6Zm0-2h12v-16h-2v6.125q0 .3-.25.438t-.5-.013L13.5 11.9l-1.75 1.05q-.25.15-.5.013t-.25-.438V6.4H6v16Zm5-16h5h-5Zm-5 0h12H6Z"/>`,
43+
})
44+
</script>

packages/c-button/src/icon-button.tsx

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import { h, defineComponent, PropType, VNode } from "vue"
1+
import { h, defineComponent, PropType, VNode, type DefineComponent } from "vue"
22
import { CButton } from "./button"
3-
import { ButtonProps } from "./button.utils"
3+
import type { ButtonProps } from "./button.utils"
44
import { CIcon } from "@chakra-ui/c-icon"
5+
import { chakra } from "@chakra-ui/vue-system"
56

67
const IconButtonProps = {
78
// ...BUTTON_PROPS,
8-
icon: String as PropType<string>,
9-
isRound: Boolean as PropType<boolean>,
9+
icon: String as PropType<CIconButtonProps["icon"]>,
10+
isRound: Boolean as PropType<CIconButtonProps["isRound"]>,
1011
ariaLabel: {
11-
type: String as PropType<string>,
12+
type: String as PropType<CIconButtonProps["ariaLabel"]>,
1213
required: true,
1314
},
1415
}
1516

1617
export interface CIconButtonProps extends ButtonProps {
17-
icon: string
18+
icon: string | object | typeof chakra.svg | DefineComponent
1819
isRound?: boolean
1920
ariaLabel: string
2021
}
@@ -27,22 +28,34 @@ export interface CIconButtonProps extends ButtonProps {
2728
export const CIconButton = defineComponent({
2829
name: "CIconButton",
2930
props: IconButtonProps,
30-
setup(props, { attrs }) {
31+
setup(props, { attrs, slots }) {
3132
if (!props.ariaLabel) {
3233
console.error(
3334
`chakra-ui: The \`aria-label\` prop is required for the <c-icon-button />`
3435
)
3536
}
3637

37-
return () => (
38-
<CButton
39-
padding={"0"}
40-
rounded={props.isRound ? "rounded" : "md"}
41-
aria-label={props.ariaLabel}
42-
{...attrs}
43-
>
44-
<CIcon aria-hidden focusable={0} name={props.icon} />
45-
</CButton>
46-
)
38+
return () => {
39+
const children = slots?.default?.()
40+
return (
41+
<CButton
42+
padding={"0"}
43+
rounded={props.isRound ? "rounded" : "md"}
44+
aria-label={props.ariaLabel}
45+
{...attrs}
46+
>
47+
{!!children ? (
48+
children
49+
) : typeof props.icon === "string" ? (
50+
<CIcon aria-hidden focusable={0} name={props.icon} />
51+
) : (
52+
<CIcon __label="button__icon" {...attrs}>
53+
{/* @ts-ignore */}
54+
<props.icon />
55+
</CIcon>
56+
)}
57+
</CButton>
58+
)
59+
}
4760
},
4861
})

0 commit comments

Comments
 (0)