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

[EuiPopover] Handle initialFocus #4768

Merged
merged 5 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `32.3.0`.
**Bug fixes**

- Fixed `initialFocus` prop functionality in `EuiPopover` ([#4768](https://github.com/elastic/eui/pull/4768))

## [`32.3.0`](https://github.com/elastic/eui/tree/v32.3.0)

Expand Down
3 changes: 2 additions & 1 deletion src-docs/src/views/form_layouts/inline_popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export default () => {
id="formPopover"
button={button2}
isOpen={isPopover2Open}
closePopover={closePopover2}>
closePopover={closePopover2}
initialFocus="[name='popfirst']">
<div style={{ width: '300px' }}>{formSample2}</div>
</EuiPopover>
</div>
Expand Down
24 changes: 18 additions & 6 deletions src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ export interface EuiPopoverProps {
* node, or a selector string (which will be passed to
* document.querySelector() to find the DOM node), or a function that
* returns a DOM node
* Set to `false` to prevent initial auto-focus. Use only
* when your app handles setting initial focus state.
*/
initialFocus?: FocusTarget;
initialFocus?: FocusTarget | false;
/**
* Passed directly to EuiPortal for DOM positioning. Both properties are
* required if prop is specified
Expand Down Expand Up @@ -358,6 +360,7 @@ export class EuiPopover extends Component<Props, State> {
private updateFocusAnimationFrame: number | undefined;
private button: HTMLElement | null = null;
private panel: HTMLElement | null = null;
private hasSetInitialFocus: boolean = false;

constructor(props: Props) {
super(props);
Expand Down Expand Up @@ -408,12 +411,19 @@ export class EuiPopover extends Component<Props, State> {
updateFocus() {
// Wait for the DOM to update.
this.updateFocusAnimationFrame = window.requestAnimationFrame(() => {
if (!this.props.ownFocus || !this.panel) {
if (
!this.props.ownFocus ||
!this.panel ||
this.props.initialFocus === false
) {
return;
}

// If we've already focused on something inside the panel, everything's fine.
if (this.panel.contains(document.activeElement)) {
if (
this.hasSetInitialFocus &&
this.panel.contains(document.activeElement)
) {
return;
}

Expand Down Expand Up @@ -453,7 +463,10 @@ export class EuiPopover extends Component<Props, State> {
}
}

if (focusTarget != null) focusTarget.focus();
if (focusTarget != null) {
this.hasSetInitialFocus = true;
focusTarget.focus();
}
});
}

Expand Down Expand Up @@ -506,8 +519,6 @@ export class EuiPopover extends Component<Props, State> {
if (this.props.repositionOnScroll) {
window.addEventListener('scroll', this.positionPopoverFixed);
}

this.updateFocus();
}

componentDidUpdate(prevProps: Props) {
Expand All @@ -530,6 +541,7 @@ export class EuiPopover extends Component<Props, State> {
// If the user has just closed the popover, queue up the removal of the content after the
// transition is complete.
this.closingTransitionTimeout = window.setTimeout(() => {
this.hasSetInitialFocus = false;
this.setState({
isClosing: false,
});
Expand Down