Skip to content

Commit

Permalink
Merge pull request #8 from eduzz/feature-cpi-1245-cookie-theme
Browse files Browse the repository at this point in the history
Feature cpi 1245 cookie theme
  • Loading branch information
vitorvmrs authored Aug 8, 2024
2 parents 6f2e101 + b4974c0 commit 1a730d6
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5,078 deletions.
2 changes: 1 addition & 1 deletion _dev/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function App() {

return (
<>
<Layout mode='light'>
<Layout mode='light' persistMode={true}>
<Topbar
currentApplication='orbita'
user={{
Expand Down
12 changes: 6 additions & 6 deletions hooks/useMode.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';

import { getLocalStorageInstance } from '../utils/localStorage';
import Cookies from 'universal-cookie';

export type PossibleModes = 'dark' | 'light';

Expand All @@ -12,7 +12,7 @@ type UseModeOptions = {
};

export default function useMode({ mode, acceptModeBySearchParam, persistMode, onModeChange }: UseModeOptions) {
const localStorageInstance = getLocalStorageInstance();
const cookies = useMemo(() => new Cookies(), []);

const [currentMode, setCurrentMode] = useState<'light' | 'dark'>(() => {
const getSearchParamsMode = (searchParamsAllowed?: boolean) => {
Expand All @@ -31,7 +31,7 @@ export default function useMode({ mode, acceptModeBySearchParam, persistMode, on
return searchParamsMode;
}

const storageMode = localStorageInstance?.getItem('eduzz-ui-mode') as PossibleModes | undefined;
const storageMode = cookies.get('eduzz-theme') as PossibleModes | undefined;

if (!storageMode) {
return mode || 'light';
Expand All @@ -57,10 +57,10 @@ export default function useMode({ mode, acceptModeBySearchParam, persistMode, on
document.body.setAttribute('data-eduzz-theme', desiredTheme);

if (persistMode) {
localStorageInstance?.setItem('eduzz-ui-mode', desiredTheme);
cookies.set('eduzz-theme', desiredTheme);
}
},
[localStorageInstance, persistMode]
[persistMode, cookies]
);

useEffect(() => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eduzz/ui-layout",
"version": "1.3.0",
"version": "1.4.0-beta.1",
"keywords": [
"eduzz",
"react",
Expand All @@ -25,6 +25,7 @@
"@types/node": "^20.12.7",
"clsx": "^2.1.0",
"tailwind-merge": "^2.3.0",
"universal-cookie": "^7.2.0",
"use-context-selector": "^1.4.4"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 1a730d6

Please sign in to comment.