|
| 1 | +import { useState, useEffect, useRef, useCallback } from "react"; |
| 2 | +import styled from "styled-components"; |
| 3 | +import { WidgetContainer } from "../styled/shared"; |
| 4 | +import { Tooltip } from "./Tooltip"; |
| 5 | +import type { DockApp } from "../utils/fetch"; |
| 6 | + |
| 7 | +const DockGlass = styled.div<{ $isOverflowing: boolean }>` |
| 8 | + display: flex; |
| 9 | + justify-content: ${({ $isOverflowing }) => ($isOverflowing ? "flex-start" : "center")}; |
| 10 | + gap: 0.5rem; |
| 11 | + padding: 0.5rem; |
| 12 | + border-radius: 1.25rem; |
| 13 | + max-width: 100%; |
| 14 | +
|
| 15 | + background: linear-gradient(135deg, rgba(255, 255, 255, 0.25), rgba(255, 255, 255, 0.1)); |
| 16 | + backdrop-filter: blur(20px) saturate(180%); |
| 17 | + border: 1px solid rgba(255, 255, 255, 0.18); |
| 18 | + box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1), inset 0 1px 0 rgba(255, 255, 255, 0.3); |
| 19 | +
|
| 20 | + flex-wrap: nowrap; |
| 21 | + overflow-x: auto; |
| 22 | + overscroll-behavior-x: contain; |
| 23 | + -webkit-overflow-scrolling: touch; |
| 24 | + scrollbar-width: none; |
| 25 | + &::-webkit-scrollbar { |
| 26 | + display: none; |
| 27 | + } |
| 28 | +
|
| 29 | + @media (max-width: 768px) { |
| 30 | + gap: 0.4rem; |
| 31 | + border-radius: 1rem; |
| 32 | + } |
| 33 | +`; |
| 34 | + |
| 35 | +const DockInfoItem = styled.div` |
| 36 | + width: 56px; |
| 37 | + height: 56px; |
| 38 | + flex-shrink: 0; |
| 39 | + transform-origin: bottom; |
| 40 | + transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1); |
| 41 | + cursor: pointer; |
| 42 | + user-select: none; |
| 43 | +
|
| 44 | + &:hover { |
| 45 | + transform: translateY(-4px) scale(1.08); |
| 46 | + } |
| 47 | +
|
| 48 | + &:active { |
| 49 | + transform: translateY(-2px) scale(1.04); |
| 50 | + transition: transform 0.1s ease; |
| 51 | + } |
| 52 | +
|
| 53 | + @media (max-width: 768px) { |
| 54 | + width: 48px; |
| 55 | + height: 48px; |
| 56 | + } |
| 57 | +`; |
| 58 | + |
| 59 | +const DockImage = styled.img` |
| 60 | + width: 100%; |
| 61 | + height: 100%; |
| 62 | + object-fit: cover; |
| 63 | + pointer-events: none; |
| 64 | +`; |
| 65 | + |
| 66 | +const DockWidget = ({ items }: { items?: DockApp[] }) => { |
| 67 | + const [openTooltip, setOpenTooltip] = useState<string | null>(null); |
| 68 | + const [isOverflowing, setIsOverflowing] = useState(false); |
| 69 | + const containerRef = useRef<HTMLDivElement>(null); |
| 70 | + const dockRef = useRef<HTMLDivElement>(null); |
| 71 | + |
| 72 | + const handleToggleTooltip = useCallback((appName: string) => { |
| 73 | + setOpenTooltip((current) => (current === appName ? null : appName)); |
| 74 | + }, []); |
| 75 | + |
| 76 | + // Check if dock content overflows |
| 77 | + useEffect(() => { |
| 78 | + const checkOverflow = () => { |
| 79 | + if (dockRef.current) { |
| 80 | + const isOverflow = dockRef.current.scrollWidth > dockRef.current.clientWidth; |
| 81 | + setIsOverflowing(isOverflow); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + checkOverflow(); |
| 86 | + |
| 87 | + // Debounce resize events for better performance |
| 88 | + let timeoutId: NodeJS.Timeout; |
| 89 | + const debouncedCheckOverflow = () => { |
| 90 | + clearTimeout(timeoutId); |
| 91 | + timeoutId = setTimeout(checkOverflow, 150); |
| 92 | + }; |
| 93 | + |
| 94 | + window.addEventListener("resize", debouncedCheckOverflow); |
| 95 | + return () => { |
| 96 | + window.removeEventListener("resize", debouncedCheckOverflow); |
| 97 | + clearTimeout(timeoutId); |
| 98 | + }; |
| 99 | + }, [items]); |
| 100 | + |
| 101 | + useEffect(() => { |
| 102 | + if (!openTooltip) return; |
| 103 | + |
| 104 | + const handleClickOutside = (event: MouseEvent | TouchEvent) => { |
| 105 | + if (containerRef.current && !containerRef.current.contains(event.target as Node)) { |
| 106 | + setOpenTooltip(null); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + document.addEventListener("mousedown", handleClickOutside); |
| 111 | + document.addEventListener("touchstart", handleClickOutside, { passive: true }); |
| 112 | + |
| 113 | + return () => { |
| 114 | + document.removeEventListener("mousedown", handleClickOutside); |
| 115 | + document.removeEventListener("touchstart", handleClickOutside); |
| 116 | + }; |
| 117 | + }, [openTooltip]); |
| 118 | + |
| 119 | + return ( |
| 120 | + <WidgetContainer ref={containerRef}> |
| 121 | + <DockGlass ref={dockRef} $isOverflowing={isOverflowing}> |
| 122 | + {items?.map((app) => ( |
| 123 | + <Tooltip |
| 124 | + portal |
| 125 | + key={app.name} |
| 126 | + content={app.name} |
| 127 | + side="top" |
| 128 | + sideOffset={12} |
| 129 | + open={openTooltip === app.name} |
| 130 | + onOpenChange={(isOpen) => setOpenTooltip(isOpen ? app.name : null)}> |
| 131 | + <DockInfoItem onTouchStart={() => handleToggleTooltip(app.name)}> |
| 132 | + <DockImage src={app.image} alt={app.name} loading="lazy" /> |
| 133 | + </DockInfoItem> |
| 134 | + </Tooltip> |
| 135 | + ))} |
| 136 | + </DockGlass> |
| 137 | + </WidgetContainer> |
| 138 | + ); |
| 139 | +}; |
| 140 | + |
| 141 | +export default DockWidget; |
0 commit comments