diff --git a/src/scripts/components/story-header/story-header.tsx b/src/scripts/components/story-header/story-header.tsx index 5276639d9..8992aa4f6 100644 --- a/src/scripts/components/story-header/story-header.tsx +++ b/src/scripts/components/story-header/story-header.tsx @@ -1,6 +1,6 @@ -import React, {FunctionComponent} from 'react'; +import React, {FunctionComponent, useEffect} from 'react'; import {FormattedMessage} from 'react-intl'; -import {Link} from 'react-router-dom'; +import {Link, useHistory} from 'react-router-dom'; import cx from 'classnames'; import {StoryListItem} from '../../types/story-list'; @@ -15,6 +15,7 @@ interface Props { } const StoryHeader: FunctionComponent = ({storyIds, story, mode}) => { + const history = useHistory(); const isPresenterMode = mode === StoryMode.Present; const isShowcaseMode = mode === StoryMode.Showcase; @@ -26,6 +27,21 @@ const StoryHeader: FunctionComponent = ({storyIds, story, mode}) => { const backLink = storyIds ? `/showcase/${storyIds}` : `/${mode}`; + const onKeyDownHandler = (event: KeyboardEvent) => { + if (event.keyCode === 27) { + history.push(backLink); + } + }; + + // add and remove event listener for keyboard events + useEffect(() => { + window.addEventListener('keydown', onKeyDownHandler); + return () => { + window.removeEventListener('keydown', onKeyDownHandler); + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + return (
{!isPresenterMode && ( diff --git a/src/scripts/components/story-pagination/story-pagination.tsx b/src/scripts/components/story-pagination/story-pagination.tsx index c05ba0446..464505b9e 100644 --- a/src/scripts/components/story-pagination/story-pagination.tsx +++ b/src/scripts/components/story-pagination/story-pagination.tsx @@ -1,5 +1,5 @@ -import React, {FunctionComponent} from 'react'; -import {Link} from 'react-router-dom'; +import React, {FunctionComponent, useEffect, useCallback} from 'react'; +import {Link, useHistory} from 'react-router-dom'; import {useIntl} from 'react-intl'; import cx from 'classnames'; @@ -26,14 +26,38 @@ const StoryPagination: FunctionComponent = ({ previousSlideLink, nextSlideLink }) => { + const history = useHistory(); const intl = useIntl(); const isPresenterMode = mode === StoryMode.Present; + const isShowcaseMode = mode === StoryMode.Showcase; const classes = cx(styles.pagination, isPresenterMode && styles.present); + const onKeyDownHandler = useCallback( + event => { + if (!isShowcaseMode) { + if (event.keyCode === 37) { + previousSlideLink && history.push(previousSlideLink); + } + if (event.keyCode === 39) { + nextSlideLink && history.push(nextSlideLink); + } + } + }, + [isShowcaseMode, history, previousSlideLink, nextSlideLink] + ); + + // add and remove event listener for keyboard events + useEffect(() => { + window.addEventListener('keydown', onKeyDownHandler); + return () => { + window.removeEventListener('keydown', onKeyDownHandler); + }; + }, [onKeyDownHandler]); + return (
- {previousSlideLink ? ( + {previousSlideLink && !isShowcaseMode ? ( @@ -45,7 +69,7 @@ const StoryPagination: FunctionComponent = ({ {slideIndex + 1}/{numberOfSlides} - {nextSlideLink ? ( + {nextSlideLink && !isShowcaseMode ? (