-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(search-and-filter) custom event for panel toggle, simplify menu hook
- Loading branch information
Showing
3 changed files
with
23 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,27 @@ | ||
import React, { | ||
createContext, | ||
Dispatch, | ||
FC, | ||
ReactNode, | ||
SetStateAction, | ||
useContext, | ||
useState, | ||
} from "react"; | ||
import { useState } from "react"; | ||
import useEventListener from "@use-it/event-listener"; | ||
import { isWidthBelow } from "util/helpers"; | ||
|
||
interface ContextProps { | ||
menuCollapsed: boolean; | ||
setMenuCollapsed: Dispatch<SetStateAction<boolean>>; | ||
} | ||
const isSmallScreen = () => isWidthBelow(620); | ||
|
||
const initialState: ContextProps = { | ||
menuCollapsed: false, | ||
setMenuCollapsed: () => undefined, | ||
}; | ||
export const useMenuCollapsed = () => { | ||
const [menuCollapsed, setMenuCollapsed] = useState(false); | ||
|
||
export const MenuCollapsedContext = createContext<ContextProps>(initialState); | ||
const collapseOnMediumScreen = (e: Event) => { | ||
if (!("detail" in e) || e.detail !== "search-and-filter") { | ||
setMenuCollapsed(isWidthBelow(820)); | ||
} | ||
}; | ||
|
||
interface ProviderProps { | ||
children: ReactNode; | ||
} | ||
useEventListener("resize", collapseOnMediumScreen); | ||
|
||
export const MenuCollapsedProvider: FC<ProviderProps> = ({ children }) => { | ||
const [menuCollapsed, setMenuCollapsed] = useState(false); | ||
const onSearchFilterPanelToggle = () => { | ||
if (!menuCollapsed && isSmallScreen()) { | ||
setMenuCollapsed(true); | ||
} | ||
}; | ||
|
||
return ( | ||
<MenuCollapsedContext.Provider | ||
value={{ | ||
menuCollapsed, | ||
setMenuCollapsed, | ||
}} | ||
> | ||
{children} | ||
</MenuCollapsedContext.Provider> | ||
); | ||
}; | ||
useEventListener("sfp-toggle", onSearchFilterPanelToggle); | ||
|
||
export function useMenuCollapsed() { | ||
return useContext(MenuCollapsedContext); | ||
} | ||
return { menuCollapsed, setMenuCollapsed }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters