Skip to content

Commit

Permalink
Add menu state context for convenience (#1012)
Browse files Browse the repository at this point in the history
- In some scenarios it's useful to have context available for menustate to avoid the need for passing it down through multiple components in props.  This mostly helps nested menus, but can be useful in other areas as well.
  • Loading branch information
andrewkfiedler authored Feb 20, 2025
1 parent 7d97cd3 commit 55df44a
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const useListenForChildUpdates = ({
}, [action.current, popoverRef])
}

export const useMenuState = ({ maxHeight }: Props = {}) => {
export function useMenuState({ maxHeight }: Props = {}) {
const anchorRef = React.useRef<HTMLDivElement>(null)
const popoverRef = useRerenderingRef<HTMLDivElement>()
const action = React.useRef<PopoverActions | null>(null)
Expand Down Expand Up @@ -140,6 +140,8 @@ export const useMenuState = ({ maxHeight }: Props = {}) => {
}
}

export default useMenuState

export const POPOVER_DEFAULTS = ({ maxHeight }: Props = {}) => {
return {
anchorOrigin: {
Expand All @@ -161,4 +163,12 @@ export const POPOVER_DEFAULTS = ({ maxHeight }: Props = {}) => {
>
}

export default useMenuState
const MenuStateContext = React.createContext<ReturnType<typeof useMenuState>>(
{} as ReturnType<typeof useMenuState>
)

export const MenuStateProvider = MenuStateContext.Provider

export function useMenuStateContext() {
return React.useContext(MenuStateContext)
}

0 comments on commit 55df44a

Please sign in to comment.