Skip to content

Commit aa7850b

Browse files
Merge pull request #28 from thatblindgeye/sidebarBug
fix(PageToggle): resolved sidebar not collapsing correctly
2 parents 48449a1 + d32e1d2 commit aa7850b

File tree

3 files changed

+164
-175
lines changed

3 files changed

+164
-175
lines changed

src/components/Navigation.tsx

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import { useEffect, useState } from 'react'
2-
import {
3-
Nav,
4-
NavList,
5-
PageSidebar,
6-
PageSidebarBody,
7-
} from '@patternfly/react-core'
8-
import { useStore } from '@nanostores/react'
9-
import { isNavOpen } from '../stores/navStore'
2+
import { Nav, NavList, PageSidebarBody } from '@patternfly/react-core'
103
import { NavSection } from './NavSection'
114
import { type TextContentEntry } from './NavEntry'
125

@@ -17,7 +10,6 @@ interface NavigationProps {
1710
export const Navigation: React.FunctionComponent<NavigationProps> = ({
1811
navEntries,
1912
}: NavigationProps) => {
20-
const $isNavOpen = useStore(isNavOpen)
2113
const [activeItem, setActiveItem] = useState('')
2214

2315
useEffect(() => {
@@ -47,12 +39,11 @@ export const Navigation: React.FunctionComponent<NavigationProps> = ({
4739
})
4840

4941
return (
50-
<PageSidebar isSidebarOpen={$isNavOpen}>
51-
<PageSidebarBody>
52-
<Nav onSelect={onNavSelect}>
53-
<NavList>{navSections}</NavList>
54-
</Nav>
55-
</PageSidebarBody>
56-
</PageSidebar>
42+
// Can possibly add back PageSidebar wrapper when https://github.com/patternfly/patternfly/issues/7377 goes in
43+
<PageSidebarBody id="page-sidebar-body">
44+
<Nav onSelect={onNavSelect}>
45+
<NavList>{navSections}</NavList>
46+
</Nav>
47+
</PageSidebarBody>
5748
)
5849
}

src/components/PageToggle.tsx

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type React from 'react'
2+
import styles from '@patternfly/react-styles/css/components/Page/page'
23
import { PageToggleButton } from '@patternfly/react-core'
34
import BarsIcon from '@patternfly/react-icons/dist/esm/icons/bars-icon'
45
import { useStore } from '@nanostores/react'
@@ -8,38 +9,40 @@ import { useEffect } from 'react'
89
export const PageToggle: React.FunctionComponent = () => {
910
const $isNavOpen = useStore(isNavOpen)
1011

11-
/** Applies sidebar styles to the island element astro creates as a wrapper for the sidebar.
12-
* Without it the page content will not expand to fill the space left by the sidebar when it is collapsed.
13-
*/
14-
function applySidebarStylesToIsland() {
12+
function onToggle() {
13+
isNavOpen.set(!$isNavOpen)
14+
}
15+
16+
useEffect(() => {
17+
/** Applies sidebar styles to the island element astro creates as a wrapper for the sidebar.
18+
* Without it the page content will not expand to fill the space left by the sidebar when it is collapsed.
19+
*/
20+
// Possibly can refactor to remove applying classes when https://github.com/patternfly/patternfly/issues/7377 goes in
1521
const isClientSide = typeof window !== 'undefined'
16-
const sideBarIsland = document.getElementById('page-sidebar')?.parentElement
22+
const sideBarIsland =
23+
document.getElementById('page-sidebar-body')?.parentElement
1724

1825
if (!isClientSide || !sideBarIsland) {
1926
return
2027
}
2128

22-
if (!sideBarIsland.classList.contains('pf-v6-c-page__sidebar')) {
23-
sideBarIsland.classList.add('pf-v6-c-page__sidebar', 'pf-m-expanded')
29+
if (!sideBarIsland.classList.contains(styles.pageSidebar)) {
30+
sideBarIsland.classList.add(
31+
styles.pageSidebar,
32+
$isNavOpen ? styles.modifiers.expanded : styles.modifiers.collapsed,
33+
)
2434
} else {
25-
sideBarIsland.classList.toggle('pf-m-expanded')
26-
sideBarIsland.classList.toggle('pf-m-collapsed')
35+
sideBarIsland.classList.toggle(styles.modifiers.expanded)
36+
sideBarIsland.classList.toggle(styles.modifiers.collapsed)
2737
}
28-
}
29-
30-
function onToggle() {
31-
isNavOpen.set(!$isNavOpen)
32-
}
33-
34-
useEffect(() => {
35-
applySidebarStylesToIsland()
38+
sideBarIsland.setAttribute('aria-hidden', `${!$isNavOpen}`)
3639
}, [$isNavOpen])
3740

3841
return (
3942
<PageToggleButton
40-
variant="plain"
4143
aria-label="Global navigation"
4244
onSidebarToggle={onToggle}
45+
isSidebarOpen={$isNavOpen}
4346
>
4447
<BarsIcon />
4548
</PageToggleButton>

0 commit comments

Comments
 (0)