1.0.3 • Published 3 years ago

@tracksuitdev/use-ripple v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

useRipple

Hook that generates positions and sizes of "ripples", allows you to easily recreate material UI ripple effect.

Returns array of objects containing width, height, left and top style properties for each ripple, and onClick function that generates ripples.

Also includes ripple animation and default styles for ripple parent and ripple. You can use these styles (don't forget to specify ripple background-color yourself) or make your own, just be sure to adjust the duration parameter of useRipple hook if your custom animation has different duration.

Usage

Usage with default styles

import "use-ripple/index.css" // import this for ripple styles

const App = () => {
  const { styles, onClick } = useRipple();

  return (
    <button className="ripple-parent" onClick={onClick}>
      Ripple
      {styles?.map((style, index) => (
        <span key={index} className="ripple ripple-color" style={style} />
      ))}
    </button>
  );
};

Create css class for background color of ripple, or use something else to apply your preffered color.

.ripple-color {
  background-color: blue;
}

Usage with custom styles

Provide your animation duration in ms as parameter to useRipple if you use animation with duration different than the default duration (600ms). Also for ripples to render properly, parent node position must be relative and overflow has to be hidden and position of the ripple node must be absolute.

const { styles, onClick } = useRipple({ duration: CUSTOM_ANIMATION_DURATION })

Usage with onClick handler

const { styles, onClick } = useRipple();
const handleClick = (e) => {
  doSomething();
  onClick(e)
}

or pass click handler to useRipple and use returned value

const handleClick = (e) => {
  doSomething();
}
const { styles, onClick } = useRipple({ handleClick });

return <button onClick={onClick} ...

Props

nametypedefaultdescription
durationnumber600duration of ripple animation in miliseconds
limitnumber100maximum number of ripples to render (each click renders new ripple), use negative value for unlimited number of ripples
handleClickMouseEventHandlerundefinedonClick handler of component that renders ripple, it will be wrapped in onClick function returned from useRipple

Return value

nametypedescription
stylesPick<CSSProperties, "width" | "height" | "left" | "top">[]size and position for each ripple
onClickMouseEventHandleronClick handler that generates ripples
1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago