UNPKG

@primer/react

Version:

An implementation of GitHub's Primer Design System using React

103 lines (99 loc) 4.12 kB
import React from 'react'; import styled from 'styled-components'; import sx from '../sx.js'; import { ActionListContainerContext } from './ActionListContainerContext.js'; import { defaultSxProp } from '../utils/defaultSxProp.js'; import { useSlots } from '../hooks/useSlots.js'; import { Heading } from './Heading.js'; import { useId } from '../hooks/useId.js'; import { ListContext } from './shared.js'; import { useProvidedRefOrCreate } from '../hooks/useProvidedRefOrCreate.js'; import '@primer/behaviors/utils'; import { FocusKeys } from '@primer/behaviors'; import { useFocusZone } from '../hooks/useFocusZone.js'; import { clsx } from 'clsx'; import '../FeatureFlags/FeatureFlags.js'; import { useFeatureFlag } from '../FeatureFlags/useFeatureFlag.js'; import '../FeatureFlags/DefaultFeatureFlags.js'; import classes from './ActionList.module.css.js'; import { actionListCssModulesFlag } from './featureflag.js'; import merge from 'deepmerge'; function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } const ListBox = styled.ul.withConfig({ displayName: "List__ListBox", componentId: "sc-1x7olzq-0" })(sx); const List = /*#__PURE__*/React.forwardRef(({ variant = 'inset', selectionVariant, showDividers = false, role, sx: sxProp = defaultSxProp, className, ...props }, forwardedRef) => { var _slots$heading$props$; const styles = { margin: 0, paddingInlineStart: 0, // reset ul styles paddingY: variant === 'inset' ? 2 : 0 }; 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 } = React.useContext(ActionListContainerContext); const ariaLabelledBy = slots.heading ? (_slots$heading$props$ = slots.heading.props.id) !== null && _slots$heading$props$ !== undefined ? _slots$heading$props$ : headingId : listLabelledBy; const listRole = role || listRoleFromContainer; const listRef = useProvidedRefOrCreate(forwardedRef); let enableFocusZone = false; if (enableFocusZoneFromContainer !== undefined) enableFocusZone = enableFocusZoneFromContainer;else if (listRole) enableFocusZone = ['menu', 'menubar', 'listbox'].includes(listRole); useFocusZone({ disabled: !enableFocusZone, containerRef: listRef, bindKeys: FocusKeys.ArrowVertical | FocusKeys.HomeAndEnd | FocusKeys.PageUpDown, focusOutBehavior: listRole === 'menu' ? 'wrap' : undefined }); const enabled = useFeatureFlag(actionListCssModulesFlag); return /*#__PURE__*/React.createElement(ListContext.Provider, { value: { variant, selectionVariant: selectionVariant || containerSelectionVariant, showDividers, role: listRole, headingId } }, slots.heading, enabled ? sxProp !== defaultSxProp ? /*#__PURE__*/React.createElement(ListBox, _extends({ sx: merge(styles, sxProp), className: clsx(classes.ActionList, className), role: listRole, "aria-labelledby": ariaLabelledBy, ref: listRef, "data-dividers": showDividers, "data-variant": variant }, props), childrenWithoutSlots) : /*#__PURE__*/React.createElement("ul", _extends({ className: clsx(classes.ActionList, className), role: listRole, "aria-labelledby": ariaLabelledBy, ref: listRef, "data-dividers": showDividers, "data-variant": variant }, props), childrenWithoutSlots) : /*#__PURE__*/React.createElement(ListBox, _extends({ sx: merge(styles, sxProp), role: listRole, "aria-labelledby": ariaLabelledBy }, props, { ref: listRef, className: className }), childrenWithoutSlots)); }); List.displayName = 'ActionList'; export { List };