|
| 1 | +import { forwardRef, useId } from "react"; |
| 2 | +import type { CSSProperties, SVGProps } from "react"; |
| 3 | + |
| 4 | +// export { default as Stack } from "./Stack"; |
| 5 | + |
| 6 | +export interface IconProps extends Omit<SVGProps<SVGSVGElement>, "path" | "color" | "rotate"> { |
| 7 | + path: string; |
| 8 | + size?: number | string | null; |
| 9 | + color?: string | null; |
| 10 | + horizontal?: boolean; |
| 11 | + vertical?: boolean; |
| 12 | + rotate?: number; |
| 13 | + spin?: boolean | number; |
| 14 | + inStack?: boolean; |
| 15 | + title?: string | null; |
| 16 | + description?: string | null; |
| 17 | +} |
| 18 | + |
| 19 | +export const Icon = forwardRef<SVGSVGElement, IconProps>( |
| 20 | + ( |
| 21 | + { |
| 22 | + path, |
| 23 | + id, |
| 24 | + title = null, |
| 25 | + description = null, |
| 26 | + size = null, |
| 27 | + color = "currentColor", |
| 28 | + horizontal = false, |
| 29 | + vertical = false, |
| 30 | + rotate = 0, |
| 31 | + spin = false, |
| 32 | + style, |
| 33 | + inStack = false, |
| 34 | + ...rest |
| 35 | + }, |
| 36 | + ref, |
| 37 | + ) => { |
| 38 | + const reactId = useId(); |
| 39 | + const iconId = id ?? reactId; |
| 40 | + |
| 41 | + const computedStyle: CSSProperties = { ...style }; |
| 42 | + const pathStyle: CSSProperties = {}; |
| 43 | + const transforms: string[] = []; |
| 44 | + |
| 45 | + if (size !== null) { |
| 46 | + if (inStack) { |
| 47 | + transforms.push(`scale(${size})`); |
| 48 | + } else { |
| 49 | + const dimension = typeof size === "string" ? size : `${size * 1.5}rem`; |
| 50 | + computedStyle.width = dimension; |
| 51 | + computedStyle.height = dimension; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (horizontal) transforms.push("scaleX(-1)"); |
| 56 | + if (vertical) transforms.push("scaleY(-1)"); |
| 57 | + if (rotate !== 0) transforms.push(`rotate(${rotate}deg)`); |
| 58 | + |
| 59 | + if (color !== null) { |
| 60 | + pathStyle.fill = color; |
| 61 | + } |
| 62 | + |
| 63 | + // Cast `rest` to SVGProps<SVGPathElement> to satisfy strict event handler target checks |
| 64 | + let transformElement = <path d={path} style={pathStyle} {...(inStack ? (rest as SVGProps<SVGPathElement>) : {})} />; |
| 65 | + |
| 66 | + if (transforms.length > 0) { |
| 67 | + computedStyle.transform = transforms.join(" "); |
| 68 | + computedStyle.transformOrigin = "center"; |
| 69 | + if (inStack) { |
| 70 | + transformElement = ( |
| 71 | + <g style={computedStyle}> |
| 72 | + {transformElement} |
| 73 | + <rect width="24" height="24" fill="transparent" /> |
| 74 | + </g> |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + let spinElement = transformElement; |
| 80 | + const spinSec = spin === true || typeof spin !== "number" ? 2 : spin; |
| 81 | + let inverse = !inStack && (horizontal || vertical); |
| 82 | + |
| 83 | + if (spinSec < 0) { |
| 84 | + inverse = !inverse; |
| 85 | + } |
| 86 | + |
| 87 | + if (spin) { |
| 88 | + spinElement = ( |
| 89 | + <g |
| 90 | + style={{ |
| 91 | + animation: `spin${inverse ? "-inverse" : ""} linear ${Math.abs(spinSec)}s infinite`, |
| 92 | + transformOrigin: "center", |
| 93 | + }} |
| 94 | + > |
| 95 | + {transformElement} |
| 96 | + {!(horizontal || vertical || rotate !== 0) && <rect width="24" height="24" fill="transparent" />} |
| 97 | + </g> |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + if (inStack) { |
| 102 | + return spinElement; |
| 103 | + } |
| 104 | + |
| 105 | + const labelledById = `icon_labelledby_${iconId}`; |
| 106 | + const describedById = `icon_describedby_${iconId}`; |
| 107 | + let ariaLabelledby; |
| 108 | + let role; |
| 109 | + |
| 110 | + if (title) { |
| 111 | + ariaLabelledby = description ? `${labelledById} ${describedById}` : labelledById; |
| 112 | + } else { |
| 113 | + role = "presentation"; |
| 114 | + if (description) { |
| 115 | + throw new Error("title attribute required when description is set"); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return ( |
| 120 | + <svg ref={ref} viewBox="0 0 24 24" style={computedStyle} role={role} aria-labelledby={ariaLabelledby} {...rest}> |
| 121 | + {title && <title id={labelledById}>{title}</title>} |
| 122 | + {description && <desc id={describedById}>{description}</desc>} |
| 123 | + |
| 124 | + {!inStack && spin && ( |
| 125 | + <style> |
| 126 | + {inverse |
| 127 | + ? "@keyframes spin-inverse { from { transform: rotate(0deg) } to { transform: rotate(-360deg) } }" |
| 128 | + : "@keyframes spin { from { transform: rotate(0deg) } to { transform: rotate(360deg) } }"} |
| 129 | + </style> |
| 130 | + )} |
| 131 | + |
| 132 | + {spinElement} |
| 133 | + </svg> |
| 134 | + ); |
| 135 | + }, |
| 136 | +); |
| 137 | + |
| 138 | +Icon.displayName = "Icon"; |
| 139 | + |
| 140 | +export default Icon; |
0 commit comments