Skip to content

Commit

Permalink
feat: add support for dark theme
Browse files Browse the repository at this point in the history
  • Loading branch information
dlockhart committed Jul 26, 2023
1 parent b042d37 commit d4f0c7d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/browser/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const DEFAULT_LANG = 'en',

let currentLang = undefined,
currentRtl = false,
currentTheme = undefined,
currentViewportHeight = 0,
currentViewportWidth = 0,
shouldResetMouse = false;
Expand Down Expand Up @@ -54,6 +55,16 @@ export async function reset(opts) {
awaitNextFrame = true;
}

if (opts.theme !== currentTheme) {
if (opts.theme === 'dark') {
document.body.setAttribute('data-theme', 'dark');
} else {
document.body.removeAttribute('data-theme');
}
currentTheme = opts.theme;
awaitNextFrame = true;
}

if (opts.viewport.height !== currentViewportHeight || opts.viewport.width !== currentViewportWidth) {
await setViewport(opts.viewport).catch(() => {});
awaitNextFrame = true;
Expand Down
3 changes: 3 additions & 0 deletions src/server/report/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ class App extends LitElement {
border: 1px solid #cdd5dc;
padding: 10px;
text-align: center;
white-space: nowrap;
}
thead th {
background-color: #f5f5f5;
}
tbody th {
font-weight: normal;
text-align: left;
width: 100%;
white-space: normal;
}
td.passed {
background-color: #efffd9;
Expand Down
3 changes: 3 additions & 0 deletions src/server/wtr-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ export class WTRConfig {
line-height: 1.4rem;
padding: 30px;
}
body[data-theme="dark"] {
background-color: #000000;
}
</style>
</head>
<body>
Expand Down
13 changes: 13 additions & 0 deletions test/browser/fixture.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ describe('fixture', () => {
expect(window.innerWidth).to.equal(800);
});

it('should use no theme by default', async() => {
await fixture(html`<p>hello</p>`);
expect(document.body.hasAttribute('data-theme')).to.be.false;
});

it('should reset theme', async() => {
const elem = html`<p>hello</p>`;
await fixture(elem, { theme: 'dark' });
expect(document.body.getAttribute('data-theme')).to.equal('dark');
await fixture(elem);
expect(document.body.hasAttribute('data-theme')).to.be.false;
});

});

describe('waitForElem', () => {
Expand Down
44 changes: 44 additions & 0 deletions test/browser/theme.vdiff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { css, html, LitElement } from 'lit';
import { defineCE, expect, fixture } from '../../src/browser/index.js';

const themeTag = defineCE(
class extends LitElement {
static get properties() {
return {
theme: { reflect: true, type: String }
};
}
static get styles() {
return css`
:host {
border: 1px solid black;
display: inline-block;
text-align: center;
padding: 20px;
}
:host([theme="dark"]) {
border-color: white;
color: white;
}
`;
}
render() {
return html`Theme: ${this.theme}`;
}
}
);

describe('theme', () => {
[
'normal',
'dark'
].forEach(name => {
it(name, async() => {
const elem = await fixture(
`<${themeTag} theme="${name}"></${themeTag}>`,
{ theme: name === 'normal' ? undefined : name }
);
await expect(elem).to.be.golden();
});
});
});

0 comments on commit d4f0c7d

Please sign in to comment.