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

Check this please~ #265

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
44 changes: 44 additions & 0 deletions src/runtime/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// prettier-ignore
devjoylee marked this conversation as resolved.
Show resolved Hide resolved
export function isKeyDownDarkMode(e: KeyboardEvent) {
const ok = (
!e.ctrlKey &&
!e.altKey &&
(e.key.toLowerCase() === "`" || e.keyCode === 192)
)
return ok
}

// prettier-ignore
export function isKeyDownDebugMode(e: KeyboardEvent) {
const ok = (
e.ctrlKey &&
!e.altKey &&
(e.key.toLowerCase() === "`" || e.keyCode === 192)
)
return ok
}

// prettier-ignore
export function parseDurationMs(durStr : any){
devjoylee marked this conversation as resolved.
Show resolved Hide resolved
let [numStr, unit] = durStr.trim().split(/(ms|s|m)$/);
if(!numStr){
throw new Error(`parseDurationMs: expected \`ms\`, \`s\`, or \`m\`; durStr=${durStr}`)
}

let n = +numStr;
switch (unit) {
case 'ms':
// No-op
break;
case 's':
n *= 1000;
break;
case 'm':
n *= 1000 * 60;
break;
default:
// No-op
break;
}
return n;
}
33 changes: 11 additions & 22 deletions src/runtime/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as helper from './helper'

declare global {
interface Window {
Duomo: Runtime
Expand Down Expand Up @@ -45,25 +47,6 @@ function matchMediaPreference() {
return value
}

// prettier-ignore
function isKeyDownDarkMode(e: KeyboardEvent) {
const ok = (
!e.ctrlKey &&
!e.altKey &&
(e.key.toLowerCase() === "`" || e.keyCode === 192)
)
return ok
}

// prettier-ignore
function isKeyDownDebugMode(e: KeyboardEvent) {
const ok = (
e.ctrlKey &&
!e.altKey &&
(e.key.toLowerCase() === "`" || e.keyCode === 192)
)
return ok
}

devjoylee marked this conversation as resolved.
Show resolved Hide resolved
class Duomo implements Runtime {
static localStorageKey = "duomo-theme-preference"
Expand All @@ -77,6 +60,7 @@ class Duomo implements Runtime {
// Tracks whether dark mode was set once; for FOUC.
// FOUC: Flash Of Unstyled Content.
#didInitDarkMode: boolean = false
#shouldNoOpDarkMode: boolean = false

#deferrers: (() => void)[] = []

Expand Down Expand Up @@ -115,14 +99,14 @@ class Duomo implements Runtime {
// `keydown` handlers.
if (env === "development") {
const handleDarkMode = (e: KeyboardEvent) => {
if (isKeyDownDarkMode(e)) {
if (helper.isKeyDownDarkMode(e)) {
this.toggleDarkMode()
}
}
document.addEventListener("keydown", handleDarkMode)
this.#deferrers.push(() => document.removeEventListener("keydown", handleDarkMode))
const handleDebugMode = (e: KeyboardEvent) => {
if (isKeyDownDebugMode(e)) {
if (helper.isKeyDownDebugMode(e)) {
this.toggleDebugMode()
}
}
Expand All @@ -144,7 +128,10 @@ class Duomo implements Runtime {
return this.#darkMode
}
setDarkMode(mode: boolean) {
if (this.#shouldNoOpDarkMode) return;
this.#darkMode = mode

const durValue = getComputedStyle(document.documentElement).getPropertyValue('--default-theme-transition-duration');

const toggle = (mode: boolean) => {
const action = !mode
Expand All @@ -160,12 +147,14 @@ class Duomo implements Runtime {
this.#didInitDarkMode = true
return
} else {
this.#shouldNoOpDarkMode = true
this.#html!.setAttribute("data-theme-effect", "true")
setTimeout(() => {
toggle(mode)
setTimeout(() => {
this.#html!.removeAttribute("data-theme-effect")
}, 300)
this.#shouldNoOpDarkMode = false
}, helper.parseDurationMs(durValue))
}, 25)
}
}
Expand Down