Giter Club home page Giter Club logo

react-stay-in-view's Introduction

react-stay-in-view

Version Badge NPM license PRs Welcome GZipped size Downloads NPM total downloads

A react hook and component to create popper (popover) that never leave the screen. Created elements will not disappear from the screen when scrolling and resizing.

Features

  • ๐Ÿš€ Fast - Built with hooks and functional components only.
  • ๐Ÿ›  Written in TypeScript - It'll fit right into your existing TypeScript project.
  • ๐Ÿ‘ซ Cross-browser - Works out-of-the-box for most browsers, regardless of version.
  • ๐Ÿ“ฒ Mobile-friendly - Supports mobile devices and touch screens.
  • ๐ŸŒณ Tree-shakeable - Only include the parts you use.
  • ๐Ÿ—œ Lightweight - Around ~1.1kB.
  • ๐Ÿ’จ No dependencies

Try it live:
Try it live

Preview

Install

  # with npm
  npm install --save react-stay-in-view
  # with yarn
  yarn add react-stay-in-view

Usage

useStayInView hook

import { useStayInView } from 'react-stay-in-view';

const StayInView = ({ className, children, anchorEl, placement }) => {
  const { ref } = useStayInView({ anchorEl, placement });

  return (
    <div ref={ref} className={className}>
      {children}
    </div>
  );
};

Props

Output: ref - paste this ref in your component

Name Description Type Required Default Value
anchorEl An element relative to which the new position will be fixed HTMLElement null
placement Element position relative to anchorEl string right-start
avoidAnchorOverlap Prevents anchorEl from overlapping when scrolling to screen border boolean true

StayInView component

import { StayInView } from 'react-stay-in-view';

const App = () => {
  const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null);
  const [isVisible, setIsVisible] = useState<boolean>(false);

  const onClick = (e: React.MouseEvent<HTMLButtonElement>) => {
    setAnchorEl(e.currentTarget);
    setIsVisible((prev) => !prev);
  };

  return (
    <div>
      {isVisible && (
        <StayInView anchorEl={anchorEl}>
          <div>I stay on the screen</div>
        </StayInView>
      )}
      <button onClick={onClick}>Click me!</button>
    </div>
  );
};

Props

Name Description Type Required Default Value
children A children element ReactNode โœ…
className Container class name. string null
anchorEl An element relative to which the new position will be fixed HTMLElement null
placement Element position relative to anchorEl string right-start
avoidAnchorOverlap Prevents anchorEl from overlapping when scrolling to screen border boolean true

Contributions

Learn how to contribute

License

MIT ยฉ vdmrgv

react-stay-in-view's People

Contributors

dependabot[bot] avatar vdmrgv avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.