mirror of https://github.com/grafana/grafana
VizTooltip: Use previous positioning calculation (#36861)
* VizTooltip: Use previous positioning calculation * VizTooltip: Don't need to check for tooltip width/height here * VizTooltip: Disable pointer-events in the mixin * VizTooltipContainer: Move pointerEvents to inline style to prevent breaking AnnotationMarker * Move comment to correct place * No need to change thispull/36956/head
parent
ff56ea6ea6
commit
4f315bf48f
@ -0,0 +1,165 @@ |
||||
import { calculateTooltipPosition } from './utils'; |
||||
|
||||
describe('utils', () => { |
||||
describe('calculateTooltipPosition', () => { |
||||
// let's pick some easy numbers for these, we shouldn't need to change them
|
||||
const tooltipWidth = 100; |
||||
const tooltipHeight = 100; |
||||
const xOffset = 10; |
||||
const yOffset = 10; |
||||
const windowWidth = 200; |
||||
const windowHeight = 200; |
||||
|
||||
it('sticky positions the tooltip to the right if it would overflow at both ends but overflow to the left more', () => { |
||||
const xPos = 99; |
||||
const yPos = 50; |
||||
const result = calculateTooltipPosition( |
||||
xPos, |
||||
yPos, |
||||
tooltipWidth, |
||||
tooltipHeight, |
||||
xOffset, |
||||
yOffset, |
||||
windowWidth, |
||||
windowHeight |
||||
); |
||||
expect(result).toEqual({ |
||||
x: 90, |
||||
y: 60, |
||||
}); |
||||
}); |
||||
|
||||
it('sticky positions the tooltip to the left if it would overflow at both ends but overflow to the right more', () => { |
||||
const xPos = 101; |
||||
const yPos = 50; |
||||
const result = calculateTooltipPosition( |
||||
xPos, |
||||
yPos, |
||||
tooltipWidth, |
||||
tooltipHeight, |
||||
xOffset, |
||||
yOffset, |
||||
windowWidth, |
||||
windowHeight |
||||
); |
||||
expect(result).toEqual({ |
||||
x: 10, |
||||
y: 60, |
||||
}); |
||||
}); |
||||
|
||||
it('positions the tooltip to left of the cursor if it would overflow right', () => { |
||||
const xPos = 150; |
||||
const yPos = 50; |
||||
const result = calculateTooltipPosition( |
||||
xPos, |
||||
yPos, |
||||
tooltipWidth, |
||||
tooltipHeight, |
||||
xOffset, |
||||
yOffset, |
||||
windowWidth, |
||||
windowHeight |
||||
); |
||||
expect(result).toEqual({ |
||||
x: 40, |
||||
y: 60, |
||||
}); |
||||
}); |
||||
|
||||
it('positions the tooltip to the right of the cursor if it would not overflow', () => { |
||||
const xPos = 50; |
||||
const yPos = 50; |
||||
const result = calculateTooltipPosition( |
||||
xPos, |
||||
yPos, |
||||
tooltipWidth, |
||||
tooltipHeight, |
||||
xOffset, |
||||
yOffset, |
||||
windowWidth, |
||||
windowHeight |
||||
); |
||||
expect(result).toEqual({ |
||||
x: 60, |
||||
y: 60, |
||||
}); |
||||
}); |
||||
|
||||
it('sticky positions the tooltip to the bottom if it would overflow at both ends but overflow to the top more', () => { |
||||
const xPos = 50; |
||||
const yPos = 99; |
||||
const result = calculateTooltipPosition( |
||||
xPos, |
||||
yPos, |
||||
tooltipWidth, |
||||
tooltipHeight, |
||||
xOffset, |
||||
yOffset, |
||||
windowWidth, |
||||
windowHeight |
||||
); |
||||
expect(result).toEqual({ |
||||
x: 60, |
||||
y: 90, |
||||
}); |
||||
}); |
||||
|
||||
it('sticky positions the tooltip to the top if it would overflow at both ends but overflow to the bottom more', () => { |
||||
const xPos = 50; |
||||
const yPos = 101; |
||||
const result = calculateTooltipPosition( |
||||
xPos, |
||||
yPos, |
||||
tooltipWidth, |
||||
tooltipHeight, |
||||
xOffset, |
||||
yOffset, |
||||
windowWidth, |
||||
windowHeight |
||||
); |
||||
expect(result).toEqual({ |
||||
x: 60, |
||||
y: 10, |
||||
}); |
||||
}); |
||||
|
||||
it('positions the tooltip above the cursor if it would overflow at the bottom', () => { |
||||
const xPos = 50; |
||||
const yPos = 150; |
||||
const result = calculateTooltipPosition( |
||||
xPos, |
||||
yPos, |
||||
tooltipWidth, |
||||
tooltipHeight, |
||||
xOffset, |
||||
yOffset, |
||||
windowWidth, |
||||
windowHeight |
||||
); |
||||
expect(result).toEqual({ |
||||
x: 60, |
||||
y: 40, |
||||
}); |
||||
}); |
||||
|
||||
it('positions the tooltip below the cursor if it would not overflow', () => { |
||||
const xPos = 50; |
||||
const yPos = 50; |
||||
const result = calculateTooltipPosition( |
||||
xPos, |
||||
yPos, |
||||
tooltipWidth, |
||||
tooltipHeight, |
||||
xOffset, |
||||
yOffset, |
||||
windowWidth, |
||||
windowHeight |
||||
); |
||||
expect(result).toEqual({ |
||||
x: 60, |
||||
y: 60, |
||||
}); |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,40 @@ |
||||
export const calculateTooltipPosition = ( |
||||
xPos = 0, |
||||
yPos = 0, |
||||
tooltipWidth = 0, |
||||
tooltipHeight = 0, |
||||
xOffset = 0, |
||||
yOffset = 0, |
||||
windowWidth = 0, |
||||
windowHeight = 0 |
||||
) => { |
||||
let x = xPos; |
||||
let y = yPos; |
||||
|
||||
const overflowRight = Math.max(xPos + xOffset + tooltipWidth - (windowWidth - xOffset), 0); |
||||
const overflowLeft = Math.abs(Math.min(xPos - xOffset - tooltipWidth - xOffset, 0)); |
||||
const wouldOverflowRight = overflowRight > 0; |
||||
const wouldOverflowLeft = overflowLeft > 0; |
||||
|
||||
const overflowBelow = Math.max(yPos + yOffset + tooltipHeight - (windowHeight - yOffset), 0); |
||||
const overflowAbove = Math.abs(Math.min(yPos - yOffset - tooltipHeight - yOffset, 0)); |
||||
const wouldOverflowBelow = overflowBelow > 0; |
||||
const wouldOverflowAbove = overflowAbove > 0; |
||||
|
||||
if (wouldOverflowRight && wouldOverflowLeft) { |
||||
x = overflowRight > overflowLeft ? xOffset : windowWidth - xOffset - tooltipWidth; |
||||
} else if (wouldOverflowRight) { |
||||
x = xPos - xOffset - tooltipWidth; |
||||
} else { |
||||
x = xPos + xOffset; |
||||
} |
||||
|
||||
if (wouldOverflowBelow && wouldOverflowAbove) { |
||||
y = overflowBelow > overflowAbove ? yOffset : windowHeight - yOffset - tooltipHeight; |
||||
} else if (wouldOverflowBelow) { |
||||
y = yPos - yOffset - tooltipHeight; |
||||
} else { |
||||
y = yPos + yOffset; |
||||
} |
||||
return { x, y }; |
||||
}; |
Loading…
Reference in new issue