Skip to content

Commit ae9d17f

Browse files
authored
Merge pull request #1060 from ShallowRed/develop
Améliore le positionnement de l'infobulle (DsfrTooltip)
2 parents 1d6200b + 99f1bd2 commit ae9d17f

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/components/DsfrTooltip/DsfrTooltip.vue

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ async function computePosition () {
2626
if (typeof document === 'undefined') {
2727
return
2828
}
29+
if (typeof window === 'undefined') {
30+
return
31+
}
2932
if (!show.value) {
3033
return
3134
}
@@ -39,25 +42,26 @@ async function computePosition () {
3942
const tooltipWidth = tooltip.value?.offsetWidth as number
4043
const tooltipTop = tooltip.value?.offsetTop as number
4144
const tooltipLeft = tooltip.value?.offsetLeft as number
42-
const isSourceAtTop = (sourceTop - tooltipHeight) < 0
43-
const isSourceAtBottom = !isSourceAtTop && (sourceTop + sourceHeight + tooltipHeight) >= document.documentElement.offsetHeight
44-
top.value = isSourceAtBottom
45-
const isSourceOnRightSide = (sourceLeft + sourceWidth) >= document.documentElement.offsetWidth
46-
const isSourceOnLeftSide = (sourceLeft + (sourceWidth / 2) - (tooltipWidth / 2)) <= 0
4745
48-
translateY.value = isSourceAtBottom
46+
const isTooltipAtBottom = sourceTop + sourceHeight + tooltipHeight >= window.innerHeight
47+
top.value = isTooltipAtBottom
48+
49+
const isTooltipOnRightSide = (sourceLeft + (sourceWidth / 2) + (tooltipWidth / 2)) >= document.documentElement.offsetWidth
50+
const isTooltipOnLeftSide = (sourceLeft + (sourceWidth / 2) - (tooltipWidth / 2)) < 0
51+
52+
translateY.value = isTooltipAtBottom
4953
? `${sourceTop - tooltipTop - tooltipHeight + 8}px`
5054
: `${sourceTop - tooltipTop + sourceHeight - 8}px`
5155
opacity.value = 1
52-
translateX.value = isSourceOnRightSide
56+
translateX.value = isTooltipOnRightSide
5357
? `${sourceLeft - tooltipLeft + sourceWidth - tooltipWidth - 4}px`
54-
: isSourceOnLeftSide
58+
: isTooltipOnLeftSide
5559
? `${sourceLeft - tooltipLeft + 4}px`
5660
: `${sourceLeft - tooltipLeft + (sourceWidth / 2) - (tooltipWidth / 2)}px`
5761
58-
arrowX.value = isSourceOnRightSide
62+
arrowX.value = isTooltipOnRightSide
5963
? `${(tooltipWidth / 2) - (sourceWidth / 2) + 4}px`
60-
: isSourceOnLeftSide
64+
: isTooltipOnLeftSide
6165
? `${-(tooltipWidth / 2) + (sourceWidth / 2) - 4}px`
6266
: '0px'
6367
}
@@ -66,9 +70,11 @@ watch(show, computePosition, { immediate: true })
6670
6771
onMounted(() => {
6872
window.addEventListener('scroll', computePosition)
73+
window.addEventListener('resize', computePosition)
6974
})
7075
onUnmounted (() => {
7176
window.removeEventListener('scroll', computePosition)
77+
window.removeEventListener('resize', computePosition)
7278
})
7379
7480
const tooltipStyle = computed(() => (`transform: translate(${translateX.value}, ${translateY.value}); --arrow-x: ${arrowX.value}; opacity: ${opacity.value};'`))

0 commit comments

Comments
 (0)