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

Auto Save

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

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

CSS

              
                body {
  background: #121212;
}

p {
  margin: 0;
  text-align: center;
  color: white;
}

div {
  border-radius: 10px;
}

h1 {
  font-size: 32px;
  text-align: center;
  color: white;
}

#root {
  padding: 16px 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.container {
  border: 2px solid white;
}

.parent {
  border: 2px solid red;
}

.parent h1 {
  color: red;
  font-size: 24px;
}

.child {
  border: 2px solid blue;
}

.child h1 {
  color: blue;
}

.grand-child {
  border: 2px solid green;
}

.grand-child h1 {
  color: green;
}

.bulb-container {
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 0 60px;
  margin: 40px 0;
}

.bulb {
  height: 60px;
  width: 70%;
  color: black;
  text-align: center;
  font-weight: bold;
  font-size: 20px;
  border: 2px solid yellow;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.5s;
}

.bulb-on {
  background: yellow;
  border: none
}

.bulb-on p {
  color: black;
}

.bulb-title {
  font-size: 20px;
}

.no-provider {
  border: 2px solid gray;
  padding: 30px
}

.no-provider h1 {
  color: gray;
}

.no-provider span {
  font-weight: bold;
}
              
            
!

JS

              
                const BulbContext = React.createContext('This is the default value');

const Parent = React.memo(() => {

  return (
    <div className="parent p-3 m-3">
      <h1>Parent</h1>
      <Child />
    </div>
  );
});

const Child = React.memo(() => {
  return (
    <div className="child p-3 m-3">
      <h1>Child</h1>
      <GrandChild />
    </div>
  );
});

const GrandChild = () => {
  
  const {value, setState} = React.useContext(BulbContext)
  
  return (
    <div className="grand-child p-3 m-3">
      <h1>Grandchild</h1>
      <p className="mb-2">You can turn on the bulb by clicking the button</p>
      <button className="btn btn-success d-flex justify-content-center btn-block" onClick={() => setState(!value)}>
        {value ? 'Turn off' : 'Turn on'}
      </button>
    </div>
  );
};

const NoProvider = () => {
  const defaultValue = React.useContext(BulbContext)
  return (
    <div className="container no-provider mt-3">
      <h1>Component with no provider</h1>
      <p>This component uses the default value of the context</p>
      <p className="mt-3"><span>{defaultValue}</span></p>
    </div>
  )
}

const App = () => {
  
  const [state, setState] = React.useState(false)
  
  return (
    <>
    <BulbContext.Provider value={{value: state, setState}}>
      <div className="container p-3 m-3">
        <h1>Global App</h1>
        <div className="bulb-container">
          <p className="bulb-title">This is a bulb 😅:</p>
          <div className={`${state ? 'bulb bulb-on' : 'bulb'}`}>
            <p>{state ? 'ON' : 'OFF'}</p>
          </div>
        </div>

        <Parent />
      </div>
    </BulbContext.Provider>
    <NoProvider />
    </>
  );
};

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

              
            
!
999px

Console