All files / web/src/components/Thermometer RangeThermometer.tsx

86.7% Statements 600/692
67.62% Branches 94/139
75% Functions 6/8
86.7% Lines 600/692

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 6931x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x                   19x 19x 19x 19x 19x 19x 19x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 19x 19x 19x 19x 19x 19x 19x 2x 2x 2x 2x 19x 19x 19x 19x 19x 19x 1x 1x 1x 19x 19x 19x 19x           19x 19x 19x   19x 19x 19x 19x 19x                     19x 19x 19x 19x 19x 8x 19x 23x 23x 23x 8x 19x 19x 19x 19x 19x 19x 19x 19x 57x 57x             57x 57x 57x 57x 57x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 2x 2x 2x 2x 1x 1x 1x 1x 2x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 52x 3x 49x 5x   5x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x       57x 57x 57x 57x       57x 3x 54x 57x 57x 57x       57x 3x 54x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 57x 27x 27x 27x 27x 27x 26x   26x 1x   1x 27x 26x   26x 1x   1x 27x 27x 27x 27x 27x 27x 27x 27x 27x 57x 57x 57x 57x 57x 57x                                                                                               57x 57x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 3x 3x 3x 3x 5x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 3x 3x 3x 3x 8x 8x 8x 19x 19x 19x 19x 19x  
'use client'
 
import { css } from '@styled/css'
import * as Slider from '@radix-ui/react-slider'
import * as Tooltip from '@radix-ui/react-tooltip'
import * as Popover from '@radix-ui/react-popover'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import type { RangeThermometerProps } from './types'
 
/** Max region names to show in tooltip before truncating */
const MAX_TOOLTIP_NAMES = 8
 
/**
 * A range thermometer component that displays discrete options with two handles
 * for selecting a min/max range. Uses Radix Slider for accessibility and interaction.
 */
export function RangeThermometer<T extends string>({
  options,
  minValue,
  maxValue,
  onChange,
  orientation = 'vertical',
  isDark = false,
  label,
  description,
  counts,
  showTotalCount = true,
  onHoverPreview,
  regionNamesByCategory,
  selectedRegionNames,
  onRegionNameHover,
  hideCountOnMd = false,
}: RangeThermometerProps<T>) {
  const isVertical = orientation === 'vertical'
  const [isDragging, setIsDragging] = useState(false)
 
  // Track last sent values to prevent duplicate onChange calls during async updates
  const lastSentRef = useRef<[number, number] | null>(null)
 
  // Sync ref when props change from outside (e.g., parent resets values)
  useEffect(() => {
    const currentMinIndex = options.findIndex((opt) => opt.value === minValue)
    const currentMaxIndex = options.findIndex((opt) => opt.value === maxValue)
    // Only sync if we're not dragging (to avoid overwriting during drag)
    if (!isDragging) {
      lastSentRef.current = [currentMinIndex, currentMaxIndex]
    }
  }, [minValue, maxValue, options, isDragging])
 
  // Convert values to indices - memoize to prevent recalculation
  const minIndex = useMemo(
    () => options.findIndex((opt) => opt.value === minValue),
    [options, minValue]
  )
  const maxIndex = useMemo(
    () => options.findIndex((opt) => opt.value === maxValue),
    [options, maxValue]
  )
 
  // Handle slider value changes - memoized to prevent Slider re-renders
  // Only call onChange when values actually change to prevent server thrashing
  const handleValueChange = useCallback(
    (values: number[]) => {
      const [newMinIndex, newMaxIndex] = values
      const last = lastSentRef.current

      // Only fire if values differ from what we last sent (not props, which may be stale)
      if (!last || newMinIndex !== last[0] || newMaxIndex !== last[1]) {
        lastSentRef.current = [newMinIndex, newMaxIndex]
        onChange(options[newMinIndex].value, options[newMaxIndex].value)
      }
    },
    [onChange, options]
  )
 
  // Calculate what the range would be if clicking on a specific option
  // (moves nearest handle to that position)
  const getPreviewRange = useCallback(
    (hoveredIndex: number): { previewMin: T; previewMax: T } => {
      const distToMin = Math.abs(hoveredIndex - minIndex)
      const distToMax = Math.abs(hoveredIndex - maxIndex)
      if (distToMin <= distToMax) {
        // Would move min handle
        return {
          previewMin: options[hoveredIndex].value,
          previewMax: options[maxIndex].value,
        }
      } else {
        // Would move max handle
        return {
          previewMin: options[minIndex].value,
          previewMax: options[hoveredIndex].value,
        }
      }
    },
    [options, minIndex, maxIndex]
  )
 
  // Handle hover enter on an option - memoized
  // Skip hover events while dragging to prevent interference
  const handleOptionHover = useCallback(
    (index: number) => {
      if (onHoverPreview && !isDragging) {
        onHoverPreview(getPreviewRange(index))
      }
    },
    [onHoverPreview, getPreviewRange, isDragging]
  )
 
  // Handle hover leave - memoized
  // Skip hover events while dragging to prevent interference
  const handleOptionLeave = useCallback(() => {
    if (onHoverPreview && !isDragging) {
      onHoverPreview(null)
    }
  }, [onHoverPreview, isDragging])
 
  // Handle drag start/end for the slider
  const handlePointerDown = useCallback(() => {
    setIsDragging(true)
    // Clear any existing preview when starting to drag
    if (onHoverPreview) {
      onHoverPreview(null)
    }
  }, [onHoverPreview])
 
  const handlePointerUp = useCallback(() => {
    setIsDragging(false)
  }, [])
 
  // Listen for global pointer up to handle drag release anywhere on screen
  useEffect(() => {
    if (isDragging) {
      const handleGlobalPointerUp = () => {
        setIsDragging(false)
      }
      window.addEventListener('pointerup', handleGlobalPointerUp)
      window.addEventListener('pointercancel', handleGlobalPointerUp)
      return () => {
        window.removeEventListener('pointerup', handleGlobalPointerUp)
        window.removeEventListener('pointercancel', handleGlobalPointerUp)
      }
    }
  }, [isDragging])
 
  // Calculate total count for selected range
  const totalCount = useMemo(() => {
    if (!counts || !showTotalCount) return null
    let total = 0
    for (let i = minIndex; i <= maxIndex; i++) {
      const opt = options[i]
      total += counts[opt.value] || 0
    }
    return total
  }, [counts, showTotalCount, options, minIndex, maxIndex])
 
  // Check if an option is within the selected range
  const isInRange = (index: number) => index >= minIndex && index <= maxIndex
 
  // Format region names for tooltip display
  const formatTooltipContent = useCallback(
    (option: (typeof options)[0]) => {
      const names = regionNamesByCategory?.[option.value]
      if (!names || names.length === 0) return null

      const count = names.length
      const displayNames = names.slice(0, MAX_TOOLTIP_NAMES)
      const remaining = count - displayNames.length

      return {
        header: `${option.emoji || ''} ${option.label} (${count} region${count !== 1 ? 's' : ''})`,
        names: displayNames,
        remaining,
      }
    },
    [regionNamesByCategory]
  )
 
  return (
    <Tooltip.Provider delayDuration={150} skipDelayDuration={100}>
      <div
        data-component="range-thermometer"
        className={css({
          display: 'flex',
          flexDirection: 'column',
          gap: '2',
        })}
      >
        {/* Label and description */}
        {(label || description) && (
          <div>
            {label && (
              <div
                className={css({
                  fontSize: 'xs',
                  fontWeight: 'semibold',
                  color: isDark ? 'gray.300' : 'gray.700',
                  mb: '0.5',
                })}
              >
                {label}
              </div>
            )}
            {description && (
              <div
                className={css({
                  fontSize: '2xs',
                  color: isDark ? 'gray.400' : 'gray.500',
                  lineHeight: '1.3',
                })}
              >
                {description}
              </div>
            )}
          </div>
        )}
 
        {/* Main container with labels and slider */}
        <div
          data-element="range-thermometer-track"
          className={css({
            display: 'flex',
            flexDirection: isVertical ? 'row' : 'column',
            justifyContent: 'space-between',
            gap: '1',
          })}
        >
          {/* Labels column */}
          <div
            className={css({
              display: 'flex',
              flexDirection: isVertical ? 'column' : 'row',
              justifyContent: 'space-between',
              flex: isVertical ? 'none' : 1,
            })}
          >
            {options.map((option, index) => {
              const inRange = isInRange(index)
              const isOnly = minIndex === index && maxIndex === index
              const count = counts?.[option.value]
              const tooltipContent = formatTooltipContent(option)
 
              const buttonElement = (
                <div
                  data-option={option.value}
                  data-in-range={inRange}
                  onMouseEnter={() => handleOptionHover(index)}
                  onMouseLeave={handleOptionLeave}
                  className={css({
                    display: 'flex',
                    alignItems: 'center',
                    gap: '1',
                    py: '1',
                    px: '1.5',
                    rounded: 'md',
                    transition: 'all 0.15s',
                    bg: inRange ? (isDark ? 'blue.900/40' : 'blue.50') : 'transparent',
                    opacity: inRange ? 1 : 0.5,
                    _hover: {
                      bg: isDark ? 'gray.700' : 'gray.100',
                      opacity: 1,
                      '& [data-only-button]': {
                        opacity: 1,
                      },
                    },
                  })}
                >
                  {/* Main clickable area */}
                  <button
                    type="button"
                    onClick={() => {
                      // Click moves nearest handle to this position
                      const distToMin = Math.abs(index - minIndex)
                      const distToMax = Math.abs(index - maxIndex)
                      if (distToMin <= distToMax) {
                        onChange(option.value, options[maxIndex].value)
                      } else {
                        onChange(options[minIndex].value, option.value)
                      }
                    }}
                    className={css({
                      display: 'flex',
                      alignItems: 'center',
                      gap: '1',
                      flex: 1,
                      cursor: 'pointer',
                      bg: 'transparent',
                      border: 'none',
                      padding: 0,
                    })}
                  >
                    {/* Emoji */}
                    {option.emoji && (
                      <span className={css({ fontSize: 'sm' })}>{option.emoji}</span>
                    )}
 
                    {/* Label */}
                    <span
                      className={css({
                        fontSize: 'xs',
                        fontWeight: inRange ? '600' : '500',
                        color: inRange
                          ? isDark
                            ? 'blue.300'
                            : 'blue.700'
                          : isDark
                            ? 'gray.400'
                            : 'gray.600',
                        flex: 1,
                        textAlign: 'left',
                      })}
                    >
                      {option.shortLabel || option.label}
                    </span>
                  </button>
 
                  {/* Only button */}
                  <button
                    type="button"
                    data-only-button
                    title={`Show only ${option.label}`}
                    onClick={(e) => {
                      e.stopPropagation()
                      onChange(option.value, option.value)
                    }}
                    className={css({
                      fontSize: '2xs',
                      fontWeight: '600',
                      color: isOnly
                        ? isDark
                          ? 'green.300'
                          : 'green.700'
                        : isDark
                          ? 'gray.400'
                          : 'gray.500',
                      bg: isOnly ? (isDark ? 'green.900/50' : 'green.100') : 'transparent',
                      border: '1px solid',
                      borderColor: isOnly
                        ? isDark
                          ? 'green.700'
                          : 'green.300'
                        : isDark
                          ? 'gray.600'
                          : 'gray.300',
                      px: '1',
                      py: '0',
                      rounded: 'sm',
                      cursor: 'pointer',
                      opacity: isOnly ? 1 : 0,
                      transition: 'all 0.15s',
                      _hover: {
                        bg: isDark ? 'green.900/50' : 'green.100',
                        borderColor: isDark ? 'green.600' : 'green.400',
                        color: isDark ? 'green.300' : 'green.700',
                      },
                    })}
                  >
                    only
                  </button>
 
                  {/* Count badge */}
                  {count !== undefined && (
                    <span
                      className={css({
                        fontSize: '2xs',
                        fontWeight: '600',
                        color: inRange
                          ? isDark
                            ? 'blue.200'
                            : 'blue.600'
                          : isDark
                            ? 'gray.500'
                            : 'gray.400',
                        bg: inRange
                          ? isDark
                            ? 'blue.800'
                            : 'blue.100'
                          : isDark
                            ? 'gray.700'
                            : 'gray.200',
                        px: '1.5',
                        py: '0.5',
                        rounded: 'full',
                        minWidth: '6',
                        textAlign: 'center',
                      })}
                    >
                      {count}
                    </span>
                  )}
                </div>
              )
 
              // Wrap with tooltip if we have region names to show
              if (tooltipContent) {
                return (
                  <Tooltip.Root key={option.value}>
                    <Tooltip.Trigger asChild>{buttonElement}</Tooltip.Trigger>
                    <Tooltip.Portal>
                      <Tooltip.Content
                        side="left"
                        sideOffset={8}
                        className={css({
                          bg: isDark ? 'gray.800' : 'gray.900',
                          color: 'white',
                          px: '3',
                          py: '2',
                          rounded: 'lg',
                          fontSize: 'xs',
                          maxWidth: '220px',
                          boxShadow: 'lg',
                          zIndex: 50,
                          lineHeight: '1.4',
                        })}
                      >
                        <div className={css({ fontWeight: '600', mb: '1' })}>
                          {tooltipContent.header}
                        </div>
                        <div className={css({ color: 'gray.300' })}>
                          {tooltipContent.names.join(', ')}
                          {tooltipContent.remaining > 0 && (
                            <span
                              className={css({
                                color: 'gray.400',
                                fontStyle: 'italic',
                              })}
                            >
                              {' '}
                              ...and {tooltipContent.remaining} more
                            </span>
                          )}
                        </div>
                        <Tooltip.Arrow
                          className={css({
                            fill: isDark ? 'gray.800' : 'gray.900',
                          })}
                        />
                      </Tooltip.Content>
                    </Tooltip.Portal>
                  </Tooltip.Root>
                )
              }
 
              return <span key={option.value}>{buttonElement}</span>
            })}
          </div>
 
          {/* Slider track */}
          <div
            className={css({
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              width: isVertical ? '8' : 'auto',
              height: isVertical ? 'auto' : '8',
              minHeight: isVertical ? '180px' : 'auto',
            })}
          >
            <Slider.Root
              value={[minIndex, maxIndex]}
              min={0}
              max={options.length - 1}
              step={1}
              orientation={isVertical ? 'vertical' : 'horizontal'}
              inverted={isVertical} // Vertical sliders need inversion so top = index 0
              onValueChange={handleValueChange}
              onPointerDown={handlePointerDown}
              className={css({
                position: 'relative',
                display: 'flex',
                alignItems: 'center',
                userSelect: 'none',
                touchAction: 'none',
                width: isVertical ? '3' : '100%',
                height: isVertical ? '100%' : '3',
              })}
            >
              <Slider.Track
                className={css({
                  bg: isDark ? 'gray.700' : 'gray.200',
                  position: 'relative',
                  flexGrow: 1,
                  rounded: 'full',
                  width: isVertical ? '3' : '100%',
                  height: isVertical ? '100%' : '3',
                })}
              >
                <Slider.Range
                  className={css({
                    position: 'absolute',
                    bg: isDark ? 'blue.600' : 'blue.500',
                    rounded: 'full',
                    width: isVertical ? '100%' : 'auto',
                    height: isVertical ? 'auto' : '100%',
                  })}
                />
              </Slider.Track>
 
              {/* Min thumb */}
              <Slider.Thumb
                data-handle="min"
                className={css({
                  display: 'block',
                  w: '4',
                  h: '4',
                  bg: 'white',
                  border: '2px solid',
                  borderColor: isDark ? 'blue.400' : 'blue.500',
                  rounded: 'full',
                  cursor: 'grab',
                  boxShadow: '0 2px 4px rgba(0,0,0,0.2)',
                  _hover: {
                    bg: isDark ? 'blue.100' : 'blue.50',
                    transform: 'scale(1.1)',
                  },
                  _focus: {
                    outline: 'none',
                    boxShadow: '0 0 0 3px rgba(59, 130, 246, 0.3)',
                  },
                  _active: {
                    cursor: 'grabbing',
                  },
                })}
              />
 
              {/* Max thumb */}
              <Slider.Thumb
                data-handle="max"
                className={css({
                  display: 'block',
                  w: '4',
                  h: '4',
                  bg: 'white',
                  border: '2px solid',
                  borderColor: isDark ? 'blue.400' : 'blue.500',
                  rounded: 'full',
                  cursor: 'grab',
                  boxShadow: '0 2px 4px rgba(0,0,0,0.2)',
                  _hover: {
                    bg: isDark ? 'blue.100' : 'blue.50',
                    transform: 'scale(1.1)',
                  },
                  _focus: {
                    outline: 'none',
                    boxShadow: '0 0 0 3px rgba(59, 130, 246, 0.3)',
                  },
                  _active: {
                    cursor: 'grabbing',
                  },
                })}
              />
            </Slider.Root>
          </div>
        </div>
 
        {/* Total count display - clickable to show all selected regions */}
        {totalCount !== null && (
          <div
            className={css({
              display: hideCountOnMd ? { base: 'block', md: 'none' } : 'block',
            })}
          >
            <Popover.Root>
              <Popover.Trigger asChild>
                <button
                  type="button"
                  data-element="total-count"
                  className={css({
                    display: 'flex',
                    alignItems: 'center',
                    justifyContent: 'center',
                    gap: '1',
                    py: '1.5',
                    px: '2',
                    bg: isDark ? 'blue.900/30' : 'blue.50',
                    rounded: 'md',
                    border: '1px solid',
                    borderColor: isDark ? 'blue.800' : 'blue.200',
                    cursor: selectedRegionNames?.length ? 'pointer' : 'default',
                    transition: 'all 0.15s',
                    width: '100%',
                    _hover: selectedRegionNames?.length
                      ? {
                          bg: isDark ? 'blue.900/50' : 'blue.100',
                          borderColor: isDark ? 'blue.700' : 'blue.300',
                        }
                      : {},
                  })}
                >
                  <span
                    className={css({
                      fontSize: 'sm',
                      fontWeight: 'bold',
                      color: isDark ? 'blue.300' : 'blue.700',
                      textDecoration: selectedRegionNames?.length ? 'underline' : 'none',
                      textDecorationStyle: 'dotted',
                      textUnderlineOffset: '2px',
                    })}
                  >
                    {totalCount}
                  </span>
                  <span
                    className={css({
                      fontSize: 'xs',
                      color: isDark ? 'blue.400' : 'blue.600',
                    })}
                  >
                    regions
                  </span>
                </button>
              </Popover.Trigger>
              {selectedRegionNames && selectedRegionNames.length > 0 && (
                <Popover.Portal>
                  <Popover.Content
                    side="top"
                    sideOffset={8}
                    align="center"
                    className={css({
                      bg: isDark ? 'gray.800' : 'white',
                      border: '1px solid',
                      borderColor: isDark ? 'gray.700' : 'gray.200',
                      rounded: 'lg',
                      boxShadow: 'xl',
                      zIndex: 50,
                      width: '200px',
                      maxHeight: '300px',
                      overflow: 'hidden',
                      display: 'flex',
                      flexDirection: 'column',
                    })}
                  >
                    {/* Header */}
                    <div
                      className={css({
                        px: '3',
                        py: '2',
                        borderBottom: '1px solid',
                        borderColor: isDark ? 'gray.700' : 'gray.200',
                        fontWeight: '600',
                        fontSize: 'sm',
                        color: isDark ? 'gray.100' : 'gray.800',
                      })}
                    >
                      {selectedRegionNames.length} regions selected
                    </div>
                    {/* Scrollable list */}
                    <div
                      className={css({
                        overflowY: 'auto',
                        flex: 1,
                        py: '1',
                      })}
                    >
                      {selectedRegionNames
                        .slice()
                        .sort((a, b) => a.localeCompare(b))
                        .map((name) => (
                          <div
                            key={name}
                            onMouseEnter={() => onRegionNameHover?.(name)}
                            onMouseLeave={() => onRegionNameHover?.(null)}
                            className={css({
                              px: '3',
                              py: '1',
                              fontSize: 'xs',
                              color: isDark ? 'gray.300' : 'gray.600',
                              cursor: onRegionNameHover ? 'pointer' : 'default',
                              _hover: {
                                bg: isDark ? 'gray.700' : 'gray.100',
                                color: isDark ? 'gray.100' : 'gray.900',
                              },
                            })}
                          >
                            {name}
                          </div>
                        ))}
                    </div>
                    <Popover.Arrow
                      className={css({
                        fill: isDark ? 'gray.800' : 'white',
                      })}
                    />
                  </Popover.Content>
                </Popover.Portal>
              )}
            </Popover.Root>
          </div>
        )}
      </div>
    </Tooltip.Provider>
  )
}