Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/rotten-horses-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@primer/react": patch
"docs": patch
---

ActionBar: Add new component for submenu
13 changes: 13 additions & 0 deletions packages/react/src/drafts/ActionBar/ActionBar.docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@
{
"name": "ActionBar.Divider",
"props": []
},
{
"name": "ActionBar.SubMenu",
"description": "Component to be used when having a menu in the actionbar. This facilitates smooth transition to submenu during overflow cases",
"props": [
{
"name": "anchor",
"type": "React.ReactElement",
"required": true,
"description": "This is the anchor element to be used as menu trigger. Ideally ActionBar.IconButton"
},
{"name": "children", "type": "React.ReactElement", "required": true}
]
}
]
}
7 changes: 7 additions & 0 deletions packages/react/src/drafts/ActionBar/ActionBar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ export const CommentBox = () => {
icon={ReplyIcon}
aria-label="Saved Replies"
></ActionBar.IconButton>
<ActionBar.SubMenu anchor={<ActionBar.IconButton icon={ThreeBarsIcon} aria-label="Random Menu" />}>
<ActionList>
<ActionList.Item>First Item</ActionList.Item>
<ActionList.Item>Second Item</ActionList.Item>
<ActionList.Item>Third Item</ActionList.Item>
</ActionList>
</ActionBar.SubMenu>
</ActionBar>
</Box>
</Box>
Expand Down
58 changes: 45 additions & 13 deletions packages/react/src/drafts/ActionBar/ActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {useOnOutsideClick} from '../../hooks/useOnOutsideClick'
import type {IconButtonProps} from '../../Button'
import {IconButton} from '../../Button'
import Box from '../../Box'
import {ActionMenu} from '../..'
import {ActionMenu} from '../../ActionMenu'
import {Action} from 'history'
Copy link
Member

Choose a reason for hiding this comment

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

Oops :)


type ChildSize = {
text: string
Expand Down Expand Up @@ -43,6 +44,31 @@ export type ActionBarProps = {

export type ActionBarIconButtonProps = IconButtonProps

export const ActionBarIconButton = forwardRef((props: ActionBarIconButtonProps, forwardedRef) => {
const backupRef = useRef<HTMLElement>(null)
const ref = (forwardedRef ?? backupRef) as RefObject<HTMLAnchorElement>
const {size, setChildrenWidth} = React.useContext(ActionBarContext)
useIsomorphicLayoutEffect(() => {
const text = props['aria-label'] ? props['aria-label'] : ''
const domRect = (ref as MutableRefObject<HTMLElement>).current.getBoundingClientRect()
setChildrenWidth({text, width: domRect.width})
}, [ref, setChildrenWidth])
return <IconButton ref={ref} size={size} {...props} variant="invisible" />
})

export type ActionBarSubMenuProps = {anchor: React.ReactElement; children: React.ReactElement[] | React.ReactElement}

export const ActionBarSubMenu = (props: ActionBarSubMenuProps) => {
const {anchor, children} = props

return (
<ActionMenu>
<ActionMenu.Anchor>{anchor}</ActionMenu.Anchor>
<ActionMenu.Overlay>{children}</ActionMenu.Overlay>
</ActionMenu>
)
}

const NavigationList = styled.div`
${sx};
`
Expand Down Expand Up @@ -239,6 +265,24 @@ export const ActionBar: React.FC<React.PropsWithChildren<ActionBarProps>> = prop
{menuItems.map((menuItem, index) => {
if (menuItem.type === ActionList.Divider) {
return <ActionList.Divider key={index} />
} else if (menuItem.type === ActionBarSubMenu) {
const {children: menuItemChildren, anchor} = menuItem.props
const {icon: Icon, 'aria-label': ariaLabel} = anchor.props
return (
<ActionMenu>
<ActionMenu.Anchor>
<ActionList.Item>
{Icon ? (
<ActionList.LeadingVisual>
<Icon />
</ActionList.LeadingVisual>
) : null}
{ariaLabel}
</ActionList.Item>
</ActionMenu.Anchor>
<ActionMenu.Overlay>{menuItemChildren}</ActionMenu.Overlay>
</ActionMenu>
)
} else {
const {
children: menuItemChildren,
Expand Down Expand Up @@ -279,18 +323,6 @@ export const ActionBar: React.FC<React.PropsWithChildren<ActionBarProps>> = prop
)
}

export const ActionBarIconButton = forwardRef((props: ActionBarIconButtonProps, forwardedRef) => {
const backupRef = useRef<HTMLElement>(null)
const ref = (forwardedRef ?? backupRef) as RefObject<HTMLAnchorElement>
const {size, setChildrenWidth} = React.useContext(ActionBarContext)
useIsomorphicLayoutEffect(() => {
const text = props['aria-label'] ? props['aria-label'] : ''
const domRect = (ref as MutableRefObject<HTMLElement>).current.getBoundingClientRect()
setChildrenWidth({text, width: domRect.width})
}, [ref, setChildrenWidth])
return <IconButton ref={ref} size={size} {...props} variant="invisible" />
})

const sizeToHeight = {
small: '24px',
medium: '28px',
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/drafts/ActionBar/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {ActionBar as Bar, ActionBarIconButton, VerticalDivider} from './ActionBar'
import {ActionBar as Bar, ActionBarIconButton, ActionBarSubMenu, VerticalDivider} from './ActionBar'
export type {ActionBarProps} from './ActionBar'

const ActionBar = Object.assign(Bar, {
IconButton: ActionBarIconButton,
SubMenu: ActionBarSubMenu,
Divider: VerticalDivider,
})

Expand Down