mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
fix(frontend): keep help tooltips aligned after scrolling
This commit is contained in:
1 parent
0f66714571
commit
afd87f38be
2 files changed
+70
-10
No files matched your search
@@ -81,8 +81,8 @@ function updatePosition() {
|
||||
if (!el) return
|
||||
const rect = el.getBoundingClientRect()
|
||||
tooltipStyle.value = {
|
||||
top: `${rect.top + window.scrollY}px`,
|
||||
left: `${rect.left + rect.width / 2 + window.scrollX}px`,
|
||||
top: `${rect.top}px`,
|
||||
left: `${rect.left + rect.width / 2}px`,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { enableAutoUnmount, mount } from '@vue/test-utils'
|
||||
import { nextTick } from 'vue'
|
||||
import HelpTooltip from '@/components/common/HelpTooltip.vue'
|
||||
|
||||
enableAutoUnmount(afterEach)
|
||||
|
||||
function getTooltipElement(): HTMLDivElement {
|
||||
const tooltip = document.body.querySelector('[role="tooltip"]')
|
||||
if (!(tooltip instanceof HTMLDivElement)) {
|
||||
@@ -13,6 +15,8 @@ function getTooltipElement(): HTMLDivElement {
|
||||
|
||||
describe('HelpTooltip', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
vi.unstubAllGlobals()
|
||||
document.body.innerHTML = ''
|
||||
})
|
||||
|
||||
@@ -36,8 +40,6 @@ describe('HelpTooltip', () => {
|
||||
await trigger.trigger('mouseleave')
|
||||
await nextTick()
|
||||
expect(tooltip.style.display).toBe('none')
|
||||
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
it('keeps a hover tooltip open while the pointer moves between the trigger and the tooltip', async () => {
|
||||
@@ -66,8 +68,6 @@ describe('HelpTooltip', () => {
|
||||
tooltip.dispatchEvent(new MouseEvent('mouseleave', { relatedTarget: null }))
|
||||
await nextTick()
|
||||
expect(tooltip.style.display).toBe('none')
|
||||
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
it('supports click-to-toggle details and closes on outside click', async () => {
|
||||
@@ -104,7 +104,67 @@ describe('HelpTooltip', () => {
|
||||
document.body.dispatchEvent(new MouseEvent('click', { bubbles: true }))
|
||||
await nextTick()
|
||||
expect(tooltip.style.display).toBe('none')
|
||||
|
||||
wrapper.unmount()
|
||||
})
|
||||
|
||||
it.each([
|
||||
{ scrollX: 120, scrollY: 0 },
|
||||
{ scrollX: 0, scrollY: 240 },
|
||||
{ scrollX: 120, scrollY: 240 },
|
||||
])('uses viewport coordinates when opening with scroll ($scrollX, $scrollY)', async ({ scrollX, scrollY }) => {
|
||||
vi.stubGlobal('scrollX', scrollX)
|
||||
vi.stubGlobal('scrollY', scrollY)
|
||||
const wrapper = mount(HelpTooltip, {
|
||||
attachTo: document.body,
|
||||
props: { content: 'positioned details' },
|
||||
})
|
||||
const trigger = wrapper.get('.group')
|
||||
vi.spyOn(trigger.element, 'getBoundingClientRect').mockReturnValue(new DOMRect(80, 160, 24, 24))
|
||||
|
||||
await trigger.trigger('mouseenter')
|
||||
await nextTick()
|
||||
|
||||
const tooltip = getTooltipElement()
|
||||
expect(tooltip.style.display).not.toBe('none')
|
||||
expect(tooltip.classList.contains('fixed')).toBe(true)
|
||||
expect(tooltip.style.left).toBe('92px')
|
||||
expect(tooltip.style.top).toBe('calc(152px)')
|
||||
})
|
||||
|
||||
it.each(['window scroll', 'nested scroll', 'resize'] as const)(
|
||||
'recalculates viewport coordinates for an open tooltip on %s',
|
||||
async (change) => {
|
||||
const container = document.createElement('div')
|
||||
document.body.appendChild(container)
|
||||
const wrapper = mount(HelpTooltip, {
|
||||
attachTo: container,
|
||||
props: { content: 'moving details', trigger: 'click' },
|
||||
})
|
||||
const trigger = wrapper.get('.group')
|
||||
const getBoundingClientRect = vi.spyOn(trigger.element, 'getBoundingClientRect')
|
||||
.mockReturnValue(new DOMRect(80, 160, 24, 24))
|
||||
|
||||
await trigger.trigger('click')
|
||||
await nextTick()
|
||||
|
||||
const tooltip = getTooltipElement()
|
||||
expect(tooltip.style.left).toBe('92px')
|
||||
expect(tooltip.style.top).toBe('calc(152px)')
|
||||
|
||||
vi.stubGlobal('scrollX', 120)
|
||||
vi.stubGlobal('scrollY', 240)
|
||||
getBoundingClientRect.mockReturnValue(new DOMRect(40, 100, 32, 24))
|
||||
|
||||
if (change === 'nested scroll') {
|
||||
container.dispatchEvent(new Event('scroll', { bubbles: false }))
|
||||
} else {
|
||||
window.dispatchEvent(new Event(change === 'resize' ? 'resize' : 'scroll'))
|
||||
}
|
||||
await nextTick()
|
||||
|
||||
expect(tooltip.style.display).not.toBe('none')
|
||||
expect(getBoundingClientRect).toHaveBeenCalledTimes(2)
|
||||
expect(tooltip.style.left).toBe('56px')
|
||||
expect(tooltip.style.top).toBe('calc(92px)')
|
||||
},
|
||||
)
|
||||
})
|
||||
Reference in new issue
Block a user