-
Notifications
You must be signed in to change notification settings - Fork 536
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7980fe1
Add ActionList.Heading component
broccolinisoup 5f1a322
Update .changeset/slimy-starfishes-agree.md
broccolinisoup 576161f
heading styles
broccolinisoup 6b613d3
Merge branch 'main' into bs/actionlist-heading
broccolinisoup 5a316b5
Update heading style and add visually hidden wrapper with story updates
broccolinisoup 7f9711e
warning for heading in menu & make as prop required
broccolinisoup 77f5364
fix linting π
broccolinisoup 4449f26
Merge branch 'main' into bs/actionlist-heading
broccolinisoup 47f8cc9
import useId from hooks not react
broccolinisoup 32bb440
snaps
broccolinisoup 3213b5e
Merge branch 'main' into bs/actionlist-heading
broccolinisoup c5d2038
Merge branch 'main' into bs/actionlist-heading
broccolinisoup c24a4eb
Merge branch 'main' into bs/actionlist-heading
broccolinisoup c68c0cc
remove the full variant story because there is no full variant list hβ¦
broccolinisoup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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' | ||
import {ActionListContainerContext} from './ActionListContainerContext' | ||
import {invariant} from '../utils/invariant' | ||
|
||
type HeadingLevels = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | ||
export type ActionListHeadingProps = { | ||
as: HeadingLevels | ||
visuallyHidden?: boolean | ||
} & SxProp | ||
|
||
export const Heading = forwardRef( | ||
({as, children, sx = defaultSxProp, visuallyHidden = false, ...props}, forwardedRef) => { | ||
const innerRef = React.useRef<HTMLHeadingElement>(null) | ||
useRefObjectAsForwardedRef(forwardedRef, innerRef) | ||
|
||
const {headingId: headingId, variant: listVariant} = React.useContext(ListContext) | ||
const {container} = React.useContext(ActionListContainerContext) | ||
|
||
// Semantic <menu>s don't have a place for headers within them, they should be aria-labelledby the menu button's name. | ||
invariant( | ||
container !== 'ActionMenu', | ||
`ActionList.Heading shouldn't be used within an ActionMenu container. Menus are labelled by the menu button's name.`, | ||
) | ||
|
||
const styles = { | ||
marginBottom: 2, | ||
marginX: listVariant === 'full' ? 2 : 3, | ||
} | ||
|
||
return ( | ||
<VisuallyHidden isVisible={!visuallyHidden}> | ||
<HeadingComponent | ||
as={as} | ||
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<HeadingLevels, ActionListHeadingProps> | ||
|
||
Heading.displayName = 'ActionList.Heading' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
ActionList: ['ActionList.Header'],
in this objectAlthough, this rule will only prevent folks from using
ActionList.Heading
outside ofActionList
, this won't warn if theActionList
is missingActionList.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 toas
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
:)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
orBranchList
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is so true! I missed that π which is already restricted anyway!
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 π