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

              
                <main id="app">
  <h1 class="h1">Grab a beer&trade;</h1>
  <p class="p">Cycle through BrewDog’s beer offering, compare them and find your favourite!</p>
  <div class="cards">
    <div class="card-controls">
      <button
        @click="showBeer(`prev`)"
              
        class="btn"

        :disabled="!nextPrev.prev"
        v-html="`Prev`"
      />
      <button
        @click="showBeer(`next`)"
              
        class="btn"

        :disabled="!nextPrev.next || loading"
        v-html="loading ? `Getting more beers` : `Next`"
      />
      <button
        @click="setSaved(current)"
              
        class="btn btn--save"

        v-html="`Save`"
      >
    </div>
    <div class="card-wrapper">
      <module-card
        :beer="beers[saved]"
        :compare="beers[current]"
      />
    </div>
    <div class="card-wrapper">
      <!-- TODO: Work out why these components need wrappers -->
      <div>
        <module-card
          :beer="beers[current]"

          @select-beer="setSaved($event)"
          @show-beer="showBeer($event)"
        />
      </div>
    </div>
  </div>
  <!--<div>
    <pre>Loading: {{ loading }}</pre>
    <pre>Count: {{ beersLength }}</pre>
    <pre>Saved: {{ saved }}</pre>
    <pre>Page: {{ page }}</pre>
    <pre>Keys: {{ beersKeys }}</pre>
    <pre>Current: {{ current }}</pre>
    <pre>{{ nextPrev }}</pre>
  </div>-->
</main>
              
            
!

CSS

              
                $card-padding: 2rem;
$card-margin: 1rem;
$card-radius: 0.25rem;

$heading-family: 'Lora', serif;

html,
body {
  font-family: 'Open Sans', sans-serif;
  line-height: 1.4;
}

.h1 {
  font-size: 2rem;
  font-weight: 600;
  margin: 1em auto .25em;
  text-align: center;
  font-family: $heading-family;
}

.p {
  font-size: 1rem;
  text-align: center;
  max-width: 20em;
  margin: 0 auto 1em;
}

.cards {
  display: flex;
  flex-wrap: wrap;
  max-width: 800px;
  margin: 0 auto;
  position: relative;
  
  &::before {
    content: '';
    display: block;
  }
}

.card-wrapper {
  display: flex;
  flex-direction: column;
  width: 50%;
}

.card-controls {
  width: 100%;
  text-align: center;
}

.card {
  border-radius: $card-radius;
  box-shadow: rgba(black, 0.1) 0 0 0.5rem;
  padding: $card-padding;
  display: block;
  margin: $card-margin;
  position: relative;
  
  &__saved {
    color: red;
    position: absolute;
    top: 0;
    right: 0;
    padding: 0.5em;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    font-size: 0.75rem;
    font-weight: bold;
  }
  
  &__title {
    font-family: $heading-family;
    display: block;
    font-weight: 600;
    font-size: 1.35rem;
    margin: 0 auto 0.25em;
  }
  
  &__abv {
    display: block;
    font-weight: bold;
  }
  
  &__tagline {
    display: block;
    margin: .5em auto 1em;
  }
  
  &__details {
    display: grid;
    grid-template-columns: auto 1fr;
    grid-gap: 0.25rem .5rem;
    margin-bottom: .5em;
    
    dt {
      font-weight: 600;
    }
    
    dd {
      font-variant-numeric: tabular-nums;
      text-align: right;
      overflow: hidden;
      display: flex;
      
      .compared {
        margin-left: .25em;
      }
      
      &::before {
        content: '';
        opacity: 0.25;
        flex: 1;
        margin-right: 1rem;
        background: linear-gradient(white 50%, black 50%) repeat-x center center;
        background-size: 2px 2px;
      }
    }
  }
  
  &__ingredients {
    
    > span {
      font-weight: bold;
    }
    
    > ul {
      
      > li {
        margin-top: .5rem;
      }
      
      ul {
        list-style: disc;
        padding-left: 1.25rem;
      }
    }
  }
  
  > hr {
    height: 1px;
    background: currentColor;
    opacity: 0.1;
    border: 0;
    border-radius: 0;
    margin: $card-padding * 0.75 0 $card-padding * 0.75 $card-padding * -1;
    width: calc(100% + #{$card-padding});
    display: block;
  }
  
  > img {
    max-width: 100%;
  }
}

.btn {
  padding: .5em 1em;
  margin: .5em;
  display: inline-block;
  cursor: pointer;
  
  &--save {
    display: block;
    margin-left: auto;
    margin-right: auto;
    padding: 1em 2em;
  }
  
  &[disabled] {
    opacity: 0.5;
    cursor: default;
  }
}

.compared {
  color: red;
}
              
            
!

JS

              
                const apiPrefix = `https://api.punkapi.com/v2/`;

const ModuleCompared = Vue.component(`module-compared`, {
  // Props
  props: {
    value: {
      type: [
        Number,
        String,
      ],
    },
    decimals: {
      type: Number,
      default: 1,
    },
    unit: {
      type: String,
      default: ``,
    },
  },
  
  // Template
  template: `
    <span class="compared" v-if="value || value === 0">
      <template v-if="value === 0">(=)</template>
      <template v-else-if="typeof(value) === \`string\`">String comparison</template>
      <template v-else>({{ value >= 0 ? \`+\` : \`\` }}{{ value.toFixed(decimals) }}{{ unit ? \` \${unit}\` : \`\` }})</template>
    </span>`,
});

const ModuleCard = Vue.component('module-card', {
  // Components
  components: {
    'module-compared': ModuleCompared,
  },
  
  // Props
  props: {
    beer: {
      type: Object,
      default: () => {},
    },
    compare: {
      type: Object,
      default: () => {},
    },
  },
  
  // Data
  computed: {
    compared() {
      const compare = this.compare;
      const beer = this.beer;
      
      if (!compare || compare.id === beer.id) return {};
      
      console.log(this.compare.name, this.compare);
      
      return {
        abv: compare.abv ? (beer.abv - compare.abv) : false,
        ph: compare.ph ? (beer.ph - compare.ph) : false,
        ibu: compare.ibu ? (beer.ibu - compare.ibu) : false,
        ebc: compare.ebc ? (beer.ebc - compare.ebc) : false,
        volumeValue: beer.volume && compare.volume ? (beer.volume.value - compare.volume.value) : false,
        boilVolumeValue: beer.volume && compare.boil_volume ? (beer.boil_volume.value - compare.boil_volume.value) : false,
      };
    },
  },
  
  // Methods
  methods: {
    onClick() {
      this.$emit(`select-beer`, this.beer.id);
    },
    getSet(set) {
      const map = {
        malt: `Malt`,
        hops: `Hops`,
        yeast: `Yeast`,
      };
      
      return map[set] || set;
    },
  },
  
  // Template
  // template: `<div>Test</div>`;
  template: `
    <div
      v-if="beer"

      :class="[
        \`card\`,
        {
          'card--compare': compare,
        },
      ]"

      @click="onClick"
    >
      <span class="card__saved" v-if="compare">Saved</span>
      <span class="card__title">{{ beer.name }}</span>
      <span class="card__abv">
        {{ beer.abv }}% ABV
        <module-compared :value="compared.abv" />
      </span>
      <span class="card__tagline">{{ beer.tagline }}</span>

      <dl class="card__details">
        <template v-if="beer.ibu">
          <dt><abbr title="International Bitterness Units">IBU</abbr></dt>
          <dd>{{ beer.ibu.toFixed(1) }} <module-compared :value="compared.ibu" /></dd>
        </template>

        <template v-if="beer.ebc">
          <dt><abbr title="European Brewery Convention">EBC</abbr></dt>
          <dd>{{ beer.ebc.toFixed(1) }} <module-compared :value="compared.ebc" /></dd>
        </template>

        <template v-if="beer.ph">
          <dt><abbr title="Acidity">pH</abbr></dt>
          <dd>{{ beer.ph.toFixed(1) }} <module-compared :value="compared.ph" /></dd>
        </template>

        <template v-if="beer.volume">
          <dt><attr title="Volume">Vol.</attr></dt>
          <dd>{{ beer.volume.value }} {{ beer.volume.unit }} <module-compared :value="compared.volumeValue" :unit="beer.volume.unit" :decimals="0" /></dd>
        </template>

        <template v-if="beer.boil_volume">
          <dt><attr title="Boil Volume">Boil Vol.</attr></dt>
          <dd>{{ beer.boil_volume.value }} {{ beer.boil_volume.unit }} <module-compared :value="compared.boilVolumeValue" :unit="beer.boil_volume.unit" :decimals="0" /></dd>
        </template>
      </dl>

      <hr>

      <span class="card__ingredients">
        <span>Ingredients</span>
        <ul>
            <li v-for="(ingredients, set) in beer.ingredients" :key="set">
              <span class="">{{ getSet(set) }}</span>
              <ul>
                <li v-if="typeof(ingredients) === \`string\`">{{ ingredients }}</li>
                <template v-else>
                  <li v-for="(ingredient, index) in ingredients" :key="index">
                    {{ ingredient.name }}
                    <template v-if="ingredient.amount">({{ ingredient.amount.value }} {{ ingredient.amount.unit }})</template>
                  </li>
                </template>
              </ul>
            <li>
        </ul>
      </span>
    </div>`,
});

const app = new Vue({
  el: '#app',
  
  // Components
  components: {
    'module-card': ModuleCard,
  },
  
  // Data
  data() {
    return {
      loading: false,
      beers: {},
      saved: ``,
      current: ``,
      perPage: 10,
      page: 1,
    };
  },
  computed: {
    beersKeys() {
      return Object.keys(this.beers);
    },
    beersLength() {
      return this.beersKeys.length;
    },
    nextPrev() {
      const keys = this.beersKeys;
      const index = keys.indexOf(this.current);
      
      let next = false;
      let prev = false;
      
      if (index >= 0) {
        next = keys[index + 1] || false;
        prev = keys[index - 1] || false;

        // if (prev < 0) prev = false;
        // if (next > this.beersLength) next = false;
      }
      
      return {
        next,
        prev,
      };
    },
  },
  watch: {
    'nextPrev.next': {
      handler(next) {
        if (!next) this.getBeers({
          page: `next`,
        });
      },
    },
  },
  
  // Methods
  methods: {
    getBeers({ page }) {
      if (this.loading) return;
      
      this.loading = true;
      this.page = page === `next` ? this.page + 1 : page;
      
      fetch(`${apiPrefix}beers/?page=${this.page}&per_page=${this.perPage}`)
        .then(res => res.json())
        .then((json = []) => {
          json.forEach(beer => {
            beer.id = `${beer.id}`;
            const id = beer.id;

            this.$set(this.beers, id, beer);
            
            if (!this.saved) this.saved = id;
            else if (!this.current) this.current = id;
          });
        
          setTimeout(() => {
            this.loading = false;
          }, 250);
        });
    },
    showBeer(nextPrev) {
      if ((nextPrev === `next` && !this.nextPrev.next) || (nextPrev === `prev` && !this.nextPrev.prev)) return;
      
      const index = this.nextPrev[nextPrev];
      
      this.current = index;
    },
    setSaved(id) {
      this.saved = id;
    },
  },
  
  // Lifecycle
  mounted() {
    this.getBeers({
      page: 1,
    });
  },
})
              
            
!
999px

Console