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

Support links opening in new tab #363

Merged
merged 6 commits into from
Dec 26, 2022
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
67 changes: 51 additions & 16 deletions app/helpers/govuk_link_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require "html_attributes_utils"

module GovukLinkHelper
using HTMLAttributesUtils

LINK_STYLES = {
inverse: "govuk-link--inverse",
muted: "govuk-link--muted",
Expand All @@ -13,6 +17,11 @@ module GovukLinkHelper
warning: "govuk-button--warning",
}.freeze

NEW_TAB_ATTRIBUTES = {
target: "_blank",
rel: "noreferrer noopener"
}.freeze

def govuk_link_classes(*styles, default_class: 'govuk-link')
if (invalid_styles = (styles - LINK_STYLES.keys)) && invalid_styles.any?
fail(ArgumentError, "invalid styles #{invalid_styles.to_sentence}. Valid styles are #{LINK_STYLES.keys.to_sentence}")
Expand All @@ -29,14 +38,14 @@ def govuk_button_classes(*styles, default_class: 'govuk-button')
[default_class] + BUTTON_STYLES.values_at(*styles).compact
end

def govuk_link_to(name = nil, options = nil, extra_options = {}, &block)
def govuk_link_to(name = nil, options = nil, extra_options = {}, new_tab: false, &block)
extra_options = options if block_given?
html_options = build_html_options(extra_options)
html_options = build_html_options(extra_options, new_tab: new_tab)

if block_given?
link_to(name, html_options, &block)
else
link_to(name, options, html_options)
link_to(prepare_link_text(name, new_tab), options, html_options)
end
end

Expand All @@ -62,15 +71,15 @@ def govuk_button_to(name = nil, options = nil, extra_options = {}, &block)
end
end

def govuk_button_link_to(name = nil, options = nil, extra_options = {}, &block)
def govuk_button_link_to(name = nil, options = nil, extra_options = {}, new_tab: false, &block)
extra_options = options if block_given?
html_options = GovukComponent::StartButtonComponent::LINK_ATTRIBUTES
.merge build_html_options(extra_options, style: :button)
.merge build_html_options(extra_options, style: :button, new_tab: new_tab)

if block_given?
link_to(name, html_options, &block)
else
link_to(name, options, html_options)
link_to(prepare_link_text(name, new_tab), options, html_options)
end
end

Expand All @@ -87,18 +96,22 @@ def govuk_breadcrumb_link_to(name = nil, options = nil, extra_options = {}, &blo

private

def build_html_options(provided_options, style: :link)
styles = case style
when :link then LINK_STYLES
when :button then BUTTON_STYLES
else {}
end
def build_html_options(provided_options, style: :link, new_tab: false)
element_styles = { link: LINK_STYLES, button: BUTTON_STYLES }.fetch(style, {})

# we need to take a couple of extra steps here because we don't want the style
# params (inverse, muted, etc) to end up as extra attributes on the link.

remaining_options = provided_options&.slice!(*styles.keys)
remaining_options = new_tab_options(new_tab)
.deep_merge_html_attributes(remove_styles_from_provided_options(element_styles, provided_options))

return {} unless (style_classes = build_style_classes(style, provided_options))
style_classes = build_style_classes(style, extract_styles_from_provided_options(element_styles, provided_options))

inject_class(remaining_options, class_name: style_classes)
combine_attributes(remaining_options, class_name: style_classes)
end

def new_tab_options(new_tab)
new_tab ? NEW_TAB_ATTRIBUTES : {}
end

def build_style_classes(style, provided_options)
Expand All @@ -111,13 +124,35 @@ def build_style_classes(style, provided_options)
end
end

def inject_class(attributes, class_name:)
def combine_attributes(attributes, class_name:)
attributes ||= {}

attributes.with_indifferent_access.tap do |attrs|
attrs[:class] = Array.wrap(attrs[:class]).prepend(class_name).flatten.join(" ")
end
end

def extract_styles_from_provided_options(styles, provided_options)
return {} if provided_options.blank?

provided_options.slice(*styles.keys)
end

def remove_styles_from_provided_options(styles, provided_options)
return {} if provided_options.blank?

provided_options&.except(*styles.keys)
end

def prepare_link_text(text, new_tab)
return text unless new_tab

new_tab_text = new_tab.is_a?(String) ? new_tab : Govuk::Components.config.default_link_new_tab_text

return text if new_tab_text.blank?

%(#{text} #{new_tab_text})
end
end

ActiveSupport.on_load(:action_view) { include GovukLinkHelper }
2 changes: 1 addition & 1 deletion govuk-components.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec|

spec.files = Dir["{app,config,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]

spec.add_dependency("html-attributes-utils", "~> 0.9", ">= 0.9.2")
spec.add_dependency("html-attributes-utils", "~> 1.0.0", ">= 1.0.0")
spec.add_dependency("pagy", "~> 6.0")
spec.add_dependency "view_component", "~> 2.74.1"

Expand Down
16 changes: 8 additions & 8 deletions guide/lib/examples/link_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ def govuk_link_to_normal
end

def govuk_link_to_inverse
%(= govuk_link_to 'An inverse hyperlink', '#', inverse: true)
%(= govuk_link_to('An inverse hyperlink', '#', { inverse: true }))
end

def govuk_link_to_muted
%(= govuk_link_to 'A muted hyperlink', '#', muted: true)
%(= govuk_link_to('A muted hyperlink', '#', { muted: true }))
end

def govuk_link_other_styles
<<~LINKS
p
= govuk_link_to 'A hyperlink without an underline', '#', no_underline: true
= govuk_link_to('A hyperlink without an underline', '#', { no_underline: true })

p
= govuk_link_to 'A hyperlink without a visited state', '#', no_visited_state: true
= govuk_link_to('A hyperlink without a visited state', '#', { no_visited_state: true })

p
= govuk_link_to 'A text-coloured hyperlink', '#', text_colour: true
= govuk_link_to('A text-coloured hyperlink', '#', { text_colour: true })
LINKS
end

Expand All @@ -36,9 +36,9 @@ def govuk_button_link_to_normal
def govuk_button_other_styles
<<~BUTTONS
.govuk-button-group
= govuk_button_link_to 'A disabled button', '#', disabled: true
= govuk_button_link_to 'A secondary button', '#', secondary: true
= govuk_button_link_to 'A warning button', '#', warning: true
= govuk_button_link_to('A disabled button', '#', { disabled: true })
= govuk_button_link_to('A secondary button', '#', { secondary: true })
= govuk_button_link_to('A warning button', '#', { warning: true })
BUTTONS
end

Expand Down
2 changes: 2 additions & 0 deletions lib/govuk/components/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def reset!
default_warning_text_icon_fallback_text: "Warning",
default_warning_text_icon: "!",

default_link_new_tab_text: "(opens in new tab)",

require_summary_list_action_visually_hidden_text: false,
}.freeze

Expand Down
Loading