UNPKG

@carbon/react

Version:

React components for the Carbon Design System

53 lines (51 loc) 1.29 kB
/** * Copyright IBM Corp. 2016, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ import { createContext } from "react"; //#region src/components/Menu/MenuContext.ts /** * Copyright IBM Corp. 2023, 2026 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ const menuDefaultState = { isRoot: true, hasIcons: false, hasSelectableItems: false, size: null, items: [], requestCloseRoot: () => {} }; function menuReducer(state, action) { switch (action.type) { case "enableIcons": return { ...state, hasIcons: true }; case "enableSelectableItems": return { ...state, hasSelectableItems: true }; case "registerItem": { const newItem = action.payload; const items = state.items.filter((item) => item.ref.current); const next = newItem.ref.current?.nextElementSibling; const idx = items.findIndex((item) => item.ref.current === next); items.splice(idx < 0 ? items.length : idx, 0, newItem); return { ...state, items }; } } } const MenuContext = createContext({ state: menuDefaultState, dispatch: () => {} }); //#endregion export { MenuContext, menuReducer };