-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: improvements * chore: update deps * fix: use global name for script * test: collect coverage only from module * docs: add hid and globalName * docs: mention uknown state * feat: `<color-scheme>` component * feat: always enable unknown on static mode for server plugin * feat: enable unknown if req is not available * chore: improve caveats section Co-authored-by: Sébastien Chopin <seb@chopin.io>
- Loading branch information
Showing
16 changed files
with
312 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,4 @@ dist | |
.nuxt | ||
coverage | ||
|
||
# Plugin | ||
lib/templates/* | ||
*.min.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ node_modules | |
.DS_Store | ||
coverage | ||
dist | ||
*.min.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Add dark / light detection that runs before loading Nuxt.js | ||
|
||
// Global variable minimizers | ||
const w = window | ||
const d = document | ||
const de = d.documentElement | ||
|
||
const knownColorSchemes = ['dark', 'light'] | ||
|
||
const preference = getCookie('<%= options.cookie.key %>') || '<%= options.preference %>' | ||
const value = preference === 'system' ? getColorScheme() : preference | ||
|
||
addClass(value) | ||
|
||
w['<%= options.globalName %>'] = { | ||
preference, | ||
value, | ||
getColorScheme, | ||
addClass, | ||
removeClass | ||
} | ||
|
||
function getCookie (name) { | ||
const nameEQ = name + '=' | ||
const cookies = d.cookie.split(';') | ||
|
||
for (let i = 0; i < cookies.length; i++) { | ||
let cookie = cookies[i] | ||
while (cookie.charAt(0) === ' ') { | ||
cookie = cookie.substring(1, cookie.length) | ||
} | ||
if (cookie.indexOf(nameEQ) === 0) { | ||
return cookie.substring(nameEQ.length, cookie.length) | ||
} | ||
} | ||
return null | ||
} | ||
|
||
function addClass (value) { | ||
const className = value + '-mode' | ||
if (de.classList) { | ||
de.classList.add(className) | ||
} else { | ||
de.className += ' ' + className | ||
} | ||
} | ||
|
||
function removeClass (value) { | ||
const className = value + '-mode' | ||
if (de.classList) { | ||
de.classList.remove(className) | ||
} else { | ||
de.className = de.className.replace(new RegExp(className, 'g'), '') | ||
} | ||
} | ||
|
||
function prefersColorScheme (suffix) { | ||
return w.matchMedia('(prefers-color-scheme' + suffix + ')') | ||
} | ||
|
||
function getColorScheme () { | ||
if (w.matchMedia && prefersColorScheme('').media !== 'not all') { | ||
for (const colorScheme of knownColorSchemes) { | ||
if (prefersColorScheme(':' + colorScheme).matches) { | ||
return colorScheme | ||
} | ||
} | ||
} | ||
|
||
return '<%= options.fallback %>' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export default { | ||
name: '<%= options.componentName %>', | ||
functional: true, | ||
render (createElement, context) { | ||
const tag = context.tag || 'span' | ||
const { $colorMode } = context.parent | ||
|
||
if (!$colorMode.unknown) { | ||
return createElement(tag, context.data, context.children) | ||
} | ||
|
||
return createElement('client-only', { | ||
'placeholder-tag': tag, | ||
...context.data | ||
}, context.children) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import Vue from 'vue' | ||
import { serialize } from 'cookie' | ||
import colorSchemeComponent from './color-scheme' | ||
|
||
Vue.component('<%= options.componentName %>', colorSchemeComponent) | ||
|
||
const cookieKey = '<%= options.cookie.key %>' | ||
const cookieOptions = JSON.parse('<%= JSON.stringify(options.cookie.options) %>') | ||
const colorMode = window['<%= options.globalName %>'] | ||
|
||
export default function (ctx, inject) { | ||
const $colorMode = new Vue({ | ||
data: { | ||
preference: colorMode.preference, | ||
value: colorMode.value, | ||
unknown: false | ||
}, | ||
watch: { | ||
preference (preference) { | ||
if (preference === 'system') { | ||
this.value = colorMode.getColorScheme() | ||
this._watchMedia() | ||
} else { | ||
this.value = preference | ||
} | ||
|
||
this._storePreference(preference) | ||
}, | ||
value (newValue, oldValue) { | ||
colorMode.removeClass(oldValue) | ||
colorMode.addClass(newValue) | ||
} | ||
}, | ||
created () { | ||
if (this.preference === 'system') { | ||
this._watchMedia() | ||
} | ||
if (window.localStorage) { | ||
this._watchStorageChange() | ||
} | ||
}, | ||
methods: { | ||
_watchMedia () { | ||
if (this._mediaWatcher || !window.matchMedia) { | ||
return | ||
} | ||
|
||
this._darkWatcher = window.matchMedia('(prefers-color-scheme: dark)') | ||
this._darkWatcher.addListener((e) => { | ||
if (this.preference === 'system') { | ||
this.value = colorMode.getColorScheme() | ||
} | ||
}) | ||
}, | ||
_watchStorageChange () { | ||
window.addEventListener('storage', (e) => { | ||
if (e.key === cookieKey) { | ||
this.preference = e.newValue | ||
} | ||
}) | ||
}, | ||
_storePreference (preference) { | ||
// Cookies for SSR | ||
document.cookie = serialize(cookieKey, preference, cookieOptions) | ||
|
||
// Local storage to sync with other tabs | ||
if (window.localStorage) { | ||
window.localStorage.setItem(cookieKey, preference) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
inject('colorMode', $colorMode) | ||
} |
17 changes: 11 additions & 6 deletions
17
lib/templates/plugins/color-mode.server.js → lib/templates/plugin.server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.