Fetch boundingClientRects by selector for dangerouslySetInnerHTML
This is helpful for building isolated UIs on top of content within dangerouslySetInnerHTML
.
import BoundingClientRects from 'react-boundingclientrects'
...
<BoundingClientRects
dangerouslySetInnerHTML={sanitizedHTML}
selector='li'
onTargets={targets => setState({targets: targets})} />
This is required. See React's documentaion.
This selector will be used to query DOM elements. It is these elements that will be surfaced as targets.
Alternatively, multiple selectors can be provided in a selectors
array.
selectors=['.one', '.two', '.red', '.blue']
onTargets
will be called with an array of targets. Targets contain the calculated position based on this components main DOM node.
// Calculation example
target.boundingClientRects().top - mainNode.boundingClientRects().top
// Example target
{
selector: 'li',
el: <node>,
width: 1022,
height: 18,
top: 119.875,
right: 1102,
bottom: 137,
left: 80
}