Skip to content

Commit

Permalink
add nightMode
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyanB committed May 30, 2023
1 parent 84bdf8e commit 0518cf6
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/components/NightMode/NightMode.stories.tsx
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 />
</>
),
};
13 changes: 13 additions & 0 deletions src/components/NightMode/NightMode.test.tsx
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();
});
});
64 changes: 64 additions & 0 deletions src/components/NightMode/NightMode.tsx
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>
);
}
1 change: 1 addition & 0 deletions src/components/NightMode/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './NightMode';
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './SideMenu';
export * from './Icons';
export * from './Toggle';
export * from './Balance';
export * from './NightMode';

0 comments on commit 0518cf6

Please sign in to comment.