Skip to content

Commit 5be0a0f

Browse files
authoredAug 26, 2024··
Fire an event when banners are dismissed (#3026)
1 parent 50d8e7a commit 5be0a0f

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed
 

‎.changeset/chatty-beds-double.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/view-components': minor
3+
---
4+
5+
Fire an event when banners are dismissed

‎app/components/primer/alpha/banner.rb

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ module Primer
44
module Alpha
55
# Use `Banner` to highlight important information.
66
#
7+
# ### Events
8+
#
9+
# |Name |Type |Bubbles |Cancelable |
10+
# |:---------|:-------------------|:-------|:----------|
11+
# |`dismiss` |`CustomEvent<void>` |No |No |
12+
#
713
# @accessibility
814
# ### Improve discoverability with a heading and landmark
915
# Banners are made visually prominent with icons and colors to immediately draw attention.

‎app/components/primer/alpha/x_banner.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import {controller, target} from '@github/catalyst'
22

3+
declare global {
4+
interface HTMLElementEventMap {
5+
'banner:dismiss': CustomEvent<void>
6+
}
7+
}
8+
39
@controller
410
class XBannerElement extends HTMLElement {
511
@target titleText: HTMLElement
612

713
dismiss() {
8-
const parentElement = this.parentElement
9-
if (!parentElement) return
10-
1114
if (this.#dismissScheme === 'remove') {
15+
const parentElement = this.parentElement
16+
if (!parentElement) return
17+
1218
parentElement.removeChild(this)
1319
} else {
1420
this.hide()
1521
}
22+
23+
this.dispatchEvent(new CustomEvent('banner:dismiss'))
1624
}
1725

1826
show() {

‎test/system/alpha/banner_test.rb

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
module Alpha
66
class IntegrationBannerTest < System::TestCase
7+
include Primer::JsTestHelpers
8+
79
def test_dismiss_button_remove
810
visit_preview(:playground, dismiss_scheme: :remove)
911
assert_selector(".Banner")
@@ -21,5 +23,21 @@ def test_dismiss_button_hide
2123

2224
assert_selector(".Banner", visible: :hidden)
2325
end
26+
27+
def test_dismiss_fires_event
28+
visit_preview(:playground, dismiss_scheme: :hide)
29+
30+
evaluate_multiline_script(<<~JS)
31+
window.bannerDismissed = false
32+
33+
document.querySelector('x-banner').addEventListener('banner:dismiss', () => {
34+
window.bannerDismissed = true
35+
})
36+
JS
37+
38+
find(".Banner-close button").click
39+
40+
assert page.evaluate_script("window.bannerDismissed")
41+
end
2442
end
2543
end

0 commit comments

Comments
 (0)
Please sign in to comment.