Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <form>
  <div class="row">
    <input type="search" name="pokemon" placeholder="pokemon" aria-label="Search for a pokemon by name" value="pikachu" required>
    <button type="submit" aria-label="search">🔍</button>
  </div>
  <output>
    <pre>
      <!-- Where our pokemon data will go -->
    </pre>
  </output>
</form>
            
          
!
            
              body {
  font-family: -apple-system, BlinkMacSystemFont, Helvetica, Arial, sans-serif;
  color: hsl(200, 20%, 20%);
  background-color: hsl(200, 10%, 98%);
}

form {
  max-width: 22rem;
  margin: 0 auto;
  padding: 2rem;
}

input,
button {
  display: block;
  background: #fff;
  border: 0;
  padding: 0.5rem;
  font: inherit;
  color: inherit;
}

input { 
  flex: 1;
}

button {
  padding: 0.5rem 1rem;
  background-color: hsl(200, 10%, 94%);
}

.row {
  display: flex;
  box-shadow: 0 1px 4px hsla(200, 10%, 40%, 0.5);
  border-radius: 0.25rem;
  overflow: hidden;
}
            
          
!
            
              // our GraphQL query accepts a $name variable and uses it to query for a specific pokemon
// it asks for the pokemon's  ID, name and weight to be returned
const query = `
query Pikachu($name: String!) {
  pokemon(name: $name) {
    id
    name
    weight
  }
}
`;

function fetchPokemon(name) {
  // send a POST request with our query as a JSON body to our API
  return fetch("https://pokemon-gql.now.sh/api", {
    method: "POST",
    headers: { "content-type": "application/json" },
    // we send the query and variables separately—the GraphQL server will inject the variables into the query
    body: JSON.stringify({ query, variables: { name } })
  })
    .then(response => {
      // if we get a bad response we error
      if (!response.ok) throw new Error("Request failed");
      // we know this API returns JSON
      return response.json();
    })
}

const output = document.querySelector("output > pre");

function update(json) {
  if (!json || !json.data || !json.data.pokemon) {
    output.textContent = "Pokemon not found";
  } else {
    // shove the response JSON into a <pre> so we can see it
    // the third argument to JSON.stringify is the indentation size (this causes it to pretty print)
    output.textContent = JSON.stringify(json, null, 2);
  }
}

function search(event) {
  // stop the default HTML form submit that refreshes the page
  event.preventDefault();
  // grab the name the user entered from the input with `name="pokemon"`
  const name = event.target.elements.pokemon.value;
  fetchPokemon(name).then(update)
}

const form = document.querySelector("form")
form.addEventListener("submit", search);

// run the fetch once so the page starts off with a pokemon visible
fetchPokemon("pikachu").then(update);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console