npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-ripple-hook

v1.0.24

Published

Customizable, lightweight React hook for implementing Google's Material UI style ripple effect

Downloads

5,207

Readme

useRipple - Material UI ripple effect

Fully customizable, lightweight React hook for implementing Google's Material UI style ripple effect

useRipple showcase GIF

Edit great-nash-zhyfm

Installation

npm install use-ripple-hook

or

yarn add use-ripple-hook

Usage

import React from "react";
import useRipple from "use-ripple-hook";

function Button() {
    const [ripple, event] = useRipple();

    return (
        <button ref={ripple} onPointerDown={event}>
            Default Ripple
        </button>
    );
}

Options

Default options

useRipple({
    duration: 450,
    color: "rgba(255, 255, 255, .3)",
    cancelAutomatically: false,
    className: "__useRipple--ripple",
    containerClassName: "__useRipple--ripple-container",
    ignoreNonLeftClick: true,
    timingFunction: "cubic-bezier(.42,.36,.28,.88)",
    disabled: false,
    ref: internalRef,
    onSpawn: undefined,
});

Options reference

| Property | Description | Type | Default | Optional | | --------------------- | ------------------------------------------------------------------------------------------------ | ---------------------------------- | ------------------------------- | -------- | | duration | Duration in milliseconds | number | 450 | ✔️ | | color | Color of the ripple effect | string | rgba(255, 255, 255, .3) | ✔️ | | cancelAutomatically | If true, the ripple will begin to cancel after 40% of the duration | boolean | false | ✔️ | | className | The ripple element's class name | string | __useRipple--ripple | ✔️ | | containerClassName | The container element for the ripples | string | __useRipple--ripple-container | ✔️ | | ignoreNonLeftClick | If false, non left click events such as right click and middle click will also trigger ripples | boolean | true | ✔️ | | timingFunction | Transition timing function of the transform animation | string | cubic-bezier(.42,.36,.28,.88) | ✔️ | | disabled | If true, no ripples will be spawned | boolean | false | ✔️ | | ref | Optional outside ref, if unset, internal ref will be used | React.RefObject<T> | undefined | ✔️ | | onSpawn | A callback which is triggered when a ripple is spawned | options.onspawn | undefined | ✔️ |

options.onSpawn

Type

type OnSpawnCB = (ctx: {
    /** the ripple element */
    readonly ripple: HTMLDivElement;

    /** cancels the current ripple animation */
    readonly cancelRipple: () => void;

    /** the ref to the ripple host element */
    readonly ref: React.RefObject<T>;

    /** the event that triggered the ripple (ts: casting required) */
    readonly event: unknown;

    /** the ripple container element */
    readonly container: HTMLDivElement;
}) => void;

Example

useRipple({
  /* ... */
  onSpawn: ({
    ripple, ref, event, container
  }) => {
    console.table({ ripple, ref, event, container });
  }
});

Perfect circle

As demonstrated in the below GIF, useRipple adjusts the circle size to always for the host element's border box.

useRipple showcase GIF

Higher order function (HOF)

If you want to memoize a configuration for your ripple you can use the built in customRipple() function.

You can override the options you memoized for your custom ripple hook. The two options will be merged.

Usage

import { customRipple } from "use-ripple-hook";

const useMyRipple = customRipple({
    color: "rgb(144, 238, 144, .7)",
    duration: 700,
});

function Button() {
    const [ripple, event] = useMyRipple({}); // Optionally override previous config

    return (
        <button ref={ripple} onPointerDown={event}>
            Memoized Ripple
        </button>
    );
}

This is useful if you want to avoid repetition in your code or if you want multiple different ripple effects for different components.

Examples

For examples of useRipple usage please click here.

Dos and don'ts

✔ Do this:

Using components 👍

import React from "react";
import useRipple from "use-ripple-hook";

function App() {
    return (
        <>
            <Button color="red" />
            <Button color="yellow" />
        </>
    )
}

function Button({ color }) {
    const [ripple, event] = useRipple({ color });

    return (
        <button ref={ripple} onPointerDown={event}>
            Button
        </button>
    );
}

❌ Don't do this:

Sharing references 👎

import React from "react";
import useRipple from "use-ripple-hook";

function App() {
    const [ripple, event] = useRipple();

    /* This will NOT work! Do not do this */
    return (
        <>
            <button color="red" ref={ripple} onPointerDown={event}>
                Button
            </button>
            <button color="yellow" ref={ripple} onPointerDown={event}>
                Button
            </button>
        </>
    )
}

Contributing

Contributions of any form are appreciated, opening issues on the Github repository as well as creating pull requests are both welcomed for anyone to do.