mirror of
https://github.com/jaywcjlove/reference.git
synced 2025-06-16 20:21:22 +08:00
feat: add dark/light theme.
This commit is contained in:
40
scripts/utils/darkMode.mjs
Normal file
40
scripts/utils/darkMode.mjs
Normal file
@ -0,0 +1,40 @@
|
||||
import path from 'path';
|
||||
import { getSVGNode } from './getSVGNode.mjs';
|
||||
|
||||
const scripts = `
|
||||
const button = document.querySelector('#darkMode');
|
||||
button.onclick = () => {
|
||||
const theme = document.documentElement.dataset.colorMode;
|
||||
document.documentElement.setAttribute('data-color-mode', theme === 'light' ? 'dark' : 'light');
|
||||
}
|
||||
`;
|
||||
|
||||
const ICONS_PATH = path.resolve(process.cwd(), 'scripts/assets');
|
||||
|
||||
export function darkMode() {
|
||||
const iconSunPath = path.resolve(ICONS_PATH, `sun.svg`);
|
||||
const iconMoonPath = path.resolve(ICONS_PATH, `moon.svg`);
|
||||
const sunNode = getSVGNode(iconSunPath)
|
||||
const moonNode = getSVGNode(iconMoonPath)
|
||||
return [
|
||||
{
|
||||
type: 'element',
|
||||
tagName: 'button',
|
||||
properties: {
|
||||
id: 'darkMode',
|
||||
type: 'button'
|
||||
},
|
||||
children: [
|
||||
...sunNode,
|
||||
...moonNode
|
||||
]
|
||||
}, {
|
||||
type: 'element',
|
||||
tagName: 'script',
|
||||
children: [{
|
||||
type: 'text',
|
||||
value: scripts,
|
||||
}]
|
||||
}
|
||||
];
|
||||
}
|
Reference in New Issue
Block a user