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

Feat/implement dark mode #13

Merged
merged 6 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ The CBP Design System (1.0) exists to provide a unifying user experience and red
$ cd design-system
$ npm install
$ npm run styles-dev # run the styles dev server
$ npm run styles-build # runs vite build
$ npm run styles-build # runs vite build to build out css and js
$ npm run styles-sb # runs storybook for the styles package
```

The CBP Design System repo is set up as a monorepo that uses the "workspaces" feature from `npm` for managing multiple packages from a singular top-level, root package. [[NPM Workspaces]](https://docs.npmjs.com/cli/v8/using-npm/workspaces)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"build-tokens": "npm run sd -w packages/styles",
"styles-dev": "npm run dev -w packages/styles",
"styles-build": "npm run build -w packages/styles",
"styles-sb": "npm run storybook -w packages/styles",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
Expand Down
8 changes: 7 additions & 1 deletion packages/styles/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,13 @@ <h6 class="cbp-banner__info-title d-none-sm">
<div class="sandbox__demo-container">
<div class="sandbox__demo">
<!-- *COMPONENT GOES HERE* -->

<label class="cbp-toggle demo-toggle" data-component="cbp-toggle">
<input type="checkbox" data-theme-toggle>
<span class="slider">
<i class="fas fa-moon" style="color: yellow;"></i>
<i class="fas fa-sun" style="color: orange;"></i>
</span>
</label>
</div>
<aside class="sandbox__modifiers">
<h2>State Modifiers</h2>
Expand Down
5 changes: 5 additions & 0 deletions packages/styles/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Toggle from './components/toggle/toggle';
import FileUploader from './components/fileupload/fileupload';
import HashedField from './components/form/hashed-field/hashedField';
import NumberCounter from './components/form/number-counter/numberCounter';
import DarkMode from './utilities/darkMode';

import './sass/main.scss';

Expand Down Expand Up @@ -92,4 +93,8 @@ document.addEventListener('DOMContentLoaded', (event) => {
SelectorEngine.findAll('.cbp-form__number--counter').forEach((counter) => {
addOrInstantiate(NumberCounter, counter);
});

SelectorEngine.findAll('[data-theme-toggle]').forEach((themeToggle) => {
addOrInstantiate(DarkMode, themeToggle);
});
});
14 changes: 9 additions & 5 deletions packages/styles/src/sass/base/_core.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
@use 'colors' as *;
@use '../tokens/font' as *;

html {
html[data-cbp-theme="light"] {
color: $cbp-text-darkest;
font-family: $cbp-font-family-roboto;
background-color: $cbp-bg-light;
}

[data-theme="dark"] {
background-color: $cbp-bg-dark;
html[data-cbp-theme="dark"] {
color: $cbp-text-lightest;
}
background-color: $cbp-bg-dark;
}

body {
font-family: $cbp-font-family-roboto;
}
34 changes: 34 additions & 0 deletions packages/styles/src/utilities/darkMode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class DarkMode {
constructor(themeToggle) {
// Toggle component needs to have 'data-theme-toggle' attribute in order for this to work.
this.themeToggle = themeToggle;
this.prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)');
this.storedTheme = localStorage.getItem('theme');

this.preferredTheme() === 'dark' ? this.themeToggle.checked = true : this.themeToggle.checked = false;

this.setTheme(this.preferredTheme());

this.handleThemeToggle(this.themeToggle);
}

preferredTheme() {
if (this.storedTheme) {
return this.storedTheme;
}
return this.prefersDarkMode.matches ? 'dark' : 'light';
}

setTheme(theme) {
localStorage.setItem('theme', theme)
document.documentElement.setAttribute('data-cbp-theme', theme);
}

handleThemeToggle(themeToggle) {
themeToggle.addEventListener('click', () => {
themeToggle.checked ? this.setTheme('dark') : this.setTheme('light');
})
}
}

export default DarkMode;