From e2bcb3f446f25280ce54bb5504efdfce63d393e1 Mon Sep 17 00:00:00 2001 From: Jamie Henson Date: Wed, 30 Oct 2024 14:18:28 +0000 Subject: [PATCH] chore: assert presence of units in Icon size prop values --- src/core/Icon.tsx | 4 ++-- src/core/Icon/EncapsulatedIcon.tsx | 30 ++++++++++++++++++++++-------- src/core/Icon/types.ts | 6 ++++++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/core/Icon.tsx b/src/core/Icon.tsx index a6e4370e8..7bc8f1592 100644 --- a/src/core/Icon.tsx +++ b/src/core/Icon.tsx @@ -1,12 +1,12 @@ import React, { CSSProperties } from "react"; import { defaultIconSecondaryColor } from "./Icon/secondary-colors"; -import { IconName } from "./Icon/types"; +import { IconName, IconSize } from "./Icon/types"; import { ColorClass } from "./styles/colors/types"; import { convertTailwindClassToVar } from "./styles/colors/utils"; export type IconProps = { name: IconName; - size?: string; + size?: IconSize; color?: ColorClass; secondaryColor?: ColorClass; additionalCSS?: string; diff --git a/src/core/Icon/EncapsulatedIcon.tsx b/src/core/Icon/EncapsulatedIcon.tsx index 50e993291..0e2b7976c 100644 --- a/src/core/Icon/EncapsulatedIcon.tsx +++ b/src/core/Icon/EncapsulatedIcon.tsx @@ -2,14 +2,18 @@ import React from "react"; import Icon, { IconProps } from "../Icon"; import useTheming from "../hooks/useTheming"; import { Theme } from "../styles/colors/types"; +import { IconSize } from "./types"; type EncapsulatedIconProps = { theme?: Theme; className?: string; innerClassName?: string; - iconSize?: string; + iconSize?: IconSize; } & IconProps; +const ICON_SIZE_REDUCTION = 12; +const ICON_HEIGHT_REDUCTION = 2; + const EncapsulatedIcon = ({ theme = "dark", size = "40px", @@ -22,21 +26,31 @@ const EncapsulatedIcon = ({ baseTheme: "dark", theme, }); - const numericalSize = parseInt(size, 10); - const numericalIconSize = iconSize - ? parseInt(iconSize, 10) - : numericalSize - 12; + let computedIconSize = size, + computedIconHeight = size; + + if (iconSize) { + computedIconSize = iconSize; + } else if (size.toString().endsWith("px")) { + const numericalSize = parseInt(size, 10); + computedIconSize = `${numericalSize - ICON_SIZE_REDUCTION}px`; + computedIconHeight = `${numericalSize - ICON_HEIGHT_REDUCTION}px`; + } else { + console.warn( + `EncapsulatedIcon: Non-pixel units might not behave as expected`, + ); + } return (
- +
); diff --git a/src/core/Icon/types.ts b/src/core/Icon/types.ts index ee19d3645..35da05048 100644 --- a/src/core/Icon/types.ts +++ b/src/core/Icon/types.ts @@ -16,3 +16,9 @@ export type IconName = | (typeof iconNames.other)[number] | (typeof iconNames.tech)[number] | (typeof iconNames.product)[number]; + +export type IconSize = + | `${number}px` + | `${number}em` + | `${number}rem` + | `calc(${string})`;