diff --git a/src/components/Heatmap.ts b/src/components/Heatmap.ts index bb00202..84d8f7b 100644 --- a/src/components/Heatmap.ts +++ b/src/components/Heatmap.ts @@ -4,7 +4,11 @@ import { StreamerSummary } from '@/types'; import { getYearMonthDateString } from '@/utils/date-to-year-month-date'; import { parseHtml } from '@/utils/domParser'; -function block(level: number, tooltip?: HTMLElement) { +function block( + level: number, + tooltip?: HTMLElement, + tooltipWrapper?: HTMLElement, +) { const parser = new DOMParser(); const block = parser.parseFromString( `
`, 'text/html', - ).body.children[0]; - - if (tooltip) { - block.addEventListener('mouseover', () => { - const { left, width, top } = block.getBoundingClientRect(); - tooltip.style.position = 'absolute'; - tooltip.style.left = left + width / 2 + 'px'; - tooltip.style.top = top + 'px'; - const translate = 'translate(-50%, calc(-100% - 3px))'; - tooltip.style.transform = translate; - document.body.append(tooltip); - tooltip.animate( - [ - { - opacity: '0', - transform: translate + ' scale(0.85)', - }, - { - opacity: '1', - transform: translate + ' scale(1)', - }, - ], - { - duration: 200, - easing: 'ease-out', - }, - ); - }); + ).body.children[0] as HTMLElement; + if (tooltip && tooltipWrapper) { + block.addEventListener('mouseover', () => + showTooltip(block, tooltip, tooltipWrapper), + ); block.addEventListener('mouseleave', () => tooltip.remove()); } @@ -74,6 +55,7 @@ function getTooltip(date: string, price: number) { function drawBlocks( dateWithLevel: Record, + tooltipWrapper: HTMLElement, ) { const blocks = []; const today = new Date(); @@ -83,7 +65,7 @@ function drawBlocks( const key = getYearMonthDateString(today); const { level = 0, price = 0 } = dateWithLevel[key] ?? {}; const tooltip = getTooltip(key, price); - blocks.push(block(level, tooltip)); + blocks.push(block(level, tooltip, tooltipWrapper)); today.setTime(today.getTime() - DAY); } @@ -143,7 +125,11 @@ export function heatmap(groupedChzInfos: StreamerSummary[]) { } } - const div = parseHtml(` + const div = document.createElement('div'); + div.style.minWidth = '0'; + div.style.position = 'relative'; + + const scrollWrapper = parseHtml(`