@carbon/react
Version:
React components for the Carbon Design System
44 lines (40 loc) • 977 B
JavaScript
/**
* 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':
return {
...state,
items: [...state.items, action.payload].filter(item => item.ref.current !== null)
};
}
}
const MenuContext = /*#__PURE__*/createContext({
state: menuDefaultState,
// 'dispatch' is populated by the root menu
dispatch: _ => {}
});
export { MenuContext, menuReducer };