-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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 keyboard (Space or Enter) operation of buttons. Fixes #2888. Supercedes #2889 #3032
Closed
OwenEdwards
wants to merge
2
commits into
videojs:master
from
OwenEdwards:fix/split-clickables-from-buttons
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/** | ||
* @file button.js | ||
*/ | ||
import Component from './component'; | ||
import * as Dom from './utils/dom.js'; | ||
import * as Events from './utils/events.js'; | ||
import * as Fn from './utils/fn.js'; | ||
import log from './utils/log.js'; | ||
import document from 'global/document'; | ||
import assign from 'object.assign'; | ||
|
||
/** | ||
* Clickable Component which is clickable or keyboard actionable, but is not a native HTML button | ||
* | ||
* @param {Object} player Main Player | ||
* @param {Object=} options Object of option names and values | ||
* @extends Component | ||
* @class ClickableComponent | ||
*/ | ||
class ClickableComponent extends Component { | ||
|
||
constructor(player, options) { | ||
super(player, options); | ||
|
||
this.emitTapEvents(); | ||
|
||
this.on('tap', this.handleClick); | ||
this.on('click', this.handleClick); | ||
this.on('focus', this.handleFocus); | ||
this.on('blur', this.handleBlur); | ||
} | ||
|
||
/** | ||
* Create the component's DOM element | ||
* | ||
* @param {String=} type Element's node type. e.g. 'div' | ||
* @param {Object=} props An object of properties that should be set on the element | ||
* @param {Object=} attributes An object of attributes that should be set on the element | ||
* @return {Element} | ||
* @method createEl | ||
*/ | ||
createEl(tag='div', props={}, attributes={}) { | ||
props = assign({ | ||
className: this.buildCSSClass(), | ||
tabIndex: 0 | ||
}, props); | ||
|
||
if (tag === 'button') { | ||
log.error(`Creating a ClickableComponent with an HTML element of ${tag} is not supported; use a Button instead.`); | ||
} | ||
|
||
// Add ARIA attributes for clickable element which is not a native HTML button | ||
attributes = assign({ | ||
role: 'button', | ||
'aria-live': 'polite' // let the screen reader user know that the text of the element may change | ||
}, attributes); | ||
|
||
let el = super.createEl(tag, props, attributes); | ||
|
||
this.createControlTextEl(el); | ||
|
||
return el; | ||
} | ||
|
||
/** | ||
* create control text | ||
* | ||
* @param {Element} el Parent element for the control text | ||
* @return {Element} | ||
* @method controlText | ||
*/ | ||
createControlTextEl(el) { | ||
this.controlTextEl_ = Dom.createEl('span', { | ||
className: 'vjs-control-text' | ||
}); | ||
|
||
if (el) { | ||
el.appendChild(this.controlTextEl_); | ||
} | ||
|
||
this.controlText(this.controlText_); | ||
|
||
return this.controlTextEl_; | ||
} | ||
|
||
/** | ||
* Controls text - both request and localize | ||
* | ||
* @param {String} text Text for element | ||
* @return {String} | ||
* @method controlText | ||
*/ | ||
controlText(text) { | ||
if (!text) return this.controlText_ || 'Need Text'; | ||
|
||
this.controlText_ = text; | ||
this.controlTextEl_.innerHTML = this.localize(this.controlText_); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Allows sub components to stack CSS class names | ||
* | ||
* @return {String} | ||
* @method buildCSSClass | ||
*/ | ||
buildCSSClass() { | ||
return `vjs-control vjs-button ${super.buildCSSClass()}`; | ||
} | ||
|
||
/** | ||
* Adds a child component inside this clickable-component | ||
* | ||
* @param {String|Component} child The class name or instance of a child to add | ||
* @param {Object=} options Options, including options to be passed to children of the child. | ||
* @return {Component} The child component (created by this process if a string was used) | ||
* @method addChild | ||
*/ | ||
addChild(child, options={}) { | ||
// TODO: Fix adding an actionable child to a ClickableComponent; currently | ||
// it will cause issues with assistive technology (e.g. screen readers) | ||
// which support ARIA, since an element with role="button" cannot have | ||
// actionable child elements. | ||
|
||
//let className = this.constructor.name; | ||
//log.warn(`Adding a child to a ClickableComponent (${className}) can cause issues with assistive technology which supports ARIA, since an element with role="button" cannot have actionable child elements.`); | ||
|
||
return super.addChild(child, options); | ||
} | ||
|
||
/** | ||
* Handle Click - Override with specific functionality for component | ||
* | ||
* @method handleClick | ||
*/ | ||
handleClick() {} | ||
|
||
/** | ||
* Handle Focus - Add keyboard functionality to element | ||
* | ||
* @method handleFocus | ||
*/ | ||
handleFocus() { | ||
Events.on(document, 'keydown', Fn.bind(this, this.handleKeyPress)); | ||
} | ||
|
||
/** | ||
* Handle KeyPress (document level) - Trigger click when Space or Enter key is pressed | ||
* | ||
* @method handleKeyPress | ||
*/ | ||
handleKeyPress(event) { | ||
// Support Space (32) or Enter (13) key operation to fire a click event | ||
if (event.which === 32 || event.which === 13) { | ||
event.preventDefault(); | ||
this.handleClick(event); | ||
} else if (super.handleKeyPress) { | ||
super.handleKeyPress(event); // Pass keypress handling up for unsupported keys | ||
} | ||
} | ||
|
||
/** | ||
* Handle Blur - Remove keyboard triggers | ||
* | ||
* @method handleBlur | ||
*/ | ||
handleBlur() { | ||
Events.off(document, 'keydown', Fn.bind(this, this.handleKeyPress)); | ||
} | ||
} | ||
|
||
Component.registerComponent('ClickableComponent', ClickableComponent); | ||
export default ClickableComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍