@@ -19,6 +19,8 @@ type NodeLabelProps = {
1919 x ?: number ;
2020 y ?: number ;
2121 position ?: LabelPosition ;
22+ centerLabelOnEdge ?: boolean ;
23+ boxRef ?: React . Ref < SVGRectElement > ;
2224 cornerRadius ?: number ;
2325 status ?: NodeStatus ;
2426 secondaryLabel ?: string ;
@@ -54,6 +56,7 @@ const NodeLabel: React.FunctionComponent<NodeLabelProps> = ({
5456 x = 0 ,
5557 y = 0 ,
5658 position = LabelPosition . bottom ,
59+ centerLabelOnEdge,
5760 secondaryLabel,
5861 status,
5962 badge,
@@ -76,6 +79,7 @@ const NodeLabel: React.FunctionComponent<NodeLabelProps> = ({
7679 actionIcon,
7780 actionIconClassName,
7881 onActionIconClick,
82+ boxRef,
7983 ...other
8084} ) => {
8185 const [ labelHover , labelHoverRef ] = useHover ( ) ;
@@ -123,26 +127,26 @@ const NodeLabel: React.FunctionComponent<NodeLabelProps> = ({
123127 const primaryWidth = iconSpace + badgeSpace + paddingX + textSize . width + actionSpace + contextSpace + paddingX ;
124128 const secondaryWidth = secondaryLabel && secondaryTextSize ? secondaryTextSize . width + 2 * paddingX : 0 ;
125129 const width = Math . max ( primaryWidth , secondaryWidth ) ;
130+ const backgroundHeight =
131+ height + ( secondaryLabel && secondaryTextSize ? secondaryTextSize . height + paddingY * 2 : 0 ) ;
126132
127133 let startX : number ;
128134 let startY : number ;
129135 if ( position === LabelPosition . top ) {
130136 startX = x - width / 2 ;
131- startY = - y - height - paddingY ;
137+ startY = - y - height - ( centerLabelOnEdge ? - backgroundHeight / 2 : paddingY ) ;
132138 } else if ( position === LabelPosition . right ) {
133- startX = x + iconSpace ;
139+ startX = x + iconSpace - ( centerLabelOnEdge ? width / 2 : 0 ) ;
134140 startY = y - height / 2 ;
135141 } else if ( position === LabelPosition . left ) {
136- startX = - width - paddingX ;
137- startY = y - height / 2 + paddingY ;
142+ startX = centerLabelOnEdge ? x - width / 2 : - width - paddingX ;
143+ startY = y - height / 2 ;
138144 } else {
139145 startX = x - width / 2 + iconSpace / 2 ;
140- startY = y ;
146+ startY = y - ( centerLabelOnEdge ? backgroundHeight / 2 : 0 ) ;
141147 }
142148 const actionStartX = iconSpace + badgeSpace + paddingX + textSize . width + paddingX ;
143149 const contextStartX = actionStartX + actionSpace ;
144- const backgroundHeight =
145- height + ( secondaryLabel && secondaryTextSize ? secondaryTextSize . height + paddingY * 2 : 0 ) ;
146150 let badgeStartX = 0 ;
147151 let badgeStartY = 0 ;
148152 if ( badgeSize ) {
@@ -183,6 +187,7 @@ const NodeLabel: React.FunctionComponent<NodeLabelProps> = ({
183187 contextSize ,
184188 secondaryLabel ,
185189 secondaryTextSize ,
190+ centerLabelOnEdge ,
186191 position ,
187192 x ,
188193 y
@@ -200,8 +205,9 @@ const NodeLabel: React.FunctionComponent<NodeLabelProps> = ({
200205 < NodeShadows />
201206 { textSize && (
202207 < rect
208+ ref = { boxRef }
203209 className = { css ( styles . topologyNodeLabelBackground ) }
204- key = { `rect-${ filterId } ` } // update key to force remount on filter update
210+ key = { `rect-${ filterId } - ${ width } ` } // update key to force remount on filter or size update
205211 filter = { filterId && createSvgIdUrl ( filterId ) }
206212 x = { 0 }
207213 y = { 0 }
0 commit comments