govuk-components 3.0.6 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29b10e73219c963307d21f99d72735e6a3c111864f539f854a7909959079a7a9
4
- data.tar.gz: 6d0f7e00ac6997453d35257300d6691ebe34522fb472198366e35a31fe2e4159
3
+ metadata.gz: e7d91723138110b84d128cc377b0c458ee7dca3714b77708581ffc2ce7c55e8e
4
+ data.tar.gz: 784446ce4999d80eefe5c176ce17d4e4952163cd460996b9530a01e6a51d17e1
5
5
  SHA512:
6
- metadata.gz: f04e75ad394385e5307995baee4389ef1e564a09511db958afb49996888fa21bba1bb5216e9e9af7f23baed47c5733f7afde289efa591ece0d388c7c87c37627
7
- data.tar.gz: 5671279d11164ea6d9acc834ee34feaada6f9d3ed9a3851ad3d73d56f2ace240c98ff7bbb7b65dd214f1e398b5c010d61770fc602448690e1958cfb86ef6542e
6
+ metadata.gz: 106c70d02e199030904967f1a814baa564d6c4397978d99b1afdbcbe80f8414d94589a83c39317c80a0fa75fa0841d835090f49b76b2597956f0838ec763824f
7
+ data.tar.gz: 5aa69c78b7879be82256aac6d6e7ba967de6179ae4ba9386501d4f31179a5b0b07cd2d16648e0c135d2cf32b5462725a2a7931433a4af08bb2133f9889dbef4d
data/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
  [![Gem](https://img.shields.io/gem/dt/govuk-components?logo=rubygems)](https://rubygems.org/gems/govuk-components)
7
7
  [![Test Coverage](https://api.codeclimate.com/v1/badges/cbcbc140f300b920d833/test_coverage)](https://codeclimate.com/github/DFE-Digital/govuk-components/test_coverage)
8
8
  [![GitHub license](https://img.shields.io/github/license/DFE-Digital/govuk-components)](https://github.com/DFE-Digital/govuk-components/blob/main/LICENSE)
9
- [![GOV.UK Design System Version](https://img.shields.io/badge/GOV.UK%20Design%20System-4.1.0-brightgreen)](https://design-system.service.gov.uk)
9
+ [![GOV.UK Design System Version](https://img.shields.io/badge/GOV.UK%20Design%20System-4.2.0-brightgreen)](https://design-system.service.gov.uk)
10
10
  [![Rails](https://img.shields.io/badge/Rails-6.1.5%20%E2%95%B1%207.0.3-E16D6D)](https://weblog.rubyonrails.org/releases/)
11
11
  [![Ruby](https://img.shields.io/badge/Ruby-2.7.5%20%20%E2%95%B1%203.0.3%20%20%E2%95%B1%203.1.0-E16D6D)](https://www.ruby-lang.org/en/downloads/)
12
12
 
@@ -4,6 +4,11 @@ class GovukComponent::Base < ViewComponent::Base
4
4
  attr_reader :html_attributes
5
5
 
6
6
  def initialize(classes:, html_attributes:)
7
+ if classes.nil?
8
+ Rails.logger.warn("classes is nil, if no custom classes are needed omit the param")
9
+
10
+ classes = []
11
+ end
7
12
  # FIXME: remove first merge when we deprecate classes
8
13
  #
9
14
  # This step only needs to be here while we still accept classes:, now
@@ -32,7 +32,7 @@
32
32
  <% if service_name.present? || navigation_items.present? %>
33
33
  <div class="govuk-header__content">
34
34
  <% if service_name.present? %>
35
- <%= link_to(service_name, service_url, class: %w(govuk-header__link govuk-header__link--service-name)) %>
35
+ <%= link_to(service_name, service_url, class: %w(govuk-header__link govuk-header__service-name)) %>
36
36
  <% end %>
37
37
 
38
38
  <% if navigation_items.any? %>
@@ -0,0 +1,60 @@
1
+ class GovukComponent::PaginationComponent::AdjacentPage < GovukComponent::Base
2
+ attr_reader :href, :label_text, :text, :suffix, :block_mode, :visually_hidden_text
3
+ alias_method :block_mode?, :block_mode
4
+
5
+ def initialize(href:, suffix:, visually_hidden_text:, text:, block_mode: true, label_text: nil, classes: [], html_attributes: {})
6
+ @href = href
7
+ @label_text = label_text
8
+ @text = text
9
+ @block_mode = block_mode
10
+ @suffix = suffix
11
+ @visually_hidden_text = visually_hidden_text
12
+
13
+ super(html_attributes: html_attributes, classes: classes)
14
+ end
15
+
16
+ def call
17
+ tag.div(**html_attributes) do
18
+ tag.a(href: href, class: %w(govuk-link govuk-pagination__link), rel: suffix, aria: { label: aria_label }) do
19
+ safe_join([body, divider, label_content])
20
+ end
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def default_attributes
27
+ { class: ["govuk-pagination__#{suffix}"] }
28
+ end
29
+
30
+ def body
31
+ [arrow, title_span]
32
+ end
33
+
34
+ def title_span
35
+ tag.span(text, class: title_classes)
36
+ end
37
+
38
+ def divider
39
+ return if label_text.blank?
40
+
41
+ tag.span(":", class: %w(govuk-visually-hidden))
42
+ end
43
+
44
+ def label_content
45
+ return if label_text.blank?
46
+
47
+ tag.span(label_text, class: label_classes)
48
+ end
49
+
50
+ def title_classes
51
+ class_names(
52
+ %(govuk-pagination__link-title),
53
+ %(govuk-pagination__link-title--decorated) => label_text.blank?
54
+ )
55
+ end
56
+
57
+ def label_classes
58
+ %w(govuk-pagination__link-label)
59
+ end
60
+ end
@@ -0,0 +1,77 @@
1
+ class GovukComponent::PaginationComponent::Item < GovukComponent::Base
2
+ attr_reader :number, :href, :visually_hidden_text, :mode
3
+
4
+ def initialize(number: nil, href: nil, current: false, ellipsis: false, from_pagy: false, visually_hidden_text: nil, classes: [], html_attributes: {})
5
+ @number = number
6
+ @href = href
7
+ @visually_hidden_text = visually_hidden_text
8
+
9
+ # We have three modes for rendering links:
10
+ #
11
+ # * link (a link to another page)
12
+ # * current (a link to the current page)
13
+ # * gap (an ellipsis symbol)
14
+ #
15
+ # Pagy sets these by object type:
16
+ # Integer = link
17
+ # String = current
18
+ # :gap = gap
19
+ #
20
+ # The original Nunjucks component has two boolean settings instead,
21
+ # ellipsis and current. When ellipsis is true all other arguments are
22
+ # ignored
23
+ @mode = from_pagy ? pagy_mode(number) : manual_mode(ellipsis, current)
24
+
25
+ super(classes: classes, html_attributes: html_attributes)
26
+ end
27
+
28
+ def call
29
+ case mode
30
+ when :link
31
+ link(current: false)
32
+ when :current
33
+ link(current: true)
34
+ when :gap
35
+ ellipsis_item
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def pagy_mode(number)
42
+ return :link if number.is_a?(Integer)
43
+ return :current if number.is_a?(String)
44
+
45
+ :gap
46
+ end
47
+
48
+ def manual_mode(ellipsis, current)
49
+ return :gap if ellipsis
50
+ return :current if current
51
+
52
+ :link
53
+ end
54
+
55
+ def link(current: false)
56
+ attributes = html_attributes.tap { |ha| ha[:class] << "govuk-pagination__item--current" if current }
57
+
58
+ tag.li(**attributes) do
59
+ tag.a(href: href, class: %w(govuk-link govuk-pagination__link)) { number.to_s }
60
+ end
61
+ end
62
+
63
+ def ellipsis_item
64
+ tag.li("⋯", class: %w(govuk-pagination__item govuk-pagination__item--ellipses))
65
+ end
66
+
67
+ def default_attributes
68
+ {
69
+ class: %w(govuk-pagination__item),
70
+ aria: { label: aria_label }
71
+ }
72
+ end
73
+
74
+ def aria_label
75
+ visually_hidden_text || "Page #{number}"
76
+ end
77
+ end
@@ -0,0 +1,32 @@
1
+ class GovukComponent::PaginationComponent::NextPage < GovukComponent::PaginationComponent::AdjacentPage
2
+ def initialize(href:, text:, label_text: nil, visually_hidden_text: nil, block_mode: true, classes: [], html_attributes: {})
3
+ super(
4
+ suffix: "next",
5
+ text: text,
6
+ href: href,
7
+ label_text: label_text,
8
+ block_mode: block_mode,
9
+ visually_hidden_text: visually_hidden_text,
10
+ classes: classes,
11
+ html_attributes: html_attributes
12
+ )
13
+ end
14
+
15
+ def body
16
+ return [arrow, title_span] if block_mode?
17
+
18
+ [title_span, arrow]
19
+ end
20
+
21
+ private
22
+
23
+ def aria_label
24
+ @visually_hidden_text || "Next page"
25
+ end
26
+
27
+ def arrow
28
+ tag.svg(class: "govuk-pagination__icon govuk-pagination__icon--next", xmlns: "http://www.w3.org/2000/svg", height: "13", width: "15", focusable: "false", viewBox: "0 0 15 13", aria: { hidden: "true" }) do
29
+ tag.path(d: "m8.107-0.0078125-1.4136 1.414 4.2926 4.293h-12.986v2h12.896l-4.1855 3.9766 1.377 1.4492 6.7441-6.4062-6.7246-6.7266z")
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ class GovukComponent::PaginationComponent::PreviousPage < GovukComponent::PaginationComponent::AdjacentPage
2
+ def initialize(href:, text:, label_text: nil, visually_hidden_text: nil, block_mode: true, classes: [], html_attributes: {})
3
+ super(
4
+ suffix: "prev",
5
+ text: text,
6
+ href: href,
7
+ label_text: label_text,
8
+ block_mode: block_mode,
9
+ visually_hidden_text: visually_hidden_text,
10
+ classes: classes,
11
+ html_attributes: html_attributes
12
+ )
13
+ end
14
+
15
+ private
16
+
17
+ def aria_label
18
+ @visually_hidden_text || "Previous page"
19
+ end
20
+
21
+ def arrow
22
+ tag.svg(class: "govuk-pagination__icon govuk-pagination__icon--prev", xmlns: "http://www.w3.org/2000/svg", height: "13", width: "15", focusable: "false", viewBox: "0 0 15 13", aria: { hidden: "true" }) do
23
+ tag.path(d: "m6.5938-0.0078125-6.7266 6.7266 6.7441 6.4062 1.377-1.449-4.1856-3.9768h12.896v-2h-12.984l4.2931-4.293-1.414-1.414z")
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,121 @@
1
+ class GovukComponent::PaginationComponent < GovukComponent::Base
2
+ include Pagy::UrlHelpers
3
+
4
+ attr_reader :pagy,
5
+ :next_text,
6
+ :previous_text,
7
+ :visually_hidden_next_text,
8
+ :visually_hidden_previous_text,
9
+ :page_items,
10
+ :previous_content,
11
+ :next_content,
12
+ :block_mode,
13
+ :landmark_label
14
+
15
+ alias_method :block_mode?, :block_mode
16
+
17
+ renders_many :items, "GovukComponent::PaginationComponent::Item"
18
+
19
+ renders_one :next_page, ->(href:, text: default_next_text, label_text: nil, visually_hidden_text: nil, classes: [], html_attributes: {}) do
20
+ GovukComponent::PaginationComponent::NextPage.new(
21
+ text: text,
22
+ href: href,
23
+ label_text: label_text,
24
+ block_mode: block_mode?,
25
+ visually_hidden_text: visually_hidden_text,
26
+ classes: classes,
27
+ html_attributes: html_attributes
28
+ )
29
+ end
30
+
31
+ renders_one :previous_page, ->(href:, text: default_previous_text, label_text: nil, visually_hidden_text: nil, classes: [], html_attributes: {}) do
32
+ GovukComponent::PaginationComponent::PreviousPage.new(
33
+ text: text,
34
+ href: href,
35
+ label_text: label_text,
36
+ block_mode: block_mode?,
37
+ visually_hidden_text: visually_hidden_text,
38
+ classes: classes,
39
+ html_attributes: html_attributes
40
+ )
41
+ end
42
+
43
+ def initialize(pagy: nil, next_text: nil, previous_text: nil, visually_hidden_next_text: nil, visually_hidden_previous_text: nil, block_mode: false, landmark_label: "results", classes: [], html_attributes: {})
44
+ @pagy = pagy
45
+ @next_text = next_text
46
+ @previous_text = previous_text
47
+ @visually_hidden_next_text = visually_hidden_next_text
48
+ @visually_hidden_previous_text = visually_hidden_previous_text
49
+ @block_mode = block_mode
50
+ @landmark_label = landmark_label
51
+
52
+ build_items if pagy.present?
53
+
54
+ super(classes: classes, html_attributes: html_attributes)
55
+ end
56
+
57
+ def before_render
58
+ @page_items = items || build_items
59
+ @previous_content = previous_page || build_previous
60
+ @next_content = next_page || build_next
61
+ end
62
+
63
+ def call
64
+ attributes = html_attributes.tap { |ha| (ha[:class] << "govuk-pagination--block") if items.empty? }
65
+
66
+ tag.nav(**attributes) do
67
+ safe_join([
68
+ previous_content,
69
+ tag.ul(class: "govuk-pagination__list") { safe_join(page_items) },
70
+ next_content
71
+ ])
72
+ end
73
+ end
74
+
75
+ def render?
76
+ # probably isn't any point rendering if there's only one page
77
+ (pagy.present? && pagy.series.size > 1) || @previous_content.present? || @next_content.present?
78
+ end
79
+
80
+ private
81
+
82
+ def default_attributes
83
+ { role: "navigation", aria: { label: landmark_label }, class: %w(govuk-pagination) }
84
+ end
85
+
86
+ def build_previous
87
+ return unless pagy&.prev
88
+
89
+ kwargs = {
90
+ href: pagy_url_for(pagy, pagy.prev),
91
+ text: @previous_text,
92
+ visually_hidden_text: visually_hidden_previous_text,
93
+ }
94
+
95
+ previous_page(**kwargs.compact)
96
+ end
97
+
98
+ def build_next
99
+ return unless pagy&.next
100
+
101
+ kwargs = {
102
+ href: pagy_url_for(pagy, pagy.next),
103
+ text: @next_text,
104
+ visually_hidden_text: visually_hidden_next_text,
105
+ }
106
+
107
+ next_page(**kwargs.compact)
108
+ end
109
+
110
+ def build_items
111
+ pagy.series.map { |i| item(number: i, href: pagy_url_for(pagy, i), from_pagy: true) }
112
+ end
113
+
114
+ def default_next_text
115
+ safe_join(["Next", tag.span(class: "govuk-visually-hidden") { " page" }])
116
+ end
117
+
118
+ def default_previous_text
119
+ safe_join(["Previous", tag.span(class: "govuk-visually-hidden") { " page" }])
120
+ end
121
+ end
@@ -9,6 +9,7 @@ module GovukComponentsHelper
9
9
  govuk_header: 'GovukComponent::HeaderComponent',
10
10
  govuk_inset_text: 'GovukComponent::InsetTextComponent',
11
11
  govuk_notification_banner: 'GovukComponent::NotificationBannerComponent',
12
+ govuk_pagination: 'GovukComponent::PaginationComponent',
12
13
  govuk_panel: 'GovukComponent::PanelComponent',
13
14
  govuk_phase_banner: 'GovukComponent::PhaseBannerComponent',
14
15
  govuk_section_break: 'GovukComponent::SectionBreakComponent',
@@ -1,5 +1,5 @@
1
1
  module Govuk
2
2
  module Components
3
- VERSION = '3.0.6'.freeze
3
+ VERSION = '3.1.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk-components
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.6
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DfE developers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-09 00:00:00.000000000 Z
11
+ date: 2022-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: activemodel
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,28 +58,34 @@ dependencies:
44
58
  requirements:
45
59
  - - "~>"
46
60
  - !ruby/object:Gem::Version
47
- version: 0.9.0
61
+ version: '0.9'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 0.9.2
48
65
  type: :runtime
49
66
  prerelease: false
50
67
  version_requirements: !ruby/object:Gem::Requirement
51
68
  requirements:
52
69
  - - "~>"
53
70
  - !ruby/object:Gem::Version
54
- version: 0.9.0
71
+ version: '0.9'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 0.9.2
55
75
  - !ruby/object:Gem::Dependency
56
76
  name: view_component
57
77
  requirement: !ruby/object:Gem::Requirement
58
78
  requirements:
59
79
  - - "~>"
60
80
  - !ruby/object:Gem::Version
61
- version: 2.49.1
81
+ version: 2.56.2
62
82
  type: :runtime
63
83
  prerelease: false
64
84
  version_requirements: !ruby/object:Gem::Requirement
65
85
  requirements:
66
86
  - - "~>"
67
87
  - !ruby/object:Gem::Version
68
- version: 2.49.1
88
+ version: 2.56.2
69
89
  - !ruby/object:Gem::Dependency
70
90
  name: deep_merge
71
91
  requirement: !ruby/object:Gem::Requirement
@@ -206,6 +226,20 @@ dependencies:
206
226
  - - "~>"
207
227
  - !ruby/object:Gem::Version
208
228
  version: '4.11'
229
+ - !ruby/object:Gem::Dependency
230
+ name: pagy
231
+ requirement: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - "~>"
234
+ - !ruby/object:Gem::Version
235
+ version: 5.10.1
236
+ type: :development
237
+ prerelease: false
238
+ version_requirements: !ruby/object:Gem::Requirement
239
+ requirements:
240
+ - - "~>"
241
+ - !ruby/object:Gem::Version
242
+ version: 5.10.1
209
243
  - !ruby/object:Gem::Dependency
210
244
  name: redcarpet
211
245
  requirement: !ruby/object:Gem::Requirement
@@ -348,6 +382,11 @@ files:
348
382
  - app/components/govuk_component/inset_text_component.rb
349
383
  - app/components/govuk_component/notification_banner_component.html.erb
350
384
  - app/components/govuk_component/notification_banner_component.rb
385
+ - app/components/govuk_component/pagination_component.rb
386
+ - app/components/govuk_component/pagination_component/adjacent_page.rb
387
+ - app/components/govuk_component/pagination_component/item.rb
388
+ - app/components/govuk_component/pagination_component/next_page.rb
389
+ - app/components/govuk_component/pagination_component/previous_page.rb
351
390
  - app/components/govuk_component/panel_component.rb
352
391
  - app/components/govuk_component/phase_banner_component.html.erb
353
392
  - app/components/govuk_component/phase_banner_component.rb