@springernature/global-expander
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -21,2 +21,5 @@ import {Expander} from '../js/expander'; | ||
break; | ||
case 'Space': | ||
event.key = 'Space'; | ||
break; | ||
case 'Escape': | ||
@@ -29,3 +32,3 @@ event.key = 'Escape'; | ||
default: | ||
throw new Error(`${key} was should be 'Enter' or 'Escape'`); | ||
throw new Error('key should be "Enter", "Space, "Escape" or "Tab"'); | ||
} | ||
@@ -68,2 +71,9 @@ | ||
const pressSpaceKeyTwice = () => { | ||
for (let i = 0; i < 2; i++) { | ||
const keydownEnterEvent = createKeydownEvent('Space'); | ||
element.BUTTON.dispatchEvent(keydownEnterEvent); | ||
} | ||
}; | ||
test('Should open when button is clicked', () => { | ||
@@ -92,2 +102,14 @@ // Given | ||
test('Should open when space key is pressed', () => { | ||
// Given | ||
const expander = new Expander(element.BUTTON, element.TARGET); | ||
expander.init(); | ||
// When | ||
const keydownEnterEvent = createKeydownEvent('Space'); | ||
element.BUTTON.dispatchEvent(keydownEnterEvent); | ||
// Then | ||
expect(element.BUTTON.classList.contains(className.OPEN)).toBe(true); | ||
expect(element.TARGET.classList.contains(className.HIDE)).toBe(false); | ||
}); | ||
test('Should close when button is clicked a second time', () => { | ||
@@ -115,2 +137,13 @@ // Given | ||
test('Should close when space key is pressed a second time', () => { | ||
// Given | ||
const expander = new Expander(element.BUTTON, element.TARGET); | ||
expander.init(); | ||
// When | ||
pressSpaceKeyTwice(); | ||
// Then | ||
expect(element.BUTTON.classList.contains(className.OPEN)).toBe(false); | ||
expect(element.TARGET.classList.contains(className.HIDE)).toBe(true); | ||
}); | ||
test('Should set aria attributes when button is clicked', () => { | ||
@@ -169,2 +202,15 @@ // Given | ||
test('Should unset aria attributes when space key is pressed a second time', () => { | ||
// Given | ||
const expander = new Expander(element.BUTTON, element.TARGET); | ||
expander.init(); | ||
element.BUTTON.setAttribute('aria-expanded', 'false'); | ||
element.TARGET.setAttribute('aria-hidden', 'true'); | ||
// When | ||
pressSpaceKeyTwice(); | ||
// Then | ||
expect(element.BUTTON.getAttribute('aria-expanded')).toBe('false'); | ||
expect(element.TARGET.getAttribute('aria-hidden')).toBe('true'); | ||
}); | ||
test('Should make target element focusable and focus on it when button is clicked', () => { | ||
@@ -171,0 +217,0 @@ // Given |
# History | ||
## 0.3.2 (2020-03-06) | ||
* Bug Fix: Adds keyboard button interaction with 'Space' keypress | ||
## 0.3.1 (2020-03-06) | ||
@@ -4,0 +7,0 @@ * Bug Fix: this._tabbableItems is null |
@@ -28,3 +28,3 @@ import {makeArray} from '@springernature/global-context/js/helpers'; | ||
this._handleButtonClick = this._handleButtonClick.bind(this); | ||
this._handleButtonEnter = this._handleButtonEnter.bind(this); | ||
this._handleButtonKeydown = this._handleButtonKeydown.bind(this); | ||
this._handleDocumentClick = this._handleDocumentClick.bind(this); | ||
@@ -48,4 +48,4 @@ this._handleDocumentKeydown = this._handleDocumentKeydown.bind(this); | ||
_handleButtonEnter(event) { | ||
if (event.key === 'Enter') { | ||
_handleButtonKeydown(event) { | ||
if (event.key === 'Enter' || event.key === 'Space') { | ||
event.preventDefault(); | ||
@@ -189,3 +189,3 @@ | ||
this._triggerEl.addEventListener('click', this._handleButtonClick); | ||
this._triggerEl.addEventListener('keydown', this._handleButtonEnter); | ||
this._triggerEl.addEventListener('keydown', this._handleButtonKeydown); | ||
} | ||
@@ -192,0 +192,0 @@ }; |
{ | ||
"name": "@springernature/global-expander", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Frontend package for expanding a target when clicking a toggle", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23593
619