diff --git a/src/components/context_menu/context_menu.test.tsx b/src/components/context_menu/context_menu.test.tsx index e836c32b3dc..f6a55f13380 100644 --- a/src/components/context_menu/context_menu.test.tsx +++ b/src/components/context_menu/context_menu.test.tsx @@ -139,8 +139,13 @@ describe('EuiContextMenu', () => { }); it('allows you to click the title button to go back to the previous panel', async () => { + const onPanelChange = jest.fn(); const component = mount( - + ); await tick(20); @@ -155,6 +160,10 @@ describe('EuiContextMenu', () => { await tick(20); expect(takeMountedSnapshot(component)).toMatchSnapshot(); + expect(onPanelChange).toHaveBeenCalledWith({ + panelId: 1, + direction: 'previous', + }); }); }); diff --git a/src/components/context_menu/context_menu.tsx b/src/components/context_menu/context_menu.tsx index 01eaf7d9b02..e85c646ed77 100644 --- a/src/components/context_menu/context_menu.tsx +++ b/src/components/context_menu/context_menu.tsx @@ -72,6 +72,14 @@ export const SIZES = keysOf(sizeToClassNameMap); export type EuiContextMenuProps = CommonProps & Omit, 'style'> & { panels?: EuiContextMenuPanelDescriptor[]; + /** + * Optional callback that fires on every panel change. Passes back + * the new panel ID and whether its direction was `next` or `previous`. + */ + onPanelChange?: (panelDetails: { + panelId: EuiContextMenuPanelId; + direction?: EuiContextMenuPanelTransitionDirection; + }) => void; initialPanelId?: EuiContextMenuPanelId; /** * Alters the size of the items and the title @@ -221,6 +229,8 @@ export class EuiContextMenu extends Component { transitionDirection: direction, isOutgoingPanelVisible: true, }); + + this.props.onPanelChange?.({ panelId, direction }); } showNextPanel = (itemIndex?: number) => { @@ -407,7 +417,14 @@ export class EuiContextMenu extends Component { } render() { - const { panels, className, initialPanelId, size, ...rest } = this.props; + const { + panels, + onPanelChange, + className, + initialPanelId, + size, + ...rest + } = this.props; const incomingPanel = this.renderPanel(this.state.incomingPanelId!, 'in'); let outgoingPanel; diff --git a/upcoming_changelogs/6767.md b/upcoming_changelogs/6767.md new file mode 100644 index 00000000000..acc1fc04044 --- /dev/null +++ b/upcoming_changelogs/6767.md @@ -0,0 +1 @@ +- Added `onPanelChange` callback to `EuiContextMenu` to provide consumer access to `panelId` and `direction`.