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 Nav Mobile Toggling #5

Merged
merged 1 commit into from
Jul 15, 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
18 changes: 10 additions & 8 deletions src/components/header/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { APP_TITLE } from "@utils/constants";
// import navigation from "@uswds/uswds/js/usa-header";
import React, { SyntheticEvent, useEffect, useState } from "react";
import useAuth from "@hooks/use-auth";
import useUswds from "@hooks/use-uswds";
import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import { Banner, Icon, Search } from "@components/comet";
Expand All @@ -13,6 +14,7 @@ export const Header = (): React.ReactElement => {
const [showMenu, setShowMenu] = useState(false);

const { isSignedIn, signOut } = useAuth();
const { headerOn, headerOff } = useUswds();

const handleMenuClick = (): void => {
if (typeof window === "undefined") return;
Expand All @@ -21,15 +23,15 @@ export const Header = (): React.ReactElement => {
};

// Ensure navigation JS is loaded
// useEffect(() => {
// const bodyElement = document.body;
// navigation.on(bodyElement);
useEffect(() => {
const bodyElement = document.body;
headerOn(bodyElement);

// // Ensure cleanup after the effect
// return () => {
// navigation.off(bodyElement);
// };
// });
// // Ensure cleanup after the effect
return () => {
headerOff(bodyElement);
};
});

useEffect(() => {
const ref = document.body.style;
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/use-uswds.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { act, renderHook } from "@testing-library/react";
import useUswds from "./use-uswds";

test("should call signIn successfully", async () => {
const { result } = renderHook(() => useUswds());

await act(async () => {
result.current.headerOn(document.body);
});
expect(result.current.headerOn).toBeTruthy();
});

test("should call signOut successfully", async () => {
const { result } = renderHook(() => useUswds());

await act(async () => {
result.current.headerOff(document.body);
});
expect(result.current.headerOff).toBeTruthy();
});
15 changes: 15 additions & 0 deletions src/hooks/use-uswds.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const useUswds = () => {
const headerOn = async (bodyElement: HTMLElement) => {
const navigation = (await import("@uswds/uswds/js/usa-header")).default;
navigation.on(bodyElement);
};

const headerOff = async (bodyElement: HTMLElement) => {
const navigation = (await import("@uswds/uswds/js/usa-header")).default;
navigation.off(bodyElement);
};

return { headerOn, headerOff };
};

export default useUswds;
Loading