Skip to content

Commit c157d8f

Browse files
ChiamakaSaraVieira
authored andcommitted
Navigate between featured sandboxes using left and right arrow keys (codesandbox#1452)
1 parent b4b7143 commit c157d8f

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

.all-contributorsrc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,16 @@
689689
"contributions": [
690690
"code"
691691
]
692-
}
692+
},
693+
{
694+
"login": "chiamaka",
695+
"name": "Chiamaka Nwolisa",
696+
"avatar_url": "https://avatars3.githubusercontent.com/u/2737103?v=4",
697+
"profile": "https://www.chiamaka.xyz/",
698+
"contributions": [
699+
"code"
700+
]
701+
},
693702
],
694703
"repoType": "github"
695704
}

packages/homepage/src/pages/explore.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22

33
import getTemplate from 'common/templates';
44
import { protocolAndHost } from 'common/utils/url-generator';
5+
import { ARROW_LEFT, ARROW_RIGHT } from 'common/utils/keycodes';
56

67
import TitleAndMetaTags from '../components/TitleAndMetaTags';
78
import PageContainer from '../components/PageContainer';
@@ -44,6 +45,21 @@ export default class Explore extends React.PureComponent {
4445
// render and server render are not the same. So we force a rerender.
4546
// eslint-disable-next-line
4647
this.setState({ renderModal: true });
48+
49+
document.addEventListener('keyup', ({ keyCode }) => {
50+
const { featuredSandboxIndex } = this.state;
51+
switch (keyCode) {
52+
case ARROW_LEFT:
53+
if (featuredSandboxIndex === 0) return;
54+
this.navigateToPreviousSandbox();
55+
break;
56+
case ARROW_RIGHT:
57+
if (featuredSandboxIndex === featuredSandboxes.length - 1) return;
58+
this.navigateToNextSandbox();
59+
break;
60+
default:
61+
}
62+
});
4763
}
4864

4965
loadSandboxes = () => {
@@ -99,6 +115,18 @@ export default class Explore extends React.PureComponent {
99115
this.openSandbox(currentIndex + 1);
100116
};
101117

118+
navigateToNextSandbox = () => {
119+
this.setState(state => ({
120+
featuredSandboxIndex: state.featuredSandboxIndex + 1,
121+
}));
122+
};
123+
124+
navigateToPreviousSandbox = () => {
125+
this.setState(state => ({
126+
featuredSandboxIndex: state.featuredSandboxIndex - 1,
127+
}));
128+
};
129+
102130
getCurrentIndex = () =>
103131
this.state.selectedSandbox
104132
? this.state.sandboxes.findIndex(
@@ -156,11 +184,7 @@ export default class Explore extends React.PureComponent {
156184
<Dots>
157185
<StyledLeftArrow
158186
disable={featuredSandboxIndex === 0}
159-
onClick={() =>
160-
this.setState(state => ({
161-
featuredSandboxIndex: state.featuredSandboxIndex - 1,
162-
}))
163-
}
187+
onClick={this.navigateToPreviousSandbox}
164188
/>
165189

166190
{featuredSandboxes.map((sandbox, i) => {
@@ -186,11 +210,7 @@ export default class Explore extends React.PureComponent {
186210
disable={
187211
featuredSandboxIndex === featuredSandboxes.length - 1
188212
}
189-
onClick={() =>
190-
this.setState(state => ({
191-
featuredSandboxIndex: state.featuredSandboxIndex + 1,
192-
}))
193-
}
213+
onClick={this.navigateToNextSandbox}
194214
/>
195215
</Dots>
196216
</Navigation>

0 commit comments

Comments
 (0)