Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(components): update Icon component to support a svg that has multiple paths #16468

Closed
wants to merge 12 commits into from
21 changes: 18 additions & 3 deletions components/src/icons/Icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { ICON_DATA_BY_NAME } from './icon-data'

import type { SvgProps } from '../primitives'

export interface IconData {
fill?: string
paths: Array<React.SVGProps<SVGPathElement>>
viewBox: string
}

export type IconName = keyof typeof ICON_DATA_BY_NAME

export interface IconProps extends SvgProps {
Expand Down Expand Up @@ -60,19 +66,28 @@ export function Icon(props: IconProps): JSX.Element | null {
return null
}

const { viewBox, path } = ICON_DATA_BY_NAME[name]
const { fill = 'currentColor', viewBox, paths } = ICON_DATA_BY_NAME[
name
] as IconData

if (paths.length < 1) {
return null
}

return (
<Svg
aria-hidden="true"
fill="currentColor"
aria-roledescription={name}
fill={fill}
viewBox={viewBox}
className={cx(className, { spin })}
css={spinStyle}
{...svgProps}
id={id}
>
<path aria-roledescription={name} fillRule="evenodd" d={path} />
{paths.map((pathData, index) => (
<path key={index} {...pathData} />
))}
{props.children}
</Svg>
)
Expand Down
4 changes: 2 additions & 2 deletions components/src/icons/NotificationIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { COLOR_WARNING } from '../styles'
import { Icon } from './Icon'
import { ICON_DATA_BY_NAME } from './icon-data'

import type { IconProps, IconName } from './Icon'
import type { IconProps, IconName, IconData } from './Icon'

export interface NotificationIconProps extends IconProps {
/** name constant of the optional notifcation icon to display */
Expand All @@ -19,7 +19,7 @@ const SCALE_FACTOR = 3
*/
export function NotificationIcon(props: NotificationIconProps): JSX.Element {
const { childName, childColor, ...iconProps } = props
const { viewBox } = ICON_DATA_BY_NAME[iconProps.name]
const { viewBox } = ICON_DATA_BY_NAME[iconProps.name] as IconData
const [x, y, width, height] = viewBox.split(' ').map(Number)
const scaledWidth = width / SCALE_FACTOR
const scaledHeight = height / SCALE_FACTOR
Expand Down
Loading
Loading