From 4d3579ca9876d3ca6612589d116c3a300a60b446 Mon Sep 17 00:00:00 2001 From: Jacky Zhao Date: Tue, 6 Jun 2023 19:48:37 -0700 Subject: [PATCH] darkmode scripts --- quartz.config.ts | 4 +- quartz/bootstrap-cli.mjs | 1 + quartz/build.ts | 2 +- quartz/components/Darkmode.tsx | 47 ++++++++++++++++++++ quartz/components/Head.tsx | 2 +- quartz/components/Header.tsx | 8 +++- quartz/components/scripts/darkmode.inline.ts | 26 ++++++++++- quartz/components/types.ts | 3 +- quartz/plugins/emitters/contentPage.tsx | 4 +- quartz/styles/base.scss | 6 +-- quartz/styles/callouts.scss | 2 +- quartz/styles/darkmode.scss | 45 +++++++++++++++++++ quartz/styles/header.scss | 10 +++++ 13 files changed, 145 insertions(+), 15 deletions(-) create mode 100644 quartz/components/Darkmode.tsx create mode 100644 quartz/styles/darkmode.scss create mode 100644 quartz/styles/header.scss diff --git a/quartz.config.ts b/quartz.config.ts index 6808d932..25518d5f 100644 --- a/quartz.config.ts +++ b/quartz.config.ts @@ -1,6 +1,6 @@ import { QuartzConfig } from "./quartz/cfg" -import * as Head from "./quartz/components/Head" -import * as Header from "./quartz/components/Header" +import Head from "./quartz/components/Head" +import Header from "./quartz/components/Header" import { ContentPage, CreatedModifiedDate, diff --git a/quartz/bootstrap-cli.mjs b/quartz/bootstrap-cli.mjs index be6270e2..f9b53043 100755 --- a/quartz/bootstrap-cli.mjs +++ b/quartz/bootstrap-cli.mjs @@ -74,6 +74,7 @@ yargs(hideBin(process.argv)) const transpiled = await esbuild.build({ stdin: { contents: text, + loader: 'ts', sourcefile: path.relative(path.resolve('.'), args.path), }, write: false, diff --git a/quartz/build.ts b/quartz/build.ts index 26e354c2..60a1a516 100644 --- a/quartz/build.ts +++ b/quartz/build.ts @@ -59,7 +59,7 @@ export default async function buildQuartz(argv: Argv, version: string) { const server = http.createServer(async (req, res) => { return serveHandler(req, res, { public: output, - directoryListing: false + directoryListing: false, }) }) server.listen(argv.port) diff --git a/quartz/components/Darkmode.tsx b/quartz/components/Darkmode.tsx new file mode 100644 index 00000000..0161e0ad --- /dev/null +++ b/quartz/components/Darkmode.tsx @@ -0,0 +1,47 @@ +import darkmodeScript from "./scripts/darkmode.inline" +import styles from '../styles/darkmode.scss' + +export default function Darkmode() { + return
+ + + +
+} + +Darkmode.beforeDOMLoaded = darkmodeScript +Darkmode.css = styles diff --git a/quartz/components/Head.tsx b/quartz/components/Head.tsx index 29050a79..9e182c7f 100644 --- a/quartz/components/Head.tsx +++ b/quartz/components/Head.tsx @@ -8,7 +8,7 @@ export interface HeadProps { externalResources: StaticResources } -export function Component({ title, description, slug, externalResources }: HeadProps) { +export default function Head({ title, description, slug, externalResources }: HeadProps) { const { css, js } = externalResources const baseDir = resolveToRoot(slug) const iconPath = baseDir + "/static/icon.png" diff --git a/quartz/components/Header.tsx b/quartz/components/Header.tsx index 229e210e..cb2fae8d 100644 --- a/quartz/components/Header.tsx +++ b/quartz/components/Header.tsx @@ -1,14 +1,20 @@ import { resolveToRoot } from "../path" +import Darkmode from "./Darkmode" +import style from '../styles/header.scss' export interface HeaderProps { title: string slug: string } -export function Component({ title, slug }: HeaderProps) { +export default function Header({ title, slug }: HeaderProps) { const baseDir = resolveToRoot(slug) return

{title}

+
+
} +Header.beforeDOMLoaded = Darkmode.beforeDOMLoaded +Header.css = style + Darkmode.css diff --git a/quartz/components/scripts/darkmode.inline.ts b/quartz/components/scripts/darkmode.inline.ts index c17013aa..d2b6e064 100644 --- a/quartz/components/scripts/darkmode.inline.ts +++ b/quartz/components/scripts/darkmode.inline.ts @@ -1,3 +1,25 @@ -export default "Darkmode" +export default "Darkmode" -console.log("HELLOOOO FROM CONSOLE") +const currentTheme = localStorage.getItem("theme") +const theme = + currentTheme ?? + (window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark") + +document.documentElement.setAttribute("saved-theme", theme) + +window.addEventListener("DOMContentLoaded", () => { + const toggleSwitch = document.querySelector("#darkmode-toggle") as HTMLInputElement + toggleSwitch.addEventListener("change", (e: any) => { + if (e.target.checked) { + document.documentElement.setAttribute("saved-theme", "dark") + localStorage.setItem("theme", "dark") + } else { + document.documentElement.setAttribute("saved-theme", "light") + localStorage.setItem("theme", "light") + } + }) + + if (theme === "dark") { + toggleSwitch.checked = true + } +}) diff --git a/quartz/components/types.ts b/quartz/components/types.ts index e1bdebc3..96ead5f9 100644 --- a/quartz/components/types.ts +++ b/quartz/components/types.ts @@ -1,7 +1,6 @@ import { ComponentType } from "preact" -export type QuartzComponent = { - Component: ComponentType +export type QuartzComponent = ComponentType & { css?: string, beforeDOMLoaded?: string, afterDOMLoaded?: string, diff --git a/quartz/plugins/emitters/contentPage.tsx b/quartz/plugins/emitters/contentPage.tsx index 05c4c4b4..9cca0af1 100644 --- a/quartz/plugins/emitters/contentPage.tsx +++ b/quartz/plugins/emitters/contentPage.tsx @@ -48,14 +48,14 @@ export class ContentPage extends QuartzEmitterPlugin { const title = file.data.frontmatter?.title const doc = -
- +
{file.data.slug !== "index" &&

{title}

} {content} diff --git a/quartz/styles/base.scss b/quartz/styles/base.scss index f0390357..d7a1891a 100644 --- a/quartz/styles/base.scss +++ b/quartz/styles/base.scss @@ -24,7 +24,7 @@ body { border-radius: 5px; } -p, ul, text, a, tr, td, li, ol, ul { +p, ul, text, a, tr, td, li, ol, ul, .katex { color: var(--darkgray); fill: var(--darkgray); } @@ -67,7 +67,7 @@ a { } blockquote { - margin-left: 0; + margin: 1rem 0; border-left: 3px solid var(--secondary); padding-left: 1rem; transition: border-color 0.2s ease; @@ -134,7 +134,7 @@ pre { & > code { background: none; padding: 0; - font-size: 0.9rem; + font-size: 0.85rem; counter-reset: line; counter-increment: line 0; display: grid; diff --git a/quartz/styles/callouts.scss b/quartz/styles/callouts.scss index 9e2ff2dd..26cb0ba4 100644 --- a/quartz/styles/callouts.scss +++ b/quartz/styles/callouts.scss @@ -4,7 +4,7 @@ border: 1px solid var(--border); background-color: var(--bg); border-radius: 5px; - padding: 0 0.7rem; + padding: 0 1rem; &[data-callout="note"] { --color: #448aff; diff --git a/quartz/styles/darkmode.scss b/quartz/styles/darkmode.scss new file mode 100644 index 00000000..561a83c5 --- /dev/null +++ b/quartz/styles/darkmode.scss @@ -0,0 +1,45 @@ +.darkmode { + float: right; + padding: 1rem; + min-width: 30px; + position: relative; + + @media all and (max-width: 450px) { + padding: 1rem; + } + + & > .toggle { + display: none; + box-sizing: border-box; + } + + & svg { + cursor: pointer; + opacity: 0; + position: absolute; + width: 20px; + height: 20px; + top: calc(50% - 10px); + margin: 0 7px; + fill: var(--darkgray); + transition: opacity 0.1s ease; + } +} + +:root[saved-theme="dark"] .toggle ~ label { + & > #dayIcon { + opacity: 0; + } + & > #nightIcon { + opacity: 1; + } +} + +:root .toggle ~ label { + & > #dayIcon { + opacity: 1; + } + & > #nightIcon { + opacity: 0; + } +} diff --git a/quartz/styles/header.scss b/quartz/styles/header.scss new file mode 100644 index 00000000..c3ea4878 --- /dev/null +++ b/quartz/styles/header.scss @@ -0,0 +1,10 @@ +header { + display: flex; + flex-direction: row; + align-items: center; + margin: 1em 0 2em 0; + & > h1 { + margin: 0; + flex: auto; + } +}