|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | 3 | import {baseColor, focusRing, space, style} from '@react-spectrum/s2/style' with { type: 'macro' }; |
| 4 | +import {Button, Link} from 'react-aria-components'; |
| 5 | +import Contrast from '@react-spectrum/s2/icons/Contrast'; |
| 6 | +import {Divider, pressScale} from '@react-spectrum/s2'; |
4 | 7 | import {getBaseUrl} from './pageUtils'; |
5 | 8 | import {getLibraryFromPage, getLibraryIcon, getLibraryLabel} from './library'; |
6 | 9 | import GithubLogo from './icons/GithubLogo'; |
7 | 10 | import {HeaderLink} from './Link'; |
8 | | -// @ts-ignore |
9 | | -import {Link} from 'react-aria-components'; |
| 11 | +import Lighten from '@react-spectrum/s2/icons/Lighten'; |
10 | 12 | import {NpmLogo} from './icons/NpmLogo'; |
11 | | -import {pressScale} from '@react-spectrum/s2'; |
12 | 13 | import React, {useId, useRef, useState} from 'react'; |
13 | 14 | import SearchMenuTrigger, {preloadSearchMenu} from './SearchMenuTrigger'; |
14 | 15 | import {useLayoutEffect} from '@react-aria/utils'; |
15 | 16 | import {useRouter} from './Router'; |
| 17 | +import {useSettings} from './SettingsContext'; |
16 | 18 |
|
17 | 19 | function getButtonText(currentPage) { |
18 | 20 | return getLibraryLabel(getLibraryFromPage(currentPage)); |
@@ -44,6 +46,69 @@ const libraryStyles = style({ |
44 | 46 | marginStart: space(26) |
45 | 47 | }); |
46 | 48 |
|
| 49 | +const colorSchemeToggleStyles = style({ |
| 50 | + ...focusRing(), |
| 51 | + font: 'ui', |
| 52 | + color: 'neutral', |
| 53 | + textDecoration: 'none', |
| 54 | + transition: 'default', |
| 55 | + backgroundColor: { |
| 56 | + default: { |
| 57 | + ...baseColor('gray-100'), |
| 58 | + default: 'transparent' |
| 59 | + } |
| 60 | + }, |
| 61 | + size: 32, |
| 62 | + display: 'flex', |
| 63 | + alignItems: 'center', |
| 64 | + justifyContent: 'center', |
| 65 | + borderRadius: 'lg', |
| 66 | + borderWidth: 0 |
| 67 | +}); |
| 68 | + |
| 69 | +const iconContainerStyles = style({ |
| 70 | + position: 'relative', |
| 71 | + size: 20 |
| 72 | +}); |
| 73 | + |
| 74 | +function ColorSchemeToggle() { |
| 75 | + let {colorScheme, toggleColorScheme, systemColorScheme} = useSettings(); |
| 76 | + let isOverriding = colorScheme !== systemColorScheme; |
| 77 | + let label = isOverriding |
| 78 | + ? `Using ${colorScheme} mode (press to use system)` |
| 79 | + : `Using system ${systemColorScheme} mode (press to switch)`; |
| 80 | + let ref = useRef(null); |
| 81 | + let isDark = colorScheme === 'dark'; |
| 82 | + |
| 83 | + return ( |
| 84 | + <Button |
| 85 | + ref={ref} |
| 86 | + aria-label={label} |
| 87 | + onPress={toggleColorScheme} |
| 88 | + className={renderProps => colorSchemeToggleStyles(renderProps)} |
| 89 | + style={pressScale(ref)}> |
| 90 | + <span className={iconContainerStyles}> |
| 91 | + <Contrast |
| 92 | + UNSAFE_style={{ |
| 93 | + position: 'absolute', |
| 94 | + inset: 0, |
| 95 | + opacity: isDark ? 0 : 1, |
| 96 | + transform: isDark ? 'rotate(-90deg) scale(0.5)' : 'rotate(0deg) scale(1)', |
| 97 | + transition: 'opacity 200ms ease-out, transform 200ms ease-out' |
| 98 | + }} /> |
| 99 | + <Lighten |
| 100 | + UNSAFE_style={{ |
| 101 | + position: 'absolute', |
| 102 | + inset: 0, |
| 103 | + opacity: isDark ? 1 : 0, |
| 104 | + transform: isDark ? 'rotate(0deg) scale(1)' : 'rotate(90deg) scale(0.5)', |
| 105 | + transition: 'opacity 200ms ease-out, transform 200ms ease-out' |
| 106 | + }} /> |
| 107 | + </span> |
| 108 | + </Button> |
| 109 | + ); |
| 110 | +} |
| 111 | + |
47 | 112 | export default function Header() { |
48 | 113 | const {currentPage} = useRouter(); |
49 | 114 | const [searchOpen, setSearchOpen] = useState(false); |
@@ -166,6 +231,12 @@ export default function Header() { |
166 | 231 | <HeaderLink href={blog} target={subdirectory === 's2' ? '_blank' : ''} rel="noopener noreferrer">Blog</HeaderLink> |
167 | 232 | <HeaderLink aria-label="GitHub" href="https://github.com/adobe/react-spectrum" target="_blank" rel="noopener noreferrer" ><GithubLogo /></HeaderLink> |
168 | 233 | <HeaderLink aria-label="npm" href={`https://npmjs.com/${npm}`} target="_blank" rel="noopener noreferrer"><NpmLogo /></HeaderLink> |
| 234 | + {library !== 'react-aria' && ( |
| 235 | + <> |
| 236 | + <Divider orientation="vertical" UNSAFE_style={{marginBlock: 4}} /> |
| 237 | + <ColorSchemeToggle /> |
| 238 | + </> |
| 239 | + )} |
169 | 240 | </div> |
170 | 241 | </div> |
171 | 242 | </header> |
|
0 commit comments