Skip to content

Commit 44c02a0

Browse files
fix(docs-framework): remove useTheme from footer to fix theme state bug (#4817)
* fix(docs-framework): remove useTheme from footer to fix theme state bug * fix(theme-switcher): add loading spinner for the theme switcher * add missing comma from conflict resolution --------- Co-authored-by: Nicole Thoen <nthoen@redhat.com>
1 parent 9b6d110 commit 44c02a0

File tree

2 files changed

+63
-33
lines changed

2 files changed

+63
-33
lines changed

packages/documentation-framework/components/themeSelector/themeSelector.js

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ToggleGroupItem,
1212
Icon,
1313
Divider,
14+
Spinner,
1415
Label,
1516
Popover,
1617
Button
@@ -125,8 +126,10 @@ export const ThemeSelector = ({ id }) => {
125126
return SunIcon;
126127
case colorModes.DARK:
127128
return MoonIcon;
128-
default:
129+
case colorModes.SYSTEM:
129130
return DesktopIcon;
131+
default:
132+
return <Spinner size="sm" />;
130133
}
131134
};
132135

@@ -148,7 +151,7 @@ export const ThemeSelector = ({ id }) => {
148151
)}
149152
shouldFocusToggleOnSelect
150153
onOpenChangeKeys={['Escape']}
151-
popperProps={{
154+
popperProps={{
152155
position: 'right',
153156
enableFlip: true,
154157
preventOverflow: true
@@ -167,35 +170,36 @@ export const ThemeSelector = ({ id }) => {
167170
</SelectOption>
168171
</SelectList>
169172
</SelectGroup>
170-
{process.env.hasHighContrastSwitcher && (<>
171-
<Divider />
172-
<SelectGroup label={HighContrastGroupLabel}>
173-
<MenuSearch>
174-
<MenuSearchInput>
175-
<ToggleGroup aria-label="High contrast theme switcher">
176-
<ToggleGroupItem
177-
text="System"
178-
buttonId={highContrastModes.SYSTEM}
179-
isSelected={highContrastMode === highContrastModes.SYSTEM}
180-
onChange={handleHighContrastThemeSelection}
181-
/>
182-
<ToggleGroupItem
183-
text="On"
184-
buttonId={highContrastModes.ON}
185-
isSelected={highContrastMode === highContrastModes.ON}
186-
onChange={handleHighContrastThemeSelection}
187-
/>
188-
<ToggleGroupItem
189-
text="Off"
190-
buttonId={highContrastModes.OFF}
191-
isSelected={highContrastMode === highContrastModes.OFF}
192-
onChange={handleHighContrastThemeSelection}
193-
/>
194-
</ToggleGroup>
195-
</MenuSearchInput>
196-
</MenuSearch>
197-
</SelectGroup>
198-
</>
173+
{process.env.hasHighContrastSwitcher && (
174+
<>
175+
<Divider />
176+
<SelectGroup label="High Contrast">
177+
<MenuSearch>
178+
<MenuSearchInput>
179+
<ToggleGroup aria-label="High contrast theme switcher">
180+
<ToggleGroupItem
181+
text="System"
182+
buttonId={highContrastModes.SYSTEM}
183+
isSelected={highContrastMode === highContrastModes.SYSTEM}
184+
onChange={handleHighContrastThemeSelection}
185+
/>
186+
<ToggleGroupItem
187+
text="On"
188+
buttonId={highContrastModes.ON}
189+
isSelected={highContrastMode === highContrastModes.ON}
190+
onChange={handleHighContrastThemeSelection}
191+
/>
192+
<ToggleGroupItem
193+
text="Off"
194+
buttonId={highContrastModes.OFF}
195+
isSelected={highContrastMode === highContrastModes.OFF}
196+
onChange={handleHighContrastThemeSelection}
197+
/>
198+
</ToggleGroup>
199+
</MenuSearchInput>
200+
</MenuSearch>
201+
</SelectGroup>
202+
</>
199203
)}
200204
</Select>
201205
);

packages/documentation-framework/layouts/sideNavLayout/sideNavLayout.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ export function attachDocSearch(algolia, inputSelector, timeout) {
208208
}
209209
}
210210

211+
function getIsDarkTheme() {
212+
return document.documentElement.classList.contains('pf-v6-theme-dark');
213+
}
214+
211215
export const SideNavLayout = ({ children, groupedRoutes, navOpen: navOpenProp }) => {
212216
const algolia = process.env.algolia;
213217
const hasGdprBanner = process.env.hasGdprBanner;
@@ -221,8 +225,30 @@ export const SideNavLayout = ({ children, groupedRoutes, navOpen: navOpenProp })
221225

222226
const [versions, setVersions] = useState({ ...staticVersions });
223227
const [isRTL, setIsRTL] = useState(false);
228+
const [isDarkTheme, setIsDarkTheme] = useState(true);
229+
230+
useEffect(() => {
231+
setIsDarkTheme(getIsDarkTheme());
224232

225-
const { resolvedTheme } = useTheme(THEME_TYPES.COLOR);
233+
const observer = new MutationObserver((mutationsList) => {
234+
for (const mutation of mutationsList) {
235+
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
236+
setIsDarkTheme(getIsDarkTheme());
237+
}
238+
}
239+
});
240+
241+
const config = {
242+
attributes: true,
243+
attributeFilter: ['class']
244+
};
245+
246+
observer.observe(document.documentElement, config);
247+
248+
return () => {
249+
observer.disconnect();
250+
};
251+
}, []);
226252

227253
useEffect(() => {
228254
if (typeof window === 'undefined') {
@@ -335,7 +361,7 @@ export const SideNavLayout = ({ children, groupedRoutes, navOpen: navOpenProp })
335361
defaultManagedSidebarIsOpen={navOpenProp}
336362
>
337363
{children}
338-
{process.env.hasFooter && <Footer isDarkTheme={resolvedTheme === 'dark'} />}
364+
{process.env.hasFooter && <Footer isDarkTheme={isDarkTheme} />}
339365
</Page>
340366
<div id="ws-page-banners">{hasGdprBanner && <GdprBanner />}</div>
341367
</RtlContext.Provider>

0 commit comments

Comments
 (0)