Skip to content

Commit 8274ec8

Browse files
authored
Merge pull request #1607 from projectblacklight/hierarchy-buttons
Extract HierarchyToggleComponent and related icons
2 parents 727e9c5 + a9151b8 commit 8274ec8

File tree

8 files changed

+148
-23
lines changed

8 files changed

+148
-23
lines changed

app/assets/stylesheets/arclight/modules/hierarchy_and_online_contents.scss

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,60 @@
11
:root {
22
--al-toggle-icon-color: #{$navbar-light-color};
3-
/* Font Awesome Free 6.5.2 by @fontawesome - https: //fontawesome.com License - https://fontawesome.com/license/free (CC BY 4.0 License) Copyright 2024 Fonticons, Inc. */
4-
--al-hierarchy-view-expand-icon: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 475 512'><path stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' stroke='rgb(0, 0, 0)' fill='rgb(0, 0, 0)' d='M64 32C28.7 32 0 60.7 0 96V416c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V96c0-35.3-28.7-64-64-64H64zM200 344V280H136c-13.3 0-24-10.7-24-24s10.7-24 24-24h64V168c0-13.3 10.7-24 24-24s24 10.7 24 24v64h64c13.3 0 24 10.7 24 24s-10.7 24-24 24H248v64c0 13.3-10.7 24-24 24s-24-10.7-24-24z'/></svg>");
5-
--al-hierarchy-view-collapse-icon: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 475 512'><path stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' stroke='rgb(0, 0, 0)' fill='rgb(0, 0, 0)' d='M64 32C28.7 32 0 60.7 0 96V416c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V96c0-35.3-28.7-64-64-64H64zm88 200H296c13.3 0 24 10.7 24 24s-10.7 24-24 24H152c-13.3 0-24-10.7-24-24s10.7-24 24-24z'/></svg>");
63
}
74

85
// Collapse +/- indicators
96
.al-toggle-view-children {
10-
background-color: var(--al-toggle-icon-color);
7+
display: flex;
8+
align-items: center;
9+
justify-content: center;
1110
flex-shrink: 0;
1211
height: 1.25rem;
1312
margin-left: -1.25rem;
1413
margin-right: 0.25 * $spacer;
1514
margin-top: 0.1 * $spacer;
16-
mask: var(--al-hierarchy-view-expand-icon);
1715
width: 1.1rem;
1816

17+
svg {
18+
fill: var(--al-toggle-icon-color);
19+
}
20+
21+
.icon-minus {
22+
display: none;
23+
}
1924
&:not(.collapsed) {
20-
mask: var(--al-hierarchy-view-collapse-icon);
25+
.icon-plus {
26+
display: none;
27+
}
28+
.icon-minus {
29+
display: inline;
30+
}
31+
}
32+
&.collapsed {
33+
.icon-plus {
34+
display: inline;
35+
}
36+
.icon-minus {
37+
display: none;
38+
}
2139
}
2240
}
2341

2442
.al-hierarchy-highlight > .documentHeader {
2543
background: $mark-bg;
2644
}
2745

46+
ul.documents {
47+
list-style: none;
48+
li {
49+
list-style: none;
50+
}
51+
}
52+
2853
#collection-context {
54+
list-style: none;
55+
2956
ul {
3057
list-style: none;
31-
3258
padding-left: 1.5rem;
3359

3460
ul {
@@ -68,6 +94,7 @@
6894

6995
li {
7096
padding: 0.25 * $spacer 0.5 * $spacer;
97+
list-style: none;
7198
}
7299

73100
li:nth-of-type(odd) {

app/components/arclight/document_collection_hierarchy_component.html.erb

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,7 @@
88
itemtype: @document.itemtype,
99
class: (classes.flatten + ['al-collection-context', ('al-hierarchy-highlight' if current_target?)].compact).join(' ') do %>
1010
<div class="documentHeader" data-document-id="<%= document.id %>">
11-
<% if document.children? %>
12-
<%= link_to('',
13-
"#collapsible-hierarchy-#{document.id}",
14-
class: "al-toggle-view-children#{ ' collapsed' unless show_expanded?}",
15-
aria: {
16-
label: t('arclight.hierarchy.view_all'),
17-
expanded: show_expanded?
18-
},
19-
data: {
20-
bs_toggle: 'collapse',
21-
toggle: 'collapse'
22-
}
23-
)
24-
%>
25-
<% end %>
11+
<%= render(Arclight::HierarchyToggleComponent.new(document: document, expanded: show_expanded?)) %>
2612
<div class="index_title document-title-heading" data-turbo="false">
2713
<% if current_target? %>
2814
<%= document.normalized_title %>

app/components/arclight/expand_hierarchy_button_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
module Arclight
4-
# Component for rendering an expand button in the hierarchy view
4+
# Component for rendering an expand button inside the hierarchy view
55
class ExpandHierarchyButtonComponent < Blacklight::Component
66
def initialize(path:, classes: 'btn btn-secondary btn-sm')
77
super
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<%= link_to(
2+
"#collapsible-hierarchy-#{document.id}",
3+
class: "al-toggle-view-children#{' collapsed' unless expanded }",
4+
aria: {
5+
label: t('arclight.hierarchy.view_all'),
6+
expanded: expanded
7+
},
8+
data: {
9+
bs_toggle: 'collapse',
10+
toggle: 'collapse'
11+
}
12+
) do %>
13+
<span class="al-toggle-view-icon" aria-hidden="true">
14+
<%= render Blacklight::Icons::PlusComponent.new(classes: 'icon-plus') %>
15+
<%= render Blacklight::Icons::MinusComponent.new(classes: 'icon-minus') %>
16+
</span>
17+
<% end %>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module Arclight
4+
# Component for rendering the plus/minus icons at each level of expandable hierarchy components
5+
class HierarchyToggleComponent < ViewComponent::Base
6+
attr_reader :document, :expanded
7+
8+
def initialize(document:, expanded:)
9+
@document = document
10+
@expanded = expanded
11+
super
12+
end
13+
14+
delegate :blacklight_icon, to: :helpers
15+
16+
def render?
17+
@document.children?
18+
end
19+
end
20+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module Blacklight
4+
module Icons
5+
# The minus icon
6+
class MinusComponent < Blacklight::Icons::IconComponent
7+
self.svg = <<~SVG
8+
<svg xmlns='http://www.w3.org/2000/svg' fill="currentColor" viewBox='0 0 475 512'><path stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' stroke='rgb(0, 0, 0)' d='M64 32C28.7 32 0 60.7 0 96V416c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V96c0-35.3-28.7-64-64-64H64zm88 200H296c13.3 0 24 10.7 24 24s-10.7 24-24 24H152c-13.3 0-24-10.7-24-24s10.7-24 24-24z'/></svg>
9+
SVG
10+
end
11+
end
12+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module Blacklight
4+
module Icons
5+
# The plus icon
6+
class PlusComponent < Blacklight::Icons::IconComponent
7+
self.svg = <<~SVG
8+
<svg xmlns='http://www.w3.org/2000/svg' fill="currentColor" viewBox='0 0 475 512'><path stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' stroke='rgb(0, 0, 0)' d='M64 32C28.7 32 0 60.7 0 96V416c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V96c0-35.3-28.7-64-64-64H64zM200 344V280H136c-13.3 0-24-10.7-24-24s10.7-24 24-24h64V168c0-13.3 10.7-24 24-24s24 10.7 24 24v64h64c13.3 0 24 10.7 24 24s-10.7 24-24 24H248v64c0 13.3-10.7 24-24 24s-24-10.7-24-24z'/></svg>
9+
SVG
10+
end
11+
end
12+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe Arclight::HierarchyToggleComponent, type: :component do
6+
let(:document) do
7+
instance_double(SolrDocument, id: 'abc123', children?: has_children)
8+
end
9+
10+
let(:rendered) do
11+
render_inline(described_class.new(document: document, expanded: expanded))
12+
end
13+
14+
context 'when the document has children' do
15+
let(:has_children) { true }
16+
17+
context 'when expanded is true' do
18+
let(:expanded) { true }
19+
20+
it 'does not include the .collapsed class' do
21+
expect(rendered.to_html).to include('class="al-toggle-view-children"')
22+
expect(rendered.to_html).not_to include('collapsed')
23+
end
24+
25+
it 'renders the link with aria-expanded=true' do
26+
expect(rendered.to_html).to include('aria-expanded="true"')
27+
end
28+
end
29+
30+
context 'when expanded is false' do
31+
let(:expanded) { false }
32+
33+
it 'includes the .collapsed class' do
34+
expect(rendered.to_html).to include('class="al-toggle-view-children collapsed"')
35+
end
36+
37+
it 'renders the link with aria-expanded=false' do
38+
expect(rendered.to_html).to include('aria-expanded="false"')
39+
end
40+
end
41+
end
42+
43+
context 'when the document has no children' do
44+
let(:has_children) { false }
45+
let(:expanded) { false }
46+
47+
it 'does not render the component' do
48+
expect(rendered.to_html).to be_empty
49+
end
50+
end
51+
end

0 commit comments

Comments
 (0)