Skip to content

Commit

Permalink
Icon - Allow sizing of icon to match an element that is not it's parent
Browse files Browse the repository at this point in the history
  • Loading branch information
meissadia committed Jul 25, 2023
1 parent c1d63ac commit 90c6fc7
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/components/Icon/Icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import classNames from 'classnames';
import { useIconSvg } from '../../hooks/useIconSvg';
import { numberIcons } from './iconLists';

// Design System font sizes for HTML elements
const sizeMap: Record<string, string> = {
h1: '34px',
h2: '26px',
h3: '22px',
h4: '18px',
h5: '14px',
p: '16px',
sub: '12px'
};

// Icons who's background is square as opposed to round
const isSquare = new Set([
'email',
'facebook',
Expand All @@ -13,6 +25,7 @@ const isSquare = new Set([
'youtube'
]);

// Is this an number icon, based on the icon name?
const isNumber = new Set(numberIcons);

/**
Expand Down Expand Up @@ -40,6 +53,7 @@ interface IconProperties {
name: string;
alt?: string;
withBg?: boolean;
size?: string;
}

/**
Expand All @@ -50,12 +64,14 @@ interface IconProperties {
* @param name Canonical icon name
* @param alt Alt text for image
* @param withBg With background?
* @param size Match the icon size to a specified HTML element or provide a custom size. By default the icon size is determined by it's parent element's font-size.
* @returns ReactElement | null
*/
export const Icon = ({
name,
alt,
withBg = false,
size = 'inherit',
...others
}: IconProperties): JSX.Element | null => {
const shapeModifier = getShapeModifier(name, withBg);
Expand All @@ -69,7 +85,8 @@ export const Icon = ({
const iconAttributes = [
`class="${classNames(classes)}"`,
'role="img"',
`alt="${alt ?? name}"`
`alt="${alt ?? name}"`,
`style="font-size: ${sizeMap[size] || size}"`
].join(' ');

const iconHtml = `${icon}`.replace('<svg', `<svg ${iconAttributes} `);
Expand Down

0 comments on commit 90c6fc7

Please sign in to comment.