Data display
Icon
Render svg component with Hope UI styling features.
Import
tsximport { Icon } from "@hope-ui/core";
tsximport { Icon } from "@hope-ui/core";
Usage with a third-party icon library
The Icon component accept an as prop, which you can use to render icon from third-party libraries.
tsximport { Icon } from "@hope-ui/core";import { PlusIcon } from "some-icon-library";function Demo() {return <Icon as={PlusIcon} />;}
tsximport { Icon } from "@hope-ui/core";import { PlusIcon } from "some-icon-library";function Demo() {return <Icon as={PlusIcon} />;}
Creating custom icon
Hope UI provides two methods for creating custom icons:
- The
Iconcomponent - The
createIconfunction
Using the Icon component
Icon render an svg element and can be used to create custom icon components.
tsximport { Icon, IconProps } from "@hope-ui/core";function BoxIcon(props: IconProps) {return (<Icon viewBox="0 0 24 24" {...props}><pathd="m20 7l-8-4l-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"fill="none"stroke="currentColor"stroke-linecap="round"stroke-linejoin="round"stroke-width="2"/></Icon>);}function Demo() {// Use the icon and pass style props, if neededreturn <BoxIcon color="neutral.700" boxSize="24px" />;}
tsximport { Icon, IconProps } from "@hope-ui/core";function BoxIcon(props: IconProps) {return (<Icon viewBox="0 0 24 24" {...props}><pathd="m20 7l-8-4l-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"fill="none"stroke="currentColor"stroke-linecap="round"stroke-linejoin="round"stroke-width="2"/></Icon>);}function Demo() {// Use the icon and pass style props, if neededreturn <BoxIcon color="neutral.700" boxSize="24px" />;}
Using the createIcon function
You can use the createIcon function to achieve the same functionality with less effort.
tsximport { createIcon } from "@hope-ui/core";const BoxIcon = createIcon({viewBox: "0 0 24 24",path: () => (<pathd="m20 7l-8-4l-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"fill="none"stroke="currentColor"stroke-linecap="round"stroke-linejoin="round"stroke-width="2"/>),});
tsximport { createIcon } from "@hope-ui/core";const BoxIcon = createIcon({viewBox: "0 0 24 24",path: () => (<pathd="m20 7l-8-4l-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"fill="none"stroke="currentColor"stroke-linecap="round"stroke-linejoin="round"stroke-width="2"/>),});
Tips for generating icon from design tools
- Export icons as
svgfrom your design tool (Sketch, Figma, etc...). - Use a tool like SVGminify to reduce the size and minify the markup.
- To use the
colorstyle prop, set thefillorstrokeprop of your svg path tocurrentColor.
Fallback icon
Icon will render a fallback when children is not provided.
tsx<Icon />
tsx<Icon />
Props
Icon
Icon supports any svg attributes, Hope UI's style props and sx.
createIcon
| Name | Type | Description | Default value |
|---|---|---|---|
| viewBox | string | The icon svg viewBox. | "0 0 24 24" |
| path | () => JSX.Element | A function that return the svg path or group element. | |
| defaultProps | IconProps | Default props automatically passed to the component. |