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

Feature/anchor buttons #297

Merged
merged 3 commits into from
Aug 10, 2016
Merged
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
61 changes: 61 additions & 0 deletions javascripts/govuk/anchor-buttons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// javascript 'shim' to trigger the click event of element(s)
// when the space key is pressed.
//
// usage instructions:
// GOVUK.anchorButtons.init();
//
// If you want to customise the shim you can pass in a custom configuration
// object with your own selector for the target elements and addional keyup
// codes if there becomes a need to do so. For example:
// GOVUK.anchorButtons.init({ selector: '[role="button"]' });
(function(global) {
"use strict";

var $ = global.jQuery;
var GOVUK = global.GOVUK || {};

GOVUK.anchorButtons = {

// default configuration that can be overridden by passing object as second parameter to module
config: {
// the target element(s) to attach the shim event to
selector: 'a[role="button"]',
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add this role to other elements such as spans?

https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_button_role

Maybe the selector should be just [role="button"].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure about that one. Do we always want space to trigger a click on something with role="button" ? @LJWatson @aduggin to advise perhaps?

// array of keys to match against upon the keyup event
keycodes: [
32 // spacekey
],
},

// event behaviour (not a typical anonymous function for resuse if needed)
triggerClickOnTarget: function triggerClickOnTarget(event) {
var code = event.charCode || event.keyCode;
// if the keyCode/charCode from this event is in the keycodes array then
if ($.inArray(code, this.config.keycodes) !== -1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

A nit (sorry) but I'm wondering, if we're only ever going to be shimming the spacekey, could we just do a boring

code === 32

here instead of an array comparison, not super important (sorry again :P)

Copy link
Contributor Author

@paulmsmith paulmsmith Jul 27, 2016

Choose a reason for hiding this comment

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

Again fair point. Originally there was talk of triggering on enter/return as well. Left it open to additional key-codes just in case more than anything. Happy to change if we feel that makes it more readable/explicit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Generally for me I prefer to avoid anticipating future updates since more often than not they don't happen but don't think this blocks merging, so up to you. ✌️

event.preventDefault();
// trigger the target's click event
$(event.target).trigger("click");
}
},

// By default this will find all anchors with role attribute set to
// 'button' and will trigger their click event when the spaceky (32) is pressed.
Copy link
Contributor

Choose a reason for hiding this comment

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

mini-nit, typo in comment (spacekey)

// @method init
// @param {Object} customConfig object to override default configuration
// {String} customConfig.selector a selector for the elements to be 'clicked'
// {Array} customConfig.keycodes an array of javascript keycode values to match against that when pressed will trigger the click
init: function init(customConfig) {
// extend the default config with any custom attributes passed in
this.config = $.extend(this.config, customConfig);
// if we have found elements then:
if($(this.config.selector).length > 0) {
// listen to 'document' for keyup event on the elements and fire the triggerClickOnTarget
$(document).on('keyup', this.config.selector, this.triggerClickOnTarget.bind(this));
}
}

};

// hand back to global
global.GOVUK = GOVUK;

})(window);
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing EOF.