React component for positioning an element aligned to another
npm install react-float-anchor  
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.
The above example can be tried here:
https://streakyc.github.io/react-float-anchor/example/
``ts`
anchor={anchorRef => (
Planets
)}
float={
Mercury
...
}
/>
You can find its code in the example directory. The example may be compiled
by running:
``
yarn
yarn example-buildor use this to auto-rebuild on changes:
yarn example-watch
This module exports the FloatAnchor React component, which takes the
following props:
* anchor must be a function that takes a React ref value ("anchorRef"), andref
returns a React node. The anchorRef value must be passed as the prop toFloatAnchor
an HTML element. The returned node will be placed in the page where the
element was used, with no added wrapper elements around it.anchor
Alternatively, may be a reference to a pre-existing HTMLElement.float
Use this if you want to attach to a non-React HTMLElement on the page.
* must be null, a React node, or a function that returns a React node.anchor
If null, then FloatAnchor won't do anything other than render as-is.float
If non-null, the React node will be rendered in a container div whichposition:fixed
has styling, is attached directly to the document body (orfloat
parentElement), and is positioned to line up with the anchorRef element.
If is a function, then the function will receive the return value fromoptions
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.
* is an optional object of options to control how the float element'szIndex
container is aligned to the anchor element. The options are the same as those
supported by contain-by-screen.
* is an optional number controlling the z-index CSS property of thefloatContainerClassName
float element's container.
* is an optional string specifying a CSS class toparentElement
apply to the float element's container div.
* 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 thefloat
node and then each parentNode, unless the current node is a 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!
Both TypeScript and
Flow type definitions for this module are included!
The type definitions won't require any configuration to use.