forked from primer/view_components
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollapsible_section.rb
More file actions
89 lines (74 loc) · 3.22 KB
/
Copy pathcollapsible_section.rb
File metadata and controls
89 lines (74 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true
module Primer
module OpenProject
# A component consisting of a title and collapsible content.
# Clicking the title will hide the collapsible content
class CollapsibleSection < Primer::Component
status :open_project
TITLE_TAG_FALLBACK = :h2
TITLE_TAG_OPTIONS = [:h1, TITLE_TAG_FALLBACK, :h3, :h4, :h5, :h6, :span].freeze
# Required Title
#
# @param tag [Symbol] Customize the element type of the rendered title container.
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
renders_one :title, lambda { |tag: TITLE_TAG_FALLBACK, **system_arguments, &block|
system_arguments[:tag] = fetch_or_fallback(TITLE_TAG_OPTIONS, tag, TITLE_TAG_FALLBACK)
system_arguments[:font_size] ||= 3
system_arguments[:mr] ||= 2
Primer::OpenProject::Heading.new(**system_arguments, &block)
}
# Optional caption
#
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
renders_one :caption, lambda { |**system_arguments, &block|
system_arguments[:color] ||= :subtle
system_arguments[:mr] ||= 2
system_arguments[:display] ||= [:none, :block]
Primer::Beta::Text.new(**system_arguments, &block)
}
# Optional right-side content
#
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
renders_one :additional_information, lambda { |**system_arguments|
Primer::BaseComponent.new(tag: :div, **system_arguments)
}
# Required collapsible content
#
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
renders_one :collapsible_content, lambda { |**system_arguments|
Primer::BaseComponent.new(tag: :div, **system_arguments)
}
# @param id [String] The unique ID of the collapsible section.
# @param collapsed [Boolean] Whether the section is collapsed on initial render.
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
def initialize(id: self.class.generate_id, collapsed: false, **system_arguments)
@title_id = "#{id}-title"
@content_id = "#{id}-content"
@heading_id = "#{id}-heading"
@collapsed = collapsed
@system_arguments = deny_tag_argument(**system_arguments)
@system_arguments[:tag] = "collapsible-section"
@system_arguments[:id] = id
@system_arguments[:classes] = class_names(
@system_arguments[:classes],
"CollapsibleSection",
"CollapsibleSection--collapsed" => @collapsed
)
@system_arguments[:data] ||= {}
@system_arguments[:data][:collapsed] = true if @collapsed
@toggle_button_arguments = {
scheme: :invisible,
type: :button,
data: { target: "collapsible-section.triggerElement", "collapsible-toggle": "" },
aria: { expanded: !@collapsed, controls: @content_id, labelledby: @heading_id }
}
end
private
def render?
raise ArgumentError, "Title must be present" unless title.present?
raise ArgumentError, "Collapsible content must be present" unless collapsible_content.present?
true
end
end
end
end