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 289f73f
Show file tree
Hide file tree
Showing 28 changed files with 1,360 additions and 1,746 deletions.
81 changes: 0 additions & 81 deletions scripts/compute-colors.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`JS Components/Contact Footer Default smoke-test 1`] = `
>
<div class="w-full bp-lg max-w-screen-xl mx-auto py-64 grid grid-cols-1 md:grid-cols-3 ui-grid-gap ui-grid-px">
<div class="ui-contact-footer-box">
<svg class=" block mb-16"
<svg class="block mb-16"
style="width: 3rem; height: 3rem;"
>
<use xlink:href="#sprite-icon-display-live-chat">
Expand All @@ -31,7 +31,7 @@ exports[`JS Components/Contact Footer Default smoke-test 1`] = `
</button>
</div>
<div class="ui-contact-footer-box">
<svg class=" block mb-16"
<svg class="block mb-16"
style="width: 3rem; height: 3rem;"
>
<use xlink:href="#sprite-icon-display-call-mobile">
Expand All @@ -58,7 +58,7 @@ exports[`JS Components/Contact Footer Default smoke-test 1`] = `
</div>
</div>
<div class="ui-contact-footer-box">
<svg class=" block mb-16"
<svg class="block mb-16"
style="width: 3rem; height: 3rem;"
>
<use xlink:href="#sprite-icon-display-tech-account-comms">
Expand Down
2 changes: 1 addition & 1 deletion src/core/Expander/__snapshots__/Expander.stories.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ exports[`JS Components/Expander OverriddenControls play-test 1`] = `
>
<span class="flex items-center gap-8">
Away with you, knave.
<svg class="text-pink-500 "
<svg class="text-pink-500"
style="width: 24px; height: 24px;"
>
<use xlink:href="#sprite-icon-gui-warning">
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`JS Components/Featured Link Default smoke-test 1`] = `
style="--featured-link-icon-size: var(--fs-p2);"
>
Featured link
<svg class=" align-middle ml-8 relative -top-1 -left-4 transition-[left] group-hover/featured-link:left-0"
<svg class="align-middle ml-8 relative -top-1 -left-4 transition-[left] group-hover/featured-link:left-0"
style="width: calc(var(--featured-link-icon-size) * 1.25); height: calc(var(--featured-link-icon-size) * 1.25);"
>
<use xlink:href="#sprite-icon-gui-link-arrow">
Expand All @@ -21,7 +21,7 @@ exports[`JS Components/Featured Link Disabled smoke-test 1`] = `
style="--featured-link-icon-size: var(--fs-p2);"
>
Featured link
<svg class=" align-middle ml-8 relative -top-1 -left-4 transition-[left] "
<svg class="align-middle ml-8 relative -top-1 -left-4 transition-[left] "
style="width: calc(var(--featured-link-icon-size) * 1.25); height: calc(var(--featured-link-icon-size) * 1.25);"
>
<use xlink:href="#sprite-icon-gui-link-arrow">
Expand All @@ -36,7 +36,7 @@ exports[`JS Components/Featured Link Large smoke-test 1`] = `
style="--featured-link-icon-size: var(--fs-p1);"
>
Featured link
<svg class=" align-middle ml-8 relative -top-1 -left-4 transition-[left] group-hover/featured-link:left-0"
<svg class="align-middle ml-8 relative -top-1 -left-4 transition-[left] group-hover/featured-link:left-0"
style="width: calc(var(--featured-link-icon-size) * 1.25); height: calc(var(--featured-link-icon-size) * 1.25);"
>
<use xlink:href="#sprite-icon-gui-link-arrow">
Expand Down Expand Up @@ -65,7 +65,7 @@ exports[`JS Components/Featured Link Reverse smoke-test 1`] = `
class="font-sans font-bold block text-gui-default hover:text-gui-hover focus:text-gui-focus focus:outline-gui-focus group/featured-link ui-text-p2 py-8 "
style="--featured-link-icon-size: var(--fs-p2);"
>
<svg class=" align-middle mr-8 relative -top-1 -right-4 transition-[right] group-hover/featured-link:right-0 transform rotate-180"
<svg class="align-middle mr-8 relative -top-1 -right-4 transition-[right] group-hover/featured-link:right-0 transform rotate-180"
style="width: calc(var(--featured-link-icon-size) * 1.25); height: calc(var(--featured-link-icon-size) * 1.25);"
>
<use xlink:href="#sprite-icon-gui-link-arrow">
Expand All @@ -81,7 +81,7 @@ exports[`JS Components/Featured Link Small smoke-test 1`] = `
style="--featured-link-icon-size: var(--fs-p3);"
>
Featured link
<svg class=" align-middle ml-8 relative -top-1 -left-4 transition-[left] group-hover/featured-link:left-0"
<svg class="align-middle ml-8 relative -top-1 -left-4 transition-[left] group-hover/featured-link:left-0"
style="width: calc(var(--featured-link-icon-size) * 1.25); height: calc(var(--featured-link-icon-size) * 1.25);"
>
<use xlink:href="#sprite-icon-gui-link-arrow">
Expand Down
12 changes: 6 additions & 6 deletions src/core/Footer/__snapshots__/Footer.stories.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ exports[`JS Components/Footer Default smoke-test 1`] = `
href="https://twitter.com/ablyrealtime"
title="Ably on X"
>
<svg class=" "
<svg class
style="width: 1.5rem; height: 1.5rem;"
>
<use xlink:href="#sprite-icon-social-x">
Expand All @@ -323,7 +323,7 @@ exports[`JS Components/Footer Default smoke-test 1`] = `
href="https://www.linkedin.com/company/ably-realtime"
title="Ably on LinkedIn"
>
<svg class=" "
<svg class
style="width: 1.5rem; height: 1.5rem;"
>
<use xlink:href="#sprite-icon-social-linkedin">
Expand All @@ -334,7 +334,7 @@ exports[`JS Components/Footer Default smoke-test 1`] = `
href="https://github.com/ably/"
title="Ably on Github"
>
<svg class=" "
<svg class
style="width: 1.5rem; height: 1.5rem;"
>
<use xlink:href="#sprite-icon-social-github">
Expand All @@ -345,7 +345,7 @@ exports[`JS Components/Footer Default smoke-test 1`] = `
href="https://discord.gg/jwBPhEZ9g5"
title="Ably on Discord"
>
<svg class=" "
<svg class
style="width: 1.5rem; height: 1.5rem;"
>
<use xlink:href="#sprite-icon-social-discord">
Expand All @@ -360,7 +360,7 @@ exports[`JS Components/Footer Default smoke-test 1`] = `
class="h-24 text-cool-black hover:text-icon-glassdoor"
title="Ably reviews on glassdoor"
>
<svg class=" "
<svg class
style="width: 1.5rem; height: 1.5rem;"
>
<use xlink:href="#sprite-icon-social-glassdoor">
Expand Down Expand Up @@ -388,7 +388,7 @@ exports[`JS Components/Footer Default smoke-test 1`] = `
href="https://www.glassdoor.co.uk/Overview/Working-at-Ably-EI_IE2184188.11,15.htm"
title="Ably reviews on glassdoor"
>
<svg class=" "
<svg class
style="width: 1.5rem; height: 1.5rem;"
>
<use xlink:href="#sprite-icon-social-glassdoor">
Expand Down
101 changes: 78 additions & 23 deletions src/core/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,96 @@
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;
};

const Icon = ({
name,
size = "0.75rem",
color,
secondaryColor = defaultIconSecondaryColor(name),
secondaryColor,
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, isThemed?: boolean) => {
let secondaryColorValue;
if (secondaryColor) {
secondaryColorValue = convertTailwindClassToVar(secondaryColor);
} else if (defaultIconSecondaryColor(name)) {
secondaryColorValue = convertTailwindClassToVar(
defaultIconSecondaryColor(name),
);
}

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

if (secondaryColor) {
return (
<>
{iconSvg(lightSecondaryColor, !!darkSecondaryColor)}
{darkSecondaryColor ? iconSvg(darkSecondaryColor) : null}
</>
);
} 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
Loading

0 comments on commit 289f73f

Please sign in to comment.