-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lilyanB
committed
May 30, 2023
1 parent
84bdf8e
commit 0518cf6
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { NightMode } from './NightMode'; | ||
|
||
export default { | ||
title: 'Components/Night Mode', | ||
component: NightMode, | ||
}; | ||
|
||
export const _NightMode = { | ||
render: () => ( | ||
<> | ||
<NightMode /> | ||
</> | ||
), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import '@testing-library/jest-dom'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { NightMode } from '.'; | ||
|
||
describe('Components | Button | NightMode', () => { | ||
test('it should render', () => { | ||
render(<NightMode />); | ||
|
||
let button = screen.getByTestId('nightMode'); | ||
|
||
expect(button).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
import { useState, useEffect, ComponentPropsWithoutRef } from 'react'; | ||
import { FiMoon, FiSun } from 'react-icons/fi'; | ||
|
||
export function NightMode() { | ||
const containerFlexClass = 'flex justify-center items-center'; | ||
const [theme, setTheme] = useState('dark'); | ||
|
||
function isLocalStorageAvailable() { | ||
try { | ||
return typeof localStorage === 'object' && navigator.cookieEnabled; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
function changeTheme() { | ||
if (isLocalStorageAvailable()) { | ||
const massaStation = localStorage.getItem('massaStation'); | ||
if (massaStation) { | ||
const parsedData = JSON.parse(massaStation); | ||
const newTheme = parsedData.theme === 'dark' ? 'light' : 'dark'; | ||
const newData = { ...parsedData, theme: newTheme }; | ||
localStorage.setItem('massaStation', JSON.stringify(newData)); | ||
setTheme(newTheme); | ||
} | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (isLocalStorageAvailable()) { | ||
const massaStation = localStorage.getItem('massaStation'); | ||
if (!massaStation) { | ||
const themeData = { theme: 'dark' }; | ||
localStorage.setItem('massaStation', JSON.stringify(themeData)); | ||
setTheme('dark'); | ||
} else { | ||
const parsedData = JSON.parse(massaStation); | ||
setTheme(parsedData.theme); | ||
} | ||
} | ||
}, []); | ||
|
||
return ( | ||
<button | ||
data-testid="nightMode" | ||
className={`bg-primary text-f-primary p-3 rounded-lg cursor-pointer | ||
flex flex-row items-center justify-center | ||
hover:bg-tertiary | ||
active:bg-secondary | ||
h-12 w-12`} | ||
onClick={changeTheme} | ||
> | ||
<div className={containerFlexClass}> | ||
{theme === 'dark' ? ( | ||
<FiSun className="h-8 w-8" /> | ||
) : ( | ||
<FiMoon className="h-8 w-8" /> | ||
)} | ||
</div> | ||
</button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './NightMode'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters