Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(navigation-bar): move context provider to not wrap parent #6960

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/components/navigation-bar/components.test-pw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect } from "react";
import NavigationBar, { NavigationBarProps } from ".";
import { Menu, MenuDivider, MenuItem } from "../menu";
import Box from "../box";
import useMediaQuery from "../../hooks/useMediaQuery";

export const NavigationBarComponent = ({
children,
Expand Down Expand Up @@ -214,6 +215,8 @@ Fixed.parameters = {
export const NavigationBarWithErrorHandler = ({
...props
}: NavigationBarProps) => {
const isLargeScreen = useMediaQuery("(min-width: 600px)");
const rightPadding = isLargeScreen ? 2 : 1;
const [error, setError] = useState("");
useEffect(() => {
const handleError = (e: ErrorEvent) => {
Expand All @@ -225,7 +228,7 @@ export const NavigationBarWithErrorHandler = ({
});
return (
<>
<NavigationBar {...props} />
<NavigationBar pr={rightPadding} {...props} />
<div id="error-div">{error}</div>
</>
);
Expand Down
24 changes: 23 additions & 1 deletion src/components/navigation-bar/navigation-bar-test.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React, { useRef } from "react";
import React, { useRef, useState, useEffect } from "react";
import NavigationBar, { NavigationBarProps } from ".";
import { Menu, MenuItem } from "../menu";
import useMediaQuery from "../../hooks/useMediaQuery";

export default {
title: "Navigation Bar/Test",
includeStories: [
"DefaultStory",
"NavigationBarWithSubmenuAndChangingHeight",
"WithMediaQuery",
"ResponsivePadding",
],
parameters: {
Expand Down Expand Up @@ -74,6 +76,26 @@ export const NavigationBarWithSubmenuAndChangingHeight = () => {
);
};

export const WithMediaQuery = () => {
const isLargeScreen = useMediaQuery("(min-width: 600px)");
const rightPadding = isLargeScreen ? 2 : 1;
const [error, setError] = useState("");
useEffect(() => {
const handleError = (e: ErrorEvent) => {
setError(e.message);
};
window.addEventListener("error", handleError);

return () => window.removeEventListener("error", handleError);
}, []);
return (
<>
<NavigationBar pr={rightPadding} />
<div id="error-div">{error}</div>
</>
);
};

export const ResponsivePadding = () => {
return <NavigationBar>Example content</NavigationBar>;
};
Expand Down
24 changes: 12 additions & 12 deletions src/components/navigation-bar/navigation-bar.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,27 @@ export const NavigationBar = ({
const navbarRef = useRef(null);

return (
<FixedNavigationBarContextProvider
<StyledNavigationBar
role="navigation"
data-component={isGlobal ? "global-header" : "navigation-bar"}
aria-label={isGlobal ? "Global Header" : ariaLabel}
navigationType={isGlobal ? "black" : navigationType}
orientation={isGlobal ? "top" : orientation}
offset={isGlobal ? "0px" : offset}
position={isGlobal ? "fixed" : position}
navbarRef={navbarRef}
{...props}
isGlobal={isGlobal}
ref={navbarRef}
>
<StyledNavigationBar
role="navigation"
data-component={isGlobal ? "global-header" : "navigation-bar"}
aria-label={isGlobal ? "Global Header" : ariaLabel}
navigationType={isGlobal ? "black" : navigationType}
<FixedNavigationBarContextProvider
orientation={isGlobal ? "top" : orientation}
offset={isGlobal ? "0px" : offset}
position={isGlobal ? "fixed" : position}
{...props}
isGlobal={isGlobal}
ref={navbarRef}
navbarRef={navbarRef}
>
{!isLoading && children}
</StyledNavigationBar>
</FixedNavigationBarContextProvider>
</FixedNavigationBarContextProvider>
</StyledNavigationBar>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/navigation-bar/navigation-bar.pw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test.describe("Test props for NavigationBar component", () => {
}) => {
await mount(<NavigationBarWithErrorHandler />);

await expect(page.locator("#error-div")).toContainText("");
await expect(page.locator("#error-div")).toHaveText("");
});

specialCharacters.forEach((childrenValue) => {
Expand Down
Loading