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

Fix bug where a position: fixed popover content doesn't scroll #1064

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Added `onTableChange` callback to `EuiInMemoryTable` which notifies on sorting and pagination changes. ([#1060](https://github.com/elastic/eui/pull/1060))
- `EuiComboBox` now applies the provided `data-test-subj` to its options list element with the suffix `-optionsList` so you can find a specific combo box instance's options list. This wasn't previously possible because the options list is attached to the body element, not the combo box element. This is in addition to the existing `data-test-subj="comboBoxOptionsList"`. ([#1054](https://github.com/elastic/eui/pull/1054))
- EUI now provides minified versions of the themes' CSS files. ([#1070](https://github.com/elastic/eui/pull/1070))
- Add `repositionOnScroll` prop to `EuiPopover` which enables repositioning the popover when the window is scrolled. ([#1064](https://github.com/elastic/eui/pull/1064))

**Bug fixes**

Expand Down
20 changes: 20 additions & 0 deletions src-docs/src/views/popover/popover_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ import PopoverContainer from './popover_container';
const popoverContainerSource = require('!!raw-loader!./popover_container');
const popoverContainerHtml = renderToHtml(PopoverContainer);

import PopoverFixed from './popover_fixed';
const popoverFixedSource = require('!!raw-loader!./popover_fixed');
const popoverFixedHtml = renderToHtml(PopoverFixed);

export const PopoverExample = {
title: 'Popover',
Expand Down Expand Up @@ -204,5 +207,22 @@ export const PopoverExample = {
</div>
),
demo: <PopoverHTMLElementAnchor />,
}, {
title: 'Popover on a fixed element',
source: [{
type: GuideSectionTypes.JS,
code: popoverFixedSource,
}, {
type: GuideSectionTypes.HTML,
code: popoverFixedHtml,
}],
text: (
<div>
<p>
Popover content even works on <EuiCode>position: fixed;</EuiCode> elements.
</p>
</div>
),
demo: <PopoverFixed />,
}],
};
67 changes: 67 additions & 0 deletions src-docs/src/views/popover/popover_fixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React, {
Component,
} from 'react';

import {
EuiButton,
EuiPopover,
} from '../../../../src/components';

export default class PopoverContainer extends Component {
constructor(props) {
super(props);

this.state = {
isExampleShown: false,
isPopoverOpen: false,
};
}

toggleExample = () => this.setState(({ isExampleShown }) => ({ isExampleShown: !isExampleShown }))

onButtonClick = () => {
this.setState({
isPopoverOpen: !this.state.isPopoverOpen,
});
}

closePopover = () => {
this.setState({
isPopoverOpen: false,
});
}

setPanelRef = node => this.panel = node;

render() {
const button = (
<EuiButton
iconType="arrowDown"
iconSide="right"
onClick={this.onButtonClick}
style={{ background: 'white' }}
>
Show fixed popover
</EuiButton>
);

return (
<React.Fragment>
<EuiButton onClick={this.toggleExample}>Toggle Example</EuiButton>
{this.state.isExampleShown && (
<EuiPopover
button={button}
isOpen={this.state.isPopoverOpen}
closePopover={this.closePopover}
style={{ position: 'fixed', bottom: 50, right: 50 }}
repositionOnScroll={true}
>
<div>
This popover scrolls with the button element!
</div>
</EuiPopover>
)}
</React.Fragment>
);
}
}
16 changes: 16 additions & 0 deletions src/components/popover/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ export class EuiPopover extends Component {
this.setState({ suppressingPopover: false, isOpening: true }); // eslint-disable-line react/no-did-mount-set-state
}

if (this.props.repositionOnScroll) {
window.addEventListener('scroll', this.positionPopover);
}

this.updateFocus();
}

Expand All @@ -169,6 +173,15 @@ export class EuiPopover extends Component {
});
}

// update scroll listener
if (prevProps.repositionOnScroll !== this.props.repositionOnScroll) {
if (this.props.repositionOnScroll) {
window.addEventListener('scroll', this.positionPopover);
} else {
window.removeEventListener('scroll', this.positionPopover);
}
}

// The popover is being closed.
if (prevProps.isOpen && !this.props.isOpen) {
// If the user has just closed the popover, queue up the removal of the content after the
Expand All @@ -184,6 +197,7 @@ export class EuiPopover extends Component {
}

componentWillUnmount() {
window.removeEventListener('scroll', this.positionPopover);
clearTimeout(this.closingTransitionTimeout);
}

Expand Down Expand Up @@ -420,6 +434,8 @@ EuiPopover.propTypes = {
PropTypes.node,
PropTypes.instanceOf(HTMLElement)
]),
/** When `true`, the popover's position is re-calculated when the user scrolls, this supports having fixed-position popover anchors. */
repositionOnScroll: PropTypes.bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

Add an autodoc for what this does.

};

EuiPopover.defaultProps = {
Expand Down