Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Add left/right navigation for preview #217

Merged
merged 1 commit into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/components/ContentExplorer/ContentExplorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,6 @@ class ContentExplorer extends Component<Props, State> {
* Keyboard events
*
* @private
* @inheritdoc
* @return {void}
*/
onKeyDown = (event: SyntheticKeyboardEvent<HTMLElement>) => {
Expand Down
65 changes: 62 additions & 3 deletions src/components/ContentPreview/ContentPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import makeResponsive from '../makeResponsive';
import Internationalize from '../Internationalize';
import { isValidBoxFile } from '../../util/fields';
import TokenService from '../../util/TokenService';
import { isInputElement, focus } from '../../util/dom';
import {
DEFAULT_HOSTNAME_API,
DEFAULT_HOSTNAME_APP,
Expand Down Expand Up @@ -77,6 +78,7 @@ class ContentPreview extends PureComponent<Props, State> {
api: API;
previewContainer: ?HTMLDivElement;
mouseMoveTimeoutID: TimeoutID;
rootElement: HTMLElement;

static defaultProps = {
className: '',
Expand Down Expand Up @@ -182,6 +184,8 @@ class ContentPreview extends PureComponent<Props, State> {
this.loadStylesheet();
this.loadScript();
this.fetchFile(fileId);
this.rootElement = ((document.getElementById(this.id): any): HTMLElement);
focus(this.rootElement);
}

/**
Expand Down Expand Up @@ -383,6 +387,7 @@ class ContentPreview extends PureComponent<Props, State> {
container: `#${this.id} .bcpr-content`,
header: 'none',
skipServerUpdate: true,
useHotkeys: false,
...rest
});
};
Expand Down Expand Up @@ -580,7 +585,7 @@ class ContentPreview extends PureComponent<Props, State> {
*
* @return {void}
*/
mouseMoveHandler = throttle(
onMouseMove = throttle(
() => {
const viewer = this.getPreviewer();
const isPreviewing = !!viewer;
Expand Down Expand Up @@ -613,6 +618,50 @@ class ContentPreview extends PureComponent<Props, State> {
true
);

/**
* Keyboard events
*
* @private
* @return {void}
*/
onKeyDown = (event: SyntheticKeyboardEvent<HTMLElement>) => {
if (isInputElement(event.target)) {
return;
}

let consumed = false;
const key = event.key.toLowerCase();
const viewer = this.getPreviewer();

if (!key || !viewer) {
return;
}

if (typeof viewer.onKeydown === 'function') {
consumed = !!viewer.onKeydown(key);
}

if (!consumed) {
switch (key) {
case 'arrowleft':
this.navigateLeft();
consumed = true;
break;
case 'arrowright':
this.navigateRight();
consumed = true;
break;
default:
// no-op
}
}

if (consumed) {
event.preventDefault();
event.stopPropagation();
}
};

/**
* Holds the reference the preview container
*
Expand Down Expand Up @@ -666,9 +715,17 @@ class ContentPreview extends PureComponent<Props, State> {
onSidebarToggle = null;
}

/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */
return (
<Internationalize language={language} messages={messages}>
<div id={this.id} className={`be bcpr ${className}`} ref={measureRef}>
<div
id={this.id}
className={`be bcpr ${className}`}
ref={measureRef}
onKeyDown={this.onKeyDown}
tabIndex={0}
>
{hasHeader && (
<Header
file={file}
Expand All @@ -681,7 +738,7 @@ class ContentPreview extends PureComponent<Props, State> {
/>
)}
<div className='bcpr-body'>
<div className='bcpr-container' onMouseMove={this.mouseMoveHandler} ref={this.containerRef}>
<div className='bcpr-container' onMouseMove={this.onMouseMove} ref={this.containerRef}>
<Measure bounds onResize={this.onResize}>
{({ measureRef: previewRef }) => <div ref={previewRef} className='bcpr-content' />}
</Measure>
Expand Down Expand Up @@ -732,6 +789,8 @@ class ContentPreview extends PureComponent<Props, State> {
</div>
</Internationalize>
);
/* eslint-enable jsx-a11y/no-static-element-interactions */
/* eslint-enable jsx-a11y/no-noninteractive-tabindex */
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/util/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ export function isInputElement(element: HTMLElement | EventTarget | null): boole
return false;
}
const tag = element.tagName.toLowerCase();
return tag === 'input' || tag === 'select' || tag === 'textarea';
return (
tag === 'input' ||
tag === 'select' ||
tag === 'textarea' ||
(tag === 'div' && !!element.getAttribute('contenteditable'))
);
}

/**
Expand Down Expand Up @@ -54,8 +59,12 @@ export function isFocusableElement(element: HTMLElement | EventTarget | null): b
* @param {boolean|void} [focusRoot] - if root should be focused
* @return {void}
*/
export function focus(root: HTMLElement, selector: string, focusRoot: boolean = true): void {
if (!root || !selector) {
export function focus(root: HTMLElement, selector?: string, focusRoot: boolean = true): void {
if (!root) {
return;
}
if (!selector) {
root.focus();
return;
}
const element = root.querySelector(selector);
Expand Down
2 changes: 1 addition & 1 deletion test/preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
break;
}
});
explorer.show('0', 'gpQ9ccdjWqE1rJnE0SaBQWVP1V4p9q83', {
explorer.show('0', '4LNSzxzlBOa1v41vLHt57kjIuaZWnPYZ', {
currentFolderId: '39695871110',
hasPreviewSidebar: true,
sharedLink: 'https://app.box.com/s/sdfsdf'
Expand Down