From 53cf1f19cccb49b7fcb48b1bd4955a54b3190493 Mon Sep 17 00:00:00 2001 From: Simone Date: Mon, 29 Apr 2024 15:37:57 +0200 Subject: [PATCH] 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 --- .../admin/admin/src/components/LeftMenu.tsx | 14 ++- .../admin/src/components/MainNav/NavBrand.tsx | 8 +- .../admin/src/components/MainNav/NavLink.tsx | 115 ++++++++++++++++++ .../components/MainNav/tests/NavLink.test.tsx | 38 ++++++ .../core/admin/admin/src/translations/en.json | 1 + 5 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 packages/core/admin/admin/src/components/MainNav/NavLink.tsx create mode 100644 packages/core/admin/admin/src/components/MainNav/tests/NavLink.test.tsx diff --git a/packages/core/admin/admin/src/components/LeftMenu.tsx b/packages/core/admin/admin/src/components/LeftMenu.tsx index 9fce828e073..6e61da6e89b 100644 --- a/packages/core/admin/admin/src/components/LeftMenu.tsx +++ b/packages/core/admin/admin/src/components/LeftMenu.tsx @@ -15,7 +15,7 @@ import { NavSections, NavUser, } from '@strapi/design-system'; -import { SignOut, Feather, Lock } from '@strapi/icons'; +import { SignOut, Feather, Lock, House } from '@strapi/icons'; import { useIntl } from 'react-intl'; import { NavLink as RouterNavLink, useLocation } from 'react-router-dom'; import styled from 'styled-components'; @@ -28,6 +28,7 @@ import { usePersistentState } from '../hooks/usePersistentState'; import { getDisplayName } from '../utils/users'; import { NavBrand as NewNavBrand } from './MainNav/NavBrand'; +import { NavLink as NewNavLink } from './MainNav/NavLink'; const LinkUserWrapper = styled(Box)` width: 15rem; @@ -137,6 +138,17 @@ const LeftMenu = ({ generalSectionLinks, pluginsSectionLinks }: LeftMenuProps) = + {condensed && ( + handleClickOnLink('/')}> + + + + + + + )} theme.borderRadius}; object-fit: contain; - height: ${24 / 16}rem; - width: ${24 / 16}rem; - margin: ${3 / 16}rem; + height: 2.4rem; + width: 2.4rem; + margin: 0.4rem; } `; @@ -22,7 +22,7 @@ export const NavBrand = () => { } = useConfiguration('LeftMenu'); return ( - + {formatMessage({ 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 {children}; +}; + +/* ------------------------------------------------------------------------------------------------- + * Tooltip + * -----------------------------------------------------------------------------------------------*/ +const TooltipImpl = ({ children, label, position = 'right' }: NavLink.TooltipProps) => { + return ( + + + {children} + + + ); +}; + +/* ------------------------------------------------------------------------------------------------- + * Icon + * -----------------------------------------------------------------------------------------------*/ +const IconImpl = ({ children }: { children: React.ReactNode }) => { + if (!children) { + return null; + } + return ( + + {children} + + ); +}; + +/* ------------------------------------------------------------------------------------------------- + * 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 ( + + {children} + + ); +}; + +/* ------------------------------------------------------------------------------------------------- + * 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 }; diff --git a/packages/core/admin/admin/src/components/MainNav/tests/NavLink.test.tsx b/packages/core/admin/admin/src/components/MainNav/tests/NavLink.test.tsx new file mode 100644 index 00000000000..f5e02abcf3c --- /dev/null +++ b/packages/core/admin/admin/src/components/MainNav/tests/NavLink.test.tsx @@ -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 = () => ( + + + <> + + + + + + + + + + ); + + const render = () => renderRTL(); + + 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(); + }); +}); diff --git a/packages/core/admin/admin/src/translations/en.json b/packages/core/admin/admin/src/translations/en.json index f09420b1aad..1d1a2400751 100644 --- a/packages/core/admin/admin/src/translations/en.json +++ b/packages/core/admin/admin/src/translations/en.json @@ -685,6 +685,7 @@ "global.change-password": "Change password", "global.close": "Close", "global.content-manager": "Content Manager", + "global.home": "Home", "global.continue": "Continue", "global.delete": "Delete", "global.delete-target": "Delete {target}",