forked from patternfly/patternfly-doc-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageToggle.tsx
More file actions
50 lines (44 loc) · 1.68 KB
/
PageToggle.tsx
File metadata and controls
50 lines (44 loc) · 1.68 KB
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
import type React from 'react'
import styles from '@patternfly/react-styles/css/components/Page/page'
import { PageToggleButton } from '@patternfly/react-core'
import BarsIcon from '@patternfly/react-icons/dist/esm/icons/bars-icon'
import { useStore } from '@nanostores/react'
import { isNavOpen } from '../stores/navStore'
import { useEffect } from 'react'
export const PageToggle: React.FunctionComponent = () => {
const $isNavOpen = useStore(isNavOpen)
function onToggle() {
isNavOpen.set(!$isNavOpen)
}
useEffect(() => {
/** Applies sidebar styles to the island element astro creates as a wrapper for the sidebar.
* Without it the page content will not expand to fill the space left by the sidebar when it is collapsed.
*/
// Possibly can refactor to remove applying classes when https://github.com/patternfly/patternfly/issues/7377 goes in
const isClientSide = typeof window !== 'undefined'
const sideBarIsland =
document.getElementById('page-sidebar-body')?.parentElement
if (!isClientSide || !sideBarIsland) {
return
}
if (!sideBarIsland.classList.contains(styles.pageSidebar)) {
sideBarIsland.classList.add(
styles.pageSidebar,
$isNavOpen ? styles.modifiers.expanded : styles.modifiers.collapsed,
)
} else {
sideBarIsland.classList.toggle(styles.modifiers.expanded)
sideBarIsland.classList.toggle(styles.modifiers.collapsed)
}
sideBarIsland.setAttribute('aria-hidden', `${!$isNavOpen}`)
}, [$isNavOpen])
return (
<PageToggleButton
aria-label="Global navigation"
onSidebarToggle={onToggle}
isSidebarOpen={$isNavOpen}
>
<BarsIcon />
</PageToggleButton>
)
}