Skip to content

Commit

Permalink
refactor(colors): show semantic names in color component
Browse files Browse the repository at this point in the history
  • Loading branch information
masoudmanson committed Mar 12, 2024
1 parent 627a32e commit 9e831b8
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ interface ColorComponentProps {
value: string;
shade?: string;
prefix?: string;
semanticName?: string;
}

const Color = (props: ColorComponentProps) => {
const { group, value, shade, prefix = "$" } = props;
const { group, value, shade, prefix = "$", semanticName } = props;

const sassVariable = shade
? "$" + prefix + "-" + group + "-" + convertToKebabCase(shade)
Expand Down Expand Up @@ -48,7 +49,7 @@ const Color = (props: ColorComponentProps) => {

<div>
<StyledColorCode onClick={() => copyToClipboard(value)}>
{String(value)}
{String(semanticName)}
</StyledColorCode>

<StyledColorVariable onClick={() => copyToClipboard(cssVariable)}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Color as ColorType } from "src/core/styles";
import { Color as ColorType, getColors } from "src/core/styles";
import Color from "./color";
import { StyledColorGroupTitle, StyledColorsWrapper } from "./style";
import { useTheme } from "@mui/material";
import { NestedObject, flattenObj } from "./util";

interface SemanticColorsProps {
colors: object;
Expand All @@ -11,6 +13,13 @@ interface SemanticColorsProps {
const Colors = (props: SemanticColorsProps): JSX.Element => {
const { colors, type = "semantic", prefix } = props;

const theme = useTheme();
const primitiveColors = getColors({ theme });

const flattenColors = primitiveColors
? flattenObj(primitiveColors as unknown as NestedObject, null)
: {};

const renderColorGroups = (colorGroups: object) => {
if (colorGroups) {
return Object.entries(colorGroups).map(([key, value]) => (
Expand All @@ -26,7 +35,16 @@ const Colors = (props: SemanticColorsProps): JSX.Element => {

const renderColors = (sdsColors: ColorType, groupName: string) => {
if (typeof sdsColors === "string") {
return <Color group={groupName} value={sdsColors} prefix={prefix} />;
return (
<Color
group={groupName}
semanticName={
type === "semantic" ? flattenColors[sdsColors] : sdsColors
}
value={sdsColors}
prefix={prefix}
/>
);
} else {
return Object.entries(sdsColors).map(([k, v]) => {
if (v === "transparent") return;
Expand All @@ -36,6 +54,7 @@ const Colors = (props: SemanticColorsProps): JSX.Element => {
key={k}
group={groupName}
value={v}
semanticName={type === "semantic" ? flattenColors[v] : v}
shade={k}
prefix={prefix}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export type NestedObject = {
[key: string]: NestedObject | string;
};

export function flattenObj(
obj: NestedObject,
parent: string | null = null,
res: { [key: string]: string } = {}
) {
for (const key in obj) {
const propName = parent ? `${parent}-${key}` : key;
if (typeof obj[key] === "object") {
flattenObj(obj[key] as NestedObject, propName, res);
} else {
res[obj[key] as string] = propName;
}
}
return res;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useTheme } from "@mui/material";
import { getColors } from "src/core/styles";
import Colors from "../components/Color";
import Colors from "../components/Colors";

export const PrimitiveColorsTemplate = () => {
const theme = useTheme();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useTheme } from "@mui/material";
import { getSemanticComponentColors } from "src/core/styles";
import Colors from "../components/Color";
import Colors from "../components/Colors";

export const SemanticComponentColorsTemplate = () => {
const theme = useTheme();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useTheme } from "@mui/material";
import { getSemanticTextColors } from "src/core/styles";
import Colors from "../components/Color";
import Colors from "../components/Colors";

export const SemanticTextColorsTemplate = () => {
const theme = useTheme();
Expand Down

0 comments on commit 9e831b8

Please sign in to comment.