Skip to content

Commit 4ce57e5

Browse files
committed
highlighter style persisted in localStorage
1 parent 7ad5e5d commit 4ce57e5

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

src/components/HighlighterStyleSelector.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { SelectorOption } from "@types";
55
import Selector from "./Selector";
66

77
const HighlighterStyleSelector = () => {
8-
const { highlighterStyle, setHighlighterStyle } = useAppContext();
8+
const { highlighterStyle, toggleHighlighterStyle } = useAppContext();
99

1010
const options = highlighterStyles.map((style) => ({
1111
name: style.name,
@@ -18,7 +18,7 @@ const HighlighterStyleSelector = () => {
1818
if (!selected) {
1919
return;
2020
}
21-
setHighlighterStyle(selected);
21+
toggleHighlighterStyle(selected);
2222
};
2323

2424
return (

src/contexts/AppContext.tsx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@ import {
88
} from "react";
99
import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism";
1010

11-
import { AppState, LanguageType, SnippetType } from "@types";
11+
import { highlighterStyles } from "@consts/highlighter-styles";
12+
import {
13+
AppState,
14+
HighlighterStyleType,
15+
LanguageType,
16+
SnippetType,
17+
} from "@types";
1218

1319
// tokens
1420
const defaultLanguage: LanguageType = {
1521
lang: "JavaScript",
1622
icon: "/icons/javascript.svg",
1723
};
1824

19-
const defaultHighlighterStyle = { name: "oneDark", style: oneDark };
25+
const defaultHighlighterStyle: HighlighterStyleType = {
26+
name: "oneDark",
27+
style: oneDark,
28+
};
2029

2130
// TODO: add custom loading and error handling
2231
const defaultState: AppState = {
@@ -29,7 +38,7 @@ const defaultState: AppState = {
2938
theme: "dark",
3039
toggleTheme: () => {},
3140
highlighterStyle: defaultHighlighterStyle,
32-
setHighlighterStyle: () => {},
41+
toggleHighlighterStyle: () => {},
3342
};
3443

3544
const AppContext = createContext<AppState>(defaultState);
@@ -48,7 +57,13 @@ export const AppProvider: FC<{ children: React.ReactNode }> = ({
4857
);
4958
const [highlighterStyle, setHighlighterStyle] = useState<
5059
AppState["highlighterStyle"]
51-
>(defaultHighlighterStyle);
60+
>(
61+
localStorage.getItem("highlighterStyleName")
62+
? highlighterStyles.find(
63+
(style) => style.name === localStorage.getItem("highlighterStyleName")
64+
) || defaultHighlighterStyle
65+
: defaultHighlighterStyle
66+
);
5267

5368
const toggleTheme = useCallback(() => {
5469
const newTheme = theme === "dark" ? "light" : "dark";
@@ -57,6 +72,13 @@ export const AppProvider: FC<{ children: React.ReactNode }> = ({
5772
document.documentElement.setAttribute("data-theme", newTheme);
5873
}, [theme]);
5974

75+
const toggleHighlighterStyle = (
76+
newHighlighterStyle: HighlighterStyleType
77+
) => {
78+
setHighlighterStyle(newHighlighterStyle);
79+
localStorage.setItem("highlighterStyleName", newHighlighterStyle.name);
80+
};
81+
6082
/**
6183
* set the theme on initial load
6284
*/
@@ -77,7 +99,7 @@ export const AppProvider: FC<{ children: React.ReactNode }> = ({
7799
theme,
78100
toggleTheme,
79101
highlighterStyle,
80-
setHighlighterStyle,
102+
toggleHighlighterStyle,
81103
}}
82104
>
83105
{children}

src/types/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ export type AppState = {
3131
theme: "dark" | "light";
3232
toggleTheme: () => void;
3333
highlighterStyle: HighlighterStyleType;
34-
setHighlighterStyle: React.Dispatch<
35-
React.SetStateAction<HighlighterStyleType>
36-
>;
34+
toggleHighlighterStyle: (_: HighlighterStyleType) => void;
3735
};
3836

3937
export interface SelectorOption {

0 commit comments

Comments
 (0)