forked from chakra-ui/chakra-ui-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.tsx
165 lines (155 loc) · 4.59 KB
/
header.tsx
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
import {
Box,
Flex,
HStack,
HTMLChakraProps,
Icon,
IconButton,
Link,
chakra,
useColorMode,
useColorModeValue,
useDisclosure,
useUpdateEffect,
} from '@chakra-ui/react'
import { useViewportScroll } from 'framer-motion'
import NextLink from 'next/link'
import { useEffect, useRef, useState } from 'react'
import { FaMoon, FaSun, FaYoutube } from 'react-icons/fa'
import Logo, { LogoIcon } from './logo'
import { MobileNavButton, MobileNavContent } from './mobile-nav'
import Search from './omni-search'
import SponsorButton from './sponsor-button'
import VersionSwitcher from './version-switcher'
import { DiscordIcon, GithubIcon } from 'components/icons'
import siteConfig from 'configs/site-config.json'
function HeaderContent() {
const mobileNav = useDisclosure()
const { toggleColorMode: toggleMode } = useColorMode()
const text = useColorModeValue('dark', 'light')
const SwitchIcon = useColorModeValue(FaMoon, FaSun)
const mobileNavBtnRef = useRef<HTMLButtonElement>()
useUpdateEffect(() => {
mobileNavBtnRef.current?.focus()
}, [mobileNav.isOpen])
return (
<>
<Flex w='100%' h='100%' px='6' align='center' justify='space-between'>
<Flex align='center'>
<NextLink href='/' passHref>
<chakra.a display='block' aria-label='Chakra UI, Back to homepage'>
<Logo display={{ base: 'none', md: 'block' }} />
<Box minW='3rem' display={{ base: 'block', md: 'none' }}>
<LogoIcon />
</Box>
</chakra.a>
</NextLink>
</Flex>
<Flex
justify='flex-end'
w='100%'
align='center'
color='gray.400'
maxW='1100px'
>
<Search />
<VersionSwitcher
width='auto'
flexShrink={0}
display={{ base: 'none', md: 'flex' }}
/>
<HStack spacing='5' display={{ base: 'none', md: 'flex' }}>
<Link
isExternal
aria-label='Go to Chakra UI GitHub page'
href={siteConfig.repo.url}
>
<Icon
as={GithubIcon}
display='block'
transition='color 0.2s'
w='5'
h='5'
_hover={{ color: 'gray.600' }}
/>
</Link>
<Link aria-label='Go to Chakra UI Discord page' href='/discord'>
<Icon
as={DiscordIcon}
display='block'
transition='color 0.2s'
w='5'
h='5'
_hover={{ color: 'gray.600' }}
/>
</Link>
<Link
isExternal
aria-label='Go to Chakra UI YouTube channel'
href={siteConfig.youtube}
>
<Icon
as={FaYoutube}
display='block'
transition='color 0.2s'
w='5'
h='5'
_hover={{ color: 'gray.600' }}
/>
</Link>
</HStack>
<HStack spacing='5'>
<IconButton
size='md'
fontSize='lg'
aria-label={`Switch to ${text} mode`}
variant='ghost'
color='current'
ml={{ base: '0', md: '3' }}
onClick={toggleMode}
icon={<SwitchIcon />}
/>
<SponsorButton ml='5' />
<MobileNavButton
ref={mobileNavBtnRef}
aria-label='Open Menu'
onClick={mobileNav.onOpen}
/>
</HStack>
</Flex>
</Flex>
<MobileNavContent isOpen={mobileNav.isOpen} onClose={mobileNav.onClose} />
</>
)
}
function Header(props: HTMLChakraProps<'header'>) {
const { maxW = '8xl', maxWidth = '8xl' } = props
const ref = useRef<HTMLHeadingElement>()
const [y, setY] = useState(0)
const { height = 0 } = ref.current?.getBoundingClientRect() ?? {}
const { scrollY } = useViewportScroll()
useEffect(() => {
return scrollY.onChange(() => setY(scrollY.get()))
}, [scrollY])
return (
<chakra.header
ref={ref}
shadow={y > height ? 'sm' : undefined}
transition='box-shadow 0.2s, background-color 0.2s'
pos='sticky'
top='0'
zIndex='3'
bg='white'
_dark={{ bg: 'gray.800' }}
left='0'
right='0'
width='full'
{...props}
>
<chakra.div height='4.5rem' mx='auto' maxW={maxW} maxWidth={maxWidth}>
<HeaderContent />
</chakra.div>
</chakra.header>
)
}
export default Header