-
Notifications
You must be signed in to change notification settings - Fork 715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Annotation label <foreignobject> render #1383
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import React, { useContext, useMemo } from 'react'; | ||
import cx from 'classnames'; | ||
import useMeasure from 'react-use-measure'; | ||
import Group from '@visx/group/lib/Group'; | ||
import AnnotationContext from '../context/AnnotationContext'; | ||
import AnchorLine from './LabelAnchorLine'; | ||
import { LabelProps } from './Label'; | ||
|
||
const wrapperStyle = { display: 'inline-block' }; | ||
|
||
export type HtmlLabelProps = Pick< | ||
LabelProps, | ||
| 'anchorLineStroke' | ||
| 'className' | ||
| 'horizontalAnchor' | ||
| 'resizeObserverPolyfill' | ||
| 'showAnchorLine' | ||
| 'verticalAnchor' | ||
| 'x' | ||
| 'y' | ||
> & { | ||
/** Pass in a custom element as the label to style as you like. Renders inside a <foreignObject>, be aware that most non-browser SVG renderers will not render HTML <foreignObject>s. See: https://github.com/airbnb/visx/issues/1173#issuecomment-1014380545. */ | ||
children?: React.ReactNode; | ||
}; | ||
export default function HtmlLabel({ | ||
anchorLineStroke = '#222', | ||
children, | ||
className, | ||
horizontalAnchor: propsHorizontalAnchor, | ||
resizeObserverPolyfill, | ||
showAnchorLine = true, | ||
verticalAnchor: propsVerticalAnchor, | ||
x: propsX, | ||
y: propsY, | ||
}: HtmlLabelProps) { | ||
// we must measure the rendered title + subtitle to compute container height | ||
const [labelRef, titleBounds] = useMeasure({ | ||
polyfill: resizeObserverPolyfill, | ||
}); | ||
const { width, height } = titleBounds; | ||
|
||
// if props are provided, they take precedence over context | ||
const { x = 0, y = 0, dx = 0, dy = 0 } = useContext(AnnotationContext); | ||
|
||
// offset container position based on horizontal + vertical anchor | ||
const horizontalAnchor = | ||
propsHorizontalAnchor || (Math.abs(dx) < Math.abs(dy) ? 'middle' : dx > 0 ? 'start' : 'end'); | ||
const verticalAnchor = | ||
propsVerticalAnchor || (Math.abs(dx) > Math.abs(dy) ? 'middle' : dy > 0 ? 'start' : 'end'); | ||
|
||
const containerCoords = useMemo(() => { | ||
let adjustedX: number = propsX == null ? x + dx : propsX; | ||
let adjustedY: number = propsY == null ? y + dy : propsY; | ||
|
||
if (horizontalAnchor === 'middle') adjustedX -= width / 2; | ||
if (horizontalAnchor === 'end') adjustedX -= width; | ||
if (verticalAnchor === 'middle') adjustedY -= height / 2; | ||
if (verticalAnchor === 'end') adjustedY -= height; | ||
|
||
return { x: adjustedX, y: adjustedY }; | ||
}, [propsX, x, dx, propsY, y, dy, horizontalAnchor, verticalAnchor, width, height]); | ||
|
||
return ( | ||
<Group | ||
top={containerCoords.y} | ||
left={containerCoords.x} | ||
pointerEvents="none" | ||
className={cx('visx-annotationlabel', className)} | ||
> | ||
{showAnchorLine && ( | ||
<AnchorLine | ||
anchorLineOrientation={Math.abs(dx) > Math.abs(dy) ? 'vertical' : 'horizontal'} | ||
anchorLineStroke={anchorLineStroke} | ||
verticalAnchor={verticalAnchor} | ||
horizontalAnchor={horizontalAnchor} | ||
width={width} | ||
height={height} | ||
/> | ||
)} | ||
<foreignObject width={width} height={height} overflow="visible"> | ||
<div ref={labelRef} style={wrapperStyle}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when I was playing with this I found myself adding a bunch of styles to a |
||
{children} | ||
</div> | ||
</foreignObject> | ||
</Group> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
import { TextProps } from '@visx/text'; | ||
|
||
interface AnchorLineProps { | ||
anchorLineOrientation: 'horizontal' | 'vertical'; | ||
verticalAnchor: TextProps['verticalAnchor']; | ||
horizontalAnchor: TextProps['textAnchor']; | ||
anchorLineStroke: string; | ||
width: number; | ||
height: number; | ||
} | ||
|
||
export default function AnchorLine({ | ||
anchorLineOrientation, | ||
anchorLineStroke, | ||
verticalAnchor, | ||
horizontalAnchor, | ||
width, | ||
height, | ||
}: AnchorLineProps) { | ||
const backgroundOutline = { stroke: anchorLineStroke, strokeWidth: 2 }; | ||
|
||
return ( | ||
<> | ||
{anchorLineOrientation === 'horizontal' && verticalAnchor === 'start' && ( | ||
<line {...backgroundOutline} x1={0} x2={width} y1={0} y2={0} /> | ||
)} | ||
{anchorLineOrientation === 'horizontal' && verticalAnchor === 'end' && ( | ||
<line {...backgroundOutline} x1={0} x2={width} y1={height} y2={height} /> | ||
)} | ||
{anchorLineOrientation === 'vertical' && horizontalAnchor === 'start' && ( | ||
<line {...backgroundOutline} x1={0} x2={0} y1={0} y2={height} /> | ||
)} | ||
{anchorLineOrientation === 'vertical' && horizontalAnchor === 'end' && ( | ||
<line {...backgroundOutline} x1={width} x2={width} y1={0} y2={height} /> | ||
)} | ||
</> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ describe('<Label />', () => { | |
it('should render title Text', () => { | ||
expect( | ||
shallow(<Label title="title test" resizeObserverPolyfill={ResizeObserver} />) | ||
.dive() | ||
.children() | ||
.find(Text) | ||
.prop('children'), | ||
|
@@ -25,6 +26,7 @@ describe('<Label />', () => { | |
resizeObserverPolyfill={ResizeObserver} | ||
/>, | ||
) | ||
.dive() | ||
.children() | ||
.find(Text) | ||
.at(1) | ||
|
@@ -33,16 +35,18 @@ describe('<Label />', () => { | |
}); | ||
it('should render a background', () => { | ||
expect( | ||
shallow( | ||
<Label title="title test" showBackground resizeObserverPolyfill={ResizeObserver} />, | ||
).find('rect'), | ||
shallow(<Label title="title test" showBackground resizeObserverPolyfill={ResizeObserver} />) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🙏 thanks for fixing these |
||
.dive() | ||
.find('rect'), | ||
).toHaveLength(1); | ||
}); | ||
it('should render an anchor line', () => { | ||
expect( | ||
shallow( | ||
<Label title="title test" showAnchorLine resizeObserverPolyfill={ResizeObserver} />, | ||
).find('line'), | ||
shallow(<Label title="title test" showAnchorLine resizeObserverPolyfill={ResizeObserver} />) | ||
.dive() | ||
.find('AnchorLine') | ||
.dive() | ||
.find('line'), | ||
).toHaveLength(1); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one thing I noticed while playing with this is that the line renders behind the HTML, do you think we should flip this and render the anchor line after
foreignObject
?