Skip to content

Instantly share code, notes, and snippets.

@JamieDixon
Last active November 12, 2018 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamieDixon/eebe0ca56a1c4ac71ad3efafa7ce5cc8 to your computer and use it in GitHub Desktop.
Save JamieDixon/eebe0ca56a1c4ac71ad3efafa7ce5cc8 to your computer and use it in GitHub Desktop.
const themes = {
localhost: () => import('../../styles/themes/default.js'),
'client1.local.com': import('../../styles/themes/client1.js'),
'client2.local.com': import('../../styles/themes/client2.js')
};
const Select = styled.select`
position: absolute;
bottom: 1em;
left: 1em;
`;
const ThemePicker = ({ onChange }) => {
return (
<Select onChange={onChange}>
{Object.keys(themes).map(key => {
return (
<option key={key} value={key}>
{key}
</option>
);
})}
</Select>
);
};
const ThemeImporter = ({ children }) => {
const [theme, setTheme] = useState(null);
const [domain, setDomain] = useState(document.domain);
const previousDomain = useRef();
useEffect(() => {
if (previousDomain.current !== domain) {
previousDomain.current = domain;
(themes[domain] || themes.localhost)().then(nextTheme =>
setTheme(nextTheme.default)
);
}
});
if (!theme) {
return null;
}
return (
<ThemeProvider theme={theme}>
<div>
{document.domain === 'localhost' && (
<ThemePicker onChange={e => setDomain(e.target.value)} />
)}
{children}
</div>
</ThemeProvider>
);
}
@gaui
Copy link

gaui commented Nov 12, 2018

lol you mean React 16.7 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment