Skip to content

Commit

Permalink
docs: example of multiple themes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Sep 6, 2021
1 parent 704bea9 commit 7b32255
Show file tree
Hide file tree
Showing 19 changed files with 478 additions and 0 deletions.
144 changes: 144 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,150 @@ module.exports = {
};
```

### Multiple Themes

**webpack.config.js**

```js
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
entry: "./src/index.js",
module: {
rules: [
{
test: /\.s[ac]ss$/i,
oneOf: [
{
resourceQuery: "?dark",
use: [
Self.loader,
"css-loader",
{
loader: "sass-loader",
options: {
additionalData: `@use 'dark-theme/vars' as vars;`,
},
},
],
},
{
use: [
Self.loader,
"css-loader",
{
loader: "sass-loader",
options: {
additionalData: `@use 'light-theme/vars' as vars;`,
},
},
],
},
],
},
],
},
plugins: [
new Self({
filename: "[name].css",
attributes: {
id: "theme",
},
}),
],
};
```

**src/index.js**

```js
import "./style.scss";

let theme = "light";
const themes = {};

themes[theme] = document.querySelector("#theme");

async function loadTheme(newTheme) {
// eslint-disable-next-line no-console
console.log(`CHANGE THEME - ${newTheme}`);

const themeElement = document.querySelector("#theme");

if (themeElement) {
themeElement.remove();
}

if (themes[newTheme]) {
// eslint-disable-next-line no-console
console.log(`THEME ALREADY LOADED - ${newTheme}`);

document.head.appendChild(themes[newTheme]);

return;
}

if (newTheme === "dark") {
// eslint-disable-next-line no-console
console.log(`LOADING THEME - ${newTheme}`);

import(/* webpackChunkName: "dark" */ "./style.scss?dark").then(() => {
themes[newTheme] = document.querySelector("#theme");

// eslint-disable-next-line no-console
console.log(`LOADED - ${newTheme}`);
});
}
}

document.onclick = () => {
if (theme === "light") {
theme = "dark";
} else {
theme = "light";
}

loadTheme(theme);
};
```

**src/dark-theme/\_vars.scss**

```scss
$background: black;
```

**src/light-theme/\_vars.scss**

```scss
$background: white;
```

**src/styles.scss**

```scss
body {
background-color: vars.$background;
}
```

**public/index.html**

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Document</title>
<link id="theme" rel="stylesheet" type="text/css" href="./main.css" />
</head>
<body>
<script src="./main.js"></script>
</body>
</html>
```

### Media Query Plugin

If you'd like to extract the media queries from the extracted CSS (so mobile users don't need to load desktop or tablet specific CSS anymore) you should use one of the following plugins:
Expand Down
85 changes: 85 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
"memfs": "^3.0.2",
"npm-run-all": "^4.1.5",
"prettier": "^2.3.2",
"sass": "^1.39.0",
"sass-loader": "^12.1.0",
"standard-version": "^9.3.0",
"webpack": "^5.48.0",
"webpack-cli": "^4.7.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: black;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: white;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Document</title>
<link id="theme" rel="stylesheet" type="text/css" href="./main.css">
</head>
<body>
<script src="./main.js"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$background: black;
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-env browser */
import "./style.scss";

let theme = "light";
const themes = {};

themes[theme] = document.querySelector("#theme");

async function loadTheme(newTheme) {
// eslint-disable-next-line no-console
console.log(`CHANGE THEME - ${newTheme}`);

const themeElement = document.querySelector("#theme");

if (themeElement) {
themeElement.remove();
}

if (themes[newTheme]) {
// eslint-disable-next-line no-console
console.log(`THEME ALREADY LOADED - ${newTheme}`);

document.head.appendChild(themes[newTheme]);

return;
}

if (newTheme === "dark") {
// eslint-disable-next-line no-console
console.log(`LOADING THEME - ${newTheme}`);

// eslint-disable-next-line import/no-unresolved
import(/* webpackChunkName: "dark" */ "./style.scss?dark").then(() => {
themes[newTheme] = document.querySelector("#theme");

// eslint-disable-next-line no-console
console.log(`LOADED - ${newTheme}`);
});
}
}

document.onclick = () => {
if (theme === "light") {
theme = "dark";
} else {
theme = "light";
}

loadTheme(theme);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$background: white;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background-color: vars.$background;
}
Loading

0 comments on commit 7b32255

Please sign in to comment.