aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/organisms/forms/settings-form/theme-toggle/theme-toggle.tsx
blob: d7194349cdd06cf3628d4a6504f6ccc4cc3f6bbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { forwardRef, type ForwardRefRenderFunction } from 'react';
import { useIntl } from 'react-intl';
import { useTheme } from '../../../../../utils/hooks';
import { Icon, Legend } from '../../../../atoms';
import {
  Switch,
  type SwitchOption,
  type SwitchProps,
} from '../../../../molecules';

export type ThemeToggleProps = Omit<
  SwitchProps,
  'isInline' | 'items' | 'legend' | 'name' | 'onSwitch' | 'value'
>;

const ThemeToggleWithRef: ForwardRefRenderFunction<
  HTMLFieldSetElement,
  ThemeToggleProps
> = (props, ref) => {
  const intl = useIntl();
  const { resolvedTheme, toggleTheme } = useTheme();

  const messages = {
    legend: intl.formatMessage({
      defaultMessage: 'Theme:',
      description: 'ThemeToggle: theme label',
      id: 'suXOBu',
    }),
    options: {
      dark: intl.formatMessage({
        defaultMessage: 'Dark theme',
        description: 'ThemeToggle: dark theme label',
        id: '2QwvtS',
      }),
      light: intl.formatMessage({
        defaultMessage: 'Light theme',
        description: 'ThemeToggle: light theme label',
        id: 'Ygea7s',
      }),
    },
  };

  const options: [SwitchOption, SwitchOption] = [
    {
      id: 'theme-light',
      // eslint-disable-next-line react/jsx-no-literals
      label: <Icon aria-label={messages.options.light} shape="sun" size="sm" />,
      value: 'light',
    },
    {
      id: 'theme-dark',
      // eslint-disable-next-line react/jsx-no-literals
      label: <Icon aria-label={messages.options.dark} shape="moon" size="sm" />,
      value: 'dark',
    },
  ];

  return (
    <Switch
      {...props}
      isInline
      items={options}
      legend={<Legend>{messages.legend}</Legend>}
      // eslint-disable-next-line react/jsx-no-literals
      name="theme"
      onSwitch={toggleTheme}
      ref={ref}
      value={resolvedTheme}
    />
  );
};

/**
 * ThemeToggle component
 *
 * Render a Toggle component to set theme.
 */
export const ThemeToggle = forwardRef(ThemeToggleWithRef);