Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 677
Refactor(ActionList): ActionList.Item should render content as a button if parent is not interactive.#3284
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
da778f26f17814ea0d12aee3e4a1c5ae19e6fa574131d53583aec9d873b9f4f836ae6508e4496ba6d863e91fb6fa889d2624463e1af1d7a07cb37b9dc717d537639510a18def07630f6532c7c25d692fd7beeee3cdc8ebda1f3abbef12dc8fabb84c6e6ccdd3f03a12ff27e8d6a63ddd212bee91a32b5abf8602de12bc4043c1606a7478a54af4dbf7525135388426e31f965a70207296d74cbf7e32d58be3676ee8449c8fe0825ad7fe4a92a624e644faa16ecd0a34e0d5c14afd0bec9903c8dfae7d6c9cb4d313f416be8455a422725253File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@primer/react": patch | ||
| --- | ||
| Refactor(ActionList): ActionList.Item should render content as a button if parent is not interactive. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -9,10 +9,11 @@ import {defaultSxProp} from '../utils/defaultSxProp' | ||
| import {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic' | ||
| import {ActionListContainerContext} from './ActionListContainerContext' | ||
| import {Description} from './Description' | ||
| import {ActionListProps, ListContext} from './List' | ||
| import {ListContext} from './List' | ||
| import {Selection} from './Selection' | ||
| import {ActionListItemProps, getVariantStyles, ItemContext, TEXT_ROW_HEIGHT} from './shared' | ||
| import {LeadingVisual, TrailingVisual} from './Visuals' | ||
| import {MenuContext} from '../ActionMenu/ActionMenu' | ||
| import {GroupContext} from './Group' | ||
| const LiBox = styled.li<SxProp>(sx) | ||
| @@ -29,6 +30,8 @@ export const Item = React.forwardRef<HTMLLIElement, ActionListItemProps>( | ||
| id, | ||
| role, | ||
| _PrivateItemWrapper, | ||
| // @ts-ignore tabIndex is sometimes passed as a prop in dotcom. | ||
| tabIndex, | ||
| ...props | ||
| }, | ||
| forwardedRef, | ||
| @@ -38,10 +41,13 @@ export const Item = React.forwardRef<HTMLLIElement, ActionListItemProps>( | ||
| trailingVisual: TrailingVisual, | ||
| description: Description, | ||
| }) | ||
| const {variant: listVariant, showDividers, selectionVariant: listSelectionVariant} = React.useContext(ListContext) | ||
| const {container, afterSelect, selectionAttribute} = React.useContext(ActionListContainerContext) | ||
| const menuContext = React.useContext(MenuContext) | ||
| const {selectionVariant: groupSelectionVariant} = React.useContext(GroupContext) | ||
| const selectionVariant = groupSelectionVariant ?? listSelectionVariant | ||
| const onSelect = React.useCallback( | ||
| ( | ||
| event: React.MouseEvent<HTMLLIElement> | React.KeyboardEvent<HTMLLIElement>, | ||
| @@ -55,10 +61,6 @@ export const Item = React.forwardRef<HTMLLIElement, ActionListItemProps>( | ||
| [onSelectUser], | ||
| ) | ||
| const selectionVariant: ActionListProps['selectionVariant'] = groupSelectionVariant | ||
| ? groupSelectionVariant | ||
| : listSelectionVariant | ||
| /** Infer item role based on the container */ | ||
| let itemRole: ActionListItemProps['role'] | ||
| if (container === 'ActionMenu' || container === 'DropdownMenu') { | ||
| @@ -84,12 +86,22 @@ export const Item = React.forwardRef<HTMLLIElement, ActionListItemProps>( | ||
| }, | ||
| } | ||
| const isTopLevelInteractive = () => | ||
radglob marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| _PrivateItemWrapper !== undefined || | ||
| // @ts-ignore props.as may be defined, may not. | ||
| props.as === 'button' || | ||
| // @ts-ignore props.as may be defined, may not. | ||
| props.as === 'a' || | ||
| menuContext.anchorId !== undefined || | ||
| role?.match(/menuitem/) || | ||
| tabIndex !== undefined | ||
| const styles = { | ||
| position: 'relative', | ||
| display: 'flex', | ||
| paddingX: 2, | ||
| paddingX: isTopLevelInteractive() ? 2 : 0, | ||
| fontSize: 1, | ||
| paddingY: '6px', // custom value off the scale | ||
| paddingY: isTopLevelInteractive() ? '6px' : 0, // custom value off the scale | ||
| lineHeight: TEXT_ROW_HEIGHT, | ||
| minHeight: 5, | ||
| marginX: listVariant === 'inset' ? 2 : 0, | ||
| @@ -145,6 +157,10 @@ export const Item = React.forwardRef<HTMLLIElement, ActionListItemProps>( | ||
| borderTopWidth: showDividers ? `1px` : '0', | ||
| borderColor: 'var(--divider-color, transparent)', | ||
| }, | ||
| 'button[data-component="ActionList.Item--DividerContainer"]': { | ||
| textAlign: 'left', | ||
| padding: 0, | ||
| }, | ||
| // show between 2 items | ||
| ':not(:first-of-type)': {'--divider-color': theme?.colors.actionListItem.inlineDivider}, | ||
| // hide divider after dividers & group header, with higher importance! | ||
| @@ -182,13 +198,13 @@ export const Item = React.forwardRef<HTMLLIElement, ActionListItemProps>( | ||
| const inlineDescriptionId = useId(id && `${id}--inline-description`) | ||
| const blockDescriptionId = useId(id && `${id}--block-description`) | ||
| const ItemWrapper = _PrivateItemWrapper || React.Fragment | ||
| const ItemWrapper = _PrivateItemWrapper || Box | ||
| const menuItemProps = { | ||
| onClick: clickHandler, | ||
| onKeyPress: keyPressHandler, | ||
| 'aria-disabled': disabled ? true : undefined, | ||
| tabIndex: disabled ? undefined : 0, | ||
| tabIndex: disabled || !isTopLevelInteractive() ? undefined : 0, | ||
| 'aria-labelledby': `${labelId} ${ | ||
| slots.description && slots.description.props.variant !== 'block' ? inlineDescriptionId : '' | ||
| }`, | ||
| @@ -199,17 +215,41 @@ export const Item = React.forwardRef<HTMLLIElement, ActionListItemProps>( | ||
| const containerProps = _PrivateItemWrapper ? {role: role || itemRole ? 'none' : undefined} : menuItemProps | ||
| const wrapperProps = _PrivateItemWrapper ? menuItemProps : {} | ||
| const wrapperProps = _PrivateItemWrapper | ||
| ? menuItemProps | ||
| : { | ||
| sx: { | ||
| display: 'flex', | ||
| paddingX: isTopLevelInteractive() ? 0 : 2, | ||
| paddingY: isTopLevelInteractive() ? 0 : '6px', // custom value off the scale | ||
| flexGrow: 1, | ||
| }, | ||
| } | ||
| return ( | ||
| <ItemContext.Provider value={{variant, disabled, inlineDescriptionId, blockDescriptionId}}> | ||
| <LiBox ref={forwardedRef} sx={merge<BetterSystemStyleObject>(styles, sxProp)} {...containerProps} {...props}> | ||
| {/* @ts-ignore onClick prop is only passed when _PrivateItemWrapper is set by ActionList.LinkItem. */} | ||
radglob marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| <ItemWrapper {...wrapperProps}> | ||
| <Selection selected={selected} /> | ||
| {slots.leadingVisual} | ||
| <Box | ||
| data-component="ActionList.Item--DividerContainer" | ||
| sx={{display: 'flex', flexDirection: 'column', flexGrow: 1, minWidth: 0}} | ||
| sx={{ | ||
| display: 'flex', | ||
| flexDirection: 'column', | ||
| flexGrow: 1, | ||
| minWidth: 0, | ||
| borderStyle: 'none', | ||
| backgroundColor: 'transparent', | ||
| cursor: 'inherit', | ||
| fontSize: 'inherit', | ||
| color: getVariantStyles(variant, disabled).color, | ||
| lineHeight: '20px', | ||
| }} | ||
| // @ts-ignore `as` prop may be passed to ActionList.Item, even if it isn't defined in ActionListItemProps. | ||
| // If this item is inside an ActionMenu, don't render an interactive button. | ||
| as={isTopLevelInteractive() ? 'div' : 'button'} | ||
radglob marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| > | ||
| <ConditionalBox if={Boolean(slots.trailingVisual)} sx={{display: 'flex', flexGrow: 1}}> | ||
| <ConditionalBox | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.