StreakYC / react-float-anchor

React component for positioning an element aligned to another
MIT License
49 stars 5 forks source link

react-float-anchor

GitHub license npm version Circle CI

This is a React component for anchoring a fixed position element, such as a dropdown menu, to the edge of an element on the page. The fixed position element will automatically be placed so that it fits on the screen if possible, and it will automatically reposition if needed when the user scrolls.

Example

The above example can be tried here:

https://streakyc.github.io/react-float-anchor/example/

<FloatAnchor
  options={{ position: 'right', vAlign: 'top', hAlign: 'left' }}
  anchor={anchorRef => (
    <div ref={anchorRef} style={{ border: '1px dashed gray', display: 'inline-block' }}>
      Planets
    </div>
  )}
  float={
    <div style={{ border: '1px solid black', background: 'white' }}>
      Mercury
      ...
    </div>
  }
/>

You can find its code in the example directory. The example may be compiled by running:

yarn
yarn example-build
# or use this to auto-rebuild on changes:
yarn example-watch

FloatAnchor

This module exports the FloatAnchor React component, which takes the following props:

FloatAnchor has the following static methods:

The FloatAnchor component has a repositionAsync() method, which you should call if you change the size of the contents of the anchor or float elements.

There's also a reposition() method which you can call if you need the component to be repositioned synchronously. This method should not be used unless necessary. The async version of this method is able to coalesce redundant queued reposition calls together.

The container div of the float element has its rfaAnchor property set to be equal to the anchorRef DOM element.

Related

If you want interactive dropdown menus, check out the react-menu-list module that is built with this!

Types

Both TypeScript and Flow type definitions for this module are included! The type definitions won't require any configuration to use.