Skip to content

Instantly share code, notes, and snippets.

@backflip
Last active April 18, 2017 17:17
Show Gist options
  • Save backflip/2898ca35264d2b6226e6cdf3777071bf to your computer and use it in GitHub Desktop.
Save backflip/2898ca35264d2b6226e6cdf3777071bf to your computer and use it in GitHub Desktop.
Keyboard navigation for Sketch Measure
(function() {
const aside = document.querySelector('aside');
function getCurrent() {
return document.querySelector('.artboard.active') || document.querySelector('.artboard');
}
function showSidebar() {
aside.classList.add('on');
}
function hideSidebar() {
aside.classList.remove('on');
}
document.addEventListener('keydown', function(event) {
let target;
switch (event.keyCode) {
/* Down: Go to next pattern */
case 40:
event.preventDefault();
target = getCurrent().nextSibling;
break;
/* Up: Go to previous pattern */
case 38:
event.preventDefault();
target = getCurrent().previousSibling;
break;
/* Right: Show sidebar */
case 39:
event.preventDefault();
showSidebar();
break;
/* Left: Hide sidebar */
case 37:
event.preventDefault();
hideSidebar();
break;
/* Space: Show currently active item in sidebar */
case 32:
event.preventDefault();
target = getCurrent();
break;
}
if (target) {
showSidebar('on');
target.scrollIntoView();
target.click();
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment