|
| 1 | +<script lang="ts"> |
| 2 | + import { portal } from '$lib/utils/portal'; |
| 3 | + import { setPosition } from '$lib/utils/tooltipPosition'; |
| 4 | + import { flyScale } from '$lib/utils/transitions'; |
| 5 | + import type { Snippet } from 'svelte'; |
| 6 | +
|
| 7 | + interface Props { |
| 8 | + title?: string; |
| 9 | + size?: 'small' | 'medium'; |
| 10 | + children: Snippet; |
| 11 | + } |
| 12 | +
|
| 13 | + const { title, size = 'medium', children }: Props = $props(); |
| 14 | +
|
| 15 | + let targetEl: HTMLElement | undefined = $state(); |
| 16 | + let show = $state(false); |
| 17 | + let timeoutId: undefined | ReturnType<typeof setTimeout> = $state(); |
| 18 | + let isHoveringCard = false; // Track if the tooltip card is hovered |
| 19 | + const gapDelay = 150; // Delay to allow transitioning between button and card |
| 20 | +
|
| 21 | + function handleMouseEnter() { |
| 22 | + clearTimeout(timeoutId); |
| 23 | + timeoutId = setTimeout(() => { |
| 24 | + show = true; |
| 25 | + }, 500); |
| 26 | + } |
| 27 | +
|
| 28 | + function handleMouseLeave() { |
| 29 | + clearTimeout(timeoutId); |
| 30 | + timeoutId = setTimeout(() => { |
| 31 | + if (!isHoveringCard) { |
| 32 | + show = false; |
| 33 | + } |
| 34 | + }, gapDelay); |
| 35 | + } |
| 36 | +
|
| 37 | + function handleCardMouseEnter() { |
| 38 | + clearTimeout(timeoutId); |
| 39 | + isHoveringCard = true; |
| 40 | + } |
| 41 | +
|
| 42 | + function handleCardMouseLeave() { |
| 43 | + isHoveringCard = false; |
| 44 | + timeoutId = setTimeout(() => { |
| 45 | + if (!isHoveringCard) { |
| 46 | + show = false; |
| 47 | + } |
| 48 | + }, gapDelay); |
| 49 | + } |
| 50 | +</script> |
| 51 | + |
| 52 | +<div |
| 53 | + bind:this={targetEl} |
| 54 | + class="wrapper {size}" |
| 55 | + role="tooltip" |
| 56 | + onmouseenter={handleMouseEnter} |
| 57 | + onmouseleave={handleMouseLeave} |
| 58 | +> |
| 59 | + <div class="info-button" class:button-hovered={show}></div> |
| 60 | + |
| 61 | + {#if show} |
| 62 | + <div |
| 63 | + use:portal={'body'} |
| 64 | + use:setPosition={{ targetEl, position: 'bottom', align: 'center' }} |
| 65 | + class="tooltip-container" |
| 66 | + role="presentation" |
| 67 | + transition:flyScale |
| 68 | + onmouseenter={handleCardMouseEnter} |
| 69 | + onmouseleave={handleCardMouseLeave} |
| 70 | + > |
| 71 | + <div class="tooltip-arrow"></div> |
| 72 | + |
| 73 | + <div class="tooltip-card"> |
| 74 | + {#if title} |
| 75 | + <h3 class="text-13 text-semibold tooltip-title">{title}</h3> |
| 76 | + {/if} |
| 77 | + <p class="text-12 text-body tooltip-description"> |
| 78 | + {@render children()} |
| 79 | + </p> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + {/if} |
| 83 | +</div> |
| 84 | + |
| 85 | +<style lang="postcss"> |
| 86 | + .wrapper { |
| 87 | + position: relative; |
| 88 | + display: inline-flex; |
| 89 | + } |
| 90 | +
|
| 91 | + .info-button { |
| 92 | + position: relative; |
| 93 | + flex-shrink: 0; |
| 94 | + color: var(--clr-text-2); |
| 95 | + border-radius: 16px; |
| 96 | + box-shadow: inset 0 0 0 1.5px var(--clr-text-2); |
| 97 | + transition: box-shadow var(--transition-fast); |
| 98 | +
|
| 99 | + &::before, |
| 100 | + &::after { |
| 101 | + content: ''; |
| 102 | + position: absolute; |
| 103 | + left: 50%; |
| 104 | + transform: translateX(-50%); |
| 105 | + background-color: var(--clr-text-2); |
| 106 | + border-radius: 2px; |
| 107 | + transition: background-color var(--transition-fast); |
| 108 | + } |
| 109 | + } |
| 110 | +
|
| 111 | + .button-hovered { |
| 112 | + box-shadow: inset 0 0 0 10px var(--clr-text-2); |
| 113 | +
|
| 114 | + &::before, |
| 115 | + &::after { |
| 116 | + background-color: var(--clr-scale-ntrl-100); |
| 117 | + } |
| 118 | + } |
| 119 | +
|
| 120 | + .wrapper.medium { |
| 121 | + transform: translateY(20%); |
| 122 | +
|
| 123 | + & .info-button { |
| 124 | + width: 16px; |
| 125 | + height: 16px; |
| 126 | +
|
| 127 | + &::before { |
| 128 | + top: 4px; |
| 129 | + width: 2px; |
| 130 | + height: 2px; |
| 131 | + } |
| 132 | +
|
| 133 | + &::after { |
| 134 | + top: 7px; |
| 135 | + width: 2px; |
| 136 | + height: 5px; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +
|
| 141 | + .wrapper.small { |
| 142 | + transform: translateY(10%); |
| 143 | +
|
| 144 | + & .info-button { |
| 145 | + width: 12px; |
| 146 | + height: 12px; |
| 147 | +
|
| 148 | + &::before { |
| 149 | + top: 3px; |
| 150 | + width: 2px; |
| 151 | + height: 2px; |
| 152 | + } |
| 153 | +
|
| 154 | + &::after { |
| 155 | + top: 6px; |
| 156 | + width: 2px; |
| 157 | + height: 3px; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +
|
| 162 | + .tooltip-container { |
| 163 | + z-index: var(--z-blocker); |
| 164 | + position: absolute; |
| 165 | + display: flex; |
| 166 | + flex-direction: column; |
| 167 | + width: fit-content; |
| 168 | + } |
| 169 | +
|
| 170 | + .tooltip-card { |
| 171 | + display: flex; |
| 172 | + flex-direction: column; |
| 173 | + gap: 4px; |
| 174 | + background-color: var(--clr-bg-1); |
| 175 | + border: 1px solid var(--clr-border-2); |
| 176 | + border-radius: var(--radius-m); |
| 177 | + padding: 12px; |
| 178 | + width: max-content; |
| 179 | + max-width: 260px; |
| 180 | + box-shadow: var(--fx-shadow-m); |
| 181 | + } |
| 182 | +
|
| 183 | + .tooltip-title { |
| 184 | + color: var(--clr-text-1); |
| 185 | + } |
| 186 | +
|
| 187 | + .tooltip-description { |
| 188 | + color: var(--clr-scale-ntrl-40); |
| 189 | + } |
| 190 | +
|
| 191 | + .tooltip-arrow { |
| 192 | + position: relative; |
| 193 | + top: 1px; |
| 194 | + margin: 0 auto; |
| 195 | + width: 100%; |
| 196 | + height: 10px; |
| 197 | + display: flex; |
| 198 | + justify-content: center; |
| 199 | + overflow: hidden; |
| 200 | + z-index: var(--z-lifted); |
| 201 | + width: fit-content; |
| 202 | +
|
| 203 | + &::before { |
| 204 | + content: ''; |
| 205 | + position: relative; |
| 206 | + top: 4px; |
| 207 | + width: 20px; |
| 208 | + height: 20px; |
| 209 | + transform: rotate(45deg); |
| 210 | + border-radius: 2px; |
| 211 | + background-color: var(--clr-bg-1); |
| 212 | + border: 1px solid var(--clr-border-2); |
| 213 | + } |
| 214 | + } |
| 215 | +</style> |
0 commit comments