Skip to content

Commit

Permalink
Fix a bug with the rays of the sun icon not being affected by `--them…
Browse files Browse the repository at this point in the history
…e-switch-icon-color`
  • Loading branch information
mahozad committed Mar 6, 2022
1 parent 4fa5c3f commit c90d787
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 16 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions test2.html
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Theme switch test 2</title>
<script src="theme-switch.js"></script>
<style>
html {
--theme-switch-icon-color: #c37ae7;
}
theme-switch {
width: 512px;
}
</style>
</head>
<body>

<theme-switch></theme-switch>

</body>
</html>
9 changes: 3 additions & 6 deletions theme-switch.js
Expand Up @@ -200,12 +200,9 @@ function generateStyle() {
cursor: pointer;
}
/* Only change the color of the core and not rays */
/* as it seems to make a visually better animation */
#circle {
fill: var(--theme-switch-icon-color);
stroke: var(--theme-switch-icon-color);
}
#circle { fill: var(--theme-switch-icon-color, #000); }
#rays { stroke: var(--theme-switch-icon-color, #000); }
`;
}

Expand Down
9 changes: 3 additions & 6 deletions theme-switch.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion theme-switch.min.js.map

Large diffs are not rendered by default.

30 changes: 27 additions & 3 deletions theme-switch.test.js
Expand Up @@ -196,10 +196,34 @@ describe("Screenshot tests", () => {
expect(snapshotTakenNow).toMatchReferenceSnapshot();
}, 100_000);

test(`When user specifies a custom color for switch icon, the colors should be applied`, async () => {
await takeScreenshot(
() => {localStorage.setItem("theme", "light");},
element => {
// See https://stackoverflow.com/a/64487791/8583692
element.evaluate((el) => {
el.style.setProperty("--theme-switch-icon-color", "#ffe36e");
});
}
);
const snapshotTakenNow = fileSystem.readFileSync(snapshotFileName);
expect(snapshotTakenNow).toMatchReferenceSnapshot();
}, 100_000);

test(`When user specifies a custom color for switch icon in a CSS rule with low specificity (like html{}), the colors should be applied`, async () => {
await takeScreenshot(
() => {localStorage.setItem("theme", "light");},
(element) => {},
"test2.html"
);
const snapshotTakenNow = fileSystem.readFileSync(snapshotFileName);
expect(snapshotTakenNow).toMatchReferenceSnapshot();
}, 100_000);

afterAll(() => {fileSystem.rmSync(snapshotFileName);});
});

async function takeScreenshot(init, action = () => {}) {
async function takeScreenshot(init, action = () => {}, pageHTML = "test.html") {
const browser = await puppeteer.launch({
headless: true, // If false, opens the browser UI
// channel: "chrome", // this overrides executablePath
Expand All @@ -213,11 +237,11 @@ async function takeScreenshot(init, action = () => {}) {
// See https://stackoverflow.com/a/66530593/8583692
await page.evaluateOnNewDocument(init);
// page.setContent("<DOCTYPE html><html>...")
await page.goto(`file://${__dirname}\\test.html`);
await page.goto(`file://${__dirname}\\${pageHTML}`);

const element = await page.$("theme-switch");
await action(element);
// Wait for the element animation to finish
// Wait for the action or element animation to finish
await page.waitForTimeout(1000);

await element.screenshot({path: snapshotFileName});
Expand Down

0 comments on commit c90d787

Please sign in to comment.