analytics/assets/js/dashboard/theme-provider-hoc.js
Mackenzie 9c2fd9aca5
1. Remove the "airbnb" eslint plugin since it conflicts with prettier (#1374)
and so was just annoying
2. Get rid of all existing ESLint errors.
2a. Turned off `react/display-name` because I couldn't figure out how to
make that wrapped component have a display name. If anyone can figure it
out, that'd be great, because that makes things nicer when using the
React debugger.
2b. The part where it says `plausible()` is undefined in `app.js` is
bothering me. I disabled the check because I can't figure out where that
actually comes from to put in the proper import.
2c. Told ESLint we're using Babel.
3. Added `npm run format` and `npm run lint` commands.
2021-10-11 14:48:19 +02:00

38 lines
1018 B
JavaScript

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
};
this.mutationObserver = new MutationObserver((mutationsList, _observer) => {
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>
);
}
}
}