bigbuffet-rw/app/soapbox/features/ui/components/theme-selector.tsx

66 lines
2 KiB
TypeScript
Raw Normal View History

2022-05-05 16:12:08 -07:00
import React, { useMemo } from 'react';
import { defineMessages, useIntl } from 'react-intl';
import { Icon } from 'soapbox/components/ui';
import Select from '../../../components/ui/select/select';
2022-05-05 16:12:08 -07:00
const messages = defineMessages({
light: { id: 'theme_toggle.light', defaultMessage: 'Light' },
dark: { id: 'theme_toggle.dark', defaultMessage: 'Dark' },
system: { id: 'theme_toggle.system', defaultMessage: 'System' },
});
interface IThemeSelector {
value: string,
onChange: (value: string) => void,
}
/** Pure theme selector. */
const ThemeSelector: React.FC<IThemeSelector> = ({ value, onChange }) => {
const intl = useIntl();
const themeIconSrc = useMemo(() => {
switch (value) {
2022-05-11 14:06:35 -07:00
case 'system':
return require('@tabler/icons/device-desktop.svg');
2022-05-11 14:06:35 -07:00
case 'light':
return require('@tabler/icons/sun.svg');
2022-05-11 14:06:35 -07:00
case 'dark':
return require('@tabler/icons/moon.svg');
2022-05-11 14:06:35 -07:00
default:
return null;
2022-05-05 16:12:08 -07:00
}
}, [value]);
const handleChange: React.ChangeEventHandler<HTMLSelectElement> = e => {
onChange(e.target.value);
};
return (
<label>
<div className='relative rounded-md shadow-sm'>
<div className='absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none'>
<Icon src={themeIconSrc} className='h-4 w-4 text-gray-600 dark:text-gray-700' />
2022-05-05 16:12:08 -07:00
</div>
<Select
onChange={handleChange}
2022-05-05 16:12:08 -07:00
defaultValue={value}
className='pl-10'
2022-05-05 16:12:08 -07:00
>
<option value='system'>{intl.formatMessage(messages.system)}</option>
<option value='light'>{intl.formatMessage(messages.light)}</option>
<option value='dark'>{intl.formatMessage(messages.dark)}</option>
</Select>
2022-05-05 16:12:08 -07:00
<div className='absolute inset-y-0 right-0 pr-3 flex items-center pointer-events-none'>
<Icon src={require('@tabler/icons/chevron-down.svg')} className='h-4 w-4 text-gray-600 dark:text-gray-700' />
2022-05-05 16:12:08 -07:00
</div>
</div>
</label>
);
};
export default ThemeSelector;