Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Banner to be rendered as a section #2541

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nine-turtles-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/view-components": patch
---

Allow `Banner` to be rendered as a section
10 changes: 7 additions & 3 deletions app/components/primer/alpha/banner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class Banner < Primer::Component
}
}

DEFAULT_TAG = :div
TAG_OPTIONS = [DEFAULT_TAG, :section].freeze

DEFAULT_SCHEME = :default
SCHEME_MAPPINGS = {
DEFAULT_SCHEME => "",
Expand Down Expand Up @@ -57,6 +60,7 @@ class Banner < Primer::Component

DEFAULT_DISMISS_LABEL = "Dismiss"

# @param tag [Symbol] <%= one_of(Primer::Alpha::Banner::TAG_OPTIONS) %>
# @param full [Boolean] Whether the component should take up the full width of the screen.
# @param full_when_narrow [Boolean] Whether the component should take up the full width of the screen when rendered inside smaller viewports.
# @param dismiss_scheme [Symbol] Whether the component can be dismissed with an "x" button. <%= one_of(Primer::Alpha::Banner::DISMISS_SCHEMES) %>
Expand All @@ -65,15 +69,15 @@ class Banner < Primer::Component
# @param icon [Symbol] The name of an <%= link_to_octicons %> icon to use. If no icon is provided, a default one will be chosen based on the scheme.
# @param scheme [Symbol] <%= one_of(Primer::Alpha::Banner::SCHEME_MAPPINGS.keys) %>
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
def initialize(full: false, full_when_narrow: false, dismiss_scheme: DEFAULT_DISMISS_SCHEME, dismiss_label: DEFAULT_DISMISS_LABEL, description: nil, icon: nil, scheme: DEFAULT_SCHEME, **system_arguments)
def initialize(tag: DEFAULT_TAG, full: false, full_when_narrow: false, dismiss_scheme: DEFAULT_DISMISS_SCHEME, dismiss_label: DEFAULT_DISMISS_LABEL, description: nil, icon: nil, scheme: DEFAULT_SCHEME, **system_arguments)
@scheme = fetch_or_fallback(SCHEME_MAPPINGS.keys, scheme, DEFAULT_SCHEME)
@icon = icon || DEFAULT_ICONS[@scheme]
@dismiss_scheme = dismiss_scheme
@dismiss_label = dismiss_label
@description = description

@system_arguments = deny_tag_argument(**system_arguments)
@system_arguments[:tag] = :div
@system_arguments = system_arguments
@system_arguments[:tag] = fetch_or_fallback(TAG_OPTIONS, tag, DEFAULT_TAG)
@system_arguments[:classes] = class_names(
@system_arguments[:classes],
"Banner",
Expand Down
12 changes: 12 additions & 0 deletions test/components/alpha/banner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ def test_renders
assert_selector(".octicon")
end

def test_default_renders_as_div
render_inline(Primer::Alpha::Banner.new) { "foo" }

assert_selector("div.Banner")
end

def test_renders_as_section
render_inline(Primer::Alpha::Banner.new(tag: :section)) { "foo" }

assert_selector("section.Banner")
end

def test_includes_legacy_classes
render_inline(Primer::Alpha::Banner.new) { "foo" }

Expand Down
Loading