Skip to content

Commit

Permalink
chore: assert presence of units in Icon size prop values
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiehenson committed Oct 30, 2024
1 parent d0ac55e commit e2bcb3f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/core/Icon.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
30 changes: 22 additions & 8 deletions src/core/Icon/EncapsulatedIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 (
<div
className={`p-1 rounded-lg ${theme === "light" ? "bg-gradient-to-t" : "bg-gradient-to-b"} ${themeColor("from-neutral-900")} ${className ?? ""}`}
style={{ width: numericalSize, height: numericalSize }}
style={{ width: size, height: size }}
>
<div
className={`flex items-center justify-center rounded-lg ${themeColor("bg-neutral-1100")} ${innerClassName ?? ""}`}
style={{ height: numericalSize - 2 }}
style={{ height: computedIconHeight }}
>
<Icon size={`${numericalIconSize}`} {...iconProps} />
<Icon size={computedIconSize} {...iconProps} />
</div>
</div>
);
Expand Down
6 changes: 6 additions & 0 deletions src/core/Icon/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`;

0 comments on commit e2bcb3f

Please sign in to comment.