Files
overleaf-cep/services/web/frontend/js/shared/context/nestable-dropdown-context.tsx
Alf Eaton 5badb39259 Avoid closing nested menu on click (#30479)
GitOrigin-RevId: 0f966b53ee5bfd8b4bacdf7b259f3e8cab2858c8
2026-01-09 09:06:12 +00:00

40 lines
828 B
TypeScript

import {
createContext,
Dispatch,
FC,
SetStateAction,
useEffect,
useState,
} from 'react'
export type NestableDropdownContextType = {
selected: string | null
setSelected: Dispatch<SetStateAction<string | null>>
menuId: string
}
export const NestableDropdownContext = createContext<
NestableDropdownContextType | undefined
>(undefined)
export const NestableDropdownContextProvider: FC<
React.PropsWithChildren<{ id: string }>
> = ({ id, children }) => {
const [selected, setSelected] = useState<string | null>(null)
useEffect(() => {
return () => {
// unset selection on unmount
setSelected(null)
}
}, [])
return (
<NestableDropdownContext.Provider
value={{ selected, setSelected, menuId: id }}
>
{children}
</NestableDropdownContext.Provider>
)
}