@atlaskit/popup
Version:
A popup displays brief content in an overlay.
56 lines (54 loc) • 1.71 kB
JavaScript
import { useMemo } from 'react';
/**
* Props shape passed from legacy `role` / `label` / `titleId` into `@atlaskit/top-layer` `Popup.Content`.
*
* The `{ role: undefined }` variant covers the legacy contract where a
* consumer omits `role` to opt out of any popup semantics (and the
* associated role-based initial-focus movement). Legacy `Popup` left the
* popup element role-less in that case; we preserve that behaviour here
* rather than defaulting to `'dialog'`, which would move focus into the
* popup on open.
*/
/**
* Maps legacy popup role/label/titleId props to the discriminated union
* that `Popup.Content` expects.
*
* `Popup.Content` enforces at the type level that dialog/alertdialog/menu
* roles must have `label` or `labelledBy`. This hook bridges the legacy
* flat-prop API (`role`, `label`, `titleId`) to that shape.
*
* When `role` is omitted entirely, the result carries `role: undefined`
* (no implicit default). This matches the legacy `Popup` contract and
* keeps the underlying `Popover` from running role-based initial focus.
*/
export function useRoleProps({
role,
label,
titleId
}) {
return useMemo(() => {
if (role === 'dialog' || role === 'alertdialog' || role === 'menu') {
if (titleId) {
return {
role: role,
labelledBy: titleId
};
}
return {
role: role,
label: label !== null && label !== void 0 ? label : 'Popup'
};
}
if (role) {
return {
role: role,
label
};
}
// No role supplied: preserve the legacy "no popup semantics" contract.
return {
role: undefined,
label
};
}, [role, label, titleId]);
}