Skip to content

Commit

Permalink
Fire an event when banners are dismissed (#3026)
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron committed Aug 26, 2024
1 parent 50d8e7a commit 5be0a0f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-beds-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/view-components': minor
---

Fire an event when banners are dismissed
6 changes: 6 additions & 0 deletions app/components/primer/alpha/banner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ module Primer
module Alpha
# Use `Banner` to highlight important information.
#
# ### Events
#
# |Name |Type |Bubbles |Cancelable |
# |:---------|:-------------------|:-------|:----------|
# |`dismiss` |`CustomEvent<void>` |No |No |
#
# @accessibility
# ### Improve discoverability with a heading and landmark
# Banners are made visually prominent with icons and colors to immediately draw attention.
Expand Down
14 changes: 11 additions & 3 deletions app/components/primer/alpha/x_banner.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import {controller, target} from '@github/catalyst'

declare global {
interface HTMLElementEventMap {
'banner:dismiss': CustomEvent<void>
}
}

@controller
class XBannerElement extends HTMLElement {
@target titleText: HTMLElement

dismiss() {
const parentElement = this.parentElement
if (!parentElement) return

if (this.#dismissScheme === 'remove') {
const parentElement = this.parentElement
if (!parentElement) return

parentElement.removeChild(this)
} else {
this.hide()
}

this.dispatchEvent(new CustomEvent('banner:dismiss'))
}

show() {
Expand Down
18 changes: 18 additions & 0 deletions test/system/alpha/banner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

module Alpha
class IntegrationBannerTest < System::TestCase
include Primer::JsTestHelpers

def test_dismiss_button_remove
visit_preview(:playground, dismiss_scheme: :remove)
assert_selector(".Banner")
Expand All @@ -21,5 +23,21 @@ def test_dismiss_button_hide

assert_selector(".Banner", visible: :hidden)
end

def test_dismiss_fires_event
visit_preview(:playground, dismiss_scheme: :hide)

evaluate_multiline_script(<<~JS)
window.bannerDismissed = false
document.querySelector('x-banner').addEventListener('banner:dismiss', () => {
window.bannerDismissed = true
})
JS

find(".Banner-close button").click

assert page.evaluate_script("window.bannerDismissed")
end
end
end

0 comments on commit 5be0a0f

Please sign in to comment.