Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!--
Simple React Dark Mode.
Uses state to set a class on the body if dark mode is enabled.
-->

<div id="app"></div>
              
            
!

CSS

              
                // Setup
//-------------------------------------

*,
*:before,
*:after {
  box-sizing: border-box;
  margin: 0;
}

// Main Styles
//-------------------------------------

body {
  color: #1a202c;
  transition: color 0.2s ease-in;
}

#app {
  height: 100vh;
  width: 100%;
}

main {
  background-color: #f7fafc;
  height: 100%;
  width: 100%;
  padding: 20px;
  transition: background-color 0.2s ease-in;
}

header {
  position: absolute;
}

#toggle {
  width: 50px;
  display: flex;
  padding: 5px;
  background-color: #1a202c;
  display: block;
  border-radius: 1000px;
  cursor: pointer;
  box-shadow: 0px 5px 20px -10px #000000;
  transition: background-color 0.2s ease-in;
  
  .toggle-inner {
    width: 20px;
    height: 15px;
    background-color: white;
    border-radius: 1000px;
    transition: margin-left 0.2s ease-in, background-color 0.2s ease-in;

    &.toggle-active {
      margin-left: 20px;
    }
  }
}

#container {
  display: flex;
  height: 100%;
  width: 100%;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  
  h1 {
    padding: 0;
    margin: 0 0 10px 0;
  }
  
  p {
    opacity: .8;
  }
}

// Dark Mode styling
//-------------------------------------

.dark-mode {
  color: white;
    
  main {
    background-color: #1a202c;
  }
  
  #toggle {
    background-color: white;

    .toggle-inner {
      background-color: #1a202c;
    }
  }
}
              
            
!

JS

              
                function Header() {
  const [ darkMode, setDarkMode ] = React.useState(false)
   
  React.useEffect(() => {
    const body = document.body
    const toggle = document.querySelector('.toggle-inner')
    
    // If dark mode is enabled - adds classes to update dark-mode styling.
    // Else, removes and styling is as normal.
    if( darkMode === true ) {
      body.classList.add('dark-mode')
      toggle.classList.add('toggle-active')
    } else {
      body.classList.remove('dark-mode')
      toggle.classList.remove('toggle-active')
    }
  }, [darkMode])
  
  return (
    <header>
      <div
        id="toggle"
        onClick={() => darkMode === false ? setDarkMode(true) : setDarkMode(false)}
      >
        <div className="toggle-inner"/>
      </div>
    </header>
  )
}

function App() {
  return (
    <main>
      <Header />
      <div id="container">
        <h1>Simple React Dark Mode.</h1>
        <p>Uses state to set a class on the body if dark mode is enabled.</p>
      </div>
    </main>
  )
}

ReactDOM.render(
  <App />,
  document.getElementById('app')
)
              
            
!
999px

Console