11import React , {
22 useState ,
33 useEffect ,
4- createRef ,
54 useLayoutEffect ,
65 Children ,
76 ReactElement ,
@@ -165,7 +164,10 @@ export const Tooltip: React.FC<TooltipProps | ExpandedTooltipProps> = ({
165164
166165 const [ debouncedVisible , setDebouncedVisible ] = useState ( visible )
167166 const [ hasOverflow , setHasOverflow ] = useState ( false )
168- const tooltipRef = createRef < HTMLDivElement > ( )
167+ const [ horizontalLayout , setHorizontalLayout ] = useState <
168+ 'left' | 'right' | 'center'
169+ > ( 'center' )
170+ const [ tooltipEl , setTooltipEl ] = useState < HTMLDivElement | null > ( null )
169171
170172 useEffect ( ( ) => {
171173 const delayVisible = ( ) => setDebouncedVisible ( visible )
@@ -188,17 +190,24 @@ export const Tooltip: React.FC<TooltipProps | ExpandedTooltipProps> = ({
188190 } , [ anchorEl , hide , show ] )
189191
190192 useLayoutEffect ( ( ) => {
191- if ( tooltipRef . current === null || anchorEl === null ) {
193+ if ( tooltipEl === null || anchorEl === null ) {
192194 return
193195 }
194- const tooltipEl = tooltipRef . current
195196
196197 const { bottom } = anchorEl . getBoundingClientRect ( )
198+
197199 const bottomSpace = document . documentElement . clientHeight - bottom
198200 // See if `bottomSpace` is smaller than Tooltip height.
199- // "8" is margin of the TooltipWapper .
201+ // "8" is margin of the TooltipWrapper .
200202 setHasOverflow ( tooltipEl . clientHeight + 8 > bottomSpace )
201- } , [ anchorEl , tooltipRef , hide , show ] )
203+
204+ const { left, right } = tooltipEl . getBoundingClientRect ( )
205+ if ( left < 0 ) {
206+ setHorizontalLayout ( 'left' )
207+ } else if ( right > document . documentElement . clientWidth ) {
208+ setHorizontalLayout ( 'right' )
209+ }
210+ } , [ anchorEl , tooltipEl ] )
202211
203212 if ( props . variant !== 'expanded' ) {
204213 return (
@@ -209,13 +218,13 @@ export const Tooltip: React.FC<TooltipProps | ExpandedTooltipProps> = ({
209218 { debouncedVisible ? (
210219 < PopOver
211220 anchorEl = { anchorEl }
212- horizontalPosition = "center"
213- horizontalAlignment = "center"
221+ horizontalPosition = { horizontalLayout }
222+ horizontalAlignment = { horizontalLayout }
214223 verticalPosition = { hasOverflow ? 'top' : 'bottom' }
215224 verticalAlignment = { hasOverflow ? 'bottom' : 'top' }
216225 { ...props }
217226 >
218- < TooltipWrapper ref = { tooltipRef } >
227+ < TooltipWrapper ref = { setTooltipEl } >
219228 < Typography variant = "chip-tag-text" > { props . text } </ Typography >
220229 </ TooltipWrapper >
221230 </ PopOver >
@@ -233,13 +242,13 @@ export const Tooltip: React.FC<TooltipProps | ExpandedTooltipProps> = ({
233242 < >
234243 < PopOver
235244 anchorEl = { anchorEl }
236- horizontalPosition = "center"
237- horizontalAlignment = "center"
245+ horizontalPosition = { horizontalLayout }
246+ horizontalAlignment = { horizontalLayout }
238247 verticalPosition = { hasOverflow ? 'top' : 'bottom' }
239248 verticalAlignment = { hasOverflow ? 'bottom' : 'top' }
240249 { ...props }
241250 >
242- < ExpandedTooltipWrapper ref = { tooltipRef } >
251+ < ExpandedTooltipWrapper ref = { setTooltipEl } >
243252 { props . extraInfo !== undefined ? (
244253 < ExpandedTooltipTop >
245254 < ExpandedTooltipTitle > { props . tipTitle } </ ExpandedTooltipTitle >
0 commit comments