applause button React

How to implement applause buttons in React

Lyket is the ultimate tool to add applause buttons to your React app! The library allows a great deal of customization with just a few lines of code and it's compatible with all React frameworks like NextJS, Gatsby, create-react-app.

import { Provider, ClapButton } from "@lyket/react";

<Provider apiKey="acc0dbccce8e557db5ebbe6d605aaa">
  <ClapButton
    namespace="testing-react"
    id="everybody-clap-now"
  />
</Provider>
0

If you want to find out how to use the React library in detail you came to the right place. If you prefer to learn with examples and ready-made snippets to copy and paste, you can visit:

How Lyket applause buttons work

All Lyket buttons share these basic behaviors and features:

  • As soon as a button component is mounted, a fetch request is made to retrieve info on the button that identifies with the ID and namespace that you provided. If no button is found, a new one will be created using the ID/namespace identifier.

  • Notice that the server will create a new button for every different and unique identifier, so if you change ID or namespace or type the new button won’t inherit the votes.

  • Every time a visitor interacts with a button, the button counter will update and Lyket will flag that the visitor has voted. Lyket uses an unique random ID stored in the visitor's browser localStorage to identify the visitor, with no need to signup or to use any third party service. To disable the session ID, and therefore use only the IP address to identify a user, set the disableSessionId prop to true in the Provider component.

  • Lyket enforces a maximum number of sessions IDs per IPaddress to avoid receiving too many requests or DDoS attacks. The default is 3 sessions per IP. You can change this number by logging into your private area and edit the user settings.

  • In the user settings you can also specify from which domains Lyket should accept requests using your personal API key. If left empty Lyket will accept requests coming from all domains.

Install Lyket

Well, Let's get started! To install the component run

yarn add @lyket/react

or

npm install @lyket/react
Add Lyket to your app

Add the Provider component top-level and configure it using your personal public API key that you can get after registering to Lyket

import { Provider } from '@lyket/react';
								
ReactDOM.render(
	<Provider apiKey="[YOUR-API-KEY]">
		<App />
	</Provider>,
	document.getElementById('root')
);
Required props
  • apiKey: string - You can get your public API key by registering on Lyket.
Optional props
  • theme: Record<'colors' | 'fonts' , Record<string, string>> - Allows you to change the default buttons colors and fonts. It doesn't apply to all templates. Read more about it in the Styling buttons section at the end of this document.

  • recaptchaSiteKey: string - If you enabled reCAPTCHA in the private area's user settings, you need to provide your reCAPTCHA public key, otherwise your buttons will result as unauthorized. Read more in the reCAPTCHA section at the end of this document.

  • disableSessionId: boolean Default: false - If set to true Lyket won't store a unique session ID for your visitors making them anonymous. Lyket will then discriminate visitors (to tell if they already liked a button or not) only based on the IP address. Disabling the session ID can be useful if you don't want Lyket to result in your cookie detection software.

Configuring applause buttons

Once you configured the Provider you can start adding applause buttons anywhere in your app.

Lyket applause buttons behave like Medium applauses. Users can like multiple times and every other call from the same user will increment the claps number indefinitely.

import { ClapButton } from '@lyket/react';

export BlogPost = ({ title, content }) => {
  return (
    <div>
      {title}
      <ClapButton id="diy-fish-holder" namespace="post" />
      {content}
    </div>
  );
};
Try it! β†’
0
Optional props
Required props
  • id: string - The API uses the ID to find a button. It should be unique for namespace. It accepts an alphanumeric string with maximum 50 characters.
Optional props
  • namespace: string - Giving a namespace is useful to keep buttons organised, and can be used to fetch statistics. Check the API docs for more information.

  • hideCounterIfLessThan: number - You may want to hide the counter if you are not getting enough feedback. Specify the number of votes/claps/likes you want to receive before showing the counter.

  • component: React.ReactNode - If this prop is not provided you will see the Simple template. To change the aspect of your buttons you can either choose one of the ready-made templates or a custom component and pass it through the component attribute.

  • onLoad: (buttonData: UpdownButtonData | ClapButtonData | LikeButtonData) => void - This function gets called when the button has finished loading. buttonData has different format depending on the button type.

  • onPress: (buttonData: ClapButtonData) => void - This function gets called whenever a Press action is triggered.

Applause button templates

Lyket provides a set of out-of-the-box templates. You can see all the available templates in the templates page

By default, ie. if you don't specify any template or custom component, Lyket will show the Simple Template.

  • Simple (default) - supports custom theme
  • Medium: Medium style
  • Heart: Heart style - supports custom theme
import { ClapButton } from '@lyket/react';

export StandingOvation = () => {
  return (
    <>
      <h4 id="Do you like pizza?">Do you like pizza?</h4>
      <LikeButton
        id="do-you-like-pizza"
        component={ClapButton.templates.Heart}
      />
    </>
  );
};

Custom applause buttons

You may want to give a different style to your applause button, for example using your company logo as the icon. You can do that by creating your own custom button! The clap button component allows you to customize the look and feel of your button, like changing the icon, show or hide the counter and so on.

Here is an example for each button type of using a custom component by passing it as "children". You can pass a custom component also through the component prop.

These are the available props:

  • handlePress: (buttonData: ClapButtonData) => void
  • totalClaps: number
  • userClaps: number
  • isLoading: boolean
  • isCounterVisible: boolean
import { ClapButton } from '@lyket/react';

export Faq = () => {
  return (
    <>
      <h4 id="Do you like pizza?">Do you like pizza?</h4>
      <ClapButton
        id="do-you-like-pizza"
        namespace="faq"
        hideCounterIfLessThan={3}
      >
        {({
          handlePress,
          totalClaps,
          userClaps,
          isLoading,
          isCounterVisible,
        }) => (
          <>
            <button onClick={handlePress} disabled={isLoading}>
              πŸ‘
            </button>
            {isCounterVisible && <div>Total: {totalClaps}</div>}
            <div>You clapped {userClaps} times</div>
          </>
        )}
      </ClapButton>
    </>
  );
};
Try! β†’
Total claps: 0
You clapped 0 times!

Style your like button templates

Resizing

All button templates can be resized by wrapping them in a container and changing the font-size.

Apply your color scheme and fonts

Lyket uses the theme-ui library, allowing you to provide your own color palette to the buttons through the theme prop in the provider. Colors support rgba, hex, and color names.

import { Provider, LikeButton } from "@lyket/react";

<Provider
	apiKey="acc0dbccce8e557db5ebbe6d605aaa"
	theme={{
		colors: {
			background: "#b8fff3",
			text: "violet",
			primary: "rgba(255, 224, 138, 0.4)"
		}
	}}
>
	<LikeButton
		namespace="my-blog-post"
		id="how-to-beat-me-at-chess"
	/>
</Provider>
	
Click! β†’
Button colors
  • primary - Changes the background color of the "like" button, when user has liked.
  • secondary - Changes the background color of the "dislike" button, when user has disliked.
  • background - Changes the background color of the inactive button.
  • text - Changes the counter text color.
  • icon - Changes the icon's color.
  • highlight - Changes the animation color.
Button fonts
  • body - Changes counter font.

These are the default values:

const defaultTheme = {
	colors: {
		primary: '#22c1c3',
		secondary: '#ff00c3',
		background: 'transparent',
		text: '#292929',
		highlight: '#e095ed',
		icon: '#292929',
	},
	fonts: {
		body: 'inherit',
	},
};

reCAPTCHA

Lyket is integrated with Google reCAPTCHA V3 to handle malicious use without interrupting human users. To enable it you need to provide your Google reCAPTCHA secret key in the user settings in the private area and add your recaptcha site key through the recaptchaSiteKey prop in the Provider. The key will be encrypted.

In this way each time a user interacts with a button an "invisible" reCAPTCHA check will be performed, keeping your website safe.

Be aware that if you only set reCAPTCHA secret key in the user settings while not passing the reCAPTCHA site key in your Provider, the buttons won't work.