bigbuffet-rw/app/soapbox/features/ui/components/theme_toggle.js

47 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-07-19 17:50:09 -07:00
import React from 'react';
import PropTypes from 'prop-types';
import { injectIntl, defineMessages } from 'react-intl';
import ImmutablePropTypes from 'react-immutable-proptypes';
import Icon from '../../../components/icon';
import SettingToggle from '../../notifications/components/setting_toggle';
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' },
});
export default @injectIntl
2020-07-19 17:50:09 -07:00
class ThemeToggle extends React.PureComponent {
static propTypes = {
intl: PropTypes.object.isRequired,
settings: ImmutablePropTypes.map.isRequired,
onToggle: PropTypes.func.isRequired,
2020-07-19 17:50:09 -07:00
showLabel: PropTypes.bool,
};
handleToggleTheme = () => {
this.props.onToggle(this.props.settings.get('themeMode') === 'light' ? 'dark' : 'light');
2020-07-19 17:50:09 -07:00
}
render() {
const { intl, settings, showLabel } = this.props;
let toggle = (
2020-07-19 18:03:39 -07:00
<SettingToggle settings={settings} settingPath={['themeMode']} condition={'light'} onChange={this.handleToggleTheme} icons={{ checked: <Icon id='sun' />, unchecked: <Icon id='moon' /> }} ariaLabel={settings.get('themeMode') === 'light' ? intl.formatMessage(messages.switchToDark) : intl.formatMessage(messages.switchToLight)} />
2020-07-19 17:50:09 -07:00
);
if (showLabel) {
toggle = (
<SettingToggle settings={settings} settingPath={['themeMode']} condition={'light'} onChange={this.handleToggleTheme} icons={{ checked: <Icon id='sun' />, unchecked: <Icon id='moon' /> }} label={settings.get('themeMode') === 'light' ? intl.formatMessage(messages.switchToDark) : intl.formatMessage(messages.switchToLight)} />
);
}
return (
2020-07-20 13:34:54 -07:00
<div className='theme-toggle'>
2020-07-19 17:50:09 -07:00
{toggle}
</div>
);
}
2020-07-19 18:03:39 -07:00
2020-07-19 17:50:09 -07:00
}