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

Fix: Add tabindex to preview container #858

Merged
merged 6 commits into from
Nov 6, 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
28 changes: 20 additions & 8 deletions src/lib/Fullscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,39 @@ class Fullscreen extends EventEmitter {
* Fires events when the fullscreen state changes
*
* @private
* @param {HTMLElement|Event} [el] - Fullscreen element
* @param {HTMLElement|Event} el - Fullscreen element
* @return {void}
*/
fullscreenchangeHandler = (el) => {
let enter = false;

if (this.isSupported()) {
if (this.isFullscreen()) {
enter = true;
}
} else if (!this.isFullscreen(el)) {
enter = true;
}
enter = (this.isSupported() && this.isFullscreen()) || (!this.isSupported() && !this.isFullscreen(el));
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved

if (enter) {
this.focusFullscreenElement(el);
this.emit('enter');
} else {
this.emit('exit');
}
};

/**
* Focuses the element
*
* @private
* @param {HTMLElement|Event} el - Fullscreen element or event
* @return {void}
*/
focusFullscreenElement = (el) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it is pretty generic, maybe add to util.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is and it isn't, taking either a HTMLElement or Event and extracting out the HTMLElement to focus on seems a bit specific to this logic, maybe if this situation comes up again in another file we can move it then

// Focus on the fullscreen element so keyboard
// events are triggered without an extra click
// If el has target property, then it is an Event
// otherwise it is a HTMLElement
const element = el.target || el;

element.focus();
};

/**
* Toggles fullscreen mode
*
Expand Down
1 change: 1 addition & 0 deletions src/lib/__tests__/Fullscreen-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id='test-container'></div>
45 changes: 41 additions & 4 deletions src/lib/__tests__/Fullscreen-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,47 @@ describe('lib/Fullscreen', () => {
});

describe('fullscreenchangeHandler()', () => {
before(() => {
fixture.setBase('src/lib');
});

beforeEach(() => {
fixture.load('__tests__/Fullscreen-test.html');
});

afterEach(() => {
fixture.cleanup();
});

it('should emit enter if we are entering fullscreen and if true fullscreen is supported', () => {
sandbox.stub(fullscreen, 'isSupported').returns(true);
sandbox.stub(fullscreen, 'isFullscreen').returns(true);
sandbox.stub(fullscreen, 'emit');
sandbox.stub(fullscreen, 'focusFullscreenElement');

fullscreen.fullscreenchangeHandler({});

expect(fullscreen.emit).to.have.been.calledWith('enter');
expect(fullscreen.focusFullscreenElement.called).to.be.true;
});

it('should emit exit if we are exiting fullscreen and if true fullscreen is supported', () => {
sandbox.stub(fullscreen, 'isSupported').returns(true);
sandbox.stub(fullscreen, 'isFullscreen').returns(false);
sandbox.stub(fullscreen, 'emit');
sandbox.stub(fullscreen, 'focusFullscreenElement');

fullscreen.fullscreenchangeHandler({});

expect(fullscreen.emit).to.have.been.calledWith('exit');
expect(fullscreen.focusFullscreenElement.called).to.be.false;
});

it('should emit enter if we are entering fullscreen and if true fullscreen is not supported', () => {
sandbox.stub(fullscreen, 'isSupported').returns(false);
sandbox.stub(fullscreen, 'isFullscreen').returns(false);
sandbox.stub(fullscreen, 'emit');
sandbox.stub(fullscreen, 'focusFullscreenElement');

fullscreen.fullscreenchangeHandler({});

Expand All @@ -80,12 +97,11 @@ describe('lib/Fullscreen', () => {

it('should be called only once when the fullscreenchange event is emitted', () => {
const spy = sandbox.spy(fullscreen, 'fullscreenchangeHandler');
sandbox.stub(fullscreen, 'focusFullscreenElement').returns(true);
// rebind the dom listeners to use the spy
fullscreen.bindDOMListeners();

const event = new Event('webkitfullscreenchange');

window.document.dispatchEvent(event);
const event = new Event('webkitfullscreenchange', { bubbles: true });
document.getElementById('test-container').dispatchEvent(event);
expect(spy).to.be.called.once;
});
});
Expand Down Expand Up @@ -213,4 +229,25 @@ describe('lib/Fullscreen', () => {
expect(fullscreen.fullscreenchangeHandler).to.have.been.calledWith(element);
});
});

describe('focusFullscreenElement()', () => {
it('should focus the element when element passed in', () => {
const element = document.createElement('div');
sandbox.stub(element, 'focus');

fullscreen.focusFullscreenElement(element);

expect(element.focus.called).to.be.true;
});

it('should focus the element when event is passed in', () => {
const element = document.createElement('div');
sandbox.stub(element, 'focus');
const event = { target: element };

fullscreen.focusFullscreenElement(event);

expect(element.focus.called).to.be.true;
});
});
});
2 changes: 1 addition & 1 deletion src/lib/shell.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</div>
</div>
</div>
<div class="bp">
<div class="bp" tabindex="0">
<div class="bp-loading-wrapper">
<div class="bp-loading">
<div class="bp-icon bp-icon-file"></div>
Expand Down