ccs-frontend_helpers 0.1.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/.rubocop.yml +127 -0
  4. data/CHANGELOG.md +44 -0
  5. data/Gemfile +10 -0
  6. data/Gemfile.lock +241 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +102 -0
  9. data/Rakefile +10 -0
  10. data/ccs-frontend_helpers.gemspec +47 -0
  11. data/lib/ccs/frontend_helpers/ccs_frontend/dashboard_panels.rb +79 -0
  12. data/lib/ccs/frontend_helpers/ccs_frontend/footer.rb +141 -0
  13. data/lib/ccs/frontend_helpers/ccs_frontend/header.rb +205 -0
  14. data/lib/ccs/frontend_helpers/ccs_frontend/logo.rb +49 -0
  15. data/lib/ccs/frontend_helpers/ccs_frontend.rb +20 -0
  16. data/lib/ccs/frontend_helpers/govuk_frontend/accordion.rb +115 -0
  17. data/lib/ccs/frontend_helpers/govuk_frontend/back_link.rb +39 -0
  18. data/lib/ccs/frontend_helpers/govuk_frontend/breadcrumbs.rb +76 -0
  19. data/lib/ccs/frontend_helpers/govuk_frontend/button.rb +127 -0
  20. data/lib/ccs/frontend_helpers/govuk_frontend/cookie_banner.rb +136 -0
  21. data/lib/ccs/frontend_helpers/govuk_frontend/details.rb +46 -0
  22. data/lib/ccs/frontend_helpers/govuk_frontend/error_message.rb +67 -0
  23. data/lib/ccs/frontend_helpers/govuk_frontend/error_summary.rb +100 -0
  24. data/lib/ccs/frontend_helpers/govuk_frontend/field/character_count.rb +165 -0
  25. data/lib/ccs/frontend_helpers/govuk_frontend/field/checkboxes.rb +200 -0
  26. data/lib/ccs/frontend_helpers/govuk_frontend/field/date_input.rb +153 -0
  27. data/lib/ccs/frontend_helpers/govuk_frontend/field/file_upload.rb +83 -0
  28. data/lib/ccs/frontend_helpers/govuk_frontend/field/input.rb +153 -0
  29. data/lib/ccs/frontend_helpers/govuk_frontend/field/radios.rb +201 -0
  30. data/lib/ccs/frontend_helpers/govuk_frontend/field/select.rb +124 -0
  31. data/lib/ccs/frontend_helpers/govuk_frontend/field/textarea.rb +106 -0
  32. data/lib/ccs/frontend_helpers/govuk_frontend/field.rb +213 -0
  33. data/lib/ccs/frontend_helpers/govuk_frontend/fieldset.rb +71 -0
  34. data/lib/ccs/frontend_helpers/govuk_frontend/footer.rb +183 -0
  35. data/lib/ccs/frontend_helpers/govuk_frontend/form_group.rb +50 -0
  36. data/lib/ccs/frontend_helpers/govuk_frontend/header.rb +161 -0
  37. data/lib/ccs/frontend_helpers/govuk_frontend/hint.rb +38 -0
  38. data/lib/ccs/frontend_helpers/govuk_frontend/inset_text.rb +44 -0
  39. data/lib/ccs/frontend_helpers/govuk_frontend/label.rb +92 -0
  40. data/lib/ccs/frontend_helpers/govuk_frontend/notification_banner.rb +136 -0
  41. data/lib/ccs/frontend_helpers/govuk_frontend/pagination.rb +336 -0
  42. data/lib/ccs/frontend_helpers/govuk_frontend/panel.rb +51 -0
  43. data/lib/ccs/frontend_helpers/govuk_frontend/phase_banner.rb +49 -0
  44. data/lib/ccs/frontend_helpers/govuk_frontend/skip_link.rb +40 -0
  45. data/lib/ccs/frontend_helpers/govuk_frontend/step_by_step_navigation.rb +215 -0
  46. data/lib/ccs/frontend_helpers/govuk_frontend/summary_list.rb +226 -0
  47. data/lib/ccs/frontend_helpers/govuk_frontend/table.rb +124 -0
  48. data/lib/ccs/frontend_helpers/govuk_frontend/tabs.rb +95 -0
  49. data/lib/ccs/frontend_helpers/govuk_frontend/tag.rb +42 -0
  50. data/lib/ccs/frontend_helpers/govuk_frontend/warning_text.rb +53 -0
  51. data/lib/ccs/frontend_helpers/govuk_frontend.rb +82 -0
  52. data/lib/ccs/frontend_helpers/shared_methods.rb +27 -0
  53. data/lib/ccs/frontend_helpers/version.rb +7 -0
  54. data/lib/ccs/frontend_helpers.rb +20 -0
  55. data/sig/ccs/frontend_helpers.rbs +6 -0
  56. metadata +241 -0
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
6
+
7
+ module CCS
8
+ module FrontendHelpers
9
+ module GovUKFrontend
10
+ # = GOV.UK Header
11
+ #
12
+ # This helper is used for generating the header component from the
13
+ # {https://design-system.service.gov.uk/components/header GDS - Components - Header}
14
+
15
+ module Header
16
+ include SharedMethods
17
+ include ActionView::Context
18
+ include ActionView::Helpers::FormTagHelper
19
+ include ActionView::Helpers::TagHelper
20
+ include ActionView::Helpers::TextHelper
21
+ include ActionView::Helpers::UrlHelper
22
+
23
+ # Generates the HTML for the GOV.UK Header component
24
+ #
25
+ # @param govuk_header_options [Hash] options that will be used to generate the header
26
+ #
27
+ # @option govuk_header_options [String] :classes additional CSS classes for the header HTML
28
+ # @option govuk_header_options [Hash] :attributes additional attributes that will added as part of the header HTML
29
+ # @option govuk_header_options [String] :container_classes classes for the container
30
+ # @option govuk_header_options [String] :homepage_url URL of the homepage. Defaults to +/+
31
+ # @option govuk_header_options [String] :product_name product name, used when the product name follows on directly from ‘GOV.UK
32
+ # @option govuk_header_options [Hash] :service see {govuk_header_service_name}
33
+ # @option govuk_header_options [Hash] :navigation see {govuk_header_navigation}
34
+ # @option govuk_header_options [Hash] :menu_button see {govuk_header_navigation}
35
+ #
36
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Header
37
+ # which can then be rendered on the page
38
+
39
+ def govuk_header(**govuk_header_options)
40
+ determine_govuk_header_attributes(govuk_header_options)
41
+
42
+ tag.header(**govuk_header_options[:attributes]) do
43
+ tag.div(class: "govuk-header__container #{govuk_header_options[:container_classes] || 'govuk-width-container'}") do
44
+ concat(govuk_header_logo(**govuk_header_options))
45
+ if govuk_header_options[:service] || govuk_header_options[:navigation]
46
+ concat(tag.div(class: 'govuk-header__content') do
47
+ concat(govuk_header_service_name(govuk_header_options[:service])) if govuk_header_options[:service]
48
+ concat(govuk_header_navigation(govuk_header_options[:navigation], govuk_header_options[:menu_button])) if govuk_header_options[:navigation]
49
+ end)
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ # Generates the logo for {govuk_header}
58
+ #
59
+ # @param govuk_header_options [Hash] options that will be used to generate the header
60
+ #
61
+ # @option (see govuk_header)
62
+ #
63
+ # @return [ActiveSupport::SafeBuffer] the HTML for the logo used in {govuk_header}
64
+
65
+ def govuk_header_logo(govuk_header_options)
66
+ tag.div(class: 'govuk-header__logo') do
67
+ link_to(govuk_header_options[:homepage_url] || '/', class: 'govuk-header__link govuk-header__link--homepage') do
68
+ concat(tag.span(class: 'govuk-header__logotype') do
69
+ concat(tag.svg(class: 'govuk-header__logotype-crown', xmlns: 'http://www.w3.org/2000/svg', height: '30', width: '36', aria: { hidden: 'true' }, focusable: 'false', viewBox: '0 0 132 97') do
70
+ tag.path(fill: 'currentColor', 'fill-rule': 'evenodd', d: 'M25 30.2c3.5 1.5 7.7-.2 9.1-3.7 1.5-3.6-.2-7.8-3.9-9.2-3.6-1.4-7.6.3-9.1 3.9-1.4 3.5.3 7.5 3.9 9zM9 39.5c3.6 1.5 7.8-.2 9.2-3.7 1.5-3.6-.2-7.8-3.9-9.1-3.6-1.5-7.6.2-9.1 3.8-1.4 3.5.3 7.5 3.8 9zM4.4 57.2c3.5 1.5 7.7-.2 9.1-3.8 1.5-3.6-.2-7.7-3.9-9.1-3.5-1.5-7.6.3-9.1 3.8-1.4 3.5.3 7.6 3.9 9.1zm38.3-21.4c3.5 1.5 7.7-.2 9.1-3.8 1.5-3.6-.2-7.7-3.9-9.1-3.6-1.5-7.6.3-9.1 3.8-1.3 3.6.4 7.7 3.9 9.1zm64.4-5.6c-3.6 1.5-7.8-.2-9.1-3.7-1.5-3.6.2-7.8 3.8-9.2 3.6-1.4 7.7.3 9.2 3.9 1.3 3.5-.4 7.5-3.9 9zm15.9 9.3c-3.6 1.5-7.7-.2-9.1-3.7-1.5-3.6.2-7.8 3.7-9.1 3.6-1.5 7.7.2 9.2 3.8 1.5 3.5-.3 7.5-3.8 9zm4.7 17.7c-3.6 1.5-7.8-.2-9.2-3.8-1.5-3.6.2-7.7 3.9-9.1 3.6-1.5 7.7.3 9.2 3.8 1.3 3.5-.4 7.6-3.9 9.1zM89.3 35.8c-3.6 1.5-7.8-.2-9.2-3.8-1.4-3.6.2-7.7 3.9-9.1 3.6-1.5 7.7.3 9.2 3.8 1.4 3.6-.3 7.7-3.9 9.1zM69.7 17.7l8.9 4.7V9.3l-8.9 2.8c-.2-.3-.5-.6-.9-.9L72.4 0H59.6l3.5 11.2c-.3.3-.6.5-.9.9l-8.8-2.8v13.1l8.8-4.7c.3.3.6.7.9.9l-5 15.4v.1c-.2.8-.4 1.6-.4 2.4 0 4.1 3.1 7.5 7 8.1h.2c.3 0 .7.1 1 .1.4 0 .7 0 1-.1h.2c4-.6 7.1-4.1 7.1-8.1 0-.8-.1-1.7-.4-2.4V34l-5.1-15.4c.4-.2.7-.6 1-.9zM66 92.8c16.9 0 32.8 1.1 47.1 3.2 4-16.9 8.9-26.7 14-33.5l-9.6-3.4c1 4.9 1.1 7.2 0 10.2-1.5-1.4-3-4.3-4.2-8.7L108.6 76c2.8-2 5-3.2 7.5-3.3-4.4 9.4-10 11.9-13.6 11.2-4.3-.8-6.3-4.6-5.6-7.9 1-4.7 5.7-5.9 8-.5 4.3-8.7-3-11.4-7.6-8.8 7.1-7.2 7.9-13.5 2.1-21.1-8 6.1-8.1 12.3-4.5 20.8-4.7-5.4-12.1-2.5-9.5 6.2 3.4-5.2 7.9-2 7.2 3.1-.6 4.3-6.4 7.8-13.5 7.2-10.3-.9-10.9-8-11.2-13.8 2.5-.5 7.1 1.8 11 7.3L80.2 60c-4.1 4.4-8 5.3-12.3 5.4 1.4-4.4 8-11.6 8-11.6H55.5s6.4 7.2 7.9 11.6c-4.2-.1-8-1-12.3-5.4l1.4 16.4c3.9-5.5 8.5-7.7 10.9-7.3-.3 5.8-.9 12.8-11.1 13.8-7.2.6-12.9-2.9-13.5-7.2-.7-5 3.8-8.3 7.1-3.1 2.7-8.7-4.6-11.6-9.4-6.2 3.7-8.5 3.6-14.7-4.6-20.8-5.8 7.6-5 13.9 2.2 21.1-4.7-2.6-11.9.1-7.7 8.8 2.3-5.5 7.1-4.2 8.1.5.7 3.3-1.3 7.1-5.7 7.9-3.5.7-9-1.8-13.5-11.2 2.5.1 4.7 1.3 7.5 3.3l-4.7-15.4c-1.2 4.4-2.7 7.2-4.3 8.7-1.1-3-.9-5.3 0-10.2l-9.5 3.4c5 6.9 9.9 16.7 14 33.5 14.8-2.1 30.8-3.2 47.7-3.2z')
71
+ end)
72
+ concat(tag.span('GOV.UK', class: 'govuk-header__logotype-text'))
73
+ end)
74
+ concat(tag.span(govuk_header_options[:product_name], class: 'govuk-header__product-name')) if govuk_header_options[:product_name]
75
+ end
76
+ end
77
+ end
78
+
79
+ # Generates the service name section for {govuk_header}
80
+ #
81
+ # @param service [Hash] options that will be used in the service name section
82
+ #
83
+ # @option service [String] :name the name of the service, included in the header
84
+ # @option service [String] :href URL for the service name anchor
85
+ #
86
+ # @return [ActiveSupport::SafeBuffer] the HTML for the service name section which is used in {govuk_header}
87
+
88
+ def govuk_header_service_name(service)
89
+ if service[:href]
90
+ link_to(service[:name], service[:href], class: 'govuk-header__link govuk-header__service-name')
91
+ else
92
+ tag.span(service[:name], class: 'govuk-header__service-name')
93
+ end
94
+ end
95
+
96
+ # Generates the navigation section for {govuk_header}
97
+ #
98
+ # @param navigation [Hash] options for the navigation
99
+ # @param menu_button [Hash] options for the menu button
100
+ #
101
+ # @option navigation [String] :classes additional CSS classes for the navigation HTML
102
+ # @option navigation [String] :label text for the aria-label attribute of the navigation. Defaults to the menu button text
103
+ # @option navigation [Array] :items the navigation items that will be rendered on the page (see {govuk_header_navigation_item})
104
+ #
105
+ # @option menu_button [String] :text text for the button that opens the mobile navigation menu.
106
+ # By default, this is set to +Menu+.
107
+ # @option menu_button [String] :label text for the aria-label attribute of the button that opens the mobile navigation.
108
+ # Defaults to +Show or hide menu+.
109
+ #
110
+ # @return [ActiveSupport::SafeBuffer] the HTML for the navigation section which is used in {govuk_header}
111
+
112
+ def govuk_header_navigation(navigation, menu_button)
113
+ (menu_button ||= {})[:text] ||= 'Menu'
114
+
115
+ tag.nav(aria: { label: navigation[:label] || menu_button[:text] }, class: "govuk-header__navigation #{navigation[:classes]}".rstrip) do
116
+ concat(button_tag(menu_button[:text], type: :button, class: 'govuk-header__menu-button govuk-js-header-toggle', aria: { controls: 'navigation', label: menu_button[:label] || 'Show or hide menu' }, hidden: true))
117
+ concat(tag.ul(id: 'navigation', class: 'govuk-header__navigation-list') do
118
+ navigation[:items].each { |navigation_item| concat(govuk_header_navigation_item(navigation_item)) }
119
+ end)
120
+ end
121
+ end
122
+
123
+ # Generates a navigation item for {govuk_header_navigation}
124
+ #
125
+ # @param navigation_item [Hash] options that will be used in customising the HTML
126
+ #
127
+ # @option navigation_item [Boolean] :active flag to mark the navigation item as active or not
128
+ # @option navigation_item [String] :text text for the navigation item
129
+ # @option navigation_item [String] :href URL of the navigation item anchor
130
+ # @option navigation_item [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
131
+ #
132
+ # @return [ActiveSupport::SafeBuffer] the HTML for a navigation item which is used in {govuk_header_navigation}
133
+
134
+ def govuk_header_navigation_item(navigation_item)
135
+ tag.li(class: "govuk-header__navigation-item #{'govuk-header__navigation-item--active' if navigation_item[:active]}".rstrip) do
136
+ if navigation_item[:href]
137
+ (navigation_item[:attributes] ||= {})[:class] = 'govuk-header__link'
138
+
139
+ link_to(navigation_item[:text], navigation_item[:href], **navigation_item[:attributes])
140
+ else
141
+ navigation_item[:text]
142
+ end
143
+ end
144
+ end
145
+
146
+ # Sets the default attributes for {govuk_header}
147
+ #
148
+ # @param govuk_header_options [Hash] options that will be used in customising the HTML
149
+ #
150
+ # @option (see govuk_header)
151
+
152
+ def determine_govuk_header_attributes(govuk_header_options)
153
+ initialise_attributes_and_set_classes(govuk_header_options, 'govuk-header')
154
+ set_data_module(govuk_header_options, 'govuk-header')
155
+
156
+ govuk_header_options[:attributes][:role] = 'banner'
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
6
+
7
+ module CCS
8
+ module FrontendHelpers
9
+ module GovUKFrontend
10
+ # = GOV.UK Hint
11
+ #
12
+ # This helper is used for generating the hint text component from the Government Design Systems
13
+
14
+ module Hint
15
+ include SharedMethods
16
+ include ActionView::Context
17
+ include ActionView::Helpers::TagHelper
18
+
19
+ # Generates the HTML for the GOV.UK Hint component
20
+ #
21
+ # @param hint_text [String] the hint text
22
+ # @param govuk_hint_options [Hash] options that will be used in customising the HTML
23
+ #
24
+ # @option govuk_hint_options [String] :classes additional CSS classes for the hint HTML
25
+ # @option govuk_hint_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
26
+ #
27
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Hint
28
+ # which can then be rendered on the page
29
+
30
+ def govuk_hint(hint_text, **govuk_hint_options)
31
+ initialise_attributes_and_set_classes(govuk_hint_options, 'govuk-hint')
32
+
33
+ tag.div(hint_text, **govuk_hint_options[:attributes])
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
6
+
7
+ module CCS
8
+ module FrontendHelpers
9
+ module GovUKFrontend
10
+ # = GOV.UK Inset Text
11
+ #
12
+ # This helper is used for generating the inset text component from the
13
+ # {https://design-system.service.gov.uk/components/inset-text GDS - Components - Inset text}
14
+
15
+ module InsetText
16
+ include SharedMethods
17
+ include ActionView::Context
18
+ include ActionView::Helpers::TagHelper
19
+ include ActionView::Helpers::TextHelper
20
+
21
+ # Generates the HTML for the GOV.UK Inset text component
22
+ #
23
+ # @param inset_text [String] text to use within the inset text component
24
+ # @param govuk_inset_text_options [Hash] options that will be used in customising the HTML
25
+ #
26
+ # @option govuk_inset_text_options [String] :classes additional CSS classes for the inset text HTML
27
+ # @option govuk_inset_text_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
28
+ #
29
+ # @yield HTML that will be contained within the inset text div. Ignored if inset text is given
30
+ #
31
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Inset text
32
+ # which can then be rendered on the page
33
+
34
+ def govuk_inset_text(inset_text = nil, **govuk_inset_text_options)
35
+ initialise_attributes_and_set_classes(govuk_inset_text_options, 'govuk-inset-text')
36
+
37
+ tag.div(**govuk_inset_text_options[:attributes]) do
38
+ inset_text || yield
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
6
+
7
+ module CCS
8
+ module FrontendHelpers
9
+ module GovUKFrontend
10
+ # = GOV.UK label
11
+ #
12
+ # This helper is used for generating the label component from the Government Design Systems
13
+
14
+ module Label
15
+ include SharedMethods
16
+ include ActionView::Context
17
+ include ActionView::Helpers::TagHelper
18
+ include ActionView::Helpers::TextHelper
19
+ include ActionView::Helpers::FormTagHelper
20
+
21
+ # Generates the HTML for the GOV.UK label component
22
+ #
23
+ # @param attribute [String, Symbol] the attribute of the input that requires a label
24
+ # @param label_text [String] the label text, it is ignored if a block is given
25
+ # @param govuk_label_options [Hash] options that will be used in customising the HTML
26
+ #
27
+ # @option govuk_label_options [String] :classes additional CSS classes for the label HTML
28
+ # @option govuk_label_options [Boolean] :is_page_heading (false) if the legend is also the heading it will rendered in a h1
29
+ # @option govuk_label_options [ActionView::Helpers::FormBuilder] :form (nil) optional form builder used to create the label
30
+ # @option govuk_label_options [Hash] :attributes ({}) any additional attributes that will added as part of the HTML
31
+ #
32
+ # @yield HTML that will be contained within the 'govuk-label' label. Ignored if label text is given
33
+ #
34
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Label
35
+ # which can then be rendered on the page
36
+
37
+ def govuk_label(attribute, label_text = nil, **govuk_label_options, &block)
38
+ initialise_attributes_and_set_classes(govuk_label_options, 'govuk-label')
39
+
40
+ label_html = if govuk_label_options[:form]
41
+ govuk_label_form(attribute, label_text, **govuk_label_options, &block)
42
+ else
43
+ govuk_label_tag(attribute, label_text, **govuk_label_options, &block)
44
+ end
45
+
46
+ if govuk_label_options[:is_page_heading]
47
+ tag.h1(label_html, class: 'govuk-label-wrapper')
48
+ else
49
+ label_html
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ # Generates the HTML for the GOV.UK label component
56
+ # using the inbuilt rails method +label+
57
+ #
58
+ # @param (see govuk_label)
59
+ #
60
+ # @option (see govuk_label)
61
+ #
62
+ # @yield (see govuk_label)
63
+ #
64
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Label
65
+ # which can then be rendered on the page
66
+
67
+ def govuk_label_tag(attribute, label_text, **govuk_label_options)
68
+ label_tag(attribute, **govuk_label_options[:attributes]) do
69
+ label_text || yield
70
+ end
71
+ end
72
+
73
+ # Generates the HTML for the GOV.UK label component
74
+ # using the inbuilt rails ActionView::Helpers::FormBuilder
75
+ #
76
+ # @param (see govuk_label)
77
+ #
78
+ # @option (see govuk_label)
79
+ #
80
+ # @yield (see govuk_label)
81
+ #
82
+ # @return (see govuk_label_tag)
83
+
84
+ def govuk_label_form(attribute, label_text, **govuk_label_options)
85
+ govuk_label_options[:form].label(attribute, **govuk_label_options[:attributes]) do
86
+ label_text || yield
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_view'
4
+
5
+ require_relative '../shared_methods'
6
+
7
+ module CCS
8
+ module FrontendHelpers
9
+ module GovUKFrontend
10
+ # = GOV.UK Notification Banner
11
+ #
12
+ # This helper is used for generating the notification banner component from the
13
+ # {https://design-system.service.gov.uk/components/notification-banner GDS - Components - Notification banner}
14
+
15
+ module NotificationBanner
16
+ include SharedMethods
17
+ include ActionView::Context
18
+ include ActionView::Helpers::TagHelper
19
+ include ActionView::Helpers::TextHelper
20
+
21
+ # Generates the HTML for the GOV.UK Notification banner component
22
+ #
23
+ # @param text [String] the text that will be used for the heading in the content section of the banner.
24
+ # It is ignored if a block is given
25
+ # @param success_banner [Boolean] will use the success banner options if this is set to true
26
+ # @param govuk_notification_banner_options [Hash] options that will be used in customising the HTML
27
+ #
28
+ # @option govuk_notification_banner_options [String] :classes additional CSS classes for the notification banner HTML
29
+ # @option govuk_notification_banner_options [String] :title_text ('Important') the title text shown at the top of the banner
30
+ # @option govuk_notification_banner_options [String] :title_id ('govuk-notification-banner-title') the ID for the title text
31
+ # @option govuk_notification_banner_options [String] :role ('region') the role for the banner
32
+ # @option govuk_notification_banner_options [String] :heading_level (2) the heading level for the title text
33
+ # @option govuk_notification_banner_options [Hash] :attributes ({data: { module: 'govuk-notification-banner' }, aria: { labelledby: 'govuk-notification-banner-title' }})
34
+ # any additional attributes that will added as part of the HTML
35
+ #
36
+ # @yield HTML that will be contained within the 'govuk-notification-banner__content' div
37
+ #
38
+ # @return [ActiveSupport::SafeBuffer] the HTML for the GOV.UK Notification banner
39
+ # which can then be rendered on the page
40
+
41
+ def govuk_notification_banner(text = nil, success_banner = false, **govuk_notification_banner_options)
42
+ banner_options = fetch_banner_options(success_banner, govuk_notification_banner_options)
43
+
44
+ set_banner_classes(banner_options, govuk_notification_banner_options)
45
+ set_banner_attributes(banner_options, govuk_notification_banner_options)
46
+
47
+ tag.div(**govuk_notification_banner_options[:attributes]) do
48
+ concat(tag.div(class: 'govuk-notification-banner__header') do
49
+ tag.send(:"h#{govuk_notification_banner_options[:heading_level] || 2}", banner_options[:title_text], class: 'govuk-notification-banner__title', id: banner_options[:title_id])
50
+ end)
51
+ concat(tag.div(class: 'govuk-notification-banner__content') do
52
+ if block_given?
53
+ yield
54
+ else
55
+ tag.p(text, class: 'govuk-notification-banner__heading')
56
+ end
57
+ end)
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ # Determines the banner options to be used for {#govuk_notification_banner}
64
+ #
65
+ # @param success_banner [Boolean] will use the success banner options if this is set to true
66
+ # @param govuk_notification_banner_options [Hash] options that will be used in customising the HTML
67
+ #
68
+ # @option govuk_notification_banner_options [String] :title_text ('Important') the title text shown at the top of the banner
69
+ # @option govuk_notification_banner_options [String] :title_id ('govuk-notification-banner-title') the ID for the title text
70
+ # @option govuk_notification_banner_options [String] :role ('region') the role for the banner
71
+ #
72
+ # @return [Hash] contains the following options used in {#govuk_notification_banner}:
73
+ # - +classes+
74
+ # - +title_text+
75
+ # - +title_id+
76
+ # - +role+
77
+
78
+ def fetch_banner_options(success_banner, govuk_notification_banner_options)
79
+ banner_options = DEFAULT_OPTIONS.dup
80
+
81
+ banner_options.merge!(SUCCESS_BANNER_OPTIONS) if success_banner
82
+
83
+ govuk_notification_banner_options.each do |key, value|
84
+ banner_options[key] = value if banner_options.key?(key)
85
+ end
86
+
87
+ banner_options
88
+ end
89
+
90
+ # Sets the banner classes to be used for {#govuk_notification_banner}
91
+ #
92
+ # @param banner_options [Hash] this is the return value from {#fetch_banner_options}
93
+ # @param govuk_notification_banner_options [Hash] options that will be used in customising the HTML
94
+ #
95
+ # @option govuk_notification_banner_options [String] :classes additional CSS classes for the notification banner HTML
96
+
97
+ def set_banner_classes(banner_options, govuk_notification_banner_options)
98
+ initialise_attributes_and_set_classes(govuk_notification_banner_options, 'govuk-notification-banner')
99
+
100
+ govuk_notification_banner_options[:attributes][:class] << banner_options[:classes].to_s
101
+ end
102
+
103
+ # Sets the HTML attributes for {#govuk_notification_banner}
104
+ #
105
+ # @param banner_options [Hash] this is the return value from {#fetch_banner_options}
106
+ # @param govuk_notification_banner_options [Hash] options that will be used in customising the HTML
107
+ #
108
+ # @option govuk_notification_banner_options [Hash] :attributes ({data: { module: 'govuk-notification-banner' }, aria: { labelledby: 'govuk-notification-banner-title' }})
109
+ # any additional attributes that will added as part of the HTML
110
+
111
+ def set_banner_attributes(banner_options, govuk_notification_banner_options)
112
+ set_data_module(govuk_notification_banner_options, 'govuk-notification-banner')
113
+
114
+ govuk_notification_banner_options[:attributes][:role] ||= banner_options[:role]
115
+ (govuk_notification_banner_options[:attributes][:aria] ||= {})[:labelledby] = banner_options[:title_id]
116
+ end
117
+
118
+ # Default options used in normal versions of {#govuk_notification_banner}
119
+
120
+ DEFAULT_OPTIONS = {
121
+ title_text: 'Important',
122
+ title_id: 'govuk-notification-banner-title',
123
+ role: 'region'
124
+ }.freeze
125
+
126
+ # Options specific for the success version of {#govuk_notification_banner}
127
+
128
+ SUCCESS_BANNER_OPTIONS = {
129
+ classes: ' govuk-notification-banner--success',
130
+ title_text: 'Success',
131
+ role: 'alert'
132
+ }.freeze
133
+ end
134
+ end
135
+ end
136
+ end