Skip to content

Commit acab4c6

Browse files
authored
feat: cookies (#11)
1 parent 63df8bb commit acab4c6

6 files changed

Lines changed: 31 additions & 5 deletions

File tree

packages/docs/stories/simple-example.component.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const ThemeToggle = () => {
1515
};
1616

1717
const App = () => (
18-
<ThemeProvider>
18+
<ThemeProvider cookiesKey={"theme"}>
1919
<ThemeToggle />
2020
</ThemeProvider>
2121
);

packages/theme/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ To set custom localStorage key set `preferencesStorageKey` property for user pre
5959
<ThemeProvider preferencesStorageKey={"OPTIONAL_APP_THEME_STORAGE_KEY"}>
6060
```
6161

62+
To save user preferences in cookies, provide `cookiesKey` prop as a cookie key string
63+
64+
```tsx
65+
<ThemeProvider cookiesKey={"theme-preferences"}>
66+
```
67+
6268
### Hook `useTheme`
6369

6470
- `isDarkTheme` is a`boolean` what color scheme is selected, basing on user preferences and system

packages/theme/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@marcus-rise/react-theme",
33
"license": "MIT",
4-
"version": "0.4.5",
4+
"version": "1.0.0-canary.0",
55
"description": "react theme toggle hook",
66
"author": {
77
"name": "Ilya Konstantinov",

packages/theme/src/theme-preferences.hook.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ const useThemePreferences = () => {
1010
(data: ThemePreferences) => {
1111
dispatch({ type: ThemeReducerActions.SET_PREFERENCES, payload: data });
1212
localStorage.setItem(state.storageKey, data);
13+
14+
if (state.cookiesKey) {
15+
document.cookie = `${state.cookiesKey}=${data}`;
16+
}
1317
},
1418
[state.storageKey],
1519
);
1620

1721
const clearPreferences = useCallback(() => {
1822
dispatch({ type: ThemeReducerActions.CLEAR_PREFERENCES });
1923
localStorage.removeItem(state.storageKey);
24+
25+
if (state.cookiesKey) {
26+
document.cookie = `${state.cookiesKey}=""; max-age=0`;
27+
}
2028
}, [state.storageKey]);
2129

2230
return { savePreferences, clearPreferences };

packages/theme/src/theme.context.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ const ThemeContext = createContext<{
1414

1515
type Config = {
1616
preferencesStorageKey?: string;
17+
cookiesKey?: string | null;
1718
};
1819

19-
const ThemeProvider: FC<PropsWithChildren<Config>> = ({ children, preferencesStorageKey }) => {
20+
const ThemeProvider: FC<PropsWithChildren<Config>> = ({
21+
children,
22+
preferencesStorageKey,
23+
cookiesKey = null,
24+
}) => {
2025
const [state, dispatch] = useReducer(themeReducer, {
2126
...themeReducerInitialState,
2227
storageKey: preferencesStorageKey ?? themeReducerInitialState.storageKey,
28+
cookiesKey,
2329
});
2430

2531
/**
@@ -59,6 +65,10 @@ const ThemeProvider: FC<PropsWithChildren<Config>> = ({ children, preferencesSto
5965
type: ThemeReducerActions.SET_PREFERENCES,
6066
payload: preferences,
6167
});
68+
69+
if (state.cookiesKey) {
70+
document.cookie = `${state.cookiesKey}=${preferences}`;
71+
}
6272
}
6373
}, []);
6474

packages/theme/src/theme.reducer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import type { Reducer } from "react";
22
import type { ThemePreferences } from "./theme-preferences";
33

44
type ThemeReducerState = {
5-
isSystemThemeDark: boolean;
5+
cookiesKey: string | null;
66
storageKey: string;
7+
isSystemThemeDark: boolean;
78
preferences: ThemePreferences | null;
89
};
910

1011
const themeReducerInitialState: ThemeReducerState = {
11-
preferences: null,
12+
cookiesKey: null,
1213
storageKey: "THEME_PREFERENCES",
14+
preferences: null,
1315
isSystemThemeDark: false,
1416
};
1517

0 commit comments

Comments
 (0)