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

chore(themed): add token CSS variable test helper #9860

Merged
merged 6 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
69 changes: 69 additions & 0 deletions packages/calcite-components/src/demos/_assets/demo-theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* EXAMPLE USAGE:
alisonailea marked this conversation as resolved.
Show resolved Hide resolved
*
* <demo-theme tokens="
* --calcite-button-background-color,
* --calcite-button-border-color,
* --calcite-button-corner-radius,
* --calcite-button-corner-radius-start-start,
* --calcite-button-corner-radius-start-end,
* --calcite-button-corner-radius-end-start,
* --calcite-button-corner-radius-end-end,
* --calcite-button-loader-color,
* --calcite-button-shadow,
* --calcite-button-text-color,
* --calcite-button-icon-color"
* ><calcite-button kind="inverse" scale="l"> Button </calcite-button></demo-theme>
*
*/

import { setCSSVariables } from "../../utils/variableValue";

export class DemoTheme extends HTMLElement {
_slot: HTMLSlotElement;

_el: HTMLElement;

static observedAttributes = ["tokens"];

constructor() {
super();
const shadow = this.attachShadow({ mode: "open" });
const slot = document.createElement("slot");
shadow.append(slot);
this._slot = slot;
if (this._slot.assignedNodes().length === 1 && this._slot.assignedNodes()[0].nodeName.includes("calcite")) {
this._el = this._slot.assignedNodes()[0] as HTMLElement;
}
}

attributeChangedCallback(name: string, oldValue: string, newValue: string): void {
if (newValue !== oldValue && name === "tokens") {
this.updateTheme(newValue);
}
}

updateTheme(newValue: string): void {
if (typeof newValue === "string") {
let tokensList;

try {
tokensList = JSON.parse(newValue);
} catch (error) {
tokensList = newValue.split(",").map((t) => t.trim());
}

if (Array.isArray(tokensList)) {
const stringifiedTheme = setCSSVariables(tokensList);

if (this._el) {
this._el.style.cssText = stringifiedTheme;
} else {
this.setAttribute("style", stringifiedTheme);
}
}
}
}
}

customElements.define("demo-theme", DemoTheme);
21 changes: 2 additions & 19 deletions packages/calcite-components/src/tests/commonTests/themed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { E2EElement, E2EPage } from "@stencil/core/testing";
import { toHaveNoViolations } from "jest-axe";
import { ElementHandle } from "puppeteer";
import type { RequireExactlyOne } from "type-fest";
import { setVariableValue } from "../../utils/variableValue";
import type { ComponentTestSetup } from "./interfaces";
import { getTagAndPage } from "./utils";

Expand Down Expand Up @@ -101,7 +102,7 @@ export function themed(componentTestSetup: ComponentTestSetup, tokens: Component

// Set test values for each token
if (!setTokens[token]) {
setTokens[token] = assignTestTokenThemeValues(token);
setTokens[token] = setVariableValue(token);
}

// Set up styleTargets and testTargets
Expand Down Expand Up @@ -426,21 +427,3 @@ async function assertThemedProps(page: E2EPage, options: TestTarget): Promise<vo
function getStyleString(token: string, prop: string, value: string): string {
return `[${token}:${prop}] ${value}`;
}

/**
*
* Sets the value of a CSS variable to a test value.
* This is useful for testing themed components.
*
* @param token - the token as a CSS variable
* @returns string - the new value for the token
*/
function assignTestTokenThemeValues(token: string): string {
const legacyBackgroundColorToken = token.endsWith("-background");

return token.includes("color") || legacyBackgroundColorToken
? "rgb(0, 191, 255)"
: token.includes("shadow")
? "rgb(255, 255, 255) 0px 0px 0px 4px, rgb(255, 105, 180) 0px 0px 0px 5px inset, rgb(0, 191, 255) 0px 0px 0px 9px"
: `42${token.includes("z-index") ? "" : "px"}`;
}
42 changes: 42 additions & 0 deletions packages/calcite-components/src/utils/variableValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
alisonailea marked this conversation as resolved.
Show resolved Hide resolved
*
* Sets the value of a CSS variable to a test value.
* This is useful for testing themed components.
*
* @param token - the token as a CSS variable
* @returns string - the new value for the token
*/
export function setVariableValue(token: string): string {
alisonailea marked this conversation as resolved.
Show resolved Hide resolved
const tokenValueMap = {
background$: "rgb(252, 244, 52)",
"text-color$": "rgb(239,118,39)",
"border-color$": "rgb(156, 89, 209)",
"background-color$": "rgb(44, 44, 44)",
color$: "rgb(0, 191, 255)",
highlighted$: "rgb(255, 105, 180)",
selected$: "rgb(255, 255, 255)",
shadow$:
"rgb(255, 255, 255) 0px 0px 0px 4px, rgb(255, 105, 180) 0px 0px 0px 5px inset, rgb(0, 191, 255) 0px 0px 0px 9px",
"z-index$": "42",
"(size|space)$": "42px",
};
alisonailea marked this conversation as resolved.
Show resolved Hide resolved

try {
const [, value] = Object.entries(tokenValueMap).find(([regexStr]) => {
alisonailea marked this conversation as resolved.
Show resolved Hide resolved
return new RegExp(regexStr, "g").test(token);
});

return value;
} catch (error) {
console.warn("token not found in tokenValueMap", token);
return "rgb(0, 191, 255)";
}
}

export function setCSSVariables(tokens: string[]): string {
alisonailea marked this conversation as resolved.
Show resolved Hide resolved
return tokens
.map((token) => {
return `${token}: ${setVariableValue(token)};`;
})
.join("\n");
}
Loading