Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(main-nav): Main Nav refactoring, add the Home icon (#20176)
* feat(main-nav): replace DS NavLink with admin NavLink * feat(main-nav): change icon type * feat(main-nav): fix prettier errors * feat(main-nav): refactor navlink code and add more test cases * feat(main-nav): minor fixes * feat(main-nav): fix ui errors * feat(main-nav): fix merge issues * feat(main-nav): fix unit test and types * feat(main-nav): change offset values
- Loading branch information
Showing
5 changed files
with
171 additions
and
5 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
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
115 changes: 115 additions & 0 deletions
115
packages/core/admin/admin/src/components/MainNav/NavLink.tsx
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,115 @@ | ||
import * as React from 'react'; | ||
|
||
import { Tooltip, Flex, Badge } from '@strapi/design-system'; | ||
import { NavLink as RouterLink, LinkProps } from 'react-router-dom'; | ||
import styled from 'styled-components'; | ||
|
||
/* ------------------------------------------------------------------------------------------------- | ||
* Link | ||
* -----------------------------------------------------------------------------------------------*/ | ||
const MainNavLinkWrapper = styled(RouterLink)` | ||
text-decoration: none; | ||
display: block; | ||
border-radius: ${({ theme }) => theme.borderRadius}; | ||
background: ${({ theme }) => theme.colors.neutral0}; | ||
color: ${({ theme }) => theme.colors.neutral600}; | ||
position: relative; | ||
&:hover, | ||
&.active { | ||
background: ${({ theme }) => theme.colors.neutral100}; | ||
} | ||
&:hover { | ||
svg path { | ||
fill: ${({ theme }) => theme.colors.neutral600}; | ||
} | ||
color: ${({ theme }) => theme.colors.neutral700}; | ||
} | ||
&.active { | ||
svg path { | ||
fill: ${({ theme }) => theme.colors.primary600}; | ||
} | ||
color: ${({ theme }) => theme.colors.primary600}; | ||
font-weight: 500; | ||
} | ||
`; | ||
|
||
const LinkImpl = ({ children, ...props }: LinkProps) => { | ||
return <MainNavLinkWrapper {...props}>{children}</MainNavLinkWrapper>; | ||
}; | ||
|
||
/* ------------------------------------------------------------------------------------------------- | ||
* Tooltip | ||
* -----------------------------------------------------------------------------------------------*/ | ||
const TooltipImpl = ({ children, label, position = 'right' }: NavLink.TooltipProps) => { | ||
return ( | ||
<Tooltip position={position} label={label}> | ||
<Flex justifyContent="center" width={7} height={7}> | ||
{children} | ||
</Flex> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
/* ------------------------------------------------------------------------------------------------- | ||
* Icon | ||
* -----------------------------------------------------------------------------------------------*/ | ||
const IconImpl = ({ children }: { children: React.ReactNode }) => { | ||
if (!children) { | ||
return null; | ||
} | ||
return ( | ||
<Flex justifyContent="center" aria-hidden as="span" width={5} height={5}> | ||
{children} | ||
</Flex> | ||
); | ||
}; | ||
|
||
/* ------------------------------------------------------------------------------------------------- | ||
* Badge | ||
* -----------------------------------------------------------------------------------------------*/ | ||
const CustomBadge = styled(Badge)` | ||
/* override default badge styles to change the border radius of the Base element in the Design System */ | ||
border-radius: ${({ theme }) => theme.spaces[10]}; | ||
`; | ||
|
||
const BadgeImpl = ({ children, label, ...props }: NavLink.BadgeProps) => { | ||
if (!children) { | ||
return null; | ||
} | ||
return ( | ||
<CustomBadge position="absolute" top="-1.2rem" right="-0.4rem" aria-label={label} {...props}> | ||
{children} | ||
</CustomBadge> | ||
); | ||
}; | ||
|
||
/* ------------------------------------------------------------------------------------------------- | ||
* EXPORTS | ||
* -----------------------------------------------------------------------------------------------*/ | ||
|
||
const NavLink = { | ||
Link: LinkImpl, | ||
Tooltip: TooltipImpl, | ||
Icon: IconImpl, | ||
Badge: BadgeImpl, | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace NavLink { | ||
export interface BadgeProps { | ||
children: React.ReactNode; | ||
label: string; | ||
} | ||
|
||
export interface TooltipProps { | ||
position?: 'top' | 'bottom' | 'left' | 'right'; | ||
label?: string; | ||
children: React.ReactNode; | ||
} | ||
} | ||
|
||
export { NavLink }; |
38 changes: 38 additions & 0 deletions
38
packages/core/admin/admin/src/components/MainNav/tests/NavLink.test.tsx
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,38 @@ | ||
import { House, Lock } from '@strapi/icons'; | ||
import { screen, render as renderRTL } from '@tests/utils'; | ||
|
||
import { NavLink } from '../NavLink'; | ||
|
||
describe('NavLink', () => { | ||
const Component = () => ( | ||
<NavLink.Link to="/test-link"> | ||
<NavLink.Tooltip label="test-tooltip"> | ||
<> | ||
<NavLink.Icon> | ||
<House data-testid="nav-link-icon" /> | ||
</NavLink.Icon> | ||
<NavLink.Badge label="badge label"> | ||
<Lock data-testid="nav-link-badge" /> | ||
</NavLink.Badge> | ||
</> | ||
</NavLink.Tooltip> | ||
</NavLink.Link> | ||
); | ||
|
||
const render = () => renderRTL(<Component />); | ||
|
||
it('shows the NavLink with link to destination', async () => { | ||
render(); | ||
const link = screen.getByRole('link'); | ||
expect(link).toBeInTheDocument(); | ||
expect(link).toHaveAttribute('href', '/test-link'); | ||
}); | ||
it('shows the home icon in the link', async () => { | ||
render(); | ||
expect(screen.getByTestId('nav-link-icon')).toBeInTheDocument(); | ||
}); | ||
it('shows the badge next to the link', async () => { | ||
render(); | ||
expect(screen.getByTestId('nav-link-badge')).toBeInTheDocument(); | ||
}); | ||
}); |
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