Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

arthurdenner/use-lax

Repository files navigation

use-lax

npm All Contributors

React hook to use with lax.js.

Usage

import React from 'react';
import { useLax, useLaxElement } from 'use-lax';

function App() {
  const [showBubble, setBubble] = useState(false);
  const toggleBubble = () => setBubble(!showBubble);

  // Use once in the top level element
  // to configure drivers and initial elements
  // https://github.com/alexfoxy/lax.js#setup
  useLax({
    drivers: [
      {
        name: 'scrollY',
        getValueFn: () => window.scrollY,
      },
    ],
  });

  return (
    <div>
      <button className="toggle-bubble" onClick={toggleBubble}>
        Toggle Bubble
      </button>
      <p>{showBubble ? '...now scroll down...' : '^ press the button ^'}</p>
      {showBubble ? <Bubble /> : null}
    </div>
  );
}

function Bubble() {
  // Use it on every component added dynamically
  // and provide the animation driven from the drivers
  const ref = useLaxElement({
    animationData: {
      scrollY: {
        presets: ['fadeInOut:200:0'],
        translateX: [
          [0, 'screenHeight'],
          [0, 'screenWidth'],
        ],
      },
    },
  });

  return <div ref={ref} className="bubble" />;
}

Contributors

Thanks goes to these wonderful people (emoji key):


Arthur Denner

💻 🎨 📖 💡 🤔 🚧

Antoine Martin

💻

This project follows the all-contributors specification. Contributions of any kind welcome!