Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiInputPopover] Add panelMinWidth prop #7071

Merged
merged 9 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 60 additions & 40 deletions src-docs/src/views/popover/input_popover.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,80 @@
import React, { useState } from 'react';

import { EuiInputPopover, EuiFieldText, EuiSpacer } from '../../../../src';
import {
EuiInputPopover,
EuiInputPopoverProps,
EuiFieldText,
EuiTextArea,
EuiButtonGroup,
EuiFormRow,
EuiSpacer,
} from '../../../../src';

export default () => {
const [inputWidth, setInputWidth] = useState(200);
const [isPopoverOpen, setIsPopoverOpen] = useState(false);
const [isPopoverOpenTwo, setIsPopoverOpenTwo] = useState(false);
const toggleIsPopoverOpen = (shouldBeOpen = !isPopoverOpen) => {
setIsPopoverOpen(shouldBeOpen);
};
const toggleIsPopoverOpenTwo = (shouldBeOpen = !isPopoverOpenTwo) => {
setIsPopoverOpenTwo(shouldBeOpen);
};

const input = (
<EuiFieldText
onFocus={() => toggleIsPopoverOpen()}
aria-label="Popover attached to input element"
/>
);

const inputTwo = (
<EuiFieldText
onFocus={() => {
setInputWidth(300);
toggleIsPopoverOpenTwo();
}}
aria-label="Popover attached to an adjustable sized input element"
/>
);
const [isResizablePopoverOpen, setIsResizablePopoverOpen] = useState(false);
const [anchorPosition, setAnchorPosition] =
useState<EuiInputPopoverProps['anchorPosition']>('downLeft');

return (
<React.Fragment>
<>
<EuiInputPopover
input={input}
isOpen={isPopoverOpen}
closePopover={() => {
toggleIsPopoverOpen(false);
}}
closePopover={() => setIsPopoverOpen(false)}
input={
<EuiFieldText
onFocus={() => setIsPopoverOpen(true)}
placeholder="Focus me to toggle an input popover"
aria-label="Popover attached to input element"
/>
}
>
Popover content
</EuiInputPopover>

<EuiSpacer />

<EuiInputPopover
input={inputTwo}
isOpen={isPopoverOpenTwo}
style={{ width: inputWidth }}
closePopover={() => {
toggleIsPopoverOpenTwo(false);
setInputWidth(200);
}}
display="inline-block"
isOpen={isResizablePopoverOpen}
closePopover={() => setIsResizablePopoverOpen(false)}
input={
<EuiTextArea
onKeyDown={(e) => {
if (e.key === 'ArrowDown') {
e.preventDefault();
setIsResizablePopoverOpen(true);
}
}}
placeholder="Focus me, press the down arrow key, then drag the resize handle"
aria-label="Press the down arrow key to toggle the popover attached to this textarea element."
rows={2}
resize="horizontal"
/>
}
panelMinWidth={300}
anchorPosition={anchorPosition}
>
Popover will adjust in size as the input does
This popover has a minimum width of 300px, and will adjust in size as
the textarea does.
<EuiSpacer size="s" />
<EuiFormRow label="Anchor position" display="columnCompressed">
<EuiButtonGroup
buttonSize="compressed"
legend="Anchor position"
name="anchorPosition"
idSelected={anchorPosition!}
onChange={(id) =>
setAnchorPosition(id as EuiInputPopoverProps['anchorPosition'])
}
options={[
{ id: 'downLeft', label: 'Left' },
{ id: 'downCenter', label: 'Center' },
{ id: 'downRight', label: 'Right' },
]}
/>
</EuiFormRow>
</EuiInputPopover>
</React.Fragment>
</>
);
};
23 changes: 12 additions & 11 deletions src-docs/src/views/popover/popover_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,19 +364,20 @@ export const PopoverExample = {
<p>
<strong>EuiInputPopover</strong> is a specialized popover component
intended to be used with form elements. Stylistically, the popover
panel is
{'"attached"'} to the input. Functionally, consumers have control
over what events open and close the popover, and it can allow for
natural tab order.
panel is {'"attached"'} to the input. As a result, the popover will
always try to set its width to match the width of the input,
although this can be configured via <EuiCode>panelMinWidth</EuiCode>
.
</p>
<p>
Although some assumptions are made about keyboard behavior,
consumers should provide specific key event handlers depending on
the use case. For instance, a <EuiCode>type=text</EuiCode> input
could use the down key to trigger popover opening, but this
interaction would not be appropriate for{' '}
<EuiCode>type=number</EuiCode> inputs as they natively bind to the
down key.
Functionally, consumers have control over what events open and close
the popover, and it can allow for natural tab order. Although some
assumptions are made about keyboard behavior, consumers should
provide specific key event handlers depending on the use case. For
instance, a <EuiCode>type=text</EuiCode> input could use the down
key to trigger popover opening, but this interaction would not be
appropriate for <EuiCode>type=number</EuiCode> inputs as they
natively bind to the down key.
Comment on lines +373 to +380
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great explanation here. This is super informative!

</p>
</>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ exports[`EuiSuperSelect props custom display is propagated to dropdown 1`] = `
class="euiPanel euiPanel--plain euiPopover__panel emotion-euiPanel-grow-m-plain-euiPopover__panel-bottom"
data-popover-panel="true"
role="dialog"
style="top: 8px; left: 0px; will-change: transform, opacity; z-index: 2000;"
style="top: 8px; left: -22px; will-change: transform, opacity; z-index: 2000;"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally sure why this snapshot changed - I QA'd all components dogfooding EuiInputPopover (EuiComboBox, EuiSuperSelect, EuiRange, and EuiAutoRefresh) and all of them are still rendering the correct widths/positions for the popover. I'll put this down as Jest/jsdom gremlins 🤷

>
<div>
<div
Expand Down Expand Up @@ -390,7 +390,7 @@ exports[`EuiSuperSelect props more props are propogated to each option 1`] = `
class="euiPanel euiPanel--plain euiPopover__panel emotion-euiPanel-grow-m-plain-euiPopover__panel-bottom"
data-popover-panel="true"
role="dialog"
style="top: 8px; left: 0px; will-change: transform, opacity; z-index: 2000;"
style="top: 8px; left: -22px; will-change: transform, opacity; z-index: 2000;"
>
<div>
<div
Expand Down Expand Up @@ -536,7 +536,7 @@ exports[`EuiSuperSelect props options are rendered when select is open 1`] = `
class="euiPanel euiPanel--plain euiPopover__panel emotion-euiPanel-grow-m-plain-euiPopover__panel-bottom"
data-popover-panel="true"
role="dialog"
style="top: 8px; left: 0px; will-change: transform, opacity; z-index: 2000;"
style="top: 8px; left: -22px; will-change: transform, opacity; z-index: 2000;"
>
<div>
<div
Expand Down Expand Up @@ -681,7 +681,7 @@ exports[`EuiSuperSelect props renders popoverProps on the underlying EuiPopover
class="euiPanel euiPanel--plain euiPopover__panel goes-on-popover-panel emotion-euiPanel-grow-m-plain-euiPopover__panel-bottom"
data-popover-panel="true"
role="dialog"
style="top: 8px; left: 0px; will-change: transform, opacity; z-index: 2000;"
style="top: 8px; left: -22px; will-change: transform, opacity; z-index: 2000;"
>
<div>
<div
Expand Down
6 changes: 3 additions & 3 deletions src/components/form/super_select/super_select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
EuiSuperSelectControlProps,
EuiSuperSelectOption,
} from './super_select_control';
import { EuiInputPopover, EuiPopoverProps } from '../../popover';
import { EuiInputPopover, EuiInputPopoverProps } from '../../popover';
import {
EuiContextMenuItem,
EuiContextMenuItemLayoutAlignment,
Expand Down Expand Up @@ -80,15 +80,15 @@ export type EuiSuperSelectProps<T extends string> = CommonProps &
isOpen?: boolean;

/**
* Optional props to pass to the underlying [EuiPopover](/#/layout/popover).
* Optional props to pass to the underlying [EuiInputPopover](/#/layout/popover#popover-attached-to-input-element).
* Allows fine-grained control of the popover dropdown menu, including
* `repositionOnScroll` for EuiSuperSelects used within scrollable containers,
* and customizing popover panel styling.
*
* Does not accept a nested `popoverProps.isOpen` property - use the top level
* `isOpen` API instead.
*/
popoverProps?: Partial<CommonProps & Omit<EuiPopoverProps, 'isOpen'>>;
popoverProps?: Partial<CommonProps & Omit<EuiInputPopoverProps, 'isOpen'>>;
};

export class EuiSuperSelect<T extends string> extends Component<
Expand Down
78 changes: 78 additions & 0 deletions src/components/popover/input_popover.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

/// <reference types="cypress" />
/// <reference types="cypress-real-events" />
/// <reference types="../../../cypress/support" />

import React from 'react';

import { EuiFieldText, EuiTextArea } from '../../components';
import { EuiInputPopover } from './input_popover';

describe('EuiPopover', () => {
const props = {
input: <EuiFieldText />,
closePopover: () => {},
isOpen: true,
};

it('renders a popover with equal width to the input', () => {
cy.mount(<EuiInputPopover {...props}>Popover content</EuiInputPopover>);
cy.get('[data-popover-panel]')
.should('have.css', 'left', '0px')
.invoke('outerWidth')
.should('equal', 400);
});

it('respects `panelMinWidth`', () => {
cy.mount(
<EuiInputPopover {...props} panelMinWidth={450}>
Popover content
</EuiInputPopover>
);
cy.get('[data-popover-panel]').invoke('outerWidth').should('equal', 450);
});

it('respects `anchorPosition`', () => {
cy.mount(
<div className="eui-textRight">
<EuiInputPopover
{...props}
display="inline-block"
input={<EuiFieldText controlOnly={true} style={{ width: 150 }} />}
panelMinWidth={300}
anchorPosition="downRight"
>
Popover content
</EuiInputPopover>
</div>
);
cy.get('[data-popover-panel]').should('have.css', 'left', '200px');
});

it('correctly repositions the popover on input resize', () => {
cy.mount(
<div className="eui-textCenter">
<EuiInputPopover
{...props}
display="inline-block"
input={<EuiTextArea rows={1} resize="horizontal" />}
>
Popover content
</EuiInputPopover>
</div>
);
cy.get('[data-popover-panel]').should('have.css', 'left', '155.5px');
cy.wait(100); // Wait a tick, otherwise Cypress returns a false positive

// Cypress doesn't seem to have a way to mimic manual dragging/resizing, so we'll do it programmatically
cy.get('textarea').then(($el) => ($el[0].style.width = '500px'));
cy.get('[data-popover-panel]').should('have.css', 'left', '50px');
});
Comment on lines +81 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏾

});
28 changes: 22 additions & 6 deletions src/components/popover/input_popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import React, {
useState,
useEffect,
useCallback,
useRef,
} from 'react';
import classnames from 'classnames';
import { tabbable, FocusableElement } from 'tabbable';
Expand All @@ -30,12 +31,22 @@ import { css } from '@emotion/react';
import { logicalCSS } from '../../global_styling';

export interface _EuiInputPopoverProps
extends Omit<EuiPopoverProps, 'button' | 'buttonRef'> {
extends Omit<EuiPopoverProps, 'button' | 'buttonRef' | 'anchorPosition'> {
/**
* Alignment of the popover relative to the input
*/
anchorPosition?: 'downLeft' | 'downRight' | 'downCenter';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a thought, no action required. I like the decision you made here to just set the prop to equal one of these three values instead of creating a separate type (which the pattern we're currently using in most components). Especially since there are only three values being used.

disableFocusTrap?: boolean;
fullWidth?: boolean;
input: EuiPopoverProps['button'];
inputRef?: EuiPopoverProps['buttonRef'];
onPanelResize?: (width?: number) => void;
/**
* By default, **EuiInputPopovers** inherit the same width as the passed input element.
* However, if the input width is too small, you can pass a minimum panel width
* (that should be based on the popover content).
*/
panelMinWidth?: number;
}

export type EuiInputPopoverProps = CommonProps &
Expand All @@ -49,6 +60,7 @@ export const EuiInputPopover: FunctionComponent<EuiInputPopoverProps> = ({
focusTrapProps,
input,
fullWidth = false,
panelMinWidth = 0,
onPanelResize,
inputRef: _inputRef,
panelRef: _panelRef,
Expand All @@ -58,6 +70,7 @@ export const EuiInputPopover: FunctionComponent<EuiInputPopoverProps> = ({
const [inputEl, setInputEl] = useState<HTMLElement | null>(null);
const [inputElWidth, setInputElWidth] = useState<number>();
const [panelEl, setPanelEl] = useState<HTMLElement | null>(null);
const popoverClassRef = useRef<EuiPopover | null>(null);

const inputRef = useCombinedRefs([setInputEl, _inputRef]);
const panelRef = useCombinedRefs([setPanelEl, _panelRef]);
Expand All @@ -66,19 +79,21 @@ export const EuiInputPopover: FunctionComponent<EuiInputPopoverProps> = ({
(width?: number) => {
if (panelEl && (!!inputElWidth || !!width)) {
const newWidth = !!width ? width : inputElWidth;
panelEl.style.width = `${newWidth}px`;
if (onPanelResize) {
onPanelResize(newWidth);
}
const widthToSet =
newWidth && newWidth > panelMinWidth ? newWidth : panelMinWidth;

panelEl.style.width = `${widthToSet}px`;
onPanelResize?.(widthToSet);
}
},
[panelEl, inputElWidth, onPanelResize]
[panelEl, inputElWidth, onPanelResize, panelMinWidth]
);
const onResize = useCallback(() => {
if (inputEl) {
const width = inputEl.getBoundingClientRect().width;
setInputElWidth(width);
setPanelWidth(width);
popoverClassRef.current?.positionPopoverFluid();
}
}, [inputEl, setPanelWidth]);
useEffect(() => {
Expand Down Expand Up @@ -122,6 +137,7 @@ export const EuiInputPopover: FunctionComponent<EuiInputPopoverProps> = ({
buttonRef={inputRef}
panelRef={panelRef}
className={classes}
ref={popoverClassRef}
{...props}
>
<EuiFocusTrap
Expand Down
Loading