diff --git a/app/globals.css b/app/globals.css index ce7a2fc..d29b10b 100644 --- a/app/globals.css +++ b/app/globals.css @@ -25,3 +25,33 @@ body { ) rgb(var(--background-start-rgb)); } +.toggle { + height: 2em; + width: 3em; + border-radius: 50px; + margin: auto; + background-image: linear-gradient(aqua, skyblue); + position: relative; + cursor: pointer; +} +.toggle-dark { + height: 2em; + width: 3em; + border-radius: 50px; + margin: auto; + background-image: linear-gradient(midnightblue, rebeccapurple); + position: relative; + cursor: pointer; +} +.theme-icon{ + margin-left: 0.2em; + height: 2em; + width: 1.2em; + color: yellow; +} + +.theme-icon-dark{ + height: 2em; + width: 1.2em; + margin-left: auto; +} \ No newline at end of file diff --git a/components/ThemeSwitcher.tsx b/components/ThemeSwitcher.tsx index 5af10d2..6314afa 100644 --- a/components/ThemeSwitcher.tsx +++ b/components/ThemeSwitcher.tsx @@ -1,22 +1,37 @@ "use client"; -import { MdOutlineDarkMode, MdLightbulbOutline } from "react-icons/md"; +import { MdBrightness2, MdCircle } from "react-icons/md"; import useDarkMode from "@/hooks/useDarkMode"; +import { useEffect, useState} from 'react' // TODO write tests for light/mode -// TODO new SVG for light/dark theme // TODO fix TS error on setTheme const ThemeSwitcher = () => { + const [mounted, setMounted] = useState(false); const [colorTheme, setTheme] = useDarkMode(); + useEffect(() => { + setMounted(true); + }, []); + + if(!mounted) { + return null; + } + return ( - <> +
{colorTheme === "light" ? ( - setTheme("light")} /> +
setTheme("light")}> + +
+ ) : ( - setTheme("dark")} /> +
setTheme("dark")}> + +
+ )} - +
); }; diff --git a/cypress/component/themeToggle.cy.tsx b/cypress/component/themeToggle.cy.tsx new file mode 100644 index 0000000..c583362 --- /dev/null +++ b/cypress/component/themeToggle.cy.tsx @@ -0,0 +1,27 @@ +import React from 'react'; +import ThemeSwitcher from "../../components/ThemeSwitcher"; +import Home from "@/app/page"; +import useDarkMode from '@/hooks/useDarkMode'; +import '../../app/globals.css'; + +describe('ThemeSwitcher component', () => { + it('should toggle between light and dark mode', () => { + cy.mount(); + cy.mount(); + cy.wait(100); + + // checking that the toggle is there and light mode is active + cy.get('.toggle').should('exist'); + cy.get('.toggle').should(($toggle) => { + const backgroundImage = $toggle.css('background-image'); + expect(backgroundImage).to.include('linear-gradient(rgb(0, 255, 255), rgb(135, 206, 235)'); + }); + // checking that the toggle is clickable and dark mode is active + cy.get('.toggle').click(); + cy.get('.toggle-dark').should('exist'); + cy.get('.toggle-dark').should(($toggle) => { + const backgroundImage = $toggle.css('background-image'); + expect(backgroundImage).to.include('linear-gradient(rgb(25, 25, 112), rgb(102, 51, 153)'); + }); + }); +}); \ No newline at end of file