Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed onSurface to have the correct camel casing #571

Merged
merged 18 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,106 +1,155 @@
import React, { useEffect, useState } from "react";
import { Box, styled, AlertBanner, theme } from "@washingtonpost/wpds-ui-kit";
import { toast } from "react-toastify";
import { styled } from "@washingtonpost/wpds-ui-kit";
import Tokens from "@washingtonpost/wpds-theme/src/wpds.tokens.json";
import { hex, score } from "wcag-contrast";
import { useTheme } from "next-themes";

import { Grid } from "../Components/Grid";
const xMapping = [0, 20, 40, 60, 80, 100, 200, 300, 400, 500, 600, 700];
const yMapping = [
"red",
"blue",
"green",
"orange",
"teal",
"gold",
"mustard",
"purple",
"pink",
"yellow",
"gray",
];

const ColorSamples = ({ group }) => {
const [colorGroupArray, setColorGroupArray] = useState([]);
const [copyText, setCopyText] = useState("");
const PaletteGrid = styled("div", {
display: "grid",
gridTemplateColumns: "30px repeat(12, 1fr)",
gridTemplateRows: "repeat(11, 1fr)", // Adjusted to match yMapping length
gap: "$025",
});

useEffect(() => {
const colorArray = handleColor(group);
setColorGroupArray(colorArray);
}, [group]);
const Swatch = styled("div", {
minWidth: 20,
minHeight: 40,
width: "100%",
height: "100%",
borderRadius: "4px",
borderColor: "#e1e1e1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit pick could we use a color token if possible

borderWidth: "1px",
borderStyle: "solid",
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
color: "black", // Default text color
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit pick could we use a color token if possible

"@sm": {
minWidth: "unset",
minHeight: "unset",
},
"& div": {
"@sm": {
display: "none",
},
},
});
const AxisLabel = styled("div", {
fontWeight: "bold",
display: "flex",
alignItems: "end",
justifyContent: "center",
});

export default function ColorGrid() {
const { theme } = useTheme();
const [context, setContext] = useState(Tokens.color.light);

useEffect(() => {
const SuccessToast = () => (
<AlertBanner.Root variant="success">
<AlertBanner.Content css={{ minWidth: 250, paddingRight: "$050" }}>
<b>Copied: </b>
<Box as="span" css={{ fontSize: theme.fontSizes[100] }}>
<Box as="i">{copyText}</Box>
</Box>
</AlertBanner.Content>
</AlertBanner.Root>
);
setContext(theme === "dark" ? Tokens.color.dark : Tokens.color.light);
}, [theme]);

if (copyText) {
window.navigator.clipboard.writeText(copyText);
toast(<SuccessToast />, {
position: "top-center",
closeButton: false,
autoClose: 2000,
hideProgressBar: true,
draggable: false,
onClose: () => {
setCopyText(null);
},
});
}
}, [copyText]);
function lookUpValue(item) {
let value = item.value;

const handleColor = (group) => {
const colorNamesArray = Object.keys(Tokens.color[group]);
let lookUpKey = value?.substring(1, value.length - 1);

return colorNamesArray.filter((colorName) =>
Object.prototype.hasOwnProperty.call(
Tokens.color[group][colorName],
"value"
)
);
};
if (theme === "dark") {
value = item.valueDark;
lookUpKey = value?.substring(1, value.length - 1);
return Tokens.color.dark[lookUpKey]?.hex;
} else {
return Tokens.color.light[lookUpKey]?.hex;
}
}

const Swatch = styled("button", {
backgroundColor: "transparent",
border: "1px solid $subtle",
padding: 0,
"&:hover": {
opacity: 0.5,
},
});
const GetSwatchesWithLabels = () => {
let elements = [];
const background = lookUpValue(Tokens.color.theme.background);

// Y-axis labels
yMapping.forEach((label, index) => {
elements.push(
<AxisLabel
key={`y-label-${index}`}
css={{
textTransform: "capitalize",
gridColumn: index + 2,
gridRow: 1,
"@sm": {
display: "none",
},
}}
>
{label}
</AxisLabel>
);
});

const ColorExample = styled("div", {
minHeight: "$500",
width: "100%",
display: "flex",
alignItems: "center",
justifyContent: "center",
});
const ColorID = styled("p", {
fontSize: "$100",
marginBlock: 0,
padding: "$025",
fontFamily: "$meta",
color: "$primary",
});
// X-axis labels
xMapping.forEach((label, index) => {
elements.push(
<AxisLabel
key={`x-label-${index}`}
style={{
justifySelf: "end",
alignSelf: "center",
paddingRight: 4,
gridRow: index + 2,
gridColumn: 1,
}}
>
{label}
</AxisLabel>
);
});

return (
<>
<Grid maxSize={"120px"}>
{colorGroupArray.map((key, i) => (
// Swatches
yMapping.forEach((color, yIndex) => {
xMapping.forEach((_, xIndex) => {
const key = `${color.toLowerCase()}${xMapping[xIndex]}`;
const hexColor = context[key]?.hex || "transparent"; // Use transparent for empty swatches
const contrast =
hexColor != undefined &&
hexColor !== "transparent" &&
hex(hexColor, background).toFixed(2);
const TokenScore = contrast && score(contrast);
elements.push(
<Swatch
key={i}
onClick={() =>
setCopyText(
`$${key.toLowerCase()}${group == "static" ? "-static" : ""}`
)
}
css={{
backgroundColor: `$${key}`,
gridRow: xIndex + 2, // Offset by 1 due to labels
gridColumn: yIndex + 2, // Offset by 1 due to labels
color:
TokenScore && TokenScore !== "AA Large" && TokenScore !== "Fail"
? "$gray700"
: "$gray0",
}}
key={key}
>
<ColorExample
css={{
backgroundColor: `$${key}${group == "static" ? "-static" : ""}`,
}}
/>
<ColorID>{key}</ColorID>
<div>{contrast}</div>
</Swatch>
))}
</Grid>
</>
);
};

ColorSamples.displayName = "Color Samples";
);
});
});

export default ColorSamples;
return elements;
};
return <PaletteGrid>{GetSwatchesWithLabels()}</PaletteGrid>;
}
Loading
Loading