Skip to content

Commit

Permalink
[EuiPopover] Add new configurable repositionToCrossAxis prop
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Sep 22, 2023
1 parent 1449880 commit 4420a7a
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ exports[`CollapsedItemActions render with href and _target provided 1`] = `
ownFocus={true}
panelPaddingSize="none"
popoverRef={[Function]}
repositionToCrossAxis={true}
>
<EuiContextMenuPanel
items={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ exports[`EuiDataGridHeaderCell renders 1`] = `
token="euiDataGridHeaderCell.actionsPopoverScreenReaderText"
/>
}
repositionToCrossAxis={true}
>
<EuiListGroup
data-test-subj="dataGridHeaderCellActionGroup-someColumn"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ exports[`useDataGridDisplaySelector displaySelector renders a toolbar button/pop
ownFocus={true}
panelClassName="euiDataGrid__displayPopoverPanel"
panelPaddingSize="s"
repositionToCrossAxis={true}
>
<EuiI18n
defaults={
Expand Down
1 change: 1 addition & 0 deletions src/components/popover/input_popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export const EuiInputPopover: FunctionComponent<EuiInputPopoverProps> = ({
return (
<EuiPopover
css={css(fullWidth ? undefined : logicalCSS('max-width', form.maxWidth))}
repositionToCrossAxis={false}
ownFocus={false}
button={
<EuiResizeObserver onResize={onResize}>
Expand Down
40 changes: 40 additions & 0 deletions src/components/popover/popover.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,44 @@ describe('EuiPopover', () => {
});
});
});

describe('repositionToCrossAxis', () => {
beforeEach(() => {
// Set a forced viewport with not enough room to render the popover vertically
cy.viewport(500, 50);
});

it('allows the popover to reposition to the cross/secondary axis if there is not enough room on the primary axis', () => {
cy.mount(
<PopoverComponent anchorPosition="downCenter">Test</PopoverComponent>
);
cy.get('[data-test-subj="togglePopover"]').click();

// Assert that the popover rendered horizontally and not vertically
cy.get('[data-popover-panel]')
.invoke('offset')
.then(({ top, left }) => {
expect(left).to.be.gt(top);
});
});

it('does not reposition to the cross axis if set to false', () => {
cy.mount(
<PopoverComponent
anchorPosition="downCenter"
repositionToCrossAxis={false}
>
Test
</PopoverComponent>
);
cy.get('[data-test-subj="togglePopover"]').click();

// Assert that the popover vertically and not horizontally
cy.get('[data-popover-panel]')
.invoke('offset')
.then(({ top, left }) => {
expect(top).to.be.gt(left);
});
});
});
});
16 changes: 15 additions & 1 deletion src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ export interface EuiPopoverProps extends PropsWithChildren, CommonProps {
* an `EuiPopover` in a scrollable container, `repositionOnScroll` should be `true`
*/
repositionOnScroll?: boolean;
/**
* By default, popovers will attempt to position themselves along the initial
* axis specified. If there is not enough room either vertically or horizontally
* however, the popover will attempt to reposition itself along the secondary
* cross axis if there is room there instead.
*
* If you do not not want this repositioning to occur (and it is acceptable for
* the popover to appear offscreen), set this to false to disable this behavior.
*
* @default true
*/
repositionToCrossAxis?: boolean;
/**
* Must be set to true if using `EuiDragDropContext` within a popover,
* otherwise your nested drag & drop will have incorrect positioning
Expand Down Expand Up @@ -281,6 +293,7 @@ export class EuiPopover extends Component<Props, State> {
static defaultProps: Partial<PropsWithDefaults> = {
isOpen: false,
ownFocus: true,
repositionToCrossAxis: true,
anchorPosition: 'downCenter',
panelPaddingSize: 'm',
hasArrow: true,
Expand Down Expand Up @@ -517,7 +530,7 @@ export class EuiPopover extends Component<Props, State> {
arrowBuffer: 10,
},
returnBoundingBox: this.props.attachToAnchor,
allowCrossAxis: !this.props.attachToAnchor,
allowCrossAxis: this.props.repositionToCrossAxis,
buffer: this.props.buffer,
});

Expand Down Expand Up @@ -607,6 +620,7 @@ export class EuiPopover extends Component<Props, State> {
hasArrow,
arrowChildren,
repositionOnScroll,
repositionToCrossAxis,
hasDragDrop,
zIndex,
attachToAnchor,
Expand Down
2 changes: 2 additions & 0 deletions upcoming_changelogs/7211.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Updated `EuiPopover` with a new configurable `repositionToCrossAxis` prop

**Bug fixes**

- Fixed `EuiPopover`'s missing animations on popover close
Expand Down

0 comments on commit 4420a7a

Please sign in to comment.