1.0.24 • Published 7 months ago

use-ripple-hook v1.0.24

Weekly downloads
-
License
ISC
Repository
github
Last release
7 months ago

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

PropertyDescriptionTypeDefaultOptional
durationDuration in millisecondsnumber450✔️
colorColor of the ripple effectstringrgba(255, 255, 255, .3)✔️
cancelAutomaticallyIf true, the ripple will begin to cancel after 40% of the durationbooleanfalse✔️
classNameThe ripple element's class namestring__useRipple--ripple✔️
containerClassNameThe container element for the ripplesstring__useRipple--ripple-container✔️
ignoreNonLeftClickIf false, non left click events such as right click and middle click will also trigger ripplesbooleantrue✔️
timingFunctionTransition timing function of the transform animationstringcubic-bezier(.42,.36,.28,.88)✔️
disabledIf true, no ripples will be spawnedbooleanfalse✔️
refOptional outside ref, if unset, internal ref will be usedReact.RefObject<T>undefined✔️
onSpawnA callback which is triggered when a ripple is spawnedoptions.onspawnundefined✔️

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.

1.0.24

7 months ago

1.0.23

7 months ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago