|
4 | 4 | */
|
5 | 5 |
|
6 | 6 | import React from 'react';
|
| 7 | +import cn from 'classnames'; |
7 | 8 | import {ExternalLink} from './ExternalLink';
|
8 | 9 |
|
9 |
| -// TODO: Unify with the old site settings. |
10 | 10 | const bannerText = 'Support Ukraine 🇺🇦';
|
11 | 11 | const bannerLink = 'https://opensource.fb.com/support-ukraine';
|
12 | 12 | const bannerLinkText = 'Help Provide Humanitarian Aid to Ukraine';
|
13 | 13 |
|
14 |
| -// Keep these synced: |
15 |
| -const bannerHeightJs = 40; |
16 |
| -const bannerHeightTw = 'h-[40px]'; |
17 |
| - |
18 |
| -if (typeof window !== 'undefined') { |
19 |
| - // Assume it's Next.js scroll restoration. |
20 |
| - const realScrollTo = window.scrollTo; |
21 |
| - (window as any).scrollTo = function scrollToPatchedForSocialBanner( |
22 |
| - x: number, |
23 |
| - y: number |
24 |
| - ) { |
25 |
| - if (y === 0) { |
26 |
| - // We're trying to reset scroll. |
27 |
| - // If we already scrolled past the banner, consider it as y = 0. |
28 |
| - y = Math.min(window.scrollY, bannerHeightJs); |
29 |
| - } |
30 |
| - return realScrollTo(x, y); |
31 |
| - }; |
32 |
| -} |
33 |
| - |
34 | 14 | export default function SocialBanner() {
|
| 15 | + const ref = React.useRef<HTMLDivElement | null>(null); |
| 16 | + React.useEffect(() => { |
| 17 | + function patchedScrollTo(x: number, y: number) { |
| 18 | + if (y === 0) { |
| 19 | + // We're trying to reset scroll. |
| 20 | + // If we already scrolled past the banner, consider it as y = 0. |
| 21 | + const bannerHeight = ref.current!.offsetHeight; // Could be zero (e.g. mobile) |
| 22 | + y = Math.min(window.scrollY, bannerHeight); |
| 23 | + } |
| 24 | + return realScrollTo(x, y); |
| 25 | + } |
| 26 | + const realScrollTo = window.scrollTo; |
| 27 | + (window as any).scrollTo = patchedScrollTo; |
| 28 | + return () => { |
| 29 | + (window as any).scrollTo = realScrollTo; |
| 30 | + }; |
| 31 | + }, []); |
35 | 32 | return (
|
36 | 33 | <div
|
37 |
| - className={ |
38 |
| - bannerHeightTw + |
39 |
| - ` w-full bg-gray-100 dark:bg-gray-700 text-base md:text-lg py-2 sm:py-0 flex items-center justify-center flex-col sm:flex-row z-[100]` |
40 |
| - }> |
| 34 | + ref={ref} |
| 35 | + className={cn( |
| 36 | + `h-[40px] hidden lg:flex w-full bg-gray-100 dark:bg-gray-700 text-base md:text-lg py-2 sm:py-0 items-center justify-center flex-col sm:flex-row z-[100]` |
| 37 | + )}> |
41 | 38 | <div className="hidden sm:block">{bannerText}</div>
|
42 | 39 | <ExternalLink
|
43 | 40 | className="ml-0 sm:ml-1 text-link dark:text-link-dark hover:underline"
|
|
0 commit comments