Skip to content

Commit 7b5b482

Browse files
author
Eric Olkowski
committed
Move apply styles function to effect hook
1 parent 6c8acf9 commit 7b5b482

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

src/components/PageToggle.tsx

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,38 @@ import { useEffect } from 'react'
99
export const PageToggle: React.FunctionComponent = () => {
1010
const $isNavOpen = useStore(isNavOpen)
1111

12-
/** Applies sidebar styles to the island element astro creates as a wrapper for the sidebar.
13-
* Without it the page content will not expand to fill the space left by the sidebar when it is collapsed.
14-
*/
15-
// Possibly can refactor to remove applying classes when https://github.com/patternfly/patternfly/issues/7377 goes in
16-
function applySidebarStylesToIsland() {
17-
const isClientSide = typeof window !== 'undefined'
18-
const sideBarIsland =
19-
document.getElementById('page-sidebar-body')?.parentElement
20-
21-
if (!isClientSide || !sideBarIsland) {
22-
return
23-
}
24-
25-
if (!sideBarIsland.classList.contains(styles.pageSidebar)) {
26-
sideBarIsland.classList.add(
27-
styles.pageSidebar,
28-
$isNavOpen ? styles.modifiers.expanded : styles.modifiers.collapsed,
29-
)
30-
} else {
31-
sideBarIsland.classList.toggle(styles.modifiers.expanded)
32-
sideBarIsland.classList.toggle(styles.modifiers.collapsed)
33-
}
34-
sideBarIsland.setAttribute('aria-hidden', `${!$isNavOpen}`)
35-
}
36-
3712
function onToggle() {
3813
isNavOpen.set(!$isNavOpen)
3914
}
4015

4116
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
21+
function applySidebarStylesToIsland() {
22+
const isClientSide = typeof window !== 'undefined'
23+
const sideBarIsland =
24+
document.getElementById('page-sidebar-body')?.parentElement
25+
26+
if (!isClientSide || !sideBarIsland) {
27+
return
28+
}
29+
30+
if (!sideBarIsland.classList.contains(styles.pageSidebar)) {
31+
sideBarIsland.classList.add(
32+
styles.pageSidebar,
33+
$isNavOpen ? styles.modifiers.expanded : styles.modifiers.collapsed,
34+
)
35+
} else {
36+
sideBarIsland.classList.toggle(styles.modifiers.expanded)
37+
sideBarIsland.classList.toggle(styles.modifiers.collapsed)
38+
}
39+
sideBarIsland.setAttribute('aria-hidden', `${!$isNavOpen}`)
40+
}
41+
4242
applySidebarStylesToIsland()
43-
}, [$isNavOpen, applySidebarStylesToIsland])
43+
}, [$isNavOpen])
4444

4545
return (
4646
<PageToggleButton

0 commit comments

Comments
 (0)