2020-12-16 12:57:28 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { ThemeContext } from './theme-context'
|
|
|
|
|
|
|
|
export const withThemeProvider = (WrappedComponent) => {
|
|
|
|
return class extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {
|
|
|
|
dark: document.querySelector('html').classList.contains('dark') || false
|
|
|
|
};
|
|
|
|
|
2021-10-11 15:48:19 +03:00
|
|
|
this.mutationObserver = new MutationObserver((mutationsList, _observer) => {
|
2020-12-16 12:57:28 +03:00
|
|
|
mutationsList.forEach(mutation => {
|
|
|
|
if (mutation.attributeName === 'class') {
|
|
|
|
this.setState({ dark: mutation.target.classList.contains('dark') });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.mutationObserver.observe(document.querySelector('html'), { attributes: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.mutationObserver.disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<ThemeContext.Provider value={this.state.dark}>
|
|
|
|
<WrappedComponent {...this.props}/>
|
|
|
|
</ThemeContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|