-
Notifications
You must be signed in to change notification settings - Fork 592
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 TrailingAction support to NavList #4697
Changes from all commits
2a2f3c2
2c9a5b1
c3d3139
20d6c6b
9105663
81d330a
8cbd87f
cb6ff9b
8de3535
057ff7c
646110e
46f8083
ca827ed
fd19946
4dd7f35
786bcc5
5449c5f
574080c
c2d3534
0ea3d9f
2cd3746
27e01a8
File filter
Filter by extension
Conversations
Jump to
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": minor | ||
--- | ||
|
||
Add TrailingAction support to NavList |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {test, expect} from '@playwright/test' | ||
import {visit} from '../test-helpers/storybook' | ||
import {themes} from '../test-helpers/themes' | ||
|
||
test.describe('NavList', () => { | ||
test.describe('With TrailingAction', () => { | ||
for (const theme of themes) { | ||
test.describe(theme, () => { | ||
test('default @vrt', async ({page}) => { | ||
await visit(page, { | ||
id: 'components-navlist--with-trailing-action', | ||
globals: { | ||
colorScheme: theme, | ||
}, | ||
}) | ||
|
||
// Default state | ||
expect(await page.screenshot()).toMatchSnapshot(`NavList.With TrailingAction.${theme}.png`) | ||
}) | ||
|
||
test('axe @aat', async ({page}) => { | ||
await visit(page, { | ||
id: 'components-navlist--with-trailing-action', | ||
globals: { | ||
colorScheme: theme, | ||
}, | ||
}) | ||
await expect(page).toHaveNoViolations() | ||
}) | ||
}) | ||
} | ||
}) | ||
|
||
test.describe('With Bad Example of SubNav and TrailingAction', () => { | ||
for (const theme of themes) { | ||
test.describe(theme, () => { | ||
test('default @vrt', async ({page}) => { | ||
await visit(page, { | ||
id: 'components-navlist-devonly--with-bad-example-of-sub-nav-and-trailing-action', | ||
globals: { | ||
colorScheme: theme, | ||
}, | ||
}) | ||
|
||
// Default state | ||
expect(await page.screenshot()).toMatchSnapshot( | ||
`NavList.With Bad Example of SubNav and TrailingAction.${theme}.png`, | ||
) | ||
}) | ||
|
||
test('axe @aat', async ({page}) => { | ||
await visit(page, { | ||
id: 'components-navlist-devonly--with-bad-example-of-sub-nav-and-trailing-action', | ||
globals: { | ||
colorScheme: theme, | ||
}, | ||
}) | ||
await expect(page).toHaveNoViolations() | ||
}) | ||
}) | ||
} | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type {Meta} from '@storybook/react' | ||
import React from 'react' | ||
import {PageLayout} from '../PageLayout' | ||
import {NavList} from './NavList' | ||
import {ArrowRightIcon, ArrowLeftIcon, BookIcon, FileDirectoryIcon} from '@primer/octicons-react' | ||
|
||
const meta: Meta = { | ||
title: 'Components/NavList/DevOnly', | ||
component: NavList, | ||
parameters: { | ||
layout: 'fullscreen', | ||
}, | ||
} | ||
|
||
export const WithBadExampleOfSubNavAndTrailingAction = () => { | ||
return ( | ||
<PageLayout> | ||
<PageLayout.Pane position="start"> | ||
<NavList> | ||
<NavList.Item> | ||
<NavList.LeadingVisual> | ||
<FileDirectoryIcon /> | ||
</NavList.LeadingVisual> | ||
Item 1 | ||
<NavList.TrailingAction label="Expand sidebar" icon={ArrowLeftIcon} /> | ||
</NavList.Item> | ||
<NavList.Item> | ||
Item 2 | ||
<NavList.TrailingAction as="a" href="#" label="Some action" icon={ArrowRightIcon} /> | ||
</NavList.Item> | ||
<NavList.Item> | ||
Item 3 | ||
<NavList.TrailingAction label="This is not supported and should not render!!!!!" icon={BookIcon} /> | ||
<NavList.SubNav> | ||
<NavList.Item href="#"> | ||
Sub item 1 | ||
<NavList.TrailingAction label="Another action" icon={BookIcon} /> | ||
</NavList.Item> | ||
</NavList.SubNav> | ||
</NavList.Item> | ||
</NavList> | ||
</PageLayout.Pane> | ||
</PageLayout> | ||
) | ||
} | ||
|
||
WithBadExampleOfSubNavAndTrailingAction.storyName = 'With SubNav and Trailing Action (Bad example, do not copy)' | ||
|
||
export default meta |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,12 @@ import {ChevronDownIcon} from '@primer/octicons-react' | |
import type {ForwardRefComponent as PolymorphicForwardRefComponent} from '../utils/polymorphic' | ||
import React, {isValidElement} from 'react' | ||
import styled from 'styled-components' | ||
import type {ActionListDividerProps, ActionListLeadingVisualProps, ActionListTrailingVisualProps} from '../ActionList' | ||
import type { | ||
ActionListTrailingActionProps, | ||
ActionListDividerProps, | ||
ActionListLeadingVisualProps, | ||
ActionListTrailingVisualProps, | ||
} from '../ActionList' | ||
import {ActionList} from '../ActionList' | ||
import {ActionListContainerContext} from '../ActionList/ActionListContainerContext' | ||
import Box from '../Box' | ||
|
@@ -65,9 +70,9 @@ const Item = React.forwardRef<HTMLAnchorElement, NavListItemProps>( | |
// Get SubNav from children | ||
const subNav = React.Children.toArray(children).find(child => isValidElement(child) && child.type === SubNav) | ||
|
||
// Get children without SubNav | ||
const childrenWithoutSubNav = React.Children.toArray(children).filter(child => | ||
isValidElement(child) ? child.type !== SubNav : true, | ||
// Get children without SubNav or TrailingAction | ||
const childrenWithoutSubNavOrTrailingAction = React.Children.toArray(children).filter(child => | ||
isValidElement(child) ? child.type !== SubNav && child.type !== TrailingAction : true, | ||
) | ||
|
||
if (!isValidElement(subNav) && defaultOpen) | ||
|
@@ -78,7 +83,7 @@ const Item = React.forwardRef<HTMLAnchorElement, NavListItemProps>( | |
if (subNav && isValidElement(subNav)) { | ||
return ( | ||
<ItemWithSubNav subNav={subNav} depth={depth} defaultOpen={defaultOpen} sx={sxProp}> | ||
{childrenWithoutSubNav} | ||
{childrenWithoutSubNavOrTrailingAction} | ||
</ItemWithSubNav> | ||
) | ||
} | ||
|
@@ -251,6 +256,14 @@ const Divider = ActionList.Divider | |
|
||
Divider.displayName = 'NavList.Divider' | ||
|
||
// NavList.TrailingAction | ||
|
||
export type NavListTrailingActionProps = ActionListTrailingActionProps | ||
|
||
const TrailingAction = ActionList.TrailingAction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right! I think we should disallow it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has been addressed in this diff. Let me know what you think of this approach! I've supplemented this with:
|
||
|
||
TrailingAction.displayName = 'NavList.TrailingAction' | ||
|
||
// ---------------------------------------------------------------------------- | ||
// NavList.Group | ||
|
||
|
@@ -285,6 +298,7 @@ export const NavList = Object.assign(Root, { | |
SubNav, | ||
LeadingVisual, | ||
TrailingVisual, | ||
TrailingAction, | ||
Divider, | ||
Group, | ||
}) |
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.
Non-blocking: A story showing this feature within sub items could be cool!
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.
Done!
See Storybook: WithTrailingActionInSubItem