2022-03-24 13:59:29 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
import Toggle from 'react-toggle';
|
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
|
|
|
import { changeSetting } from 'soapbox/actions/settings';
|
2022-03-24 14:17:39 -07:00
|
|
|
import { Icon } from 'soapbox/components/ui';
|
2022-03-24 13:59:29 -07:00
|
|
|
import { useSettings } from 'soapbox/hooks';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
switchToLight: { id: 'tabs_bar.theme_toggle_light', defaultMessage: 'Switch to light theme' },
|
|
|
|
switchToDark: { id: 'tabs_bar.theme_toggle_dark', defaultMessage: 'Switch to dark theme' },
|
|
|
|
});
|
|
|
|
|
|
|
|
interface IThemeToggle {
|
2022-03-24 14:17:39 -07:00
|
|
|
showLabel?: boolean,
|
2022-03-24 13:59:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function ThemeToggle({ showLabel }: IThemeToggle) {
|
|
|
|
const intl = useIntl();
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const themeMode = useSettings().get('themeMode');
|
|
|
|
|
|
|
|
const id = uuidv4();
|
|
|
|
const label = intl.formatMessage(themeMode === 'light' ? messages.switchToDark : messages.switchToLight);
|
|
|
|
|
|
|
|
const onToggle = () => {
|
|
|
|
const setting = themeMode === 'light' ? 'dark' : 'light';
|
|
|
|
dispatch(changeSetting(['themeMode'], setting));
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='theme-toggle'>
|
|
|
|
<div className='setting-toggle' aria-label={label}>
|
|
|
|
<Toggle
|
|
|
|
id={id}
|
|
|
|
checked={themeMode === 'light'}
|
2022-03-24 14:17:39 -07:00
|
|
|
icons={{
|
2022-04-10 11:38:08 -07:00
|
|
|
checked: <Icon className='w-4 h-4' src={require('@tabler/icons/icons/sun.svg')} />,
|
|
|
|
unchecked: <Icon className='w-4 h-4' src={require('@tabler/icons/icons/moon.svg')} />,
|
2022-03-24 14:17:39 -07:00
|
|
|
}}
|
2022-03-24 13:59:29 -07:00
|
|
|
onChange={onToggle}
|
|
|
|
/>
|
|
|
|
{showLabel && (<label htmlFor={id} className='setting-toggle__label'>{label}</label>)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ThemeToggle;
|