UNPKG

@carbon/react

Version:

React components for the Carbon Design System

51 lines (47 loc) 1.23 kB
/** * Copyright IBM Corp. 2016, 2023 * * 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'; 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 = /*#__PURE__*/createContext({ state: menuDefaultState, // 'dispatch' is populated by the root menu dispatch: _ => {} }); export { MenuContext, menuReducer };