-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ActionList.Heading component (#3581)
* Add ActionList.Heading component * Update .changeset/slimy-starfishes-agree.md * heading styles * Update heading style and add visually hidden wrapper with story updates * warning for heading in menu & make as prop required * fix linting 🙄 * import useId from hooks not react * snaps * remove the full variant story because there is no full variant list heading
- Loading branch information
1 parent
5cad920
commit cc12464
Showing
8 changed files
with
273 additions
and
49 deletions.
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.