Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-float-anchor

StreakYC7.4kMIT3.4.1TypeScript support: included

React component for positioning an element aligned to another

react, react-component, menu, submenu

readme

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:

  • anchor must be a function that takes a React ref value ("anchorRef"), and returns a React node. The anchorRef value must be passed as the ref prop to an HTML element. The returned node will be placed in the page where the FloatAnchor element was used, with no added wrapper elements around it. Alternatively, anchor may be a reference to a pre-existing HTMLElement. Use this if you want to attach to a non-React HTMLElement on the page.
  • float must be null, a React node, or a function that returns a React node. If null, then FloatAnchor won't do anything other than render anchor as-is. If non-null, the float React node will be rendered in a container div which has position:fixed styling, is attached directly to the document body (or parentElement), and is positioned to line up with the anchorRef element. If float is a function, then the function will receive the return value from the most recent call to contain-by-screen, or null if it hasn't been called yet. The initial render will pass null, and then the component will be re-rendered after having been positioned by contain-by-screen.
  • options is an optional object of options to control how the float element's container is aligned to the anchor element. The options are the same as those supported by contain-by-screen.
  • zIndex is an optional number controlling the z-index CSS property of the float element's container.
  • floatContainerClassName is an optional string specifying a CSS class to apply to the float element's container div.
  • parentElement is an optional HTMLElement to attach the float element container div to. This defaults to the page body element.

FloatAnchor has the following static methods:

  • parentNodes(node) takes a DOM node, and returns an iterator that yields the node and then each parentNode, unless the current node is a float element's container div, then its corresponding anchorRef DOM node will be yielded next instead. This is useful when you are listening to events from the entire page and need to determine whether an event's target is logically contained by a React component that has children that use FloatAnchor.

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.

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.

changelog

3.4.1 (2023-09-29)

  • Upgraded to contain-by-screen 2.1.0 to fix issues with large menus not being fit on screen.

3.4.0 (2019-10-22)

  • Added support for passing an HTMLElement as the anchor prop.

3.3.0 (2019-10-22)

  • The choice return value from contain-by-screen is now available if you pass a callback as the float prop.

3.2.0 (2019-10-15)

  • Added the parentElement prop.

3.1.0 (2019-03-28)

  • Added the repositionAsync() method to FloatAnchor. This method should generally be used instead of reposition() because it allows multiple queued repositions to be coalesced together.
  • Fixed multiple O(n^2) issues with nested FloatAnchors that caused them to be repositioned redundantly when the outer FloatAnchors were repositioned.
  • Fixed autoFocus prop not working on input elements and similar components inside of a floated element. The float element container div is now added to the page body before any of the float elements' componentDidMount methods are called.

3.0.0 (2018-10-30)

Breaking Changes

  • React v16.6.0+ is now required.
  • The anchor prop must be a function of anchorRef => React node now.

FloatAnchor v2:

<FloatAnchor
  anchor={
    <div>a</div>
  }
/>

FloatAnchor v3:

<FloatAnchor
  anchor={anchorRef =>
    <div ref={anchorRef}>a</div>
  }
/>

Improvements

  • Removed all usages of the deprecated method React.findDOMNode.
  • No longer uses the legacy Context API.
  • Both anchor and float props may now use any React node rather than only a React element. (You can pass a string now.)

2.2.1 (2018-10-29)

  • Fixed compatibility with Flow v0.84.

2.2.0 (2018-09-24)

  • Added TypeScript type definitions.
  • Removed use of deprecated React method componentWillReceiveProps.

2.1.0 (2018-02-02)

  • Added floatContainerClassName prop #3

2.0.0 (2017-10-02)

Breaking Changes

  • React v16 is now required.
  • FloatAnchor.portal property was removed.

1.5.1 (2018-08-31)

  • Fixed compatibility with Flow v0.80.

1.5.0 (2017-08-23)

  • Fixed compatibility with Flow v0.53.

1.4.3 (2017-07-11)

  • Stopped publishing tests to npm.

1.4.2 (2017-07-11)

  • Fixed IE compatibility #2

1.4.1 (2017-04-25)

  • Fixed a few missed deprecated React.PropTypes usages.

1.4.0 (2017-04-25)

  • Use new prop-types package and stopped using deprecated React.PropTypes.

1.3.7 (2017-03-06)

  • Fixed compatibility with Flow v0.41.

1.3.6 (2017-01-24)

  • Fixed compatibility with Flow v0.38.

1.3.5 (2016-12-09)

  • Fixed an error when importing FloatAnchor in non-browser environments (such as tests).

1.3.4 (2016-09-13)

  • Fixed compatibility with Flow v0.32.

1.3.3 (2016-09-02)

  • Fixed issue with FloatAnchor.parentNodes when called after anchor element changed.

1.3.2 (2016-08-05)

  • (Flow) Re-export the Options type from contain-by-screen to be used by consumers.

1.3.1 (2016-08-05)

  • Fixed compatibility with Flow v0.30.

1.3.0 (2016-06-07)

  • Added FloatAnchor.parentNodes function.

1.2.1 (2016-05-10)

  • Use passive scroll events to allow browser optimizations.

1.2.0 (2016-04-12)

  • Added portal property to component instance.

1.1.0 (2016-04-11)

  • Added zIndex prop.

1.0.1 (2016-04-07)

  • Changed peerDependencies to mark compatibility with React 15.

1.0.0 (2016-03-30)

Initial stable release.