Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
fix: fix arrow navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
filipowm committed Jun 4, 2020
1 parent d8a905c commit 85128da
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/components/nextPrevious/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import Link from '../link';
import styled from '@emotion/styled';
import emoji from 'node-emoji';
Expand Down Expand Up @@ -202,6 +202,21 @@ const calculateNextPrevious = (nav, index) => {
return [previousInfo, nextInfo];
};

const setArrowNavigation = (previous, next) => {
useEffect(() => {
document.onkeydown = (e) => {
e = e || window.event;
if (e.keyCode == '37' && previous.url) {
// left arrow
navigate(previous.url);
} else if (e.keyCode == '39' && next.url) {
// right arrow
navigate(next.url);
}
};
}, [previous, next])
}

const nextPrevious = ({ mdx }) => {
const edges = getNavigationData();
const navigation = calculateFlatNavigation(edges);
Expand All @@ -216,17 +231,9 @@ const nextPrevious = ({ mdx }) => {
const [previous, next] = calculateNextPrevious(navigation, currentIndex);

if (config.features.previousNext.arrowKeyNavigation === true) {
document.onkeydown = (e) => {
e = e || window.event;
if (e.keyCode == '37' && previous.url) {
// left arrow
navigate(previous.url);
} else if (e.keyCode == '39' && next.url) {
// right arrow
navigate(next.url);
}
};
setArrowNavigation(previous, next);
}

return (
<NextPreviousWrapper>
{currentIndex >= 0 ? (
Expand Down

0 comments on commit 85128da

Please sign in to comment.