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 link buttons #310

Merged
merged 6 commits into from
Aug 19, 2016
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
33 changes: 33 additions & 0 deletions docs/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,36 @@ GOVUK.selectionButtons = function (elms, opts) {
```

This method will mean the `destroy` method is not available to call.

## Shim links with button role

Links styled to look like buttons lack button behaviour. This script will allow them to be triggered with a space key after they’ve been focused, to match standard buttons.

### Usage

By default, this behaviour will only be applied to links with a role of button.

```html
<a class="button" role="button">A button</a>
```

```javascript
GOVUK.shimLinksWithButtonRole.init();
```

If you need to override the elements this is applied to then you can do that by passing in a custom selector to the initialiser:

```javascript
GOVUK.shimLinksWithButtonRole.init({
selector: '.my-class'
});
```

It’s also possible to define more or different keycodes to activate against:

```javascript
// activate when the user presses space or ‘r’
GOVUK.shimLinksWithButtonRole.init({
keycodes: [32, 114]
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
// when the space key is pressed.
//
// usage instructions:
// GOVUK.anchorButtons.init();
// GOVUK.shimLinksWithButtonRole.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"]' });
// GOVUK.shimLinksWithButtonRole.init({ selector: '[role="button"]' });
(function(global) {
"use strict";

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

GOVUK.anchorButtons = {
GOVUK.shimLinksWithButtonRole = {

// default configuration that can be overridden by passing object as second parameter to module
config: {
Expand All @@ -28,16 +28,15 @@

// 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) {
// if the code from this event is in the keycodes array then
if ($.inArray(event.which, this.config.keycodes) !== -1) {
event.preventDefault();
// trigger the target's click event
$(event.target).trigger("click");
event.target.click();
}
},

// By default this will find all anchors with role attribute set to
// By default this will find all links with role attribute set to
// 'button' and will trigger their click event when the space key (32) is pressed.
// @method init
// @param {Object} customConfig object to override default configuration
Expand Down
2 changes: 2 additions & 0 deletions spec/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var manifest = {
'../../javascripts/govuk/modules/auto-track-event.js',
'../../javascripts/govuk/multivariate-test.js',
'../../javascripts/govuk/primary-links.js',
'../../javascripts/govuk/shim-links-with-button-role.js',
'../../javascripts/govuk/stick-at-top-when-scrolling.js',
'../../javascripts/govuk/stop-scrolling-at-footer.js',
'../../javascripts/govuk/selection-buttons.js',
Expand All @@ -21,6 +22,7 @@ var manifest = {
'../unit/Modules/auto-track-event.spec.js',
'../unit/multivariate-test.spec.js',
'../unit/primary-links.spec.js',
'../unit/shim-links-with-button-role.spec.js',
'../unit/stick-at-top-when-scrolling.spec.js',
'../unit/selection-button.spec.js',
'../unit/analytics/google-analytics-universal-tracker.spec.js',
Expand Down
8 changes: 4 additions & 4 deletions spec/support/LocalTestRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
<head>
<title>Jasmine Test Runner</title>

<link rel="stylesheet" type="text/css" href="../../node_modules/grunt-contrib-jasmine/vendor/jasmine-2.0.1/jasmine.css">
<link rel="stylesheet" type="text/css" href="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
<style>
#wrapper { display: none; }
</style>

<!-- JASMINE FILES -->
<script type="text/javascript" src="../../node_modules/grunt-contrib-jasmine/vendor/jasmine-2.0.1/jasmine.js"></script>
<script type="text/javascript" src="../../node_modules/grunt-contrib-jasmine/vendor/jasmine-2.0.1/jasmine-html.js"></script>
<script type="text/javascript" src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
<script type="text/javascript" src="../../node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>

<script type="text/javascript" src="../../node_modules/grunt-contrib-jasmine/vendor/jasmine-2.0.1/boot.js"></script>
<script type="text/javascript" src="../../node_modules/jasmine-core/lib/jasmine-core/boot.js"></script>
<script type="text/javascript" src="./load.js"></script>
</head>
<body>
Expand Down
36 changes: 36 additions & 0 deletions spec/unit/shim-links-with-button-role.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
describe("shim-links-with-button-role", function () {
var $body;
var $buttonLink;
var keyupEvent;

beforeEach(function () {
$buttonLink = $('<a role="button">Button</a>');
$buttonLink.on('click',function(){
$buttonLink.addClass('clicked');
});
$(document.body).append($buttonLink);
keyupEvent = $.Event('keyup');
keyupEvent.target = $buttonLink.get(0);
GOVUK.shimLinksWithButtonRole.init();
});

afterEach(function () {
$buttonLink.remove();
$(document).off('keyup');
});

it('should trigger event on space', function(){
// Ideally we’d test the page loading functionality but that seems hard to
// do within a Jasmine context. Settle for checking a bound event trigger.
keyupEvent.which = 32; // Space character
$(document).trigger(keyupEvent);
expect($buttonLink.hasClass('clicked')).toBe(true);
});

it('should not trigger event on tab', function(){
keyupEvent.which = 9; // Tab character
$(document).trigger(keyupEvent);
expect($buttonLink.hasClass('clicked')).toBe(false);
});

});