Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

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

CSS

              
                #card {
  font-family: Arial;
  font-size: 1rem;
  padding: 20px;
  border: 1px solid #ddd;
}

#buttons {
  padding: 10px 0px;
}

button {
  margin-right: 10px;
}

#loader {
  background-color: #ddf;
  padding: 5px;
  border-radius: 5px;
  color: #008;
}

#title {
  background-color: #333;
  color: #fff;
  padding: 5px;
}

#empty {
  background-color: #ffd;
  padding: 10px;
  color: #880;
}

#error {
  background-color: #fdd;
  padding: 10px;
  color: #800;
}
              
            
!

JS

              
                const Card = ({ children }) => <div id="card">{children}</div>
const Title = ({ children }) => <div id="title"><h2>{children}</h2></div>
const Loader = ({ children }) => <div id="loader">{children}</div>
const Error = ({ children }) => <div id="error">{children}</div>
const Empty = ({ children }) => <div id="empty">{children}</div>

const App = () => {
  // initial state for loading
  const [dataStatus, setDataStatus] = React.useState("Loading")
  const [person, setPerson] = React.useState()

  // fake out api calls
  // setDataStatus("Loading") when waiting for the API
  const setLoading = () => { 
    setDataStatus("Loading")
    setPerson()
  }
  // setDataStatus("Loaded") when successful with data
  const setLoaded = () => { 
    setDataStatus("Loaded")
    setPerson({ name: "Luke Skywalker" })
  }
  // setDataStatus("Empty") when successful with no data
  const setEmpty = () => { 
    setDataStatus("Empty")
    setPerson()
  }
  // setDataStatus("Error") if an error occurs
  const setError = () => { 
    setDataStatus("Error")
    setPerson()
  }

// now manage the state of the component
return (
  <Card>
    { dataStatus === "Loaded" &&
      <Title>{person.name}</Title>
    }
    { dataStatus === "Loading" &&
      <Loader>Loading...</Loader>
    }
    { dataStatus === "Error" &&
      <Error>Sorry, we had an oopsie...</Error>
    }
    { dataStatus === "Empty" &&
      <Empty>Looks like we're missing something...</Empty>
    }
    <div id="buttons">
      <button onClick={setLoaded}>Set to Loaded</button>
      <button onClick={setEmpty}>Set to Empty</button>
      <button onClick={setError}>Set to Error</button>
      <button onClick={setLoading}>Set to Loading</button>
      Current State: {dataStatus}
    </div>
  </Card>
)
}

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

Console