0
0
mirror of https://github.com/yude-jp/yude.jp synced 2024-06-09 23:36:01 +09:00

Add toggle button to switch dark-mode

This commit is contained in:
yude 2021-02-06 21:28:29 +09:00
parent 86d04bbf22
commit 17c0870ff0
5 changed files with 48 additions and 15 deletions

View File

@ -1,6 +1,9 @@
const nextTranslate = require('next-translate') const nextTranslate = require('next-translate')
module.exports = nextTranslate({ module.exports = nextTranslate({
experimental: {
darkModeVariant: true
},
i18n: { i18n: {
// These are all the locales you want to support in // These are all the locales you want to support in
// your application // your application

View File

@ -28,7 +28,7 @@ const Dropdown = ({ color }) => {
return ( return (
<> <>
<div className="relative inline-block text-left mr-2"> <div className="relative inline-block text-left mr-2 float-right">
<button type="button" className="inline-flex justify-center w-full rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-indigo-500" id="options-menu" aria-haspopup="true" aria-expanded="true" <button type="button" className="inline-flex justify-center w-full rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-indigo-500" id="options-menu" aria-haspopup="true" aria-expanded="true"
style={{ transition: "all .15s ease" }} style={{ transition: "all .15s ease" }}
ref={btnDropdownRef} ref={btnDropdownRef}

View File

@ -3,7 +3,6 @@ import Link from "next/link"
import "tailwindcss/tailwind.css"; import "tailwindcss/tailwind.css";
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
import { ThemeProvider } from "./themeContext" import { ThemeProvider } from "./themeContext"
import Toggle from "./toggle"
const Layout = (props) => { const Layout = (props) => {
const { title, children } = props const { title, children } = props
@ -23,24 +22,26 @@ const Layout = (props) => {
<div className="page-main"> <div className="page-main">
{children} {children}
</div> </div>
</main>
<style jsx global>{` <style jsx global>{`
body { body {
text-align: center; text-align: center;
} }
`}</style> `}</style>
<footer> <footer>
<div className="container mx-auto px-6"> <div className="container mx-auto px-6">
<div className="mt-16 border-t-2 border-gray-300 flex flex-col items-center"> <div className="mt-16 border-t-2 border-gray-300 flex flex-col items-center">
<div className="sm:w-full text-left py-6"> <div className="sm:w-full text-left py-6">
<p className="text-sm text-gray-700 font-bold mb-2"> <p className="text-sm text-gray-700 font-bold mb-2">
This page is licensed under the MIT License. / Powered by Tailwind CSS and Next.js. This page is licensed under the MIT License.
</p> </p>
<Toggle />
</div> </div>
</div> </div>
</div> </div>
</footer> </footer>
</main>
</body> </body>
</ThemeProvider> </ThemeProvider>
</div> </div>

View File

@ -2,6 +2,7 @@ import "tailwindcss/tailwind.css";
import Popper from "popper.js"; import Popper from "popper.js";
import Link from 'next/link'; import Link from 'next/link';
import LangSelector from "./LangSelector" import LangSelector from "./LangSelector"
import Toggle from "./toggle"
import { useState } from 'react'; import { useState } from 'react';
const Navbar = () => { const Navbar = () => {
@ -21,9 +22,9 @@ const Navbar = () => {
</span> </span>
</a> </a>
</Link> </Link>
<div className="origin-top-right absolute right-0"> <div className="origin-top-right absolute right-0">
<LangSelector /> <LangSelector />
<Toggle />
</div> </div>
</nav> </nav>
</> </>

View File

@ -1,5 +1,6 @@
import React from 'react' import React from 'react'
import ThemeContext from './themeContext' import ThemeContext from './themeContext'
import "tailwindcss/tailwind.css";
const Toggle = () => { const Toggle = () => {
const { theme, setTheme } = React.useContext(ThemeContext) const { theme, setTheme } = React.useContext(ThemeContext)
@ -7,16 +8,43 @@ const Toggle = () => {
function isDark() { function isDark() {
return theme === "dark" return theme === "dark"
} }
return ( return (
<label> <div className="float-right mr-3 my-2">
<input <label
type="checkbox" for="toogleA"
checked={isDark()} className="flex items-center cursor-pointer"
onChange={e => setTheme(e.target.checked ? "dark" : "light")} >
></input> <div className="relative">
ダーク モード <input id="toogleA" type="checkbox" className="hidden" checked={isDark()} onChange={e => setTheme(e.target.checked ? "dark" : "light")} />
</label> <div
className="toggle__line w-10 h-4 bg-gray-400 rounded-full shadow-inner"
></div>
<div
className="toggle__dot absolute w-6 h-6 bg-white rounded-full shadow inset-y-0 left-0"
></div>
</div>
<div
className="ml-3 font-medium"
>
<span class="text-black dark:text-white">ダークモード</span>
</div>
</label>
<style jsx>
{`
.toggle__dot {
top: -.25rem;
left: -.25rem;
transition: all 0.3s ease-in-out;
}
input:checked ~ .toggle__dot {
transform: translateX(100%);
background-color: #0A0E14;
}
`}
</style>
</div>
) )
} }