Skip to content

Commit

Permalink
feat: use Tailwind theming prefixes over custom approach
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiehenson committed Nov 19, 2024
1 parent 76c079b commit 2ebf07d
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 283 deletions.
81 changes: 0 additions & 81 deletions scripts/compute-colors.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/core/FeaturedLink.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { CSSProperties, ReactNode } from "react";

import Icon from "./Icon";
import { ColorClass } from "./styles/colors/types";
import { ColorClass, ColorThemeSet } from "./styles/colors/types";

type FeaturedLinkProps = {
url: string;
children: ReactNode;
textSize?: string;
iconColor?: ColorClass;
iconColor?: ColorClass | ColorThemeSet;
flush?: boolean;
reverse?: boolean;
additionalCSS?: string;
Expand Down
85 changes: 63 additions & 22 deletions src/core/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import React, { CSSProperties } from "react";
import React, { CSSProperties, useCallback } from "react";
import clsx from "clsx";

import { defaultIconSecondaryColor } from "./Icon/secondary-colors";
import { IconName, IconSize } from "./Icon/types";
import { ColorClass } from "./styles/colors/types";
import { ColorClass, ColorThemeSet } from "./styles/colors/types";
import { convertTailwindClassToVar } from "./styles/colors/utils";

export type IconProps = {
name: IconName;
size?: IconSize;
color?: ColorClass;
secondaryColor?: ColorClass;
color?: ColorClass | ColorThemeSet;
secondaryColor?: ColorClass | ColorThemeSet;
additionalCSS?: string;
};

Expand All @@ -19,23 +21,62 @@ const Icon = ({
secondaryColor = defaultIconSecondaryColor(name),
additionalCSS = "",
...additionalAttributes
}: IconProps) => (
<svg
className={`${color ?? ""} ${additionalCSS}`}
style={
{
width: size,
height: size,
...(secondaryColor && {
"--ui-icon-secondary-color":
convertTailwindClassToVar(secondaryColor),
}),
} as CSSProperties
}
{...additionalAttributes}
>
<use xlinkHref={`#sprite-${name}`} />
</svg>
);
}: IconProps) => {
const [lightSecondaryColor, darkSecondaryColor] = (
secondaryColor ?? ""
).split(" dark:") as [ColorClass, ColorClass | undefined];

const iconSvg = useCallback(
(secondaryColor?: ColorClass) => (
<svg
className={clsx(
{ [`${color}`]: color },
{ [`${additionalCSS}`]: additionalCSS },
{
"hidden dark:block":
secondaryColor && secondaryColor === darkSecondaryColor,
},
{
"dark:hidden":
secondaryColor && secondaryColor === lightSecondaryColor,
},
)}
style={
{
width: size,
height: size,
...(secondaryColor && {
"--ui-icon-secondary-color":
convertTailwindClassToVar(lightSecondaryColor),
}),
} as CSSProperties
}
{...additionalAttributes}
>
<use xlinkHref={`#sprite-${name}`} />
</svg>
),
[
name,
size,
color,
additionalCSS,
additionalAttributes,
lightSecondaryColor,
darkSecondaryColor,
],
);

if (darkSecondaryColor) {
return (
<>
{iconSvg(lightSecondaryColor)}
{iconSvg(darkSecondaryColor)}
</>
);
} else {
return iconSvg();
}
};

export default Icon;
12 changes: 2 additions & 10 deletions src/core/Icon/EncapsulatedIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
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?: IconSize;
Expand All @@ -15,17 +12,12 @@ const ICON_SIZE_REDUCTION = 12;
const ICON_HEIGHT_REDUCTION = 2;

const EncapsulatedIcon = ({
theme = "dark",
size = "40px",
iconSize,
className,
innerClassName,
...iconProps
}: EncapsulatedIconProps) => {
const { themeColor } = useTheming({
baseTheme: "dark",
theme,
});
let computedIconSize = size,
computedIconHeight = size;

Expand All @@ -43,11 +35,11 @@ const EncapsulatedIcon = ({

return (
<div
className={`p-1 rounded-lg ${theme === "light" ? "bg-gradient-to-t" : "bg-gradient-to-b"} ${themeColor("from-neutral-900")} ${className ?? ""}`}
className={`p-1 rounded-lg bg-gradient-to-t dark:bg-gradient-to-b ${className ?? "from-neutral-400 dark:from-neutral-900"}`}
style={{ width: size, height: size }}
>
<div
className={`flex items-center justify-center rounded-lg ${themeColor("bg-neutral-1100")} ${innerClassName ?? ""}`}
className={`flex items-center justify-center rounded-lg ${innerClassName ?? "bg-neutral-200 dark:bg-neutral-1100"}`}
style={{ height: computedIconHeight }}
>
<Icon size={computedIconSize} {...iconProps} />
Expand Down
11 changes: 3 additions & 8 deletions src/core/Pricing/PricingCards.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ export default {
};

const Template = ({ data, delimiter }: PricingCardsProps) => (
<>
<div className="bg-gradient-to-r from-blue-800 to-pink-800 p-32">
<PricingCards data={data} theme="dark" delimiter={delimiter} />
</div>
<div className="p-32">
<PricingCards data={data} theme="light" delimiter={delimiter} />
</div>
</>
<div className="dark:bg-gradient-to-r dark:from-blue-800 dark:to-pink-800 p-32">
<PricingCards data={data} delimiter={delimiter} />
</div>
);

export const Plans = {
Expand Down
Loading

0 comments on commit 2ebf07d

Please sign in to comment.