Skip to content

Commit

Permalink
feat: default popupId to React.useId() if it exists (#137)
Browse files Browse the repository at this point in the history
* feat: default popupId to React.useId() if it exists

fix #128
  • Loading branch information
jedwards1211 authored Aug 28, 2024
1 parent db9457e commit f91738f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ For MUI v4 you'll need `material-ui-popup-state@^1.9.3`.
- [`usePopupState`](#usepopupstate)
- [`usePopupState` Props](#usepopupstate-props)
- [`variant` (`'popover'`, `'popper'`, or `'dialog'`, **required**)](#variant-popover-popper-or-dialog-required)
- [`popupId` (`string`, **optional** but strongly encouraged)](#popupid-string-optional-but-strongly-encouraged)
- [`popupId` (`string`, **optional**)](#popupid-string-optional)
- [`disableAutoFocus` (`boolean`, **optional**)](#disableautofocus-boolean-optional)
- [`usePopupState` return value](#usepopupstate-return-value)
- [Examples with Render Props](#examples-with-render-props)
Expand All @@ -49,7 +49,7 @@ For MUI v4 you'll need `material-ui-popup-state@^1.9.3`.
- [Bind Functions](#bind-functions-1)
- [`PopupState` Props](#popupstate-props)
- [`variant` (`'popover'`, `'popper'`, or `'dialog'`, **required**)](#variant-popover-popper-or-dialog-required-1)
- [`popupId` (`string`, **optional** but strongly encouraged)](#popupid-string-optional-but-strongly-encouraged-1)
- [`popupId` (`string`, **optional**)](#popupid-string-optional)
- [`disableAutoFocus` (`boolean`, **optional**)](#disableautofocus-boolean-optional-1)
- [`children` (`(popupState: InjectedProps) => ?React.Node`, **required**)](#children-popupstate-injectedprops--reactnode-required)
- [Using `Popover` and `Menu` with `bindHover`](#using-popover-and-menu-with-bindhover)
Expand Down Expand Up @@ -278,14 +278,17 @@ popup is a `Popper`.
Right now this only affects whether `bindTrigger`/`bindToggle`/`bindHover` return
an `aria-controls` prop or an `aria-describedby` prop.

### `popupId` (`string`, **optional** but strongly encouraged)
### `popupId` (`string`, **optional**)

The `id` for the popup component. It will be passed to the child props so that
the trigger component may declare the same id in an ARIA prop.

Defaults to `React.useId()` if `React.useId` exists; in older versions of React
you will have to manually provide a `popupId`.

### `disableAutoFocus` (`boolean`, **optional**)

If `true`, will not steal focus when the popup is opened. (And `bindPopover`/`bindMenu`) will inject `disableAutoFocus`, `disableEnforceFocus`, and `disableRestoreFocus`).
If `true`, will not steal focus when the popup is opened. (And `bindPopover`/`bindMenu` will inject `disableAutoFocus`, `disableEnforceFocus`, and `disableRestoreFocus`).

Defaults to `true` when the popup is opened by the `bindHover` or `bindFocus` element.

Expand Down Expand Up @@ -553,14 +556,17 @@ popup is a `Popper`.
Right now this only affects whether `bindTrigger`/`bindToggle`/`bindHover` return
an `aria-controls` prop or an `aria-describedby` prop.

### `popupId` (`string`, **optional** but strongly encouraged)
### `popupId` (`string`, **optional**)

The `id` for the popup component. It will be passed to the child props so that
the trigger component may declare the same id in an ARIA prop.

Defaults to `React.useId()` if `React.useId` exists; in older versions of React
you will have to manually provide a `popupId`.

### `disableAutoFocus` (`boolean`, **optional**)

If `true`, will not steal focus when the popup is opened. (And `bindPopover`/`bindMenu`) will inject `disableAutoFocus`, `disableEnforceFocus`, and `disableRestoreFocus`).
If `true`, will not steal focus when the popup is opened. (And `bindPopover`/`bindMenu` will inject `disableAutoFocus`, `disableEnforceFocus`, and `disableRestoreFocus`).

Defaults to `true` when the popup is opened by the `bindHover` or `bindFocus` element.

Expand Down
9 changes: 6 additions & 3 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useRef,
useEffect,
} from 'react'
import * as React from 'react'
import { type PopoverPosition, type PopoverReference } from '@mui/material'
import { useEvent } from './useEvent'

Expand Down Expand Up @@ -72,9 +73,11 @@ export const initCoreState: CoreState = {
_deferNextClose: false,
}

const defaultPopupId = 'useId' in React ? () => React.useId() : () => undefined

export function usePopupState({
parentPopupState,
popupId,
popupId = defaultPopupId(),
variant,
disableAutoFocus,
}: {
Expand Down Expand Up @@ -321,10 +324,10 @@ function controlAriaProps({
...(variant === 'popover'
? {
'aria-haspopup': true,
'aria-controls': isOpen && popupId != null ? popupId : undefined,
'aria-controls': isOpen ? popupId : undefined,
}
: variant === 'popper'
? { 'aria-describedby': isOpen && popupId != null ? popupId : undefined }
? { 'aria-describedby': isOpen ? popupId : undefined }
: undefined),
}
}
Expand Down

0 comments on commit f91738f

Please sign in to comment.