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

              
                <p>This like button is a petite-vue component that, based on a given key (title), will look up and increment a value using Cloudflare workers and KV. As it gets more likes it will grow bigger.</p>
<div class="likes" v-scope="likes({ title:'test' })"></div>
              
            
!

CSS

              
                *, *::before, *::after { box-sizing:border-box }

body {
	margin:0;
  font-family: Roboto, sans-serif;
  background-color: var(--bg-color);
  
  display:grid;
  place-items:center;
  min-height:100vh;
}

p {
  padding:50px 10px;
  font-size:2rem;
  max-width:500px;
  font-weight:bold;
}


:root {
  --c-peach: #e5a97e;
  --bg-color: #E6D9D0;
  --fg-color: #17171e;
  --accent-color: var(--c-peach);
}

.likes {
   

  

  position:fixed;
  right:max( 10px, 2vw );
  bottom:10px;

  
  max-width: max(100px, 8vw);

  .like-button {
    
    --likes: ;
    --size: min( 3, calc( 1 + ( var(--likes) / 1000) ) );
    
    transition: transform 0.4s ease;
    transform: scale( var(--size) ) rotate3d( 1, 1, 1, 10deg);
    transform-origin: bottom right;
    
    display:grid;
    place-items:center;

    > * {
      grid-column: 1 / -1;
      grid-row: 1 / -1;
    }

    svg { 
      width:100%;
    }

    p {
      font-size: clamp( 18px, 12px + 0.66vw, 42px);
      pointer-events: none;
      position:relative;
      top:-6px;
      margin:0;
    }

    button {
      border:none;
      background:none;
      cursor:pointer;
    }
    
    
    &:hover {
      animation: beat 0.3s infinite alternate; 
    }

    @keyframes beat {
      0% {
        transform: scale( var(--size) ) rotate3d( 1, 1, 1, 10deg);
      }
      
      100% {
        transform:scale( calc( 1.1 * var(--size) ) ) rotate3d( 1, 1, 1, 15deg);
      }
    }
  }
  
    
 }
              
            
!

JS

              
                //Full Walkthrough here: https://andrewwalpole.com/blog/building-a-like-button-with-cloudflare-workers/

import { createApp } from 'https://unpkg.com/petite-vue?module';

const likes = ({title}) => {

  const endpoint = 'https://likes.walpolea.workers.dev/';
  return {
    $template: `
    <article class="like-button" @vue:mounted="mounted" :style="{'--likes':likes}">
      <button @click="postLike();">
        <svg :width="likes + '%'" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 78.09 74.75">
          <polygon points="2 34.869 2 13.192 13.181 2 36.04 2 36.04 9.188 43.221 2 64.909 2 76.09 13.192 76.09 34.869 45.464 65.495 39.052 71.92 2 34.869" fill="var(--accent-color)" />
          <path d="M64.08,4l10.01,10.02v20.02l-30.04,30.04-5,5.01L4,34.04V14.02L14.01,4h20.03V14.02l10.01-10.02h20.03m1.658-4h-23.346l-1.172,1.173-3.18,3.183V0H12.352l-1.172,1.173L1.17,11.193l-1.17,1.171v23.333l1.172,1.172,35.05,35.05,2.831,2.831,2.828-2.834,5-5.01,30.037-30.037,1.172-1.172V12.364l-1.17-1.171L66.91,1.173l-1.172-1.173h0Z" fill="var(--fg-color)" />
        </svg>
      </button>
      <p v-if="likes > 0">{{likes}}</p>
    </article>`,
    title,
    likes:0,
    likeAdded:false,
    async mounted() {
      await this.loadLikes();
    },
    async loadLikes() {
      this.likes = (await ( await fetch(`${endpoint}?title=${this.title}`)).json()).likes;
    },
    async postLike() {
      if( !this.likeAdded ) {

        const response = await ( await fetch(endpoint, {
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({title: this.title})
        })).json();

        this.likes = response.likes;

        this.likeAdded = true;
      }
    }
  }
}

createApp({likes}).mount();
              
            
!
999px

Console