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 5 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 @@ -2,6 +2,7 @@

- `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 static element',
Copy link
Contributor

Choose a reason for hiding this comment

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

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: static;</EuiCode> elements.
Copy link
Contributor

Choose a reason for hiding this comment

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

fixed element.

</p>
</div>
),
demo: <PopoverFixed />,
}],
};
59 changes: 59 additions & 0 deletions src-docs/src/views/popover/popover_fixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, {
Component,
} from 'react';

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

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

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

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 (
<EuiPopover
Copy link
Contributor

Choose a reason for hiding this comment

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

In the docs I'd provide a separate button that says "Show fixed popover example", that then toggles the fixed button display so it isn't always shown.

This falls in line with how we handle other fixed content examples in the docs like modals, fixed progress bars, the bottom bar...etc.

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 probably the best example of doing something like that.

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>
);
}
}
15 changes: 15 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 @@ -421,6 +435,7 @@ EuiPopover.propTypes = {
PropTypes.node,
PropTypes.instanceOf(HTMLElement)
]),
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