<div id="root"></div>
const { ThemeProvider, createGlobalStyle } = styled;

const GlobalStyle = createGlobalStyle`
  html {
    background: ${ props => props.theme.backgroundPrimary };
  }
`;

const Button = styled.button`
  padding: 8px 16px;
  color: ${ props => props.theme.primary };
  background: none;
  outline: none;
  border-radius: 4px;
  border: 1px solid ${ props => props.theme.primary };
  cursor: pointer;
`;

const themes = {
  dark: {
    primary: '#FFFFFF',
    backgroundPrimary: '#151618',
  },
  light: {
    primary: '#151618',
    backgroundPrimary: '#E9EAEC',
  }
};

function App() {
  const [theme, setTheme] = React.useState('dark');
  
  const toggleTheme = () => {
    if (theme === 'dark') setTheme('light');
    else setTheme('dark');
  };
  
  return (
    <ThemeProvider theme={themes[theme]}>
      <GlobalStyle />
      <div>
        <Button onClick={toggleTheme}>Toggle theme</Button>
      </div>
    </ThemeProvider>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js
  3. https://cdnjs.cloudflare.com/ajax/libs/react-is/16.13.1/umd/react-is.production.min.js
  4. https://cdnjs.cloudflare.com/ajax/libs/styled-components/5.1.1/styled-components.min.js