2020-01-22 05:37:33 +01:00
|
|
|
// @flow
|
|
|
|
import React, { useState, useCallback } from 'react';
|
|
|
|
import * as ICONS from 'constants/icons';
|
|
|
|
import Button from 'component/button';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
isBackward: boolean,
|
|
|
|
history: {
|
|
|
|
entries: { key: string, title: string }[],
|
|
|
|
go: number => void,
|
|
|
|
goBack: () => void,
|
|
|
|
goForward: () => void,
|
|
|
|
index: number,
|
|
|
|
length: number,
|
|
|
|
location: { pathname: string },
|
|
|
|
push: string => void,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-01-24 05:11:06 +01:00
|
|
|
// determines which slice of entries should make up the back or forward button drop-downs (isBackward vs !isBackward respectively)
|
|
|
|
const sliceEntries = (currentIndex, entries, historyLength, isBackward) => {
|
|
|
|
const l = isBackward ? 0 : currentIndex + 1;
|
|
|
|
const r = isBackward ? currentIndex : historyLength;
|
|
|
|
return entries.slice(l, r);
|
|
|
|
};
|
|
|
|
|
2020-01-25 22:04:48 +01:00
|
|
|
const NavigationButton = (props: Props) => {
|
2020-01-22 05:37:33 +01:00
|
|
|
const { isBackward, history } = props;
|
2020-01-24 05:11:06 +01:00
|
|
|
const { entries, go } = history;
|
|
|
|
const currentIndex = history.index;
|
|
|
|
const historyLength = history.length;
|
2020-01-22 05:37:33 +01:00
|
|
|
const [showHistory, setShowHistory] = useState(false);
|
|
|
|
|
2020-01-24 05:11:06 +01:00
|
|
|
// creates an <li> intended for the button's <ul>
|
2020-01-22 05:37:33 +01:00
|
|
|
const makeItem = useCallback(
|
|
|
|
(entry, index) => {
|
2020-01-24 05:11:06 +01:00
|
|
|
// difference between the current index and the index of the entry
|
|
|
|
const goArg = isBackward ? index - currentIndex : index + 1;
|
2020-01-22 05:37:33 +01:00
|
|
|
return (
|
|
|
|
<li
|
|
|
|
key={entry.key}
|
2020-01-24 05:11:06 +01:00
|
|
|
onMouseDown={() => {
|
|
|
|
setShowHistory(false);
|
|
|
|
go(goArg);
|
2020-01-22 05:37:33 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{entry.title}
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
},
|
2020-01-24 05:11:06 +01:00
|
|
|
[currentIndex, isBackward, go]
|
2020-01-22 05:37:33 +01:00
|
|
|
);
|
2020-01-24 05:11:06 +01:00
|
|
|
const slicedEntries = sliceEntries(currentIndex, entries, historyLength, isBackward);
|
2020-01-22 05:37:33 +01:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Button
|
2020-01-23 03:12:40 +01:00
|
|
|
className={`header__navigation-item header__navigation-item--${isBackward ? 'back' : 'forward'}`}
|
|
|
|
description={isBackward ? __('Navigate back') : __('Navigate forward')}
|
2020-01-24 05:11:06 +01:00
|
|
|
onBlur={() => setShowHistory(false)}
|
2020-01-23 03:12:40 +01:00
|
|
|
onClick={() => (isBackward ? history.goBack() : history.goForward())}
|
2020-01-22 05:37:33 +01:00
|
|
|
onContextMenu={e => {
|
|
|
|
setShowHistory(!showHistory);
|
|
|
|
// the following three lines prevent the regular context menu (right click menu) from appearing
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2020-01-24 05:11:06 +01:00
|
|
|
return false;
|
2020-01-22 05:37:33 +01:00
|
|
|
}}
|
|
|
|
icon={isBackward ? ICONS.ARROW_LEFT : ICONS.ARROW_RIGHT}
|
|
|
|
iconSize={18}
|
|
|
|
/>
|
2020-01-24 05:11:06 +01:00
|
|
|
{showHistory && <ul className={'header__navigaiton-dropdown'}>{slicedEntries.map(makeItem)}</ul>}
|
2020-01-22 05:37:33 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2020-01-25 22:04:48 +01:00
|
|
|
export default NavigationButton;
|