UNPKG

@vectara/vectara-ui

Version:

Vectara's design system, codified as a React and Sass component library

36 lines (35 loc) 1.05 kB
import { cloneElement } from "react"; import { VuiIcon } from "../icon/Icon"; const sizeToIconSizeMap = { xs: "xs", s: "xs", m: "s", l: "m" }; const defaultColorToIconColorMap = { accent: "accent", primary: "primary", success: "success", danger: "danger", warning: "warning", neutral: "neutral", subdued: "subdued" }; /** * Checks if the provided element is a VuiIcon component */ const isVuiIcon = (element) => { return Boolean(element && element.type === VuiIcon); }; export const createButtonIcon = (icon, size, color, colorToIconColorMap = defaultColorToIconColorMap) => { if (!icon) { return null; } if (!isVuiIcon(icon)) { throw new Error("Button components: The icon prop must be an instance of VuiIcon. Make sure you are passing a VuiIcon component and not a raw icon."); } return cloneElement(icon, { size: size ? sizeToIconSizeMap[size] : "s", color: icon.props.color === "inherit" ? colorToIconColorMap[color] : icon.props.color }); };