Jack Maclennan Portfolio
    Preparing search index...
    • A wrapper around Select that provides theme selection for the application. It persists the chosen theme in local storage and updates the Next.js theme context.

                <ThemeSelect />
      

      Parameters

      • className: { className?: string }

        Optional Tailwind classes for the outer wrapper.=

      Returns ReactNode

      The rendered ThemeSelect component.

      export default function ThemeSelect({ className }: { className?: string }): ReactNode {
      const { setTheme } = useTheme();
      const [localTheme, setLocalTheme] = useLocalStorage('theme', 'system', {
      deserializer: (value) => value,
      initializeWithValue: false,
      serializer: (value) => value
      });

      const getInitialSelectedOption: () => SelectOption = useCallback(
      () => options.find((option: SelectOption): boolean => option.value === localTheme) as SelectOption,
      [localTheme]
      );

      const [selectedOption, setSelectedOption] = useState<SelectOption>();

      useEffect(() => {
      setSelectedOption(getInitialSelectedOption());
      }, [getInitialSelectedOption]);

      const onChange = (value: SelectOption) => {
      setSelectedOption(value);
      setLocalTheme(value.value as string);
      setTheme(value.value as string);
      };

      return (
      <div className={clsx(className)} role="group" aria-label="theme selector">
      <Select
      selectedOption={selectedOption}
      placeholder={{ value: 'placeholder', label: placeholder }}
      options={options}
      onChange={onChange}
      />
      </div>
      );
      }