Skip to content

Commit

Permalink
feat(General): ✨ Save theme preference
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoval committed Oct 25, 2023
1 parent ec959e6 commit 5b10246
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="en" class="pf-v5-theme-dark">
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="favicon.ico" />
Expand Down
56 changes: 54 additions & 2 deletions src/layout/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FormEvent, useEffect, useState } from 'react';

import {
Brand,
Masthead,
Expand All @@ -6,13 +8,46 @@ import {
MastheadMain,
MastheadToggle,
PageToggleButton,
Title
Switch,
Title,
Toolbar,
ToolbarContent,
ToolbarGroup,
ToolbarItem
} from '@patternfly/react-core';
import { BarsIcon } from '@patternfly/react-icons';

import { brandLogo, brandName } from '@config/config';

const storageKey = 'theme-preference';
const DARK_THEME_CLASS = 'pf-v5-theme-dark';

const SkHeader = function () {
const [isChecked, setIsChecked] = useState<boolean>(false);

const handleChange = (_event: FormEvent<HTMLInputElement>, checked: boolean) => {
const htmlElement = document.querySelector('html') as HTMLElement;

checked ? htmlElement.classList.add(DARK_THEME_CLASS) : htmlElement.classList.remove(DARK_THEME_CLASS);
checked ? localStorage.setItem(storageKey, DARK_THEME_CLASS) : localStorage.removeItem(storageKey);

setIsChecked(checked);
};

useEffect(() => {
const themePreference = localStorage.getItem(storageKey);

if (themePreference) {
const htmlElement = document.querySelector('html') as HTMLElement;
htmlElement.classList.add(themePreference);
setIsChecked(true);

return;
}

setIsChecked(false);
}, []);

return (
<Masthead className="sk-header" data-testid="sk-header">
<MastheadToggle>
Expand All @@ -27,7 +62,24 @@ const SkHeader = function () {
</Brand>
</MastheadBrand>
</MastheadMain>
<MastheadContent> {brandName && <Title headingLevel="h1">{brandName}</Title>}</MastheadContent>
<MastheadContent>
{brandName && <Title headingLevel="h1">{'brandName'}</Title>}
<Toolbar>
<ToolbarContent>
<ToolbarGroup align={{ default: 'alignRight' }}>
<ToolbarItem>
<Switch
label="Dark theme"
labelOff="Dark theme"
isChecked={isChecked}
onChange={handleChange}
ouiaId="BasicSwitch"
/>
</ToolbarItem>
</ToolbarGroup>
</ToolbarContent>
</Toolbar>
</MastheadContent>
</Masthead>
);
};
Expand Down
1 change: 1 addition & 0 deletions src/layout/MainContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import NavigationViewLink from '../core/components/NavigationViewLink';
import TransitionPage from '../core/components/TransitionPages/Fade';

import '@patternfly/patternfly/patternfly-addons.css';
import '@patternfly/patternfly/patternfly-charts-theme-dark.css';

interface MainContainerProps {
dataTestId?: string;
Expand Down

0 comments on commit 5b10246

Please sign in to comment.