From 1e370817560c905a0a3520e245c317f308b6a5e7 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Tue, 1 Feb 2022 15:15:55 +0100
Subject: chore: add a new settings to handle prism theme from toolbar

---
 .../Settings/PrismThemeToggle/PrismThemeToggle.tsx | 49 ++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 src/components/Settings/PrismThemeToggle/PrismThemeToggle.tsx

(limited to 'src/components/Settings/PrismThemeToggle/PrismThemeToggle.tsx')

diff --git a/src/components/Settings/PrismThemeToggle/PrismThemeToggle.tsx b/src/components/Settings/PrismThemeToggle/PrismThemeToggle.tsx
new file mode 100644
index 0000000..06f7ac8
--- /dev/null
+++ b/src/components/Settings/PrismThemeToggle/PrismThemeToggle.tsx
@@ -0,0 +1,49 @@
+import { Toggle } from '@components/Form';
+import { MoonIcon, SunIcon } from '@components/Icons';
+import Spinner from '@components/Spinner/Spinner';
+import { usePrismTheme } from '@utils/providers/prism';
+import { useEffect, useState } from 'react';
+import { useIntl } from 'react-intl';
+
+const PrismThemeToggle = () => {
+  const intl = useIntl();
+  const [isMounted, setIsMounted] = useState<boolean>(false);
+
+  useEffect(() => {
+    setIsMounted(true);
+  }, []);
+
+  const { theme, setTheme, resolvedTheme } = usePrismTheme();
+  const [isDarkTheme, setIsDarkTheme] = useState<boolean>(theme === 'dark');
+
+  useEffect(() => {
+    if (theme === 'system') {
+      setIsDarkTheme(resolvedTheme === 'dark');
+    } else {
+      setIsDarkTheme(theme === 'dark');
+    }
+  }, [theme, resolvedTheme]);
+
+  const updateTheme = () => {
+    isDarkTheme ? setTheme('light') : setTheme('dark');
+    setIsDarkTheme(!isDarkTheme);
+  };
+
+  if (!isMounted) return <Spinner />;
+
+  return (
+    <Toggle
+      id="prism-theme"
+      label={intl.formatMessage({
+        defaultMessage: 'Code blocks:',
+        description: 'PrismThemeToggle: toggle label',
+      })}
+      leftChoice={<SunIcon />}
+      rightChoice={<MoonIcon />}
+      value={isDarkTheme}
+      changeHandler={updateTheme}
+    />
+  );
+};
+
+export default PrismThemeToggle;
-- 
cgit v1.2.3

