Skip to content

Commit

Permalink
fix(components-LandingNav): add navigation Enter keydown handler (#838
Browse files Browse the repository at this point in the history
)
  • Loading branch information
sabertazimi authored May 11, 2022
1 parent 13ba45b commit 6e94c3a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
8 changes: 8 additions & 0 deletions components/Icons/Hamburger.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { MotionProps } from '@components/utils';
import { motion } from '@components/utils';
import type { KeyboardEvent } from 'react';
import { useCallback, useState } from 'react';
import IconFactory from './IconFactory';

Expand All @@ -24,6 +25,11 @@ const Svg = (): JSX.Element => {
() => setIsChecked(isChecked => !isChecked),
[]
);
const handleKeyDown = useCallback((event: KeyboardEvent<SVGSVGElement>) => {
if (event.key === 'Enter') {
setIsChecked(isChecked => !isChecked);
}
}, []);

return (
<motion.svg
Expand All @@ -32,9 +38,11 @@ const Svg = (): JSX.Element => {
width="2em"
height="2em"
fill="currentColor"
tabIndex={0}
initial={false}
animate={isChecked ? 'checked' : 'unchecked'}
onClick={handleClick}
onKeyDown={handleKeyDown}
data-testid="hamburger-icon"
>
<motion.path
Expand Down
5 changes: 4 additions & 1 deletion components/Icons/IconFactory.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Icon from '@ant-design/icons';
import type { SVGProps } from 'react';
import type { KeyboardEvent, SVGProps } from 'react';

interface Props {
onClick?: () => void;
onKeyDown?: (event: KeyboardEvent<HTMLSpanElement>) => void;
className?: string;
}

Expand All @@ -14,6 +15,7 @@ const IconFactory = (
): IconType => {
const IconComponent = ({
onClick,
onKeyDown,
className,
...props
}: Props): JSX.Element => (
Expand All @@ -22,6 +24,7 @@ const IconFactory = (
component={svg}
aria-label={ariaLabel}
onClick={onClick}
onKeyDown={onKeyDown}
className={className}
/>
);
Expand Down
1 change: 1 addition & 0 deletions components/Icons/__snapshots__/Icons.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ exports[`Icons should render 1 icon correctly (snapshot) 1`] = `
data-testid="hamburger-icon"
fill="currentColor"
height="2em"
tabindex="0"
viewBox="0 0 1024 1024"
width="2em"
>
Expand Down
36 changes: 34 additions & 2 deletions components/LandingNav/LandingNav.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,39 @@ describe('LandingNav', () => {
navLinks.forEach(navLink => expect(nav).toContainElement(navLink));
});

test('should expanded when clicked', () => {
test('should expanded when navigation button toggled', () => {
render(<LandingNav />);
act(() => {
jest.advanceTimersByTime(1000);
});

expect(screen.getByRole('navigation')).toHaveStyle(
'transform: translateX(-100%) translateZ(0);'
);
expect(screen.getByRole('banner')).toHaveStyle('opacity: 0');

fireEvent.click(screen.getByTestId('hamburger-button'));
act(() => {
jest.advanceTimersByTime(1000);
});

expect(screen.getByRole('navigation')).toHaveStyle(
'transform: translateX(0%) translateZ(0);'
);
expect(screen.getByRole('banner')).toHaveStyle('opacity: 0.8');

fireEvent.keyDown(screen.getByTestId('hamburger-button'), { key: 'Enter' });
act(() => {
jest.advanceTimersByTime(1000);
});

expect(screen.getByRole('navigation')).toHaveStyle(
'transform: translateX(-100%) translateZ(0);'
);
expect(screen.getByRole('banner')).toHaveStyle('opacity: 0');
});

test('should expanded when navigation icon toggled', () => {
render(<LandingNav />);
act(() => {
jest.advanceTimersByTime(1000);
Expand All @@ -61,7 +93,7 @@ describe('LandingNav', () => {
);
expect(screen.getByRole('banner')).toHaveStyle('opacity: 0.8');

fireEvent.click(screen.getByTestId('hamburger-button'));
fireEvent.keyDown(screen.getByTestId('hamburger-icon'), { key: 'Enter' });
act(() => {
jest.advanceTimersByTime(1000);
});
Expand Down
12 changes: 10 additions & 2 deletions components/LandingNav/LandingNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Switch } from '@components/Motion';
import type { MotionProps } from '@components/utils';
import { routes as defaultRoutes } from '@config';
import type { Route } from '@types';
import type { KeyboardEvent } from 'react';
import { useCallback, useState } from 'react';
import styles from './LandingNav.module.css';
import LandingNavLink from './LandingNavLink';
Expand Down Expand Up @@ -43,14 +44,21 @@ const bannerTransition: MotionProps['transition'] = {
const LandingNav = ({ routes = defaultRoutes }: Props): JSX.Element => {
const [expanded, setExpanded] = useState(false);

const handleToggle = useCallback(() => {
const handleClick = useCallback(() => {
setExpanded(expanded => !expanded);
}, []);

const handleKeyDown = useCallback((event: KeyboardEvent<HTMLSpanElement>) => {
if (event.key === 'Enter') {
setExpanded(expanded => !expanded);
}
}, []);

return (
<>
<Hamburger
onClick={handleToggle}
onClick={handleClick}
onKeyDown={handleKeyDown}
className={styles.icon}
data-testid="hamburger-button"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exports[`LandingNav should render routes correctly (snapshot) 1`] = `
data-testid="hamburger-icon"
fill="currentColor"
height="2em"
tabindex="0"
viewBox="0 0 1024 1024"
width="2em"
>
Expand Down

1 comment on commit 6e94c3a

@vercel
Copy link

@vercel vercel bot commented on 6e94c3a May 11, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

blog – ./

blog-sabertaz.vercel.app
blog-git-main-sabertaz.vercel.app
blog.tazimi.dev

Please sign in to comment.