'use client'; import {ActionButton, DialogTrigger, pressScale, Provider} from '@react-spectrum/s2'; import {baseColor, focusRing, style} from '@react-spectrum/s2/style' with {type: 'macro'}; import {Button, Link, Modal, ModalOverlay} from 'react-aria-components'; import Contrast from '@react-spectrum/s2/icons/Contrast'; import {getBaseUrl} from './pageUtils'; import {getLibraryFromPage, getLibraryIcon} from './library'; import {keyframes} from '../../../@react-spectrum/s2/style/style-macro' with {type: 'macro'}; import Lighten from '@react-spectrum/s2/icons/Lighten'; import MenuHamburger from '@react-spectrum/s2/icons/MenuHamburger'; import React, {CSSProperties, lazy, useEffect, useRef, useState} from 'react'; import {TAB_DEFS} from './constants'; import {useLayoutEffect} from '@react-aria/utils'; import {useRouter} from './Router'; import {useSettings} from './SettingsContext'; import './SearchMenu.css'; const MobileSearchMenu = lazy(() => import('./SearchMenu').then(({MobileSearchMenu}) => ({default: MobileSearchMenu})) ); let fadeOut = keyframes(` 0% { opacity: 1; transform: translateY(0px); } 100% { opacity: 0; transform: translateY(calc(-100% - 12px)); width: 0px; } `); let shadow = keyframes(` 0% { box-shadow: none; background-color: var(--base-bg); } 100% { box-shadow: var(--shadow); background-color: var(--layer-2-bg); } `); let fadeIn = keyframes(` 0% { opacity: 0; transform: translateY(calc(100% + 12px)); } 100% { opacity: 1; transform: translateY(0px); } `); const animation = { animationDuration: '1s', animationPlayState: { default: 'paused', '@supports (animation-timeline: scroll())': 'running' }, animationFillMode: 'both', transitionTimingFunction: 'linear' } as const; const animationRange = '24px 64px'; const colorSchemeToggleStyles = style({ ...focusRing(), font: 'ui', color: 'neutral', textDecoration: 'none', transition: 'default', backgroundColor: { default: { ...baseColor('gray-100'), default: 'transparent' } }, size: 32, display: 'flex', alignItems: 'center', justifyContent: 'center', borderRadius: 'lg', borderWidth: 0 }); const iconContainerStyles = style({ position: 'relative', size: 20 }); function ColorSchemeToggle() { let {colorScheme, toggleColorScheme, systemColorScheme} = useSettings(); let isOverriding = colorScheme !== systemColorScheme; let label = isOverriding ? `Using ${colorScheme} mode (press to use system)` : `Using system ${systemColorScheme} mode (press to switch)`; let ref = useRef(null); let isDark = colorScheme === 'dark'; // oxlint-disable react/react-compiler return ( colorSchemeToggleStyles(renderProps)} style={pressScale(ref)}> ); // oxlint-enable react/react-compiler } export function MobileHeader({toc}) { let ref = useRef(null); let linkRef = useRef(null); let labelRef = useRef(null); let {colorScheme} = useSettings(); useEffect(() => { // Tiny polyfill for scroll driven animations. // Element must have animationDuration: 1s and animationPlayState: paused. if (!CSS.supports('(animation-timeline: scroll())') && ref.current) { let [start, end] = animationRange.split(' ').map(v => parseInt(v, 10)); let animations = ref.current.getAnimations({subtree: true}); let onScroll = () => { // Calculate animation time based on percentage of animationRange * duration. let time = Math.max(0, Math.min(end, window.scrollY - start) / (end - start)) * 1000; for (let animation of animations) { animation.currentTime = time; } }; window.addEventListener('scroll', onScroll, {passive: true}); return () => window.removeEventListener('scroll', onScroll); } }, []); let {currentPage} = useRouter(); let library = getLibraryFromPage(currentPage); let icon = getLibraryIcon(library); let subdirectory: 's2' | 'react-aria' = 's2'; if (library === 'react-aria') { // the internationalized library has no homepage so i've chosen to route it to the react aria homepage subdirectory = 'react-aria'; } let baseUrl = getBaseUrl(subdirectory); let homepage = `${baseUrl}/`; let [isOpen, setOpen] = useState(false); let [wasOpen, setWasOpen] = useState(isOpen); let [isTransitioning, setTransitioning] = useState(false); let renderCallback = useRef<(() => void) | null>(null); let onOpenChange = (isOpen: boolean) => { if (!document.startViewTransition) { setOpen(false); if (isOpen) { setWasOpen(true); } return; } // Don't transition the entire page. document.documentElement.style.viewTransitionName = 'none'; // Only transition label if it is visible (scrolled to the top of the page). if (window.scrollY === 0 && labelRef.current && isOpen) { labelRef.current.style.viewTransitionName = 'search-menu-label'; } let viewTransition = document.startViewTransition(() => { if (labelRef.current && window.scrollY === 0) { labelRef.current.style.viewTransitionName = !isOpen ? 'search-menu-label' : ''; } // Wait until next render. Using flushSync causes flickering. return new Promise(resolve => { renderCallback.current = resolve; setOpen(isOpen); setTransitioning(true); if (isOpen) { setWasOpen(true); } }); }); viewTransition.finished.then(() => { document.documentElement.style.viewTransitionName = ''; labelRef.current!.style.viewTransitionName = ''; setTransitioning(false); }); }; useLayoutEffect(() => { renderCallback.current?.(); renderCallback.current = null; }); return ( {icon} {TAB_DEFS[library].label} {toc && ( {toc} )} {library !== 'react-aria' && } ); }