Skip to content
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

Add ActionList.Heading component #3581

Merged
merged 14 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/slimy-starfishes-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@primer/react': minor
---

ActionList: Add ActionList.Heading component

<!-- Changed components: ActionList -->
131 changes: 131 additions & 0 deletions src/ActionList/ActionList.features.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import {Meta} from '@storybook/react'
import {ActionMenu} from '../ActionMenu'

Check failure on line 3 in src/ActionList/ActionList.features.stories.tsx

View workflow job for this annotation

GitHub Actions / lint

'ActionMenu' is defined but never used
import {ActionList} from '.'
import {Item} from './Item'
import {LinkItem} from './LinkItem'
Expand All @@ -23,6 +24,8 @@
AlertIcon,
TableIcon,
PeopleIcon,
FileDirectoryIcon,
PlusCircleIcon,
} from '@primer/octicons-react'

export default {
Expand All @@ -41,6 +44,134 @@
</ActionList>
)

export const WithVisualListHeading = () => (
<ActionList>
<ActionList.Heading as="h2">Filter by</ActionList.Heading>
<ActionList.Group title="Path">
<ActionList.Item onClick={() => {}}>
<ActionList.LeadingVisual>
<FileDirectoryIcon />
</ActionList.LeadingVisual>
app/assets/modules
</ActionList.Item>
<ActionList.Item onClick={() => {}}>
<ActionList.LeadingVisual>
<FileDirectoryIcon />
</ActionList.LeadingVisual>
src/react/components
</ActionList.Item>
<ActionList.Item onClick={() => {}}>
<ActionList.LeadingVisual>
<FileDirectoryIcon />
</ActionList.LeadingVisual>
memex/shared-ui/components
</ActionList.Item>
<ActionList.Item onClick={() => {}}>
<ActionList.LeadingVisual>
<FileDirectoryIcon />
</ActionList.LeadingVisual>
views/assets/modules
</ActionList.Item>
</ActionList.Group>

<ActionList.Group title="Advanced">
<ActionList.Item onClick={() => {}}>
<ActionList.LeadingVisual>
<PlusCircleIcon />
</ActionList.LeadingVisual>
Owner
</ActionList.Item>
<ActionList.Item onClick={() => {}}>
<ActionList.LeadingVisual>
<PlusCircleIcon />
</ActionList.LeadingVisual>
Symbol
</ActionList.Item>
<ActionList.Item onClick={() => {}}>
<ActionList.LeadingVisual>
<PlusCircleIcon />
</ActionList.LeadingVisual>
Exclude archived
</ActionList.Item>
</ActionList.Group>
</ActionList>
)

export const WithHeadingInFullVariant = () => (
<ActionList variant="full">
<ActionList.Heading as="h1">Details</ActionList.Heading>
<ActionList.LinkItem href="https://github.com/primer/react#readme">
<ActionList.LeadingVisual>
<BookIcon />
</ActionList.LeadingVisual>
Readme
</ActionList.LinkItem>
<ActionList.LinkItem href="https://github.com/primer/react/blob/main/LICENSE">
<ActionList.LeadingVisual>
<LawIcon />
</ActionList.LeadingVisual>
MIT License
</ActionList.LinkItem>
<ActionList.LinkItem href="https://github.com/primer/react/stargazers">
<ActionList.LeadingVisual>
<StarIcon />
</ActionList.LeadingVisual>
<strong>1.5k</strong> stars
</ActionList.LinkItem>
<ActionList.LinkItem href="https://github.com/primer/react/watchers">
<ActionList.LeadingVisual>
<EyeIcon />
</ActionList.LeadingVisual>
<strong>21</strong> watching
</ActionList.LinkItem>
<ActionList.LinkItem href="https://github.com/primer/react/network/members">
<ActionList.LeadingVisual>
<RepoForkedIcon />
</ActionList.LeadingVisual>
<strong>225</strong> forks
</ActionList.LinkItem>
</ActionList>
)

export const WithCustomHeading = () => (
<>
<Heading as="h1" id="list-heading" sx={{fontSize: 3, marginX: 3}}>
Details
</Heading>
<ActionList aria-labelledby="list-heading">
<ActionList.LinkItem href="https://github.com/primer/react#readme">
<ActionList.LeadingVisual>
<BookIcon />
</ActionList.LeadingVisual>
Readme
</ActionList.LinkItem>
<ActionList.LinkItem href="https://github.com/primer/react/blob/main/LICENSE">
<ActionList.LeadingVisual>
<LawIcon />
</ActionList.LeadingVisual>
MIT License
</ActionList.LinkItem>
<ActionList.LinkItem href="https://github.com/primer/react/stargazers">
<ActionList.LeadingVisual>
<StarIcon />
</ActionList.LeadingVisual>
<strong>1.5k</strong> stars
</ActionList.LinkItem>
<ActionList.LinkItem href="https://github.com/primer/react/watchers">
<ActionList.LeadingVisual>
<EyeIcon />
</ActionList.LeadingVisual>
<strong>21</strong> watching
</ActionList.LinkItem>
<ActionList.LinkItem href="https://github.com/primer/react/network/members">
<ActionList.LeadingVisual>
<RepoForkedIcon />
</ActionList.LeadingVisual>
<strong>225</strong> forks
</ActionList.LinkItem>
</ActionList>
</>
)
export const WithIcons = () => (
<ActionList>
<ActionList.Item>
Expand Down
44 changes: 44 additions & 0 deletions src/ActionList/Heading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, {forwardRef} from 'react'
import {BetterSystemStyleObject, SxProp, merge} from '../sx'
import {defaultSxProp} from '../utils/defaultSxProp'
import {useRefObjectAsForwardedRef} from '../hooks'
import {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'
import {default as HeadingComponent} from '../Heading'
import {ListContext} from './List'
import VisuallyHidden from '../_VisuallyHidden'

export type ActionListHeadingProps = {
as?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'
visuallyHidden?: boolean
} & SxProp

export const Heading = forwardRef(
({as: Component = 'h3', children, sx = defaultSxProp, visuallyHidden = false, ...props}, forwardedRef) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non blocking: Should there be a default h3 or should we always ask for the heading level by making as required without default?

Related work from eslint-plugin-primer-react:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh this is great! Yes I agree we should make it required!

const innerRef = React.useRef<HTMLHeadingElement>(null)
useRefObjectAsForwardedRef(forwardedRef, innerRef)

const {headingId: headingId, variant: listVariant} = React.useContext(ListContext)

const styles = {
marginBottom: 2,
marginX: listVariant === 'full' ? 2 : 3,
}

return (
<VisuallyHidden isVisible={!visuallyHidden}>
<HeadingComponent
as={Component}
ref={innerRef}
// use custom id if it is provided. Otherwise, use the id from the context
id={props.id ?? headingId}
sx={merge<BetterSystemStyleObject>(styles, sx)}
{...props}
>
{children}
</HeadingComponent>
</VisuallyHidden>
)
},
) as PolymorphicForwardRefComponent<'h3', ActionListHeadingProps>

Heading.displayName = 'ActionList.Heading'
51 changes: 33 additions & 18 deletions src/ActionList/List.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from 'react'
import React, {useId} from 'react'
import {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic'
import styled from 'styled-components'
import sx, {SxProp, merge} from '../sx'
import {AriaRole} from '../utils/types'
import {ActionListContainerContext} from './ActionListContainerContext'
import {defaultSxProp} from '../utils/defaultSxProp'
import {useSlots} from '../hooks/useSlots'
import {Heading} from './Heading'

export type ActionListProps = React.PropsWithChildren<{
/**
Expand All @@ -26,7 +28,10 @@ export type ActionListProps = React.PropsWithChildren<{
}> &
SxProp

type ContextProps = Pick<ActionListProps, 'variant' | 'selectionVariant' | 'showDividers' | 'role'>
type ContextProps = Pick<ActionListProps, 'variant' | 'selectionVariant' | 'showDividers' | 'role'> & {
headingId?: string
}

export const ListContext = React.createContext<ContextProps>({})

const ListBox = styled.ul<SxProp>(sx)
Expand All @@ -42,32 +47,42 @@ export const List = React.forwardRef<HTMLUListElement, ActionListProps>(
paddingY: variant === 'inset' ? 2 : 0,
}

const [slots, childrenWithoutSlots] = useSlots(props.children, {
heading: Heading,
})

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this already work with eslint-plugin-primer-react or do we need to update that as well?

Copy link
Member Author

@broccolinisoup broccolinisoup Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would need to make some update on the eslint plugin.

  • We need to add ActionList: ['ActionList.Header'], in this object

Although, this rule will only prevent folks from using ActionList.Heading outside of ActionList, this won't warn if the ActionList is missing ActionList.Heading which I believe this is something we need as well? Can we do this with the type system at all? 🤔 (This would make the change breaking, though) Or should we look into adding another eslint rule for that too?

Also we can add ActionList.Heading to the a11y-explicit-heading rule to restrict the values passed to as

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering action lists in menus shouldn't have a heading (reference), maybe we can enforece this with the eslint rule as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we can add ActionList.Heading to the a11y-explicit-heading rule to restrict the values passed to as

Because ActionList.Heading is a new component, we can also use typescript to restrict allowed values for as :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering action lists in menus shouldn't have a heading (#3581 (comment)), maybe we can enforece this with the eslint rule as well?

Good point. We could add an eslint rule for this, however, I'm not sure how effectively we would be able to catch incorrect usage given ActionList does not have to be a direct child of ActionMenu, it can be abstracted in a custom UserList or BranchList component that uses ActionList internally.

We also have the option of a dev environment runtime warning. Because we know when an ActionList is contained inside ActionMenu, we could use that to throw a warning

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because ActionList.Heading is a new component, we can also use typescript to restrict allowed values for as :)

That is so true! I missed that 😅 which is already restricted anyway!

We also have the option of a dev environment runtime warning. Because we know when an ActionList is contained inside ActionMenu, we could use that to throw a warning

I love this! Thanks for the context. I made it invariant to throw an error. Because first, it is a new component and it won't break anything. Second, when there is a heading the list will be labelled by the heading and completely disregard the button name. This could potentially be misleading.

All that said, if you think that is unnecessary and very little chance of creating a misleading experience, happy to make it warning as well 😊

const headingId = useId()

/** if list is inside a Menu, it will get a role from the Menu */
const {
listRole,
listLabelledBy,
selectionVariant: containerSelectionVariant, // TODO: Remove after DropdownMenu2 deprecation
} = React.useContext(ActionListContainerContext)

const ariaLabelledBy = slots.heading ? slots.heading.props.id ?? headingId : listLabelledBy

return (
<ListBox
sx={merge(styles, sxProp as SxProp)}
role={role || listRole}
aria-labelledby={listLabelledBy}
{...props}
ref={forwardedRef}
<ListContext.Provider
value={{
variant,
selectionVariant: selectionVariant || containerSelectionVariant,
showDividers,
role: role || listRole,
headingId,
}}
>
<ListContext.Provider
value={{
variant,
selectionVariant: selectionVariant || containerSelectionVariant,
showDividers,
role: role || listRole,
}}
{slots.heading}
<ListBox
sx={merge(styles, sxProp as SxProp)}
role={role || listRole}
aria-labelledby={ariaLabelledBy}
{...props}
ref={forwardedRef}
>
{props.children}
</ListContext.Provider>
</ListBox>
{childrenWithoutSlots}
</ListBox>
</ListContext.Provider>
)
},
) as PolymorphicForwardRefComponent<'ul', ActionListProps>
Expand Down
5 changes: 5 additions & 0 deletions src/ActionList/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {LinkItem} from './LinkItem'
import {Divider} from './Divider'
import {Description} from './Description'
import {LeadingVisual, TrailingVisual} from './Visuals'
import {Heading} from './Heading'

export type {ActionListProps} from './List'
export type {ActionListGroupProps} from './Group'
Expand All @@ -13,6 +14,7 @@ export type {ActionListLinkItemProps} from './LinkItem'
export type {ActionListDividerProps} from './Divider'
export type {ActionListDescriptionProps} from './Description'
export type {ActionListLeadingVisualProps, ActionListTrailingVisualProps} from './Visuals'
export type {ActionListHeadingProps} from './Heading'

/**
* Collection of list-related components.
Expand All @@ -38,4 +40,7 @@ export const ActionList = Object.assign(List, {

/** Icon (or similar) positioned after `Item` text. */
TrailingVisual,

/** Heading for an `ActionList`. */
Heading,
})
Loading