govuk-components 1.1.7 → 2.0.0b2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -1
  3. data/app/components/govuk_component/{accordion.html.erb → accordion_component.html.erb} +2 -2
  4. data/app/components/govuk_component/{accordion.rb → accordion_component.rb} +7 -6
  5. data/app/components/govuk_component/{back_link.rb → back_link_component.rb} +5 -1
  6. data/app/components/govuk_component/base.rb +9 -7
  7. data/app/components/govuk_component/{breadcrumbs.html.erb → breadcrumbs_component.html.erb} +3 -6
  8. data/app/components/govuk_component/{breadcrumbs.rb → breadcrumbs_component.rb} +1 -1
  9. data/app/components/govuk_component/{cookie_banner.html.erb → cookie_banner_component.html.erb} +2 -6
  10. data/app/components/govuk_component/{cookie_banner.rb → cookie_banner_component.rb} +3 -2
  11. data/app/components/govuk_component/details_component.rb +34 -0
  12. data/app/components/govuk_component/{footer.html.erb → footer_component.html.erb} +3 -5
  13. data/app/components/govuk_component/{footer.rb → footer_component.rb} +3 -8
  14. data/app/components/govuk_component/header_component.html.erb +49 -0
  15. data/app/components/govuk_component/header_component.rb +110 -0
  16. data/app/components/govuk_component/{inset_text.rb → inset_text_component.rb} +3 -3
  17. data/app/components/govuk_component/notification_banner_component.html.erb +14 -0
  18. data/app/components/govuk_component/{notification_banner.rb → notification_banner_component.rb} +28 -14
  19. data/app/components/govuk_component/panel_component.rb +46 -0
  20. data/app/components/govuk_component/phase_banner_component.html.erb +6 -0
  21. data/app/components/govuk_component/phase_banner_component.rb +20 -0
  22. data/app/components/govuk_component/start_button_component.rb +47 -0
  23. data/app/components/govuk_component/summary_list_component.html.erb +11 -0
  24. data/app/components/govuk_component/summary_list_component.rb +61 -0
  25. data/app/components/govuk_component/tab_component.html.erb +11 -0
  26. data/app/components/govuk_component/{tabs.rb → tab_component.rb} +23 -12
  27. data/app/components/govuk_component/{tag.rb → tag_component.rb} +21 -3
  28. data/app/components/govuk_component/warning_text_component.rb +38 -0
  29. data/app/helpers/govuk_components_helper.rb +16 -16
  30. data/lib/govuk/components.rb +1 -0
  31. data/lib/govuk/components/helpers/css_utilities.rb +22 -0
  32. data/lib/govuk/components/version.rb +1 -1
  33. metadata +52 -42
  34. data/app/components/govuk_component/back_link.html.erb +0 -1
  35. data/app/components/govuk_component/details.html.erb +0 -10
  36. data/app/components/govuk_component/details.rb +0 -16
  37. data/app/components/govuk_component/header.html.erb +0 -47
  38. data/app/components/govuk_component/header.rb +0 -53
  39. data/app/components/govuk_component/notification_banner.html.erb +0 -23
  40. data/app/components/govuk_component/panel.html.erb +0 -13
  41. data/app/components/govuk_component/panel.rb +0 -28
  42. data/app/components/govuk_component/phase_banner.html.erb +0 -8
  43. data/app/components/govuk_component/phase_banner.rb +0 -16
  44. data/app/components/govuk_component/start_now_button.html.erb +0 -6
  45. data/app/components/govuk_component/start_now_button.rb +0 -16
  46. data/app/components/govuk_component/summary_list.html.erb +0 -19
  47. data/app/components/govuk_component/summary_list.rb +0 -34
  48. data/app/components/govuk_component/tabs.html.erb +0 -19
  49. data/app/components/govuk_component/warning.html.erb +0 -7
  50. data/app/components/govuk_component/warning.rb +0 -14
@@ -1,9 +1,7 @@
1
- class GovukComponent::NotificationBanner < GovukComponent::Base
1
+ class GovukComponent::NotificationBannerComponent < GovukComponent::Base
2
2
  attr_reader :title, :title_id, :success, :title_heading_level, :disable_auto_focus
3
3
 
4
- include ViewComponent::Slotable
5
- with_slot :heading, collection: true, class_name: 'Heading'
6
- wrap_slot(:heading)
4
+ renders_many :headings, "Heading"
7
5
 
8
6
  def initialize(title:, success: false, title_heading_level: 2, title_id: "govuk-notification-banner-title", disable_auto_focus: nil, classes: [], html_attributes: {})
9
7
  super(classes: classes, html_attributes: html_attributes)
@@ -15,16 +13,16 @@ class GovukComponent::NotificationBanner < GovukComponent::Base
15
13
  @disable_auto_focus = disable_auto_focus
16
14
  end
17
15
 
18
- def success_class
19
- %(govuk-notification-banner--success) if success?
16
+ def render?
17
+ headings.any? || content.present?
20
18
  end
21
19
 
22
- def success?
23
- @success
20
+ def classes
21
+ super.append(success_class).compact
24
22
  end
25
23
 
26
- def render?
27
- headings.any? || content.present?
24
+ def success_class
25
+ %(govuk-notification-banner--success) if success
28
26
  end
29
27
 
30
28
  def title_tag
@@ -33,13 +31,29 @@ class GovukComponent::NotificationBanner < GovukComponent::Base
33
31
  "h#{title_heading_level}"
34
32
  end
35
33
 
36
- class Heading < ViewComponent::Slot
37
- attr_accessor :text, :link_target, :link_text
34
+ class Heading < GovukComponent::Base
35
+ attr_accessor :text, :link_href, :link_text
36
+
37
+ def initialize(text: nil, link_text: nil, link_href: nil, classes: [], html_attributes: {})
38
+ super(classes: classes, html_attributes: html_attributes)
38
39
 
39
- def initialize(text: nil, link_text: nil, link_target: nil)
40
40
  @text = text
41
41
  @link_text = link_text
42
- @link_target = link_target
42
+ @link_href = link_href
43
+ end
44
+
45
+ def call
46
+ tag.div(class: classes, **html_attributes) do
47
+ if text.present?
48
+ safe_join([text, link].compact)
49
+ else
50
+ content
51
+ end
52
+ end
53
+ end
54
+
55
+ def link
56
+ link_to(link_text, link_href, class: 'govuk-notification-banner__link') if link_text.present? && link_href.present?
43
57
  end
44
58
 
45
59
  def default_classes
@@ -0,0 +1,46 @@
1
+ class GovukComponent::PanelComponent < GovukComponent::Base
2
+ attr_accessor :title, :body
3
+
4
+ def initialize(title: nil, body: nil, classes: [], html_attributes: {})
5
+ super(classes: classes, html_attributes: html_attributes)
6
+
7
+ @title = title
8
+ @body = body
9
+ end
10
+
11
+ def call
12
+ tag.div(class: classes, **html_attributes) do
13
+ safe_join([panel_title, panel_body].compact)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def default_classes
20
+ %w(govuk-panel govuk-panel--confirmation)
21
+ end
22
+
23
+ def display_title?
24
+ title.present?
25
+ end
26
+
27
+ def display_body?
28
+ body.present? || content.present?
29
+ end
30
+
31
+ def panel_title
32
+ tag.h1(title, class: "govuk-panel__title") if display_title?
33
+ end
34
+
35
+ def panel_body
36
+ if display_body?
37
+ tag.div(class: "govuk-panel__body") do
38
+ content.presence || @body
39
+ end
40
+ end
41
+ end
42
+
43
+ def render?
44
+ display_title? || display_body?
45
+ end
46
+ end
@@ -0,0 +1,6 @@
1
+ <%= tag.div(class: classes, **html_attributes) do %>
2
+ <p class="govuk-phase-banner__content">
3
+ <%= render(phase_tag_component) %>
4
+ <%= tag.span((content.presence || @text), class: "govuk-phase-banner__text") %>
5
+ </p>
6
+ <% end %>
@@ -0,0 +1,20 @@
1
+ class GovukComponent::PhaseBannerComponent < GovukComponent::Base
2
+ attr_accessor :text
3
+
4
+ def initialize(phase_tag: nil, text: nil, classes: [], html_attributes: {})
5
+ super(classes: classes, html_attributes: html_attributes)
6
+
7
+ @phase_tag = phase_tag
8
+ @text = text
9
+ end
10
+
11
+ def phase_tag_component
12
+ GovukComponent::TagComponent.new(classes: "govuk-phase-banner__content__tag", **@phase_tag)
13
+ end
14
+
15
+ private
16
+
17
+ def default_classes
18
+ %w(govuk-phase-banner)
19
+ end
20
+ end
@@ -0,0 +1,47 @@
1
+ class GovukComponent::StartButtonComponent < GovukComponent::Base
2
+ attr_accessor :text, :href
3
+
4
+ def initialize(text:, href:, classes: [], html_attributes: {})
5
+ super(classes: classes, html_attributes: html_attributes)
6
+
7
+ @text = text
8
+ @href = href
9
+ end
10
+
11
+ def call
12
+ link_to(@href, **default_attributes, **html_attributes) do
13
+ safe_join([@text, icon])
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def default_attributes
20
+ {
21
+ role: 'button',
22
+ draggable: 'false',
23
+ class: classes,
24
+ data: { module: 'govuk-button' }
25
+ }
26
+ end
27
+
28
+ def default_classes
29
+ %w(govuk-button govuk-button--start)
30
+ end
31
+
32
+ def icon
33
+ svg_attributes = {
34
+ class: "govuk-button__start-icon",
35
+ xmlns: "http://www.w3.org/2000/svg",
36
+ width: "17.5",
37
+ height: "19",
38
+ viewBox: "0 0 33 40",
39
+ focusable: "false",
40
+ aria: { hidden: "true" }
41
+ }
42
+
43
+ tag.svg(**svg_attributes) do
44
+ tag.path(fill: "currentColor", d: "M0 0h13l20 20-20 20H0l20-20z")
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,11 @@
1
+ <%= tag.dl(class: classes, **html_attributes) do %>
2
+ <% rows.each do |row| %>
3
+ <%= tag.div(class: row.classes, **row.html_attributes) do %>
4
+ <%= tag.dt(row.key, class: "govuk-summary-list__key") %>
5
+ <%= tag.dd(row.value, class: "govuk-summary-list__value") %>
6
+ <% if any_row_has_actions? %>
7
+ <%= row.action %>
8
+ <% end %>
9
+ <% end %>
10
+ <% end %>
11
+ <% end %>
@@ -0,0 +1,61 @@
1
+ class GovukComponent::SummaryListComponent < GovukComponent::Base
2
+ attr_reader :borders
3
+
4
+ renders_many :rows, "Row"
5
+
6
+ def initialize(borders: true, classes: [], html_attributes: {})
7
+ super(classes: classes, html_attributes: html_attributes)
8
+
9
+ @borders = borders
10
+ end
11
+
12
+ def any_row_has_actions?
13
+ rows.any? { |row| row.href.present? }
14
+ end
15
+
16
+ def classes
17
+ super.append(borders_class).compact
18
+ end
19
+
20
+ private
21
+
22
+ def borders_class
23
+ %(govuk-summary-list--no-border) unless borders
24
+ end
25
+
26
+ def default_classes
27
+ %w(govuk-summary-list)
28
+ end
29
+
30
+ class Row < GovukComponent::Base
31
+ attr_reader :key, :value, :href, :text, :visually_hidden_text, :action_classes, :action_attributes
32
+
33
+ def initialize(key:, value:, action: {}, classes: [], html_attributes: {})
34
+ super(classes: classes, html_attributes: html_attributes)
35
+
36
+ @key = key
37
+ @value = value
38
+ @href = action[:href]
39
+ @text = action[:text] || "Change"
40
+ @visually_hidden_text = " #{action[:visually_hidden_text] || key.downcase}"
41
+ @action_classes = action[:classes] || []
42
+ @action_attributes = action[:html_attributes] || {}
43
+ end
44
+
45
+ def action
46
+ tag.dd(class: "govuk-summary-list__actions") do
47
+ if href.present?
48
+ govuk_link_to(href, class: action_classes, **action_attributes) do
49
+ safe_join([text, tag.span(visually_hidden_text, class: "govuk-visually-hidden")])
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def default_classes
58
+ %w(govuk-summary-list__row)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,11 @@
1
+ <%= tag.div(class: classes, data: { module: 'govuk-tabs' }, **html_attributes) do %>
2
+ <%= tag.h2(title, class: "govuk-tabs__title") %>
3
+ <ul class="govuk-tabs__list">
4
+ <% tabs.each.with_index do |tab, i| %>
5
+ <%= tag.li(tab.li_link, class: tab.li_classes(i)) %>
6
+ <% end %>
7
+ </ul>
8
+ <% tabs.each.with_index do |tab, i| %>
9
+ <%= tag.div(tab, class: tab.classes.append(tab.hidden_class(i)), id: tab.id, **tab.html_attributes) %>
10
+ <% end %>
11
+ <% end %>
@@ -1,15 +1,12 @@
1
- class GovukComponent::Tabs < GovukComponent::Base
2
- include ViewComponent::Slotable
1
+ class GovukComponent::TabComponent < GovukComponent::Base
2
+ renders_many :tabs, "Tab"
3
3
 
4
- attr_accessor :title
5
-
6
- with_slot :tab, collection: true, class_name: 'Tab'
7
- wrap_slot :tab
4
+ attr_reader :title
8
5
 
9
6
  def initialize(title:, classes: [], html_attributes: {})
10
7
  super(classes: classes, html_attributes: html_attributes)
11
8
 
12
- self.title = title
9
+ @title = title
13
10
  end
14
11
 
15
12
  private
@@ -18,25 +15,39 @@ private
18
15
  %w(govuk-tabs)
19
16
  end
20
17
 
21
- class Tab < GovukComponent::Slot
22
- attr_accessor :title
18
+ class Tab < GovukComponent::Base
19
+ attr_reader :title
23
20
 
24
21
  def initialize(title:, classes: [], html_attributes: {})
25
22
  super(classes: classes, html_attributes: html_attributes)
26
23
 
27
- self.title = title
24
+ @title = title
28
25
  end
29
26
 
30
27
  def id(prefix: nil)
31
28
  [prefix, title.parameterize].join
32
29
  end
33
30
 
31
+ def hidden_class(i = nil)
32
+ %(govuk-tabs__panel--hidden) unless i&.zero?
33
+ end
34
+
35
+ def li_classes(i = nil)
36
+ %w(govuk-tabs__list-item).tap do |c|
37
+ c.append("govuk-tabs__list-item--selected") if i&.zero?
38
+ end
39
+ end
40
+
41
+ def li_link
42
+ link_to(title, id(prefix: '#'), class: "govuk-tabs__tab")
43
+ end
44
+
34
45
  def default_classes
35
46
  %w(govuk-tabs__panel)
36
47
  end
37
48
 
38
- def hidden_class(i = nil)
39
- %(govuk-tabs__panel--hidden) unless i&.zero?
49
+ def call
50
+ content
40
51
  end
41
52
  end
42
53
  end
@@ -1,7 +1,17 @@
1
- class GovukComponent::Tag < GovukComponent::Base
1
+ class GovukComponent::TagComponent < GovukComponent::Base
2
2
  attr_reader :text
3
3
 
4
- COLOURS = %w(grey green turquoise blue red purple pink orange yellow).freeze
4
+ COLOURS = %w(
5
+ grey
6
+ green
7
+ turquoise
8
+ blue
9
+ red
10
+ purple
11
+ pink
12
+ orange
13
+ yellow
14
+ ).freeze
5
15
 
6
16
  def initialize(text:, colour: nil, classes: [], html_attributes: {})
7
17
  super(classes: classes, html_attributes: html_attributes)
@@ -23,8 +33,16 @@ private
23
33
  def colour_class
24
34
  return nil if @colour.blank?
25
35
 
26
- fail("invalid tag colour #{@colour}, supported colours are #{COLOURS.to_sentence}") unless @colour.in?(COLOURS)
36
+ fail(ArgumentError, colour_error_message) unless valid_colour?
27
37
 
28
38
  %(govuk-tag--#{@colour})
29
39
  end
40
+
41
+ def valid_colour?
42
+ @colour.in?(COLOURS)
43
+ end
44
+
45
+ def colour_error_message
46
+ "invalid tag colour #{@colour}, supported colours are #{COLOURS.to_sentence}"
47
+ end
30
48
  end
@@ -0,0 +1,38 @@
1
+ class GovukComponent::WarningTextComponent < GovukComponent::Base
2
+ attr_reader :text, :assistive_text
3
+
4
+ ICON = '!'.freeze
5
+
6
+ def initialize(text:, assistive_text: 'Warning', classes: [], html_attributes: {})
7
+ super(classes: classes, html_attributes: html_attributes)
8
+
9
+ @text = text
10
+ @assistive_text = assistive_text
11
+ end
12
+
13
+ def call
14
+ tag.div(class: classes, **html_attributes) do
15
+ safe_join([icon, strong])
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def icon
22
+ tag.span(ICON, class: 'govuk-warning-text__icon', aria: { hidden: true })
23
+ end
24
+
25
+ def strong
26
+ tag.strong(class: 'govuk-warning-text__text') do
27
+ safe_join([assistive, text])
28
+ end
29
+ end
30
+
31
+ def assistive
32
+ tag.span(assistive_text, class: 'govuk-warning-text__assistive')
33
+ end
34
+
35
+ def default_classes
36
+ %w(govuk-warning-text)
37
+ end
38
+ end
@@ -1,21 +1,21 @@
1
1
  module GovukComponentsHelper
2
2
  {
3
- govuk_accordion: 'GovukComponent::Accordion',
4
- govuk_back_link: 'GovukComponent::BackLink',
5
- govuk_breadcrumbs: 'GovukComponent::Breadcrumbs',
6
- govuk_cookie_banner: 'GovukComponent::CookieBanner',
7
- govuk_details: 'GovukComponent::Details',
8
- govuk_footer: 'GovukComponent::Footer',
9
- govuk_header: 'GovukComponent::Header',
10
- govuk_inset_text: 'GovukComponent::InsetText',
11
- govuk_notification_banner: 'GovukComponent::NotificationBanner',
12
- govuk_panel: 'GovukComponent::Panel',
13
- govuk_phase_banner: 'GovukComponent::PhaseBanner',
14
- govuk_start_now_button: 'GovukComponent::StartNowButton',
15
- govuk_summary_list: 'GovukComponent::SummaryList',
16
- govuk_tabs: 'GovukComponent::Tabs',
17
- govuk_tag: 'GovukComponent::Tag',
18
- govuk_warning: 'GovukComponent::Warning',
3
+ govuk_accordion: 'GovukComponent::AccordionComponent',
4
+ govuk_back_link: 'GovukComponent::BackLinkComponent',
5
+ govuk_breadcrumbs: 'GovukComponent::BreadcrumbsComponent',
6
+ govuk_cookie_banner: 'GovukComponent::CookieBannerComponent',
7
+ govuk_details: 'GovukComponent::DetailsComponent',
8
+ govuk_footer: 'GovukComponent::FooterComponent',
9
+ govuk_header: 'GovukComponent::HeaderComponent',
10
+ govuk_inset_text: 'GovukComponent::InsetTextComponent',
11
+ govuk_notification_banner: 'GovukComponent::NotificationBannerComponent',
12
+ govuk_panel: 'GovukComponent::PanelComponent',
13
+ govuk_phase_banner: 'GovukComponent::PhaseBannerComponent',
14
+ govuk_start_now_button: 'GovukComponent::StartButtonComponent',
15
+ govuk_summary_list: 'GovukComponent::SummaryListComponent',
16
+ govuk_tabs: 'GovukComponent::TabComponent',
17
+ govuk_tag: 'GovukComponent::TagComponent',
18
+ govuk_warning_text: 'GovukComponent::WarningTextComponent',
19
19
  }.each do |name, klass|
20
20
  define_method(name) do |*args, **kwargs, &block|
21
21
  capture do