reference/scripts/utils/darkMode.mjs

32 lines
847 B
JavaScript
Raw Normal View History

2022-10-01 02:29:00 +08:00
import path from 'path';
import { getSVGNode } from './getSVGNode.mjs';
const ICONS_PATH = path.resolve(process.cwd(), 'scripts/assets');
2022-11-20 03:37:25 +08:00
export function darkMode({ homePath = '', isHome } = {}) {
const relativePath = homePath.replace(/\/?index.html$/, isHome ? '' : '/');
2022-10-01 02:29:00 +08:00
const iconSunPath = path.resolve(ICONS_PATH, `sun.svg`);
const iconMoonPath = path.resolve(ICONS_PATH, `moon.svg`);
2022-10-29 00:24:39 +08:00
const sunNode = getSVGNode(iconSunPath);
const moonNode = getSVGNode(iconMoonPath);
2022-11-20 03:37:25 +08:00
const darkJSUrl = relativePath + 'js/dark.js';
2022-10-01 02:29:00 +08:00
return [
{
type: 'element',
tagName: 'button',
properties: {
id: 'darkMode',
2022-10-29 00:24:39 +08:00
type: 'button',
2022-10-01 02:29:00 +08:00
},
2022-10-29 00:24:39 +08:00
children: [...sunNode, ...moonNode],
},
2022-11-20 03:37:25 +08:00
{
type: 'element',
tagName: 'script',
properties: {
src: darkJSUrl,
},
},
2022-10-01 02:29:00 +08:00
];
}