| 1 |
import { Component, type ComponentClass, type ReactNode } from 'react'; |
| 2 |
|
| 3 |
export interface SVGPathObject { |
| 4 |
path: string; |
| 5 |
className?: string; |
| 6 |
} |
| 7 |
|
| 8 |
/** Icon data format */ |
| 9 |
export interface IconData { |
| 10 |
name?: string; |
| 11 |
width: number; |
| 12 |
height: number; |
| 13 |
xOffset?: number; |
| 14 |
yOffset?: number; |
| 15 |
svgClassName?: string; |
| 16 |
svgPathData?: string | SVGPathObject[]; |
| 17 |
} |
| 18 |
|
| 19 |
/** Internal API props */ |
| 20 |
interface CreateIconBaseProps { |
| 21 |
name?: string; |
| 22 |
icon?: IconData; |
| 23 |
rhUiIcon?: IconData | null; |
| 24 |
} |
| 25 |
|
| 26 |
/** Public API props */ |
| 27 |
export interface IconDefinition { |
| 28 |
name?: string; |
| 29 |
width: number; |
| 30 |
height: number; |
| 31 |
xOffset?: number; |
| 32 |
yOffset?: number; |
| 33 |
svgPath?: string | SVGPathObject[]; |
| 34 |
svgClassName?: string; |
| 35 |
rhUiIcon?: IconData | null; |
| 36 |
} |
| 37 |
|
| 38 |
/** Additional svg props */ |
| 39 |
export interface SVGIconProps extends Omit<React.HTMLProps<SVGElement>, 'ref'> { |
| 40 |
title?: string; |
| 41 |
className?: string; |
| 42 |
/** Indicates the icon should render using alternate svg data for the unified theme */ |
| 43 |
set?: 'default' | 'rh-ui'; |
| 44 |
/** Indicates the icon should render without its default styling specified in its IconDefinition.svgClassName. */ |
| 45 |
noDefaultStyle?: boolean; |
| 46 |
} |
| 47 |
|
| 48 |
let currentId = 0; |
| 49 |
|
| 50 |
/** Returns svg path(s) from a given svg data object. */ |
| 51 |
function getSvgPaths(svgPathData: string | SVGPathObject[] | undefined): ReactNode { |
| 52 |
return svgPathData && Array.isArray(svgPathData) ? ( |
| 53 |
svgPathData.map((pathObject, index) => ( |
| 54 |
<path className={pathObject.className} key={`${pathObject.path}-${index}`} d={pathObject.path} /> |
| 55 |
)) |
| 56 |
) : ( |
| 57 |
<path d={svgPathData as string} /> |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
const createInnerSvg = (icon: IconData, iconClassName: string) => { |
| 62 |
const { xOffset, yOffset, width, height, svgPathData, svgClassName } = icon ?? {}; |
| 63 |
const _xOffset = xOffset ?? 0; |
| 64 |
const _yOffset = yOffset ?? 0; |
| 65 |
const viewBox = [_xOffset, _yOffset, width, height].join(' '); |
| 66 |
|
| 67 |
const classNames: string[] = []; |
| 68 |
|
| 69 |
if (svgClassName) { |
| 70 |
classNames.push(svgClassName); |
| 71 |
} |
| 72 |
if (iconClassName) { |
| 73 |
classNames.push(iconClassName); |
| 74 |
} |
| 75 |
|
| 76 |
return ( |
| 77 |
<svg viewBox={viewBox} className={classNames.join(' ')}> |
| 78 |
{getSvgPaths(svgPathData)} |
| 79 |
</svg> |
| 80 |
); |
| 81 |
}; |
| 82 |
|
| 83 |
/** |
| 84 |
* Internal API for creating icons. Subject to change at any time. Please use `createIcon` instead. |
| 85 |
* @internal |
| 86 |
*/ |
| 87 |
export function createIconBase({ name, icon, rhUiIcon = null }: CreateIconBaseProps): ComponentClass<SVGIconProps> { |
| 88 |
if (icon == null) { |
| 89 |
// eslint-disable-next-line no-console |
| 90 |
console.warn( |
| 91 |
`@patternfly/react-icons: createIconBase is missing an \`icon\` definition (name: ${name ?? 'unknown'}).` |
| 92 |
); |
| 93 |
} |
| 94 |
return class SVGIcon extends Component<SVGIconProps> { |
| 95 |
static displayName = name; |
| 96 |
|
| 97 |
id = `icon-title-${currentId++}`; |
| 98 |
|
| 99 |
static defaultProps: SVGIconProps = { |
| 100 |
noDefaultStyle: false |
| 101 |
}; |
| 102 |
|
| 103 |
render() { |
| 104 |
const { title, className: propsClassName, set, noDefaultStyle, ...props } = this.props; |
| 105 |
|
| 106 |
const hasTitle = Boolean(title); |
| 107 |
const classNames = ['pf-v6-svg']; |
| 108 |
|
| 109 |
if (propsClassName) { |
| 110 |
classNames.push(propsClassName); |
| 111 |
} |
| 112 |
|
| 113 |
if (set === 'rh-ui' && rhUiIcon === null) { |
| 114 |
// eslint-disable-next-line no-console |
| 115 |
console.warn( |
| 116 |
`Set "rh-ui" was provided for ${name}, but no rh-ui icon data exists for this icon. The default icon will be rendered.` |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
if ((set === undefined && rhUiIcon === null) || set !== undefined) { |
| 121 |
const iconData = set !== undefined && set === 'rh-ui' && rhUiIcon !== null ? rhUiIcon : icon; |
| 122 |
const { xOffset, yOffset, width, height, svgPathData, svgClassName } = iconData ?? {}; |
| 123 |
const _xOffset = xOffset ?? 0; |
| 124 |
const _yOffset = yOffset ?? 0; |
| 125 |
const viewBox = [_xOffset, _yOffset, width, height].join(' '); |
| 126 |
|
| 127 |
if (svgClassName && !noDefaultStyle) { |
| 128 |
classNames.push(svgClassName); |
| 129 |
} |
| 130 |
|
| 131 |
return ( |
| 132 |
<svg |
| 133 |
className={classNames.join(' ')} |
| 134 |
fill="currentColor" |
| 135 |
viewBox={viewBox} |
| 136 |
aria-labelledby={hasTitle ? this.id : null} |
| 137 |
aria-hidden={hasTitle ? null : true} |
| 138 |
role="img" |
| 139 |
width="1em" |
| 140 |
height="1em" |
| 141 |
{...(props as Omit<React.SVGProps<SVGElement>, 'ref'>)} // Lie. |
| 142 |
> |
| 143 |
{hasTitle && <title id={this.id}>{title}</title>} |
| 144 |
{getSvgPaths(svgPathData)} |
| 145 |
</svg> |
| 146 |
); |
| 147 |
} |
| 148 |
return ( |
| 149 |
<svg |
| 150 |
className={classNames.join(' ')} |
| 151 |
fill="currentColor" |
| 152 |
aria-labelledby={hasTitle ? this.id : null} |
| 153 |
aria-hidden={hasTitle ? null : true} |
| 154 |
role="img" |
| 155 |
width="1em" |
| 156 |
height="1em" |
| 157 |
{...(props as Omit<React.SVGProps<SVGElement>, 'ref'>)} // Lie. |
| 158 |
> |
| 159 |
{hasTitle && <title id={this.id}>{title}</title>} |
| 160 |
{icon && createInnerSvg(icon, 'pf-v6-icon-default')} |
| 161 |
{rhUiIcon && createInnerSvg(rhUiIcon, 'pf-v6-icon-rh-ui')} |
| 162 |
</svg> |
| 163 |
); |
| 164 |
} |
| 165 |
}; |
| 166 |
} |
| 167 |
|
| 168 |
/** Public API for creating icons. Will be maintained up to breaking releases. */ |
| 169 |
export function createIcon(props: IconDefinition): ComponentClass<SVGIconProps> { |
| 170 |
const { rhUiIcon = null, ...rest } = props; |
| 171 |
const icon: IconData = { |
| 172 |
name: rest.name, |
| 173 |
width: rest.width, |
| 174 |
height: rest.height, |
| 175 |
xOffset: rest.xOffset, |
| 176 |
yOffset: rest.yOffset, |
| 177 |
svgClassName: rest.svgClassName, |
| 178 |
svgPathData: rest.svgPath |
| 179 |
}; |
| 180 |
return createIconBase({ name: icon.name, icon, rhUiIcon }); |
| 181 |
} |