├── global.d.ts ├── example ├── .eslintrc.json ├── next.config.js ├── public │ ├── favicon.ico │ └── vercel.svg ├── package.json ├── .gitignore ├── components │ └── ColorSwitcher.js ├── pages │ ├── index.js │ └── _app.js ├── README.md └── yarn.lock ├── renovate.json ├── src ├── index.ts ├── useColorModeValue.tsx ├── ColorModeStyles.tsx ├── ColorModeScript.tsx └── useColorSwitcher.tsx ├── .prettierrc ├── .npmignore ├── tsconfig.json ├── package.json ├── .gitignore └── README.md /global.d.ts: -------------------------------------------------------------------------------- 1 | interface Window { 2 | prefersDarkMode: boolean 3 | } -------------------------------------------------------------------------------- /example/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "next/core-web-vitals" 3 | } 4 | -------------------------------------------------------------------------------- /example/next.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | reactStrictMode: true, 3 | } 4 | -------------------------------------------------------------------------------- /example/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Blazity/nextjs-color-mode/HEAD/example/public/favicon.ico -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "config:base" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './ColorModeScript' 2 | export * from './ColorModeStyles' 3 | export * from './useColorModeValue' 4 | export * from './useColorSwitcher' 5 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "printWidth": 140, 3 | "tabWidth": 2, 4 | "useTabs": false, 5 | "semi": false, 6 | "singleQuote": true, 7 | "trailingComma": "all" 8 | } 9 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # sources 2 | preview 3 | src 4 | 5 | # locks 6 | package-lock.json 7 | yarn-lock.json 8 | 9 | # configs 10 | tslint.json 11 | tsconfig.json 12 | .prettierrc -------------------------------------------------------------------------------- /src/useColorModeValue.tsx: -------------------------------------------------------------------------------- 1 | export function useColorModeValue( 2 | name: string, 3 | lightThemeValue: string, 4 | darkThemeValue: string, 5 | ): [string, { light: string; dark: string }] { 6 | return [`var(--${name})`, { light: `--${name}: ${lightThemeValue};`, dark: `--${name}: ${darkThemeValue};` }] 7 | } 8 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "next dev", 7 | "build": "next build", 8 | "start": "next start", 9 | "lint": "next lint" 10 | }, 11 | "dependencies": { 12 | "next": "12.1.0", 13 | "nextjs-color-mode": "^1.0.3", 14 | "react": "17.0.2", 15 | "react-dom": "17.0.2" 16 | }, 17 | "devDependencies": { 18 | "eslint": "8.42.0", 19 | "eslint-config-next": "11.1.0" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env.local 29 | .env.development.local 30 | .env.test.local 31 | .env.production.local 32 | 33 | # vercel 34 | .vercel 35 | -------------------------------------------------------------------------------- /example/components/ColorSwitcher.js: -------------------------------------------------------------------------------- 1 | import { ColorModeStyles, useColorModeValue, useColorSwitcher } from 'nextjs-color-mode' 2 | 3 | export default function ColorSwitcher(props) { 4 | const { toggleTheme, colorMode } = useColorSwitcher() 5 | const [buttonColor, buttonCss] = useColorModeValue('button-color', 'green', 'yellow') 6 | 7 | return ( 8 | <> 9 | 10 | 13 | 14 | ) 15 | } 16 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "esnext", 4 | "module": "esnext", 5 | "jsx": "react", 6 | "declaration": true, 7 | "outDir": "lib", 8 | "strict": true, 9 | "noUnusedLocals": true, 10 | "noUnusedParameters": true, 11 | "noImplicitReturns": true, 12 | "noFallthroughCasesInSwitch": true, 13 | "allowSyntheticDefaultImports": true, 14 | "esModuleInterop": true, 15 | "moduleResolution": "node", 16 | "skipLibCheck": true, 17 | "baseUrl": "." 18 | }, 19 | "include": ["src", "node_modules/next/types/global.d.ts", "global.d.ts"], 20 | "exclude": ["node_modules", "lib", "**/*.spec.ts", "example"] 21 | } 22 | -------------------------------------------------------------------------------- /example/pages/index.js: -------------------------------------------------------------------------------- 1 | import { ColorModeStyles, useColorModeValue } from 'nextjs-color-mode' 2 | import { useCallback, useState } from 'react' 3 | import dynamic from 'next/dynamic' 4 | 5 | const ColorSwitcher = dynamic(() => import('../components/ColorSwitcher'), { ssr: false }) 6 | 7 | export default function Home() { 8 | const [boxColor, boxCss] = useColorModeValue('box-color', 'blue', 'red') 9 | 10 | return ( 11 | <> 12 | 13 |
14 | hello world 15 |
16 | 17 |
18 | 19 | ) 20 | } 21 | -------------------------------------------------------------------------------- /example/pages/_app.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import Head from 'next/head' 3 | import { ColorModeScript } from 'nextjs-color-mode' 4 | 5 | const criticalCss = ` 6 | .next-light-theme { 7 | --background: #fff; 8 | --text: #000; 9 | } 10 | 11 | .next-dark-theme { 12 | --background: #000; 13 | --text: #fff; 14 | } 15 | 16 | body { 17 | background: var(--background); 18 | color: var(--text); 19 | } 20 | ` 21 | 22 | function MyApp({ Component, pageProps }) { 23 | return ( 24 | <> 25 | 26 | 14 | 15 | ) 16 | } 17 | 18 | function transformColorModeStyles(...styles: ColoredValue[]) { 19 | const lightStyles = styles.map((entry) => entry.light) 20 | const darkStyles = styles.map((entry) => entry.dark) 21 | return ['.next-light-theme {', lightStyles.join(''), '}', '.next-dark-theme {', darkStyles.join(''), '}'] 22 | } 23 | -------------------------------------------------------------------------------- /src/ColorModeScript.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | 3 | export const initColorModeScript = ` 4 | const selectedColorMode = localStorage.getItem("nextColorMode"); 5 | 6 | if (!selectedColorMode) { 7 | setupPreferredColorMode(); 8 | window.colorMode = window.prefersDarkMode ? "dark" : "light"; 9 | } else { 10 | window.colorMode = selectedColorMode; 11 | } 12 | 13 | appendThemeClassName(window.colorMode) 14 | 15 | function setupPreferredColorMode() { 16 | const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); 17 | window.prefersDarkMode = darkModeMediaQuery.matches 18 | } 19 | 20 | function appendThemeClassName(colorMode) { 21 | window.document.querySelector('body').classList.remove("next-light-theme"); 22 | window.document.querySelector('body').classList.remove("next-dark-theme"); 23 | window.document.querySelector('body').classList.add("next-" + colorMode + "-theme") 24 | } 25 | ` 26 | 27 | export function ColorModeScript() { 28 | return