-
Notifications
You must be signed in to change notification settings - Fork 842
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
Changes from all commits
7900c05
8af7525
0f5c695
839e227
c59469f
e707e07
127c1ee
7bacd72
6961e64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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;" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
> | ||
<div> | ||
<div | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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', () => { | ||
// The viewport width matters for position assertions, so ensure it's explicitly defined | ||
beforeEach(() => { | ||
cy.viewport(500, 300); | ||
}); | ||
|
||
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" style={{ width: 150 }} /> | ||
} | ||
> | ||
Popover content | ||
</EuiInputPopover> | ||
</div> | ||
); | ||
cy.get('[data-popover-panel]').should('have.css', 'left', '175px'); | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏾 |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import React, { | |
useState, | ||
useEffect, | ||
useCallback, | ||
useRef, | ||
} from 'react'; | ||
import classnames from 'classnames'; | ||
import { tabbable, FocusableElement } from 'tabbable'; | ||
|
@@ -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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 & | ||
|
@@ -49,6 +60,7 @@ export const EuiInputPopover: FunctionComponent<EuiInputPopoverProps> = ({ | |
focusTrapProps, | ||
input, | ||
fullWidth = false, | ||
panelMinWidth = 0, | ||
onPanelResize, | ||
inputRef: _inputRef, | ||
panelRef: _panelRef, | ||
|
@@ -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]); | ||
|
@@ -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(() => { | ||
|
@@ -122,6 +137,7 @@ export const EuiInputPopover: FunctionComponent<EuiInputPopoverProps> = ({ | |
buttonRef={inputRef} | ||
panelRef={panelRef} | ||
className={classes} | ||
ref={popoverClassRef} | ||
{...props} | ||
> | ||
<EuiFocusTrap | ||
|
There was a problem hiding this comment.
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!