MCPcopy Create free account
hub / github.com/adobe/react-spectrum / MobileHeader

Function MobileHeader

packages/dev/s2-docs/src/MobileHeader.tsx:139–377  ·  view source on GitHub ↗
({toc})

Source from the content-addressed store, hash-verified

137}
138
139export function MobileHeader({toc}) {
140 let ref = useRef<HTMLDivElement | null>(null);
141 let linkRef = useRef<HTMLAnchorElement | null>(null);
142 let labelRef = useRef<HTMLSpanElement | null>(null);
143 let {colorScheme} = useSettings();
144
145 useEffect(() => {
146 // Tiny polyfill for scroll driven animations.
147 // Element must have animationDuration: 1s and animationPlayState: paused.
148 if (!CSS.supports('(animation-timeline: scroll())') && ref.current) {
149 let [start, end] = animationRange.split(' ').map(v => parseInt(v, 10));
150 let animations = ref.current.getAnimations({subtree: true});
151 let onScroll = () => {
152 // Calculate animation time based on percentage of animationRange * duration.
153 let time = Math.max(0, Math.min(end, window.scrollY - start) / (end - start)) * 1000;
154 for (let animation of animations) {
155 animation.currentTime = time;
156 }
157 };
158
159 window.addEventListener('scroll', onScroll, {passive: true});
160 return () => window.removeEventListener('scroll', onScroll);
161 }
162 }, []);
163
164 let {currentPage} = useRouter();
165 let library = getLibraryFromPage(currentPage);
166 let icon = getLibraryIcon(library);
167 let subdirectory: 's2' | 'react-aria' = 's2';
168 if (library === 'react-aria') {
169 // the internationalized library has no homepage so i've chosen to route it to the react aria homepage
170 subdirectory = 'react-aria';
171 }
172
173 let baseUrl = getBaseUrl(subdirectory);
174 let homepage = `${baseUrl}/`;
175
176 let [isOpen, setOpen] = useState(false);
177 let [wasOpen, setWasOpen] = useState(isOpen);
178 let [isTransitioning, setTransitioning] = useState(false);
179 let renderCallback = useRef<(() => void) | null>(null);
180 let onOpenChange = (isOpen: boolean) => {
181 if (!document.startViewTransition) {
182 setOpen(false);
183 if (isOpen) {
184 setWasOpen(true);
185 }
186 return;
187 }
188
189 // Don't transition the entire page.
190 document.documentElement.style.viewTransitionName = 'none';
191
192 // Only transition label if it is visible (scrolled to the top of the page).
193 if (window.scrollY === 0 && labelRef.current && isOpen) {
194 labelRef.current.style.viewTransitionName = 'search-menu-label';
195 }
196

Callers

nothing calls this directly

Calls 10

useSettingsFunction · 0.90
useRouterFunction · 0.90
getLibraryFromPageFunction · 0.90
getLibraryIconFunction · 0.90
getBaseUrlFunction · 0.90
pressScaleFunction · 0.90
focusRingFunction · 0.90
addEventListenerMethod · 0.80
removeEventListenerMethod · 0.80
currentMethod · 0.45

Tested by

no test coverage detected