-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This component adds a visual link that includes a print icon and can be used to help users print a page. This pattern was orginally implemented in the Government Frontend application, however we'd like to re-use this component in Smart Answers. The component adds either a button or link depending on whether the application support printing via navigation (used to add support for environments where JavaScript is disabled e.g. /print). The default for this component is to use a button and rely on JavaScript to call the DOM API. This allows the component to be added to an application without having to add extra routes to trigger printing. However for applications that have routes for printing (e.g. Government Frontend), a `href` can be specified and the component will render as anchor tag.
- Loading branch information
1 parent
77ca4fb
commit 499c953
Showing
9 changed files
with
177 additions
and
0 deletions.
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
14 changes: 14 additions & 0 deletions
14
app/assets/javascripts/govuk_publishing_components/components/print-link.js
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,14 @@ | ||
window.GOVUK = window.GOVUK || {} | ||
window.GOVUK.Modules = window.GOVUK.Modules || {}; | ||
|
||
(function (Modules) { | ||
'use strict' | ||
|
||
Modules.PrintLink = function () { | ||
this.start = function (element) { | ||
element[0].addEventListener('click', function () { | ||
window.print() | ||
}) | ||
} | ||
} | ||
})(window.GOVUK.Modules) |
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
52 changes: 52 additions & 0 deletions
52
app/assets/stylesheets/govuk_publishing_components/components/_print-link.scss
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,52 @@ | ||
.gem-c-print-link { | ||
display: none; | ||
margin-bottom: 2em; | ||
margin-top: 2em; | ||
} | ||
|
||
.gem-c-print-link.gem-c-print-link--show-without-js { | ||
display: block; | ||
} | ||
|
||
.js-enabled { | ||
.gem-c-print-link { | ||
display: block; | ||
} | ||
} | ||
|
||
.gem-c-print-link__link { | ||
background: image-url("govuk_publishing_components/icon-print.png") no-repeat 10px 50%; | ||
|
||
margin-left: -10px; | ||
padding: .5em .5em .5em 38px; | ||
|
||
@include govuk-device-pixel-ratio($ratio: 2) { | ||
background-image: image-url("govuk_publishing_components/icon-print-2x.png"); | ||
background-size: 16px 18px; | ||
} | ||
|
||
&:focus { | ||
@include govuk-focused-text; | ||
} | ||
} | ||
|
||
.gem-c-print-link__button { | ||
@extend %govuk-body-s; | ||
background: image-url("govuk_publishing_components/icon-print.png") no-repeat 10px 50%; | ||
border: 0; | ||
color: $govuk-link-colour; | ||
cursor: pointer; | ||
margin: 0; | ||
margin-left: -10px; | ||
padding: .5em .5em .5em 38px; | ||
text-decoration: underline; | ||
|
||
@include govuk-device-pixel-ratio($ratio: 2) { | ||
background-image: image-url("govuk_publishing_components/icon-print-2x.png"); | ||
background-size: 16px 18px; | ||
} | ||
|
||
&:focus { | ||
@include govuk-focused-text; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/views/govuk_publishing_components/components/_print_link.html.erb
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,27 @@ | ||
<% | ||
text ||= t('components.print_link.text') | ||
href ||= nil | ||
data_attributes ||= {} | ||
|
||
require_js ||= href.nil? | ||
data_attributes[:module] = 'print-link' if require_js | ||
%> | ||
|
||
<% if require_js %> | ||
<div class="gem-c-print-link" > | ||
<%= content_tag(:button, text, { | ||
class: %w(gem-c-print-link__button govuk-link), | ||
data: data_attributes | ||
}) %> | ||
</div> | ||
<% else %> | ||
<div class="gem-c-print-link gem-c-print-link--show-without-js"> | ||
<%= link_to( | ||
text, | ||
href, | ||
class: %w(gem-c-print-link__link govuk-link), | ||
rel: "alternate", | ||
data: data_attributes | ||
) %> | ||
</div> | ||
<% end %> |
24 changes: 24 additions & 0 deletions
24
app/views/govuk_publishing_components/components/docs/print_link.yml
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,24 @@ | ||
name: Print link | ||
description: A link with a print icon to help users print the current page | ||
body: | | ||
This component renders two different outputs depending on whether a `href` is specified. By default, when no `href` is given, the component renders as a button and triggers a print action via JavaScript. In this case the component is hidden in environments where JavaScript is disabled and can be used as a progressive enhancement. When a `href` is specified the component renders as an anchor tag and navigates to that `href` without JavaScript, suitable for applications which have paths that trigger a print event on load. | ||
accessibility_criteria: | | ||
- The print icon must be presentational and ignored by screen readers. | ||
shared_accessibility_criteria: | ||
- link | ||
examples: | ||
default: | ||
description: This component calls `print()` via the browser's DOM API. By default it relies on JavaScript and is not shown in environments where JavaScript is disabled. The \"link\" here is actually a button tag because this is not a navigation event. | ||
with_different_text: | ||
data: | ||
text: "Print this manual" | ||
with_different_href: | ||
description: You can specify a alternative `href` URL that will override the default behaviour. When a `href` is specified the print link will render as an anchor tag and be displayed even when JavaScript is disabled. | ||
data: | ||
href: "/print" | ||
with_data_attributes: | ||
data: | ||
data_attributes: | ||
track-category: "printButton" | ||
track-action: "clicked" | ||
track-label: "Print this page" |
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,41 @@ | ||
require "rails_helper" | ||
|
||
describe "Print link", type: :view do | ||
def component_name | ||
"print_link" | ||
end | ||
|
||
it "renders with default text no data is given" do | ||
render_component({}) | ||
|
||
assert_select ".gem-c-print-link" | ||
assert_select( | ||
"button.gem-c-print-link__button", | ||
text: "Print this page", | ||
) | ||
end | ||
|
||
it "renders with alternative text when given" do | ||
render_component({ | ||
text: "Print this manual", | ||
}) | ||
|
||
assert_select ".gem-c-print-link" | ||
assert_select( | ||
"button.gem-c-print-link__button", | ||
text: "Print this manual", | ||
) | ||
end | ||
|
||
it "renders with alternative href as an anchor link" do | ||
render_component({ | ||
href: "/print", | ||
}) | ||
|
||
assert_select ".gem-c-print-link" | ||
assert_select( | ||
'a.gem-c-print-link__link[href="/print"]', | ||
text: "Print this page", | ||
) | ||
end | ||
end |
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,15 @@ | ||
/* eslint-env jasmine, jquery */ | ||
/* global GOVUK */ | ||
|
||
describe('Print link', function () { | ||
it('calls the DOM print API when clicked', function () { | ||
spyOn(window, 'print') | ||
var element = $('<button class="gem-c-print-link__button govuk-link">Print this page</button>') | ||
|
||
new GOVUK.Modules.PrintLink().start(element) | ||
|
||
element.trigger('click') | ||
|
||
expect(window.print).toHaveBeenCalled() | ||
}) | ||
}) |