Skip to content

Commit f15e3fa

Browse files
authored
add option to enable a light theme (#29)
* Added two ways so set the theme. Either through the context globally or by explicitly providing it * Improved some styles that were not looking good in the light theme
1 parent bac5185 commit f15e3fa

6 files changed

Lines changed: 38 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added an option to enable the light theme. It can be provided explicitly every time when using the `SideBarMenu` component, or as a default in a SideBarLayoutProvider
13+
1014
## [2.5.0] - 2023-08-08
1115

1216
### Added

src/lib/Layout/SideBarLayout/SideBarLayout.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@ import { useSideBarLayoutContext } from "./SideBarLayoutContext";
1111
interface SideBarLayoutProps extends PropsWithChildren {
1212
brand?: ReactNode;
1313
footer?: ReactNode;
14+
theme?: "dark" | "light";
1415
}
1516

1617
const SideBarLayout = (props: SideBarLayoutProps) => {
17-
const { brand, children, footer } = props;
18+
const { brand, children, footer, theme } = props;
1819

1920
const [isOpen, setIsOpen] = useState(true);
2021
const toggleSidebar = () => setIsOpen((prev) => !prev);
2122

22-
const { brand: contextBrand, userDropDownMenu, userDropDownMenuToggle } = useSideBarLayoutContext();
23+
const { brand: contextBrand, theme: contextTheme, userDropDownMenu, userDropDownMenuToggle } = useSideBarLayoutContext();
24+
const chosenTheme = theme ?? contextTheme;
2325

2426
return (
2527
<>
26-
<nav id="nav-top" className="navbar navbar-expand navbar-dark">
28+
<nav id="nav-top" className={`navbar navbar-expand navbar-${chosenTheme}`}>
2729
{/* Navbar Brand*/}
2830
<div className="navbar-brand">{brand ?? contextBrand}</div>
2931

@@ -47,7 +49,7 @@ const SideBarLayout = (props: SideBarLayoutProps) => {
4749
</Nav>
4850
</nav>
4951
<section id="layout-sidenav" className={classNames({ toggled: !isOpen })}>
50-
<SideBarMenu />
52+
<SideBarMenu theme={chosenTheme} />
5153
<SideBarLayoutContent footer={footer}>{children}</SideBarLayoutContent>
5254
</section>
5355
</>

src/lib/Layout/SideBarLayout/SideBarLayoutContext.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import { createContext, PropsWithChildren, ReactNode, useContext } from "react";
22

33
interface SideBarLayoutProviderProps {
4+
theme?: "dark" | "light";
45
footer?: ReactNode;
56
brand?: ReactNode;
67
userDropDownMenuToggle?: ReactNode;
78
userDropDownMenu?: ReactNode;
89
}
910

10-
const SideBarLayoutContext = createContext<SideBarLayoutProviderProps | null>(null);
11+
interface SideBarLayoutContextProps extends SideBarLayoutProviderProps {
12+
theme: "dark" | "light";
13+
}
1114

12-
const SideBarLayoutProvider = (props: PropsWithChildren<SideBarLayoutProviderProps>) => {
13-
const { brand = null, children, footer = null, userDropDownMenu, userDropDownMenuToggle } = props;
15+
const SideBarLayoutContext = createContext<SideBarLayoutContextProps | null>(null);
1416

17+
const SideBarLayoutProvider = (props: PropsWithChildren<SideBarLayoutProviderProps>) => {
18+
const { brand = null, children, footer = null, theme = "dark", userDropDownMenu, userDropDownMenuToggle } = props;
1519
return (
16-
<SideBarLayoutContext.Provider value={{ brand, footer, userDropDownMenu, userDropDownMenuToggle }}>
20+
<SideBarLayoutContext.Provider value={{ brand, footer, theme, userDropDownMenu, userDropDownMenuToggle }}>
1721
{children}
1822
</SideBarLayoutContext.Provider>
1923
);

src/lib/SideBar/SideBar.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import SideBarMenu from "./SideBarMenu";
55
interface SideBarProps {
66
isOpen: boolean;
77
toggle: () => void;
8+
theme: "dark" | "light";
89
}
910

10-
const SideBar: FC<SideBarProps> = ({ isOpen, toggle }) => (
11+
const SideBar: FC<SideBarProps> = ({ isOpen, toggle, theme }) => (
1112
<div className={classNames("sidebar", { "is-open": isOpen })}>
1213
<div className="sidebar-header">
1314
<span color="info" onClick={toggle} style={{ color: "#fff" }}>
@@ -18,7 +19,7 @@ const SideBar: FC<SideBarProps> = ({ isOpen, toggle }) => (
1819
</div>
1920
</div>
2021
<div className="side-menu">
21-
<SideBarMenu></SideBarMenu>
22+
<SideBarMenu theme={theme}></SideBarMenu>
2223
</div>
2324
</div>
2425
);

src/lib/SideBar/SideBarMenu.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { useSideBarMenuContext } from "./SideBarMenuContext";
22
import { SideBarMenuItem } from "./SideBarMenuItem";
33

4-
const SideBarMenu = () => {
4+
interface SideBarMenuProps {
5+
theme: "dark" | "light";
6+
}
7+
8+
const SideBarMenu = ({ theme }: SideBarMenuProps) => {
59
const { items } = useSideBarMenuContext();
610

711
return (
812
<div id="layout-sidenav__nav">
9-
<nav className="sb-sidenav accordion sb-sidenav-dark">
13+
<nav className={`sb-sidenav accordion sb-sidenav-${theme}`}>
1014
<div className="sb-sidenav-menu">
1115
<div className="nav">
1216
{items?.map((x, i) => (

styles/Layout/SideBarLayout.scss

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ $topnav-light-toggler-color: $gray-900;
6666
z-index: $zindex-sidenav;
6767
// Mobile first transform - by default the sidenav will be moved off-canvas
6868
transform: translateX(-$sidenav-base-width);
69-
box-shadow: 0 0 0.5rem 0.125rem rgba($color: $black, $alpha: 0.75);
69+
box-shadow: 0 0 0.5rem 0.125rem rgba($color: $gray-500, $alpha: 0.75);
7070
}
7171

7272
&__content {
@@ -208,7 +208,7 @@ $topnav-light-toggler-color: $gray-900;
208208
}
209209

210210
&.navbar-light {
211-
background-color: $sidenav-light-bg;
211+
background-color: $gray-200;
212212
color: $topnav-light-toggler-color;
213213

214214
#sidebar-toggle {
@@ -244,6 +244,7 @@ $topnav-light-toggler-color: $gray-900;
244244
flex-direction: column;
245245
height: 100%;
246246
flex-wrap: nowrap;
247+
background-color: $white;
247248

248249
.sb-sidenav-menu {
249250
flex-grow: 1;
@@ -261,11 +262,6 @@ $topnav-light-toggler-color: $gray-900;
261262
}
262263

263264
.nav-item.menu-open .nav-link {
264-
&::after,
265-
div {
266-
color: $sidenav-dark-link-active-color;
267-
}
268-
269265
&::after {
270266
@include chevron-down();
271267
}
@@ -332,6 +328,14 @@ $topnav-light-toggler-color: $gray-900;
332328
background-color: $sidenav-dark-bg;
333329
color: $sidenav-dark-color;
334330

331+
.nav {
332+
.nav-item.menu-open .nav-link {
333+
&::after,
334+
div {
335+
color: $sidenav-dark-link-active-color;
336+
}
337+
}
338+
}
335339
.sb-sidenav-menu {
336340
&::-webkit-scrollbar {
337341
width: 0.25rem;

0 commit comments

Comments
 (0)