Skip to content

Commit c11e90b

Browse files
authored
Moved mainContentBodyRef to context (#69)
1 parent 7c6ebdd commit c11e90b

5 files changed

Lines changed: 23 additions & 17 deletions

File tree

CHANGELOG.md

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

1010
### dependabot: \#68 Bump the github-actions group with 5 updates
1111

12+
### Removed
13+
14+
- :boom: The property `mainContentBodyRef` from `PanelSideBarLayout`.
15+
16+
### Added
17+
18+
- The ref `mainContentBodyRef` exposed to through the context to be used to control the main content body behaviour (e.g. scroll to top on page change).
19+
1220
## [4.3.0] - 2024-10-23
1321

1422
### Added

src/lib/Layout/PanelSideBarLayout/PanelSideBar/Context/PanelSideBarContext.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Context, createContext, useContext, useEffect, useMemo, useState } from "react";
1+
import React, { Context, createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
22
import { getActivePanel, getActivePanelParentsIds } from "../Utils/getActivePanel";
33
import { PanelSideBarContextProps } from "./PanelSideBarContextProps";
44
import { getHiddenPanelIds, getPreExpandedMenuItems } from "../Utils/panelUtils";
@@ -36,6 +36,7 @@ export const PanelSideBarProvider = <TPanelItemId extends string, TPanelItem>(
3636
renderFirstItemsLevelAsTiles = true,
3737
theme = "blue",
3838
} = props;
39+
const mainContentBodyRef = useRef<HTMLElement>(null);
3940
const menuItems = useMemo(() => defaultMenuItems, [defaultMenuItems]);
4041
const [isSidebarOpen, setIsSidebarOpen] = useState(sidebarOpenByDefault);
4142
const toggleSidebar = () => setIsSidebarOpen((prev) => !prev);
@@ -106,6 +107,7 @@ export const PanelSideBarProvider = <TPanelItemId extends string, TPanelItem>(
106107
closeMenuItems,
107108
hiddenMenuItemIds,
108109
setHiddenMenuItemsIds,
110+
mainContentBodyRef,
109111
}}
110112
>
111113
{children}

src/lib/Layout/PanelSideBarLayout/PanelSideBar/Context/PanelSideBarContextProps.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Dispatch, SetStateAction } from "react";
1+
import { Dispatch, MutableRefObject, SetStateAction } from "react";
22
import { PanelItem } from "../Definitions/PanelItem";
33
import { PanelLinkRenderer } from "../Definitions/PanelLinkRenderer";
44
import { MenuItemToggleFn } from "./PanelSideBarContext";
@@ -87,4 +87,9 @@ export interface PanelSideBarContextProps<TPanelItemId extends string, TPanelIte
8787
* @param includeActivePanel whether needs to include the active panel
8888
*/
8989
closeMenuItems: (panelItemIds: TPanelItemId[], includeActivePanel?: boolean) => void;
90+
91+
/**
92+
* the main content body ref
93+
*/
94+
mainContentBodyRef: MutableRefObject<HTMLElement | null>;
9095
}

src/lib/Layout/PanelSideBarLayout/PanelSideBarLayout.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import classNames from "classnames";
2-
import { MutableRefObject, PropsWithChildren, ReactNode, useMemo } from "react";
2+
import { PropsWithChildren, ReactNode, useMemo } from "react";
33
import "../../../../styles/Layout/index.scss";
44
import { PanelSideBar } from "./PanelSideBar/PanelSidebar";
55
import { PanelSideBarLayoutContent } from "./PanelSideBarLayoutContent";
@@ -38,11 +38,6 @@ export interface PanelSideBarLayoutProps extends PropsWithChildren {
3838
* If use the responsive layout when the screen is sm in order to remove the sidebar overlay.
3939
*/
4040
useResponsiveLayout?: boolean;
41-
42-
/**
43-
* the main content body ref
44-
*/
45-
mainContentBodyRef?: MutableRefObject<HTMLElement | null>;
4641
}
4742

4843
export const PanelSideBarLayout = <TPanelItemId extends string, TPanelItem>(props: PanelSideBarLayoutProps) => {
@@ -55,7 +50,6 @@ export const PanelSideBarLayout = <TPanelItemId extends string, TPanelItem>(prop
5550
collapsible = true,
5651
useToggleButton = false,
5752
useResponsiveLayout = false,
58-
mainContentBodyRef,
5953
} = props;
6054

6155
const { isSidebarOpen, toggleSidebar, renderFirstItemsLevelAsTiles, menuItems, activePanelId } = usePanelSideBarContext<
@@ -97,11 +91,7 @@ export const PanelSideBarLayout = <TPanelItemId extends string, TPanelItem>(prop
9791
isIconShownOnSidebarCollapse={isIconShownOnSidebarCollapse}
9892
/>
9993
)}
100-
<PanelSideBarLayoutContent
101-
footer={footer}
102-
mainContentBodyRef={mainContentBodyRef}
103-
isIconShownOnSidebarCollapse={isIconShownOnSidebarCollapse}
104-
>
94+
<PanelSideBarLayoutContent footer={footer} isIconShownOnSidebarCollapse={isIconShownOnSidebarCollapse}>
10595
{children}
10696
</PanelSideBarLayoutContent>
10797
</section>

src/lib/Layout/PanelSideBarLayout/PanelSideBarLayoutContent.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import classNames from "classnames";
2-
import { MutableRefObject, PropsWithChildren, ReactNode } from "react";
2+
import { PropsWithChildren, ReactNode } from "react";
3+
import { usePanelSideBarContext } from "./PanelSideBar/Context/PanelSideBarContext";
34

45
interface PanelSideBarLayoutContentProps extends PropsWithChildren {
56
footer?: ReactNode;
6-
mainContentBodyRef?: MutableRefObject<HTMLElement | null>;
77
isIconShownOnSidebarCollapse: boolean;
88
}
99

1010
export const PanelSideBarLayoutContent = (props: PanelSideBarLayoutContentProps) => {
11-
const { children, footer, mainContentBodyRef, isIconShownOnSidebarCollapse } = props;
11+
const { children, footer, isIconShownOnSidebarCollapse } = props;
12+
const { mainContentBodyRef } = usePanelSideBarContext();
1213

1314
return (
1415
<section

0 commit comments

Comments
 (0)