Giter Club home page Giter Club logo

clappy-button-web-component's Introduction

Clappy Button

Clappy Button Logo

The "Like" button evolved.

Demo

Introduction

Clappy Button is a web component for payments that can be embedded on any webpage.

Installation

Install via NPM:

npm install clappy-button

Usage

JS:

import 'clappy-button'

HTML:

<clappy-button
  amountperclap="0.01"
  amountmax="0.10"
  currencycode="USD"
  currencySymbol="$"
  theme="light"
  instanceid="post-id-1"
></clappy-button>

Parameters

  • amountperclap - Each time user claps, total amount will be incremented by this value e.g. "0.01"
  • amountmax - set equal to the user's current spendable balance.
  • currencycode / currencysymbol
    • if currencycode is specified, it will be used as a suffix in the counter e.g. 1 USD
    • if currencysymbol is specified, it will be used as a prefix in the counter e.g. $1
  • theme - "light" and "dark" are the current built-in themes. Alternatively, you can create your own.
  • instanceid - Unique identifier if using multiple clappy buttons on the same page e.g. post-id-1

Attributes are case sensitive and must be defined in all lowercase e.g. instanceid is valid, instanceId is invalid.

Events

Communication between your app and Clappy Button is made via Window message events.

Window Message Events emitted from Clappy Button to listen for in your app:

  • loading - Sent when user is finished clapping. Use this event to trigger the payment with the amount. eventData includes instanceId and amount.

Window Message Events to emit from your app to control Clappy Button animations:

  • success - Triggers success animation
  • fail - Triggers fail animation

Framework Implementation Examples

React

import { useEffect } from 'react'
import 'clappy-button'

function App() {

  async function confirmPayment(amount) {
    confirm(amount)
      .then(result => {
        window.postMessage({ app: 'clappy-button', event: 'success', instanceId: 'cb1' })
      })
      .catch(error => {
        window.postMessage({ app: 'clappy-button', event: 'fail', instanceId: 'cb1' })
      })
  }

  function handleWindowMessage(message) {
    if (message.data.app === 'clappy-button' && message.data.event === 'loading' && message.data.instanceId === 'cb1') {
      confirmPayment(message.data.eventData.amount)
    }
  };
  useEffect(() => {
    window.addEventListener("message", handleWindowMessage);
    return () => window.removeEventListener('message', handleWindowMessage);
  }, [])
  
  return (
    <div style={{ marginTop: '75px' }}>
      <clappy-button instanceid="cb1" currencysymbol="$" amountperclap="0.01"></clappy-button>
    </div>
  );
}

Custom Theme

Custom theme can be specified using css:

  clappy-button::part(custom-theme) {
    --background: #e6e6e6;
    --counter-background: #000;
    --counter-label: #fff;
    --confetti-primary: #2599ff;
    --confetti-secondary: #fdcb01;
    --hand: #000;
    --loading: #ababab;
    --loading-background: white;
    --success: #4bb543;
    --fail: #fc100d;
    --button-border: transparent;
  }

Contribute

Feature requests, issues and pull requests are welcome.

License

MIT License. See LICENSE.md for details.

Maintainers

remjx - twitter | website

Donations

coindrop.to/clappy-button << Also built by remjx :)

Follow

@clappybutton on Twitter

clappy-button-web-component's People

Contributors

remjx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

clappy-button-web-component's Issues

Export typescript types for window messages

As the window message API could change in the future, typescript types should be exported that ensure that apps are listening & emitting the proper events, i.e.

import { ClappyButtonMessage } from 'clappy-button';
const successMessage: ClappyButtonMessage = { app: 'clappy-button', event: 'success', instanceId: 'cb1' };
window.postMessage(successMessage)

Compile as ES module / CommonJS module

Currently the web component is compiled using rollup into a single .js file, however this may include redundant dependencies when included as part of an app.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.