-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): add menu component (#53)
* feat(frontend): add menu component * feat(frontend): add menu snapshot
- Loading branch information
Showing
8 changed files
with
138 additions
and
27 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
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,69 @@ | ||
import type { ComponentProps} from "react"; | ||
import { useState } from "react" | ||
import { useTranslation } from "react-i18next"; | ||
import { cn } from "~/utils/tailwind-utils"; | ||
import { InlineLink } from "./inline-link"; | ||
|
||
type MenuItemProps = ComponentProps<typeof InlineLink> | ||
|
||
export function MenuItem({children, ...props}: MenuItemProps) { | ||
return ( | ||
<InlineLink | ||
role="menuitem" | ||
id="menu-item" | ||
className="hover:text-blue-950 active:text-white focus:text-blue-400 text-md text-white block px-4 py-2 text-md hover:bg-slate-300 focus:bg-slate-600 active:bg-slate-800 text-md" | ||
{...props} | ||
> | ||
{children} | ||
</InlineLink> | ||
) | ||
} | ||
|
||
interface MenuProps { | ||
className?: string; | ||
children: React.ReactNode; | ||
} | ||
|
||
export function Menu({ className, children }: MenuProps) { | ||
const { t } = useTranslation(['gcweb']); | ||
const [open, setOpen] = useState(false); | ||
const baseClassName = cn(`${open ? "bg-slate-900 text-white hover:bg-slate-800" : "bg-slate-700 text-white hover:bg-slate-600"} hover:underline text-lg inline-flex justify-center space-x-2 rounded-b-md border-b border-l border-r border-slate-700 px-4 py-2 ring-black ring-opacity-5`); | ||
|
||
const onClick = () => { | ||
setOpen((value) => !value) | ||
} | ||
|
||
return ( | ||
<div className="relative inline-block text-left"> | ||
<button | ||
onClick={onClick} | ||
className={cn(baseClassName, className)} | ||
aria-haspopup={true} | ||
aria-expanded={open} | ||
> | ||
<span>{t('gcweb:app.menu')}</span> | ||
{open ? ( | ||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6"> | ||
<path strokeLinecap="round" strokeLinejoin="round" d="m4.5 15.75 7.5-7.5 7.5 7.5" /> | ||
</svg> | ||
) : ( | ||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6 my-auto"> | ||
<path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /> | ||
</svg> | ||
)} | ||
</button> | ||
{open && ( | ||
<div className="origin-top-left absolute left-0 mt-2 w-64 rounded-md shadow-lg bg-slate-700 ring-1 ring-black ring-opacity-5" | ||
role="menu" | ||
aria-orientation="vertical" | ||
aria-labelledby="menu-button" | ||
tabIndex={-1} | ||
> | ||
<div className="py-1" role="none"> | ||
{children} | ||
</div> | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} |
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
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,5 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`Menu > should correctly render a Menu with a MenuItem when the file property is provided > expected html 1`] = `"<div class="relative inline-block text-left"><button class="bg-slate-700 text-white hover:bg-slate-600 hover:underline text-lg inline-flex justify-center space-x-2 rounded-b-md border-b border-l border-r border-slate-700 px-4 py-2 ring-black ring-opacity-5" aria-haspopup="true" aria-expanded="false"><span>gcweb:app.menu</span><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6 my-auto"><path stroke-linecap="round" stroke-linejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5"></path></svg></button></div>"`; | ||
|
||
exports[`Menu > should correctly render a Menu with a MenuItem when the to property is provided > expected html 1`] = `"<div class="relative inline-block text-left"><button class="bg-slate-700 text-white hover:bg-slate-600 hover:underline text-lg inline-flex justify-center space-x-2 rounded-b-md border-b border-l border-r border-slate-700 px-4 py-2 ring-black ring-opacity-5" aria-haspopup="true" aria-expanded="false"><span>gcweb:app.menu</span><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6 my-auto"><path stroke-linecap="round" stroke-linejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5"></path></svg></button></div>"`; |
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,37 @@ | ||
import { render } from '@testing-library/react'; | ||
import { createRoutesStub } from 'react-router'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
import { Menu, MenuItem } from '~/components/menu'; | ||
|
||
describe('Menu', () => { | ||
beforeEach(() => { | ||
vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
|
||
it('should correctly render a Menu with a MenuItem when the file property is provided', () => { | ||
const RoutesStub = createRoutesStub([ | ||
{ | ||
path: '/fr/public', | ||
Component: () => <MenuItem file="routes/public/index.tsx">This is a test</MenuItem>, | ||
}, | ||
]); | ||
|
||
const { container } = render(<Menu><RoutesStub initialEntries={['/fr/public']} /></Menu>); | ||
|
||
expect(container.innerHTML).toMatchSnapshot('expected html'); | ||
}); | ||
|
||
it('should correctly render a Menu with a MenuItem when the to property is provided', () => { | ||
const RoutesStub = createRoutesStub([ | ||
{ | ||
path: '/fr/public', | ||
Component: () => <MenuItem to="https://example.com/">This is a test</MenuItem>, | ||
}, | ||
]); | ||
|
||
const { container } = render(<Menu><RoutesStub initialEntries={['/fr/public']} /></Menu>); | ||
|
||
expect(container.innerHTML).toMatchSnapshot('expected html'); | ||
}); | ||
}); |