@primer/react
Version:
An implementation of GitHub's Primer Design System using React
74 lines (70 loc) • 2.82 kB
JavaScript
import React from 'react';
import { ActionListContainerContext } from './ActionListContainerContext.js';
import { useSlots } from '../hooks/useSlots.js';
import { Heading } from './Heading.js';
import { useId } from '../hooks/useId.js';
import { ListContext } from './shared.js';
import { useFocusZone } from '../hooks/useFocusZone.js';
import { clsx } from 'clsx';
import classes from './ActionList.module.css.js';
import { BoxWithFallback } from '../internal/components/BoxWithFallback.js';
import { jsxs, jsx } from 'react/jsx-runtime';
import { useProvidedRefOrCreate } from '../hooks/useProvidedRefOrCreate.js';
import { FocusKeys } from '@primer/behaviors';
const List = /*#__PURE__*/React.forwardRef(({
variant = 'inset',
selectionVariant,
showDividers = false,
role,
disableFocusZone = false,
className,
...props
}, forwardedRef) => {
var _slots$heading$props$;
const [slots, childrenWithoutSlots] = useSlots(props.children, {
heading: Heading
});
const headingId = useId();
/** if list is inside a Menu, it will get a role from the Menu */
const {
listRole: listRoleFromContainer,
listLabelledBy,
selectionVariant: containerSelectionVariant,
// TODO: Remove after DropdownMenu2 deprecation
enableFocusZone: enableFocusZoneFromContainer,
container
} = React.useContext(ActionListContainerContext);
const ariaLabelledBy = slots.heading ? (_slots$heading$props$ = slots.heading.props.id) !== null && _slots$heading$props$ !== void 0 ? _slots$heading$props$ : headingId : listLabelledBy;
const listRole = role || listRoleFromContainer;
const listRef = useProvidedRefOrCreate(forwardedRef);
let enableFocusZone = false;
if (enableFocusZoneFromContainer !== undefined) enableFocusZone = enableFocusZoneFromContainer;else if (listRole && !disableFocusZone) enableFocusZone = ['menu', 'menubar', 'listbox'].includes(listRole);
useFocusZone({
disabled: !enableFocusZone,
containerRef: listRef,
bindKeys: FocusKeys.ArrowVertical | FocusKeys.HomeAndEnd | FocusKeys.PageUpDown,
focusOutBehavior: listRole === 'menu' || container === 'SelectPanel' || container === 'FilteredActionList' ? 'wrap' : undefined
});
return /*#__PURE__*/jsxs(ListContext.Provider, {
value: {
variant,
selectionVariant: selectionVariant || containerSelectionVariant,
showDividers,
role: listRole,
headingId
},
children: [slots.heading, /*#__PURE__*/jsx(BoxWithFallback, {
as: "ul",
className: clsx(classes.ActionList, className),
role: listRole,
"aria-labelledby": ariaLabelledBy,
ref: listRef,
"data-dividers": showDividers,
"data-variant": variant,
...props,
children: childrenWithoutSlots
})]
});
});
List.displayName = 'ActionList';
export { List };