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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 12x 12x 12x 12x 24x 24x 24x 24x 12x 12x 24x 24x 24x 24x 24x 24x 24x 12x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 24x 24x 24x 24x 12x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 24x 24x 24x 2x 2x 22x 22x 22x 4x 4x 4x 4x 4x 4x 18x 18x 18x 18x 18x 18x 18x 18x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 9x 9x 8x 8x 8x 17x 17x 17x 17x 9x 8x 8x 8x 17x 17x 17x 17x 9x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 17x 17x 17x 1x 1x 16x 16x 16x 16x 16x 17x 17x 17x 2x 2x 14x 14x 14x 2x 2x 2x 2x 2x 2x 12x 12x 12x 12x 12x 12x 12x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 4x 1x 1x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | 'use client'
import { useEffect, useRef, useState } from 'react'
import { createPortal } from 'react-dom'
import { motion, useSpring } from 'framer-motion'
import useMeasure from 'react-use-measure'
import { useTheme } from '@/contexts/ThemeContext'
import {
useSessionModeBanner,
useSessionModeBannerOptional,
type SlotBounds,
type SlotDimensions,
} from '@/contexts/SessionModeBannerContext'
import { SessionModeBanner } from './SessionModeBanner'
import { CompactBanner } from './CompactBanner'
import { ActiveSessionBanner } from './ActiveSessionBanner'
import { Z_INDEX } from '@/constants/zIndex'
// ============================================================================
// Animation Config
// ============================================================================
const springConfig = { stiffness: 300, damping: 30 }
const ANIMATION_DURATION_MS = 400 // Approximate spring duration
// ============================================================================
// Content Banner Slot
// ============================================================================
interface ContentBannerSlotProps {
className?: string
/**
* Height of sticky elements above the content area (e.g., nav bar).
* When the banner scrolls under this offset, it projects to the nav slot.
* Default: 0 (no sticky offset)
*/
stickyOffset?: number
}
/**
* ContentBannerSlot - Renders the full banner in document flow.
* When active, the banner is visible and takes up space in the layout.
* During transitions, a temporary overlay animates while this slot is hidden.
* Uses IntersectionObserver to detect when scrolled under sticky nav.
*/
export function ContentBannerSlot({ className, stickyOffset = 0 }: ContentBannerSlotProps) {
const {
registerContentSlot,
unregisterContentSlot,
setContentBounds,
setContentDimensions,
setContentSlotVisible,
activeSlot,
sessionMode,
isLoading,
activeSession,
onAction,
onDefer,
onResume,
onStartFresh,
} = useSessionModeBanner()
const slotRef = useRef<HTMLDivElement>(null)
// Measure the banner content dimensions
const [measureRef, { width: measuredWidth, height: measuredHeight }] = useMeasure()
// Report dimensions to context when they change
useEffect(() => {
if (measuredWidth > 0 && measuredHeight > 0) {
setContentDimensions({ width: measuredWidth, height: measuredHeight })
}
return () => setContentDimensions(null)
}, [measuredWidth, measuredHeight, setContentDimensions])
// Register on mount
useEffect(() => {
registerContentSlot()
return () => unregisterContentSlot()
}, [registerContentSlot, unregisterContentSlot])
const isActive = activeSlot === 'content'
const hasContent = sessionMode || activeSession
// Report bounds to context for animation calculations
useEffect(() => {
if (!slotRef.current) return
const updateBounds = () => {
if (slotRef.current) {
const rect = slotRef.current.getBoundingClientRect()
setContentBounds({
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height,
})
}
}
updateBounds()
window.addEventListener('resize', updateBounds)
window.addEventListener('scroll', updateBounds)
return () => {
window.removeEventListener('resize', updateBounds)
window.removeEventListener('scroll', updateBounds)
setContentBounds(null)
}
}, [setContentBounds])
// Use IntersectionObserver to detect when banner scrolls under sticky nav
useEffect(() => {
if (!slotRef.current) return
// rootMargin: negative top margin accounts for sticky nav height
// When top of element is at/above the sticky nav bottom, it's "not visible"
const rootMargin = `-${stickyOffset}px 0px 0px 0px`
const observer = new IntersectionObserver(
(entries) => {
for (const entry of entries) {
// isIntersecting = true when any part of the element is visible below the sticky nav
setContentSlotVisible(entry.isIntersecting)
}
},
{
rootMargin,
threshold: 0, // Trigger as soon as any part enters/exits
}
)
observer.observe(slotRef.current)
return () => {
observer.disconnect()
// Reset visibility when unmounting
setContentSlotVisible(true)
}
}, [stickyOffset, setContentSlotVisible])
if (!hasContent) {
return null
}
// Render banner content
const bannerContent = activeSession ? (
<ActiveSessionBanner
session={activeSession}
onResume={onResume}
onStartFresh={onStartFresh}
variant="dashboard"
/>
) : sessionMode ? (
<SessionModeBanner
sessionMode={sessionMode}
onAction={onAction}
onDefer={onDefer}
isLoading={isLoading}
variant="dashboard"
/>
) : null
return (
<div
ref={slotRef}
data-slot="content-banner"
data-active={isActive}
className={className}
style={{
// When projecting to nav, hide visually but preserve space
visibility: isActive ? undefined : 'hidden',
// Prevent interaction when hidden
pointerEvents: isActive ? undefined : 'none',
}}
>
{/* Wrapper for measurement - always render content to preserve space */}
<div ref={measureRef}>{bannerContent}</div>
</div>
)
}
// ============================================================================
// Nav Banner Slot
// ============================================================================
interface NavBannerSlotProps {
className?: string
}
/**
* NavBannerSlot - Renders the compact banner in document flow.
* Always renders content for measurement, uses visibility to show/hide.
*/
export function NavBannerSlot({ className }: NavBannerSlotProps) {
const context = useSessionModeBannerOptional()
const { resolvedTheme } = useTheme()
const isDark = resolvedTheme === 'dark'
const slotRef = useRef<HTMLDivElement>(null)
// Extract stable callbacks from context to avoid dependency on entire context object
const setNavDimensions = context?.setNavDimensions
const setNavBounds = context?.setNavBounds
const registerNavSlot = context?.registerNavSlot
const unregisterNavSlot = context?.unregisterNavSlot
// Measure the banner content dimensions
const [measureRef, { width: measuredWidth, height: measuredHeight }] = useMeasure()
// Report dimensions to context when they change
useEffect(() => {
if (!setNavDimensions) return
if (measuredWidth > 0 && measuredHeight > 0) {
setNavDimensions({ width: measuredWidth, height: measuredHeight })
}
return () => setNavDimensions(null)
}, [setNavDimensions, measuredWidth, measuredHeight])
// Register on mount
useEffect(() => {
if (registerNavSlot && unregisterNavSlot) {
registerNavSlot()
return () => unregisterNavSlot()
}
}, [registerNavSlot, unregisterNavSlot])
// Report bounds to context
useEffect(() => {
if (!slotRef.current || !setNavBounds) return
const updateBounds = () => {
if (slotRef.current) {
const rect = slotRef.current.getBoundingClientRect()
setNavBounds({
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height,
})
}
}
updateBounds()
window.addEventListener('resize', updateBounds)
window.addEventListener('scroll', updateBounds)
return () => {
window.removeEventListener('resize', updateBounds)
window.removeEventListener('scroll', updateBounds)
setNavBounds(null)
}
}, [setNavBounds])
if (!context) {
return null
}
const { activeSlot, sessionMode, isLoading, activeSession, onAction, onResume, onStartFresh } =
context
const isActive = activeSlot === 'nav'
const hasContent = sessionMode || activeSession
if (!hasContent) {
return null
}
// Render banner content
const bannerContent = activeSession ? (
<ActiveSessionBanner
session={activeSession}
onResume={onResume}
onStartFresh={onStartFresh}
variant="nav"
/>
) : sessionMode ? (
<CompactBanner
sessionMode={sessionMode}
onAction={onAction}
isLoading={isLoading}
isDark={isDark}
/>
) : null
return (
<div
ref={slotRef}
data-slot="nav-banner"
data-active={isActive}
className={className}
style={{
// When content slot is active, collapse this completely
// We use a hidden measurement container instead
height: isActive ? undefined : 0,
overflow: isActive ? undefined : 'hidden',
pointerEvents: isActive ? undefined : 'none',
}}
>
{/* Wrapper for measurement - always render for dimension tracking */}
<div
ref={measureRef}
style={{
// When collapsed, make content invisible but still measurable
visibility: isActive ? undefined : 'hidden',
}}
>
{bannerContent}
</div>
</div>
)
}
// ============================================================================
// Projecting Banner Manager (FLIP Animation Orchestrator)
// ============================================================================
/**
* ProjectingBanner - Manages the FLIP animation between slots.
*
* Normal state: The banner is in document flow in the active slot.
* During transition: A temporary fixed overlay animates while slots are hidden.
* After transition: The overlay is removed and the banner is back in document flow.
*/
export function ProjectingBanner() {
const context = useSessionModeBannerOptional()
const { resolvedTheme } = useTheme()
const isDark = resolvedTheme === 'dark'
const [mounted, setMounted] = useState(false)
const [isAnimating, setIsAnimating] = useState(false)
const [animationFrom, setAnimationFrom] = useState<SlotBounds | null>(null)
const [animatingSlot, setAnimatingSlot] = useState<'content' | 'nav'>('content')
const [fromDimensions, setFromDimensions] = useState<SlotDimensions | null>(null)
const [toDimensions, setToDimensions] = useState<SlotDimensions | null>(null)
// Track previous slot for detecting transitions
const previousSlotRef = useRef<'content' | 'nav' | 'hidden'>('hidden')
useEffect(() => {
setMounted(true)
}, [])
// Watch for slot changes and trigger animation
useEffect(() => {
if (!context || !mounted) return
const { activeSlot, shouldAnimate, contentDimensions, navDimensions } = context
const prevSlot = previousSlotRef.current
// Detect slot change
if (prevSlot !== activeSlot && activeSlot !== 'hidden' && prevSlot !== 'hidden') {
// Get FRESH bounds directly from DOM to avoid stale state from React batching
const fromSelector =
prevSlot === 'content' ? '[data-slot="content-banner"]' : '[data-slot="nav-banner"]'
const fromEl = document.querySelector(fromSelector)
// Get dimensions for both slots
const fromDims = prevSlot === 'content' ? contentDimensions : navDimensions
const toDims = activeSlot === 'content' ? contentDimensions : navDimensions
if (fromEl && shouldAnimate && fromDims && toDims) {
const fromRect = fromEl.getBoundingClientRect()
const fromBounds: SlotBounds = {
x: fromRect.x,
y: fromRect.y,
width: fromRect.width,
height: fromRect.height,
}
// Start animation - target bounds will be tracked dynamically
setAnimationFrom(fromBounds)
setFromDimensions(fromDims)
setToDimensions(toDims)
setAnimatingSlot(activeSlot as 'content' | 'nav')
setIsAnimating(true)
// End animation after spring settles
setTimeout(() => {
setIsAnimating(false)
setAnimationFrom(null)
setFromDimensions(null)
setToDimensions(null)
}, ANIMATION_DURATION_MS)
}
}
previousSlotRef.current = activeSlot
}, [context, mounted])
// Apply CSS to hide slots during animation
useEffect(() => {
if (isAnimating) {
// Hide both slots during animation
document
.querySelectorAll('[data-slot="content-banner"], [data-slot="nav-banner"]')
.forEach((el) => {
;(el as HTMLElement).style.visibility = 'hidden'
})
} else {
// After animation, reset the ACTIVE slot so it becomes visible.
// The inactive content slot keeps React's visibility:hidden to preserve space.
// The nav slot is always reset since it doesn't need to preserve space.
document.querySelectorAll('[data-slot="nav-banner"]').forEach((el) => {
;(el as HTMLElement).style.visibility = ''
})
// If content is now active, reset its visibility too
if (animatingSlot === 'content') {
document.querySelectorAll('[data-slot="content-banner"]').forEach((el) => {
;(el as HTMLElement).style.visibility = ''
})
}
}
}, [isAnimating, animatingSlot])
if (!context || !mounted || !isAnimating || !animationFrom || !fromDimensions || !toDimensions) {
return null
}
const { sessionMode, isLoading, activeSession, onAction, onDefer, onResume, onStartFresh } =
context
// Render the appropriate banner for the destination slot
const bannerContent =
animatingSlot === 'content' ? (
activeSession ? (
<ActiveSessionBanner
session={activeSession}
onResume={onResume}
onStartFresh={onStartFresh}
variant="dashboard"
/>
) : sessionMode ? (
<SessionModeBanner
sessionMode={sessionMode}
onAction={onAction}
isLoading={isLoading}
variant="dashboard"
/>
) : null
) : activeSession ? (
<ActiveSessionBanner
session={activeSession}
onResume={onResume}
onStartFresh={onStartFresh}
variant="nav"
/>
) : sessionMode ? (
<CompactBanner
sessionMode={sessionMode}
onAction={onAction}
isLoading={isLoading}
isDark={isDark}
/>
) : null
const targetSelector =
animatingSlot === 'content' ? '[data-slot="content-banner"]' : '[data-slot="nav-banner"]'
return createPortal(
<AnimatingOverlay
from={animationFrom}
fromDimensions={fromDimensions}
toDimensions={toDimensions}
targetSelector={targetSelector}
>
{bannerContent}
</AnimatingOverlay>,
document.body
)
}
// ============================================================================
// Animating Overlay (Temporary Fixed Position During FLIP)
// ============================================================================
interface AnimatingOverlayProps {
from: SlotBounds
fromDimensions: SlotDimensions
toDimensions: SlotDimensions
targetSelector: string
children: React.ReactNode
}
function AnimatingOverlay({
from,
fromDimensions,
toDimensions,
targetSelector,
children,
}: AnimatingOverlayProps) {
const x = useSpring(from.x, springConfig)
const y = useSpring(from.y, springConfig)
const width = useSpring(fromDimensions.width, springConfig)
const height = useSpring(fromDimensions.height, springConfig)
// Animate to target dimensions on mount
useEffect(() => {
width.set(toDimensions.width)
height.set(toDimensions.height)
}, [toDimensions.width, toDimensions.height, width, height])
// Continuously track the target element's position during animation
// This handles the case where user is still scrolling during the transition
useEffect(() => {
const updateTarget = () => {
const targetEl = document.querySelector(targetSelector)
if (targetEl) {
const rect = targetEl.getBoundingClientRect()
x.set(rect.x)
y.set(rect.y)
}
}
// Initial update
updateTarget()
// Track scroll and resize to follow moving target
window.addEventListener('scroll', updateTarget, { passive: true })
window.addEventListener('resize', updateTarget)
return () => {
window.removeEventListener('scroll', updateTarget)
window.removeEventListener('resize', updateTarget)
}
}, [targetSelector, x, y])
return (
<motion.div
data-component="animating-overlay"
style={{
position: 'fixed',
left: x,
top: y,
width: width,
height: height,
overflow: 'hidden', // Clip content during size transition
zIndex: Z_INDEX.SESSION_MODE_BANNER_ANIMATING,
pointerEvents: 'none', // Don't capture clicks during animation
}}
>
{children}
</motion.div>
)
}
// ============================================================================
// Re-exports for backward compatibility
// ============================================================================
export { ContentBannerSlot as ContentSlot }
export { NavBannerSlot as NavSlot }
|