Skip to content

Commit de4d109

Browse files
Invert button functionality (#7)
* Added possibility to define the starting mode using window.defaultMode = "dark" or window.defaultMode = "light" , I have prettier installed so it formatted a lot of code but I didn't change anything else than what I mentioned in the related issue #3 also it installed version 0.0.12 so package-lock.json changed, locally stored value will ALWAYS overwrite this value so this is mostly for first visits. If left empty the default behavior apply (aka system pref) * Removed dark from window.defaultMode forgot to remove it before commit my bad * Added possibility to invert button using window.buttonMode = "inverted"; the button will now display the mode it will toggle on when clicked (aka when in lightmode the button will be black indicating it switches you to black mode) if left empty, default behavior * Removed unrelated parts of the code sorry kinda new to contributing, realised I commited the other function (set-mode) to this branch as well, removed it so that the only changes on this branch are the button inverted function if I'm not mistaken * Merged back to main + run format and lint First time merging branches, I think everything is fine you might want to check but it runs as intended on my machine * change api to use the NightowlOptions via createNightowl --------- Co-authored-by: Bufferhead (Gregor Vostrak) <hello@bufferhead.com>
1 parent d69b8f1 commit de4d109

3 files changed

Lines changed: 44 additions & 20 deletions

File tree

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,43 @@ Please only use it with fixed Minor versions.
2727

2828
Integration can be achieved by adding one of the following script tag to your website.
2929

30-
Using a CDN :
30+
Using a CDN:
3131

3232
```
3333
<script type="module" src="https://cdn.jsdelivr.net/npm/@bufferhead/nightowl@0.0.12/dist/nightowl.js"></script>
3434
```
3535

36-
Using a local install :
36+
Install via npm:
3737

38-
-First install Nightowl in your project using npm
38+
- First install Nightowl in your project using npm
3939

4040
```
4141
npm install @bufferhead/nightowl
4242
```
4343

44-
-Then add this <script> tag to your index:
44+
- Then add this <script> tag to your index:
4545

4646
```
47-
<script type="module" src="./node_modules/@bufferhead/nightowl/dist/nightowl.js"></script>
47+
<script type="module">
48+
import {createNightowl} from '/src/main.ts'
49+
50+
createNightowl({
51+
defaultMode: 'dark',
52+
toggleButtonMode: 'newState'
53+
})
54+
55+
</script>
4856
```
4957

58+
## configuration Options
59+
60+
- defaultMode: 'dark' | 'light' (Default: 'light')
61+
- Sets the default mode for users that have not set a preference yet and do not have a system preference for dark mode
62+
- toggleButtonMode: 'newState' | 'currentState' (Default: 'currentState')
63+
- Configures what state of the toggle button should be shown to the user
64+
- 'newState' will show the state that will be applied when the user clicks the button
65+
- 'currentState' will show the state that is currently applied to the website
66+
5067
## Credits
5168

5269
This project is heavily inspired by Aral Balkan who [wrote down this idea to implement dark mode in a few lines of CSS using CSS Filters](https://ar.al/2021/08/24/implementing-dark-mode-in-a-handful-of-lines-of-css-with-css-filters/).

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>Vite + TS</title>
88
<script type="module">
9-
import {createNightowl} from '/src/main.ts'
9+
import { createNightowl } from '/src/main.ts'
1010

1111
createNightowl({
12-
defaultMode: 'dark'
12+
defaultMode: 'dark',
13+
toggleButtonMode: 'newState'
1314
})
14-
1515
</script>
1616
<style>
1717
* {

src/main.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ let store: Storage | null = null
55
const persistPreference = true
66
let mode = LIGHT
77
let automaticInitialization = true
8-
8+
let toggleButtonMode = 'currentState'
99
interface NightowlOptions {
1010
defaultMode?: 'light' | 'dark'
11+
toggleButtonMode?: 'currentState' | 'newState'
1112
}
1213

1314
try {
@@ -58,17 +59,19 @@ function loadCss() {
5859
document.head.appendChild(css)
5960
}
6061

61-
export function createNightowl (options: NightowlOptions) {
62+
export function createNightowl(options: NightowlOptions) {
6263
automaticInitialization = false
6364
if (options.defaultMode === 'dark') {
6465
mode = DARK
6566
}
66-
if(document.readyState === 'complete'){
67+
if (options.toggleButtonMode) {
68+
toggleButtonMode = options.toggleButtonMode
69+
}
70+
if (document.readyState === 'complete') {
6771
loadCss()
6872
initializeNightowl()
6973
initializeSwitcher()
70-
}
71-
else {
74+
} else {
7275
window.addEventListener('load', () => {
7376
loadCss()
7477
initializeNightowl()
@@ -85,7 +88,6 @@ window.addEventListener('load', () => {
8588
}
8689
})
8790

88-
8991
function enableDarkMode() {
9092
mode = DARK
9193
const htmlElement = document.querySelector('html')
@@ -130,7 +132,11 @@ function setSwitcherIcon() {
130132
' <path stroke-linecap="round" stroke-linejoin="round" d="M21.752 15.002A9.718 9.718 0 0118 15.75c-5.385 0-9.75-4.365-9.75-9.75 0-1.33.266-2.597.748-3.752A9.753 9.753 0 003 11.25C3 16.635 7.365 21 12.75 21a9.753 9.753 0 009.002-5.998z" />\n' +
131133
'</svg>'
132134

133-
switcher.innerHTML = mode === DARK ? darkIcon : lightIcon
135+
if (toggleButtonMode === 'newState') {
136+
switcher.innerHTML = mode === DARK ? lightIcon : darkIcon
137+
} else if (toggleButtonMode === 'currentState') {
138+
switcher.innerHTML = mode === DARK ? darkIcon : lightIcon
139+
}
134140
}
135141
}
136142

@@ -144,7 +150,8 @@ function initializeSwitcher() {
144150
switcher.style.width = '50px'
145151
switcher.style.height = '50px'
146152
switcher.style.borderRadius = '50%'
147-
switcher.style.backgroundColor = 'white'
153+
switcher.style.backgroundColor =
154+
toggleButtonMode === 'newState' ? 'black' : 'white'
148155
switcher.style.display = 'flex'
149156
switcher.style.justifyContent = 'center'
150157
switcher.style.alignItems = 'center'
@@ -153,7 +160,7 @@ function initializeSwitcher() {
153160
switcher.style.boxShadow = '0 0 10px rgba(0,0,0,0.2)'
154161
switcher.style.transition = 'all 0.3s ease-in-out'
155162
switcher.style.overflow = 'hidden'
156-
switcher.style.color = 'black'
163+
switcher.style.color = toggleButtonMode === 'newState' ? 'white' : 'black'
157164

158165
switcher.addEventListener('click', () => {
159166
toggleMode()
@@ -209,8 +216,8 @@ function storeModeInLocalStorage() {
209216

210217
function hasNativeDarkPrefersColorScheme() {
211218
return (
212-
(window.matchMedia &&
213-
(window.matchMedia('(prefers-color-scheme: dark)').matches ||
214-
window.matchMedia('(prefers-color-scheme:dark)').matches))
219+
window.matchMedia &&
220+
(window.matchMedia('(prefers-color-scheme: dark)').matches ||
221+
window.matchMedia('(prefers-color-scheme:dark)').matches)
215222
)
216223
}

0 commit comments

Comments
 (0)