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

Meta keys should not match. #184

Merged
merged 2 commits into from
Jun 7, 2019
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
10 changes: 8 additions & 2 deletions src/focus-visible.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,18 @@ function applyFocusVisiblePolyfill(scope) {
}

/**
* Treat `keydown` as a signal that the user is in keyboard modality.
* If the most recent user interaction was via the keyboard;
* and the key press did not include a meta, alt/option, or control key;
* then the modality is keyboard. Otherwise, the modality is not keyboard.
* Apply `focus-visible` to any current active element and keep track
* of our keyboard modality state with `hadKeyboardEvent`.
* @param {Event} e
* @param {KeyboardEvent} e
*/
function onKeyDown(e) {
if (e.metaKey || e.altKey || e.ctrlKey) {
return;
}

if (isValidFocusTarget(scope.activeElement)) {
addFocusVisibleClass(scope.activeElement);
}
Expand Down
56 changes: 56 additions & 0 deletions test/specs/meta-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { fixture, FOCUS_RING_STYLE } = require('./helpers');
const { Key, By } = require('selenium-webdriver');
const expect = require('expect');
const driver = global.__driver;

describe('meta keys', function() {
before(function() {
// Skip these tests in Edge and IE because it's unclear how to get focus
// back to the page when pressing the Windows meta key without clicking
// on the page which would sort of negate the test.
if (
process.env.TEST_BROWSER.includes('Edge') ||
process.env.TEST_BROWSER.includes('Internet Explorer')
) {
this.skip();
}
});

beforeEach(function() {
return fixture('button.html');
});

let el;
beforeEach(async function() {
el = await driver.findElement(By.css('#el'));
});

it('should NOT apply .focus-visible if the ctrl key is pressed', async function() {
await el.click();
// Simulates "ctrl - b".
// Key.NULL tells selenium to clear the pressed state for the modifier key.
await el.sendKeys(Key.CONTROL, 'b', Key.NULL);
let actual = await driver.executeScript(`
return window.getComputedStyle(document.querySelector('#el')).outlineColor
`);
expect(actual).toNotEqual(FOCUS_RING_STYLE);
});

it('should NOT apply .focus-visible if the meta key is pressed', async function() {
await el.click();
await el.sendKeys(Key.META, 'b', Key.NULL);
let actual = await driver.executeScript(`
return window.getComputedStyle(document.querySelector('#el')).outlineColor
`);
expect(actual).toNotEqual(FOCUS_RING_STYLE);
});

it('should NOT apply .focus-visible if the option/alt key is pressed', async function() {
await el.click();
await el.sendKeys(Key.ALT, 'b', Key.NULL);
let actual = await driver.executeScript(`
return window.getComputedStyle(document.querySelector('#el')).outlineColor
`);
expect(actual).toNotEqual(FOCUS_RING_STYLE);
});
});