Skip to content

Commit

Permalink
feat[#50499]: Add back button and breadcrumbs to page header
Browse files Browse the repository at this point in the history
  • Loading branch information
dominic-braeunlein committed Oct 26, 2023
1 parent 1403c34 commit fcb3934
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-bananas-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openproject/primer-view-components': minor
---

Add back button and breadcrumbs support to PageHeader
2 changes: 2 additions & 0 deletions app/components/primer/open_project/page_header.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<%= render Primer::BaseComponent.new(**@system_arguments) do %>
<%= breadcrumbs %>
<%= back_button %>
<%= title %>
<%= description %>
<%= actions %>
Expand Down
10 changes: 10 additions & 0 deletions app/components/primer/open_project/page_header.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@
margin-top: var(--base-size-4);
}
}

.PageHeader-breadcrumbs {
display: block;
width: 100%;
margin-bottom: var(--base-size-8);
}

.PageHeader-back_button {
margin-right: var(--base-size-4);
}
48 changes: 48 additions & 0 deletions app/components/primer/open_project/page_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,44 @@ class PageHeader < Primer::Component
Primer::BaseComponent.new(**system_arguments)
}

# Optional backbutton prepend the title
renders_one :back_button, lambda { |**system_arguments|
deny_tag_argument(**system_arguments)
system_arguments[:tag] = :a
system_arguments[:icon] = "arrow-left"
system_arguments[:scheme] = :invisible
system_arguments[:size] = :medium
system_arguments[:aria] = { label: I18n.t("button_back") }
system_arguments[:classes] = class_names(system_arguments[:classes], "PageHeader-back_button")

Primer::Beta::IconButton.new(**system_arguments)
}

# Optional breadcrumbs above the title row
# Items can be an array of string, hash {href, text} or an anchor tag string
renders_one :breadcrumbs, lambda { |items, show_breadcrumb: true|
return if !show_breadcrumb

Check failure on line 69 in app/components/primer/open_project/page_header.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/NegatedIf: Favor `unless` over `if` for negative conditions.

render(Primer::Beta::Breadcrumbs.new(classes: ["PageHeader-breadcrumbs"] )) do |breadcrumbs|

Check failure on line 71 in app/components/primer/open_project/page_header.rb

View workflow job for this annotation

GitHub Actions / rubocop

Layout/SpaceInsideParens: Space inside parentheses detected.
items.each do |item|

Check failure on line 73 in app/components/primer/open_project/page_header.rb

View workflow job for this annotation

GitHub Actions / rubocop

Layout/EmptyLinesAroundBlockBody: Extra empty line detected at block body beginning.
# transform anchor tag strings to {href, text} objects
# e.g "\u003ca href=\"/admin\"\u003eAdministration\u003c/a\u003e"
if (item.is_a?(String) && item.start_with?("\u003c"))

Check failure on line 76 in app/components/primer/open_project/page_header.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/IfUnlessModifier: Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`.

Check failure on line 76 in app/components/primer/open_project/page_header.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/ParenthesesAroundCondition: Don't use parentheses around the condition of an `if`.
item = transformLinkHtmlStringToObject(item)
end

case item
when String
breadcrumbs.with_item(href: '#') { item }

Check failure on line 82 in app/components/primer/open_project/page_header.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
when Hash
breadcrumbs.with_item(href: item[:href]) { item[:text] }
end
end
end
}


Check failure on line 90 in app/components/primer/open_project/page_header.rb

View workflow job for this annotation

GitHub Actions / rubocop

Layout/EmptyLines: Extra blank line detected.
def initialize(**system_arguments)
@system_arguments = deny_tag_argument(**system_arguments)

Expand All @@ -64,6 +102,16 @@ def initialize(**system_arguments)
def render?
title?
end

private

def transformLinkHtmlStringToObject(html_string)

Check failure on line 108 in app/components/primer/open_project/page_header.rb

View workflow job for this annotation

GitHub Actions / rubocop

Naming/MethodName: Use snake_case for method names.
# Parse the HTML
doc = Nokogiri::HTML.fragment(html_string)
# Extract href and text
anchor = doc.at('a')

Check failure on line 112 in app/components/primer/open_project/page_header.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
{ href: anchor['href'], text: anchor.text }

Check failure on line 113 in app/components/primer/open_project/page_header.rb

View workflow job for this annotation

GitHub Actions / rubocop

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
end
end
end
end
36 changes: 36 additions & 0 deletions previews/primer/open_project/page_header_preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,42 @@ def large_title
end
end

# @label Back button
def back_button
render(Primer::OpenProject::PageHeader.new) do |header|
header.with_title() { "Hello" }
header.with_back_button(href: "#")
end
end

# @label Back button and breadcrumbs
def back_button_and_breadcrumbs
breadcrumb_items = [
{href: "/root", text: "Root"},
"\u003ca href=\"/root/sub\"\u003eSub\u003c/a\u003e" ,
"test"
]
render(Primer::OpenProject::PageHeader.new) do |header|
header.with_title() { "Hello" }
header.with_back_button(href: "#")
header.with_breadcrumbs(breadcrumb_items)
end
end

# @label Breadcrumbs don't show breadcrumb
def back_button_and_no_breadcrumbs
breadcrumb_items = [
"test1",
"test2" ,
"test3"
]
render(Primer::OpenProject::PageHeader.new) do |header|
header.with_title() { "Hello" }
header.with_back_button(href: "#")
header.with_breadcrumbs(breadcrumb_items, show_breadcrumb: false)
end
end

# @label With actions
def actions
render_with_template(locals: {})
Expand Down
6 changes: 6 additions & 0 deletions static/classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,12 @@
"PageHeader-actions": [
"Primer::OpenProject::PageHeader"
],
"PageHeader-back_button": [
"Primer::OpenProject::PageHeader"
],
"PageHeader-breadcrumbs": [
"Primer::OpenProject::PageHeader"
],
"PageHeader-description": [
"Primer::OpenProject::PageHeader"
],
Expand Down

0 comments on commit fcb3934

Please sign in to comment.