Tether is a great library for positioning stuff (tooltips, modals, hints, etc) in your web app.
But, as I use React, it was pretty problematic for me, as Tether mutates the DOM and React breaks miserably when it sees mutated DOM. The solution is to have the tethered element outside the part of the DOM tree which is controlled by React (in this case, I use document.body
).
That's why I created 2 helpers to use Tether with React.
The first one, TetheredElement
is a plain JS helper to create a new element, attach it to some other one via Tether, and populate it with some React component.
The second one, TetherTarget
is a React component and it uses TetheredElement
to integrate it further with React, so that you can attach components to each other with Tether, without leaving the cozy React/JSX world and worrying about manual DOM operations. Just write:
var tetherOptions = {
// element and target are set automatically
attachment: 'top center',
constraints: [
{to: 'window', pin: true, attachment: 'together'},
],
};
<TetherTarget tethered={ <i>I'm tethered!</i> } tetherOptions={ tetherOptions }>
<h1>Hello world! I'm a big box, which is the Tether target!</h1>
</TetherTarget>
Comments are welcome :)
I'm successfully using Tether with react-portal.