diff --git a/package-lock.json b/package-lock.json index b61bc1b..bfe9493 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10676,6 +10676,14 @@ "resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.7.tgz", "integrity": "sha512-TAv1KJFh3RhqxNvhzxj6LeT5NWklP6rDr2a0jaTfsZ5wSZWHOGeqQyejUp3xxLfPt2UpyJEcVQB/zyPcmonNFA==" }, + "react-icons": { + "version": "3.11.0", + "resolved": "https://registry.npmjs.org/react-icons/-/react-icons-3.11.0.tgz", + "integrity": "sha512-JRgiI/vdF6uyBgyZhVyYJUZAop95Sy4XDe/jmT3R/bKliFWpO/uZBwvSjWEdxwzec7SYbEPNPck0Kff2tUGM2Q==", + "requires": { + "camelcase": "^5.0.0" + } + }, "react-is": { "version": "16.13.1", "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", diff --git a/package.json b/package.json index 1726a27..8974330 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "@testing-library/user-event": "^7.2.1", "react": "^16.14.0", "react-dom": "^16.14.0", + "react-icons": "^3.11.0", "react-scripts": "3.4.3" }, "scripts": { diff --git a/src/App.css b/src/App.css deleted file mode 100644 index 74b5e05..0000000 --- a/src/App.css +++ /dev/null @@ -1,38 +0,0 @@ -.App { - text-align: center; -} - -.App-logo { - height: 40vmin; - pointer-events: none; -} - -@media (prefers-reduced-motion: no-preference) { - .App-logo { - animation: App-logo-spin infinite 20s linear; - } -} - -.App-header { - background-color: #282c34; - min-height: 100vh; - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - font-size: calc(10px + 2vmin); - color: white; -} - -.App-link { - color: #61dafb; -} - -@keyframes App-logo-spin { - from { - transform: rotate(0deg); - } - to { - transform: rotate(360deg); - } -} diff --git a/src/App.js b/src/App.js index ce9cbd2..5afdc80 100644 --- a/src/App.js +++ b/src/App.js @@ -1,24 +1,63 @@ -import React from 'react'; -import logo from './logo.svg'; -import './App.css'; - +import React from "react"; +import useDarkMode from "./useDarkMode"; +import { BsMoon, BsSun } from "react-icons/bs"; function App() { + const [isDark, setIsDark] = useDarkMode(); return ( -
-
- logo -

- Edit src/App.js and save to reload. -

- - Learn React - -
+
+ +

This is a heading

+

+ Lorem ipsum dolor sit amet consectetur adipisicing elit. Impedit earum + molestias, error, architecto unde accusamus quae quo reprehenderit, + quasi nesciunt perferendis saepe debitis necessitatibus perspiciatis + vitae eaque quaerat libero corporis? Eligendi iste ipsam harum veritatis + aspernatur deleniti, aliquid autem earum nemo recusandae natus officia + dolores neque ad asperiores nihil voluptate voluptas. Minus, at + doloribus commodi veritatis enim corrupti quis voluptatem! Ex soluta ut + quo dignissimos molestiae voluptate corrupti ab laborum quaerat, + mollitia labore delectus possimus ea vitae architecto voluptatum sed + fugit distinctio! Minus perferendis harum praesentium rerum et, tenetur + quod. Dolorum obcaecati possimus, magni ea earum hic consectetur. Eos + placeat recusandae dolores commodi tenetur officiis. Fugit ipsa odit + eveniet omnis quasi. Numquam natus temporibus architecto tempore ducimus + minima ex molestiae. Necessitatibus iure nesciunt exercitationem magni + omnis a, obcaecati, laudantium deserunt sit fugiat, quibusdam aspernatur + placeat. Vero at tempora, voluptatem ducimus minima impedit eius + reiciendis eaque repellendus maiores deleniti fugit repudiandae? +

+

+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus + asperiores assumenda earum voluptatibus a. Nam ullam alias consequuntur + odio voluptates laudantium enim architecto a, distinctio deserunt + corrupti, placeat quisquam minus. Reprehenderit sequi ipsam fuga minus + at harum quo aspernatur suscipit minima illum commodi laboriosam sit + autem officiis, magni error! Accusamus iusto dolores voluptatem + inventore quae, velit nisi! Odit, in mollitia. Dicta, sunt debitis, + nesciunt reprehenderit vel aperiam accusamus vitae tenetur officia + exercitationem iusto harum voluptate officiis corrupti odio nulla soluta + quibusdam quae. Optio dicta recusandae, quia animi delectus deleniti + placeat? Tempora, nam sequi? Maxime assumenda, quo animi tempore + inventore cupiditate deleniti aut harum nam porro, fugiat quaerat + consequuntur excepturi expedita facilis maiores eveniet ullam, eum + saepe! Sunt cumque voluptates nobis. Nisi perspiciatis, necessitatibus, + aperiam sequi debitis consectetur veniam exercitationem autem quis natus + aut laborum omnis non. Quam, rem ratione praesentium sit ab dolores + facilis tenetur quia deleniti beatae, illum officia. Accusantium + recusandae ab numquam quaerat quos deserunt unde magni, debitis animi + incidunt, aliquam corporis repellat quod soluta! Perspiciatis molestias + possimus aperiam nobis at fuga iusto officia deleniti omnis beatae. + Aspernatur? +

); } diff --git a/src/App.test.js b/src/App.test.js deleted file mode 100644 index 4db7ebc..0000000 --- a/src/App.test.js +++ /dev/null @@ -1,9 +0,0 @@ -import React from 'react'; -import { render } from '@testing-library/react'; -import App from './App'; - -test('renders learn react link', () => { - const { getByText } = render(); - const linkElement = getByText(/learn react/i); - expect(linkElement).toBeInTheDocument(); -}); diff --git a/src/index.css b/src/index.css index ec2585e..550316e 100644 --- a/src/index.css +++ b/src/index.css @@ -1,13 +1,28 @@ body { - margin: 0; - font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', - 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', - sans-serif; - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale; -} - -code { - font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', - monospace; + margin: 1rem auto; + max-width: 800px; + background-color: #fff; + color: #000; + --button-text: #000; + --button-bg: #fff; +} +body.dark { + background-color: #000; + color: #fff; + --button-text: #fff; + --button-bg: #000; +} +.navigation { + display: flex; + flex-direction: row; + justify-content: space-between; +} +.logo { + font-weight: bold; + font-size: 1.2rem; +} +.toggle_btn { + color: var(--button-text); + background-color: var(--button-bg); + cursor: pointer; } diff --git a/src/index.js b/src/index.js index f5185c1..846ed2c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,17 +1,11 @@ -import React from 'react'; -import ReactDOM from 'react-dom'; -import './index.css'; -import App from './App'; -import * as serviceWorker from './serviceWorker'; +import React from "react"; +import ReactDOM from "react-dom"; +import "./index.css"; +import App from "./App"; ReactDOM.render( , - document.getElementById('root') + document.getElementById("root") ); - -// If you want your app to work offline and load faster, you can change -// unregister() to register() below. Note this comes with some pitfalls. -// Learn more about service workers: https://bit.ly/CRA-PWA -serviceWorker.unregister(); diff --git a/src/logo.svg b/src/logo.svg deleted file mode 100644 index 6b60c10..0000000 --- a/src/logo.svg +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/src/serviceWorker.js b/src/serviceWorker.js deleted file mode 100644 index b04b771..0000000 --- a/src/serviceWorker.js +++ /dev/null @@ -1,141 +0,0 @@ -// This optional code is used to register a service worker. -// register() is not called by default. - -// This lets the app load faster on subsequent visits in production, and gives -// it offline capabilities. However, it also means that developers (and users) -// will only see deployed updates on subsequent visits to a page, after all the -// existing tabs open on the page have been closed, since previously cached -// resources are updated in the background. - -// To learn more about the benefits of this model and instructions on how to -// opt-in, read https://bit.ly/CRA-PWA - -const isLocalhost = Boolean( - window.location.hostname === 'localhost' || - // [::1] is the IPv6 localhost address. - window.location.hostname === '[::1]' || - // 127.0.0.0/8 are considered localhost for IPv4. - window.location.hostname.match( - /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ - ) -); - -export function register(config) { - if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { - // The URL constructor is available in all browsers that support SW. - const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); - if (publicUrl.origin !== window.location.origin) { - // Our service worker won't work if PUBLIC_URL is on a different origin - // from what our page is served on. This might happen if a CDN is used to - // serve assets; see https://github.com/facebook/create-react-app/issues/2374 - return; - } - - window.addEventListener('load', () => { - const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; - - if (isLocalhost) { - // This is running on localhost. Let's check if a service worker still exists or not. - checkValidServiceWorker(swUrl, config); - - // Add some additional logging to localhost, pointing developers to the - // service worker/PWA documentation. - navigator.serviceWorker.ready.then(() => { - console.log( - 'This web app is being served cache-first by a service ' + - 'worker. To learn more, visit https://bit.ly/CRA-PWA' - ); - }); - } else { - // Is not localhost. Just register service worker - registerValidSW(swUrl, config); - } - }); - } -} - -function registerValidSW(swUrl, config) { - navigator.serviceWorker - .register(swUrl) - .then(registration => { - registration.onupdatefound = () => { - const installingWorker = registration.installing; - if (installingWorker == null) { - return; - } - installingWorker.onstatechange = () => { - if (installingWorker.state === 'installed') { - if (navigator.serviceWorker.controller) { - // At this point, the updated precached content has been fetched, - // but the previous service worker will still serve the older - // content until all client tabs are closed. - console.log( - 'New content is available and will be used when all ' + - 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' - ); - - // Execute callback - if (config && config.onUpdate) { - config.onUpdate(registration); - } - } else { - // At this point, everything has been precached. - // It's the perfect time to display a - // "Content is cached for offline use." message. - console.log('Content is cached for offline use.'); - - // Execute callback - if (config && config.onSuccess) { - config.onSuccess(registration); - } - } - } - }; - }; - }) - .catch(error => { - console.error('Error during service worker registration:', error); - }); -} - -function checkValidServiceWorker(swUrl, config) { - // Check if the service worker can be found. If it can't reload the page. - fetch(swUrl, { - headers: { 'Service-Worker': 'script' }, - }) - .then(response => { - // Ensure service worker exists, and that we really are getting a JS file. - const contentType = response.headers.get('content-type'); - if ( - response.status === 404 || - (contentType != null && contentType.indexOf('javascript') === -1) - ) { - // No service worker found. Probably a different app. Reload the page. - navigator.serviceWorker.ready.then(registration => { - registration.unregister().then(() => { - window.location.reload(); - }); - }); - } else { - // Service worker found. Proceed as normal. - registerValidSW(swUrl, config); - } - }) - .catch(() => { - console.log( - 'No internet connection found. App is running in offline mode.' - ); - }); -} - -export function unregister() { - if ('serviceWorker' in navigator) { - navigator.serviceWorker.ready - .then(registration => { - registration.unregister(); - }) - .catch(error => { - console.error(error.message); - }); - } -} diff --git a/src/setupTests.js b/src/setupTests.js deleted file mode 100644 index 74b1a27..0000000 --- a/src/setupTests.js +++ /dev/null @@ -1,5 +0,0 @@ -// jest-dom adds custom jest matchers for asserting on DOM nodes. -// allows you to do things like: -// expect(element).toHaveTextContent(/react/i) -// learn more: https://github.com/testing-library/jest-dom -import '@testing-library/jest-dom/extend-expect'; diff --git a/src/useDarkMode.js b/src/useDarkMode.js new file mode 100644 index 0000000..e53fe4c --- /dev/null +++ b/src/useDarkMode.js @@ -0,0 +1,30 @@ +import { useEffect, useState } from "react"; + +export default () => { + const [isDark, setIsDark] = useState(() => { + try { + const item = window.localStorage.getItem("isDarkMode"); + return item ? JSON.parse(item) : undefined; + } catch (e) { + return false; + } + }); + + const isDarkModeEnabled = + typeof isDark !== "undefined" + ? isDark + : window.matchMedia("(prefers-color-scheme: dark)").matches; + useEffect(() => { + if (isDarkModeEnabled) { + window.document.body.classList.add("dark"); + } else { + window.document.body.classList.remove("dark"); + } + try { + window.localStorage.setItem("isDarkMode", isDarkModeEnabled); + } catch (e) { + console.error("Unable to access local storage"); + } + }, [isDarkModeEnabled]); + return [isDarkModeEnabled, setIsDark]; +};