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

49 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-07-19 17:50:09 -07:00
import PropTypes from 'prop-types';
import React from 'react';
import ImmutablePureComponent from 'react-immutable-pure-component';
import { defineMessages } from 'react-intl';
import Toggle from 'react-toggle';
2022-01-10 14:01:24 -08:00
import Icon from '../../../components/icon';
2020-07-19 17:50:09 -07:00
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 class ThemeToggle extends ImmutablePureComponent {
2020-07-19 17:50:09 -07:00
static propTypes = {
intl: PropTypes.object.isRequired,
themeMode: PropTypes.string.isRequired,
onToggle: PropTypes.func.isRequired,
2020-07-19 17:50:09 -07:00
showLabel: PropTypes.bool,
};
handleToggleTheme = () => {
this.props.onToggle(this.props.themeMode === 'light' ? 'dark' : 'light');
2020-07-19 17:50:09 -07:00
}
render() {
const { intl, themeMode, showLabel } = this.props;
const id ='theme-toggle';
const label = intl.formatMessage(themeMode === 'light' ? messages.switchToDark : messages.switchToLight);
2020-07-19 17:50:09 -07:00
return (
2020-07-20 13:34:54 -07:00
<div className='theme-toggle'>
<div className='setting-toggle' aria-label={label}>
<Toggle
id={id}
checked={themeMode === 'light'}
onChange={this.handleToggleTheme}
icons={{ checked: <Icon src={require('@tabler/icons/icons/sun.svg')} />, unchecked: <Icon src={require('@tabler/icons/icons/moon.svg')} /> }}
onKeyDown={this.onKeyDown}
/>
{showLabel && (<label htmlFor={id} className='setting-toggle__label'>{label}</label>)}
</div>
2020-07-19 17:50:09 -07:00
</div>
);
}
2020-07-19 18:03:39 -07:00
2020-07-19 17:50:09 -07:00
}