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

Exposed close function on Popover #12341

Merged
merged 3 commits into from
Jul 11, 2024

Conversation

AndrewMusgrave
Copy link
Member

WHY are these changes introduced?

Fixes #12336
Fixes #12249

WHAT is this pull request doing?

This PR exposes a close function on popovers imperative handle to handle accessibility when closing the popover from an outside source.

WHY did you take this approach?

Originally I thought about using an effect to watch the active status, and automatically manage focus when the popover is closed. However, we can't guarantee that the user will always want popover to manage focus. Perhaps, they'll self manage focus to another area of the page, or navigate entirely.

Exposing a close function allows us to:

  • Release this in a minor version, rather than a major one
  • There's less work required to migrate as you won't need to audit all instance of popover
  • Allows for accessible focus, while allowing popover to contain the business logic
  • It allows for the flexibility of how you'll manage the user experience when the popover is closed

Giphy

Monosnap.screencast.2024-07-04.17-52-19.mp4

How to 🎩

Playground code

import React, {useRef, useState, useEffect} from 'react';

import type {PopoverPublicAPI} from '../src';
import {Page, Button, Popover, ActionList} from '../src';

console.log('Logging active element every 15 seconds');

export const Playground = {
  tags: ['skip-tests'],
  render() {
    useEffect(() => {
      const interval = setInterval(() => {
        console.log(document.activeElement);
      }, 15000);

      return () => {
        clearInterval(interval);
      };
    }, []);

    const popoverRef = useRef<PopoverPublicAPI>(null);
    const [popoverActive, setPopoverActive] = useState(true);

    const togglePopoverActive = () =>
      setPopoverActive((popoverActive) => !popoverActive);

    const activator = (
      <Button onClick={togglePopoverActive} disclosure>
        More actions
      </Button>
    );

    return (
      <Page title="Playground">
        <Popover
          active={popoverActive}
          activator={activator}
          autofocusTarget="first-node"
          onClose={togglePopoverActive}
          ref={popoverRef}
        >
          <ActionList
            actionRole="menuitem"
            items={[
              {
                content: 'Focus activator',
                onAction() {
                  popoverRef.current?.close();
                },
              },
              {
                content: 'Focus next node',
                onAction() {
                  popoverRef.current?.close('next-node');
                },
              },
            ]}
          />
        </Popover>
        <Button>Next focusable node</Button>
      </Page>
    );
  },
};

🎩 checklist

Sorry, something went wrong.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
describe('close', () => {
it('exposes a function that closes the popover & focuses the activator by default', () => {
const activatorId = 'focus-target';
let popoverRef: React.RefObject<PopoverPublicAPI> | null = null;
Copy link
Member Author

Choose a reason for hiding this comment

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

This pattern is what's used above to test the other function in the imperative handle

@AndrewMusgrave AndrewMusgrave marked this pull request as ready for review July 4, 2024 22:10
@AndrewMusgrave
Copy link
Member Author

@kellydanma @zaquille-oneil Before I reach out to Polaris for a review, what do you think about the approach?

Copy link
Member

@kellydanma kellydanma left a comment

Choose a reason for hiding this comment

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

✅ Approach looks good to me! Seems like a much better way to manage focus than the approach in https://github.com/Shopify/web/pull/131141; I'm going to close my PR and wait for the changes in Polaris to ship before resolving the bug. 😸

@zaquille-oneil
Copy link
Contributor

LGTM

@zaquille-oneil
Copy link
Contributor

/snapit

Copy link
Contributor

🫰✨ Thanks @zaquille-oneil! Your snapshot has been published to npm.

Test the snapshot by updating your package.json with the newly published version:

Copy link
Contributor

@dleroux dleroux left a comment

Choose a reason for hiding this comment

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

🐑 🇮🇹 🎩 👍

@AndrewMusgrave AndrewMusgrave merged commit f6728a4 into main Jul 11, 2024
12 of 13 checks passed
@AndrewMusgrave AndrewMusgrave deleted the am-expose-close-function-for-accessible-close branch July 11, 2024 15:05
alex-page pushed a commit that referenced this pull request Jul 16, 2024
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @shopify/polaris@13.7.0

### Minor Changes

- [#12341](#12341)
[`f6728a410`](f6728a4)
Thanks [@AndrewMusgrave](https://github.com/AndrewMusgrave)! - Exposed a
`close` function on popovers imperative handle

### Patch Changes

- [#12335](#12335)
[`d71b3a282`](d71b3a2)
Thanks [@AndrewMusgrave](https://github.com/AndrewMusgrave)! - Fixed
`Tabs` automatically closing when opened in a scrolled position

- Updated dependencies
\[[`b4cbdefd4`](b4cbdef)]:
    -   @shopify/polaris-tokens@9.3.0

## @shopify/polaris-tokens@9.3.0

### Minor Changes

- [#12398](#12398)
[`b4cbdefd4`](b4cbdef)
Thanks [@alex-page](https://github.com/alex-page)! - Update the green
and red token values

## @shopify/polaris-migrator@1.0.4

### Patch Changes

- Updated dependencies
\[[`b4cbdefd4`](b4cbdef)]:
    -   @shopify/polaris-tokens@9.3.0
    -   @shopify/stylelint-polaris@16.0.4

## @shopify/stylelint-polaris@16.0.4

### Patch Changes

- Updated dependencies
\[[`b4cbdefd4`](b4cbdef)]:
    -   @shopify/polaris-tokens@9.3.0

## polaris-for-vscode@1.0.4

### Patch Changes

- Updated dependencies
\[[`b4cbdefd4`](b4cbdef)]:
    -   @shopify/polaris-tokens@9.3.0

## polaris.shopify.com@1.0.10

### Patch Changes

- Updated dependencies
\[[`b4cbdefd4`](b4cbdef),
[`d71b3a282`](d71b3a2),
[`f6728a410`](f6728a4)]:
    -   @shopify/polaris-tokens@9.3.0
    -   @shopify/polaris@13.7.0

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Copy link

@kimhawthorne-shopify kimhawthorne-shopify left a comment

Choose a reason for hiding this comment

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

(Deleted comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Polaris ActionList a11y issue. Popover does not refocus onto activator after close
5 participants