From a6ff5eee45215effb3344cb5d631a27a7c0369aa Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Fri, 22 Sep 2023 19:34:01 +0200
Subject: refactor(components): rewrite form components

---
 .../organisms/forms/theme-toggle/theme-toggle.tsx  | 76 ++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 create mode 100644 src/components/organisms/forms/theme-toggle/theme-toggle.tsx

(limited to 'src/components/organisms/forms/theme-toggle/theme-toggle.tsx')

diff --git a/src/components/organisms/forms/theme-toggle/theme-toggle.tsx b/src/components/organisms/forms/theme-toggle/theme-toggle.tsx
new file mode 100644
index 0000000..da303d3
--- /dev/null
+++ b/src/components/organisms/forms/theme-toggle/theme-toggle.tsx
@@ -0,0 +1,76 @@
+import { useTheme } from 'next-themes';
+import { ChangeEvent, FC } from 'react';
+import { useIntl } from 'react-intl';
+import { Legend, Moon, Sun } from '../../../atoms';
+import { Switch, SwitchOption, SwitchProps } from '../../../molecules';
+
+export type ThemeToggleProps = Omit<
+  SwitchProps,
+  'isInline' | 'items' | 'name' | 'onSwitch' | 'value'
+>;
+
+/**
+ * ThemeToggle component
+ *
+ * Render a Toggle component to set theme.
+ */
+export const ThemeToggle: FC<ThemeToggleProps> = (props) => {
+  const intl = useIntl();
+  const { resolvedTheme, setTheme } = useTheme();
+  const isDarkTheme = resolvedTheme === 'dark';
+
+  const updateTheme = (e: ChangeEvent<HTMLInputElement>) => {
+    setTheme(e.target.value === 'light' ? 'light' : 'dark');
+  };
+
+  const themeLabel = intl.formatMessage({
+    defaultMessage: 'Theme:',
+    description: 'ThemeToggle: theme label',
+    id: 'suXOBu',
+  });
+  const lightThemeLabel = intl.formatMessage({
+    defaultMessage: 'Light theme',
+    description: 'ThemeToggle: light theme label',
+    id: 'Ygea7s',
+  });
+  const darkThemeLabel = intl.formatMessage({
+    defaultMessage: 'Dark theme',
+    description: 'ThemeToggle: dark theme label',
+    id: '2QwvtS',
+  });
+
+  const options: [SwitchOption, SwitchOption] = [
+    {
+      id: 'theme-light',
+      label: (
+        <>
+          <span className="screen-reader-text">{lightThemeLabel}</span>
+          <Sun />
+        </>
+      ),
+      value: 'light',
+    },
+    {
+      id: 'theme-dark',
+      label: (
+        <>
+          <span className="screen-reader-text">{darkThemeLabel}</span>
+          <Moon />
+        </>
+      ),
+      value: 'dark',
+    },
+  ];
+
+  return (
+    <Switch
+      {...props}
+      isInline
+      items={options}
+      legend={<Legend>{themeLabel}</Legend>}
+      name="theme"
+      onSwitch={updateTheme}
+      value={isDarkTheme ? 'dark' : 'light'}
+    />
+  );
+};
-- 
cgit v1.2.3

