Skip to content

Commit 024654c

Browse files
authored
Merge pull request #3532 from AlchemyCMS/longer-dismiss-delay
feat(Alerts): Allow to change auto dismiss delay
2 parents 223069b + 1ab2d8c commit 024654c

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

app/javascript/alchemy_admin/components/message.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Message extends HTMLElement {
2626
if (this.dismissable && this.type !== "error") {
2727
setTimeout(() => {
2828
this.dismiss()
29-
}, this.delay)
29+
}, this.dismissDelay)
3030
}
3131
}
3232

@@ -43,8 +43,10 @@ class Message extends HTMLElement {
4343
return this.getAttribute("type") || "notice"
4444
}
4545

46-
get delay() {
47-
return parseInt(this.getAttribute("delay") || DISMISS_DELAY)
46+
get dismissDelay() {
47+
return parseInt(
48+
this.noticesWrapper?.dataset.autoDismissDelay || DISMISS_DELAY
49+
)
4850
}
4951

5052
get iconName() {
@@ -64,6 +66,10 @@ class Message extends HTMLElement {
6466
return this.type
6567
}
6668
}
69+
70+
get noticesWrapper() {
71+
return this.closest("#flash_notices")
72+
}
6773
}
6874

6975
customElements.define("alchemy-message", Message)

app/views/alchemy/admin/partials/_flash_notices.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div id="flash_notices">
1+
<div id="flash_notices" data-auto-dismiss-delay="5000">
22
<% flash.keys.each do |flash_type| %>
33
<% if flash[flash_type.to_sym].present? %>
44
<%= render Alchemy::Admin::Message.new(flash[flash_type.to_sym], type: flash_type, dismissable: true) %>

spec/javascript/alchemy_admin/components/message.spec.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ describe("alchemy-message", () => {
2020
it("dismisses after delay", () => {
2121
return new Promise((resolve) => {
2222
const html = `
23-
<alchemy-message dismissable delay=10>
24-
A message
25-
</alchemy-message>
23+
<div id="flash_notices" data-auto-dismiss-delay="10">
24+
<alchemy-message dismissable>
25+
A message
26+
</alchemy-message>
27+
</div>
2628
`
2729
const component = renderComponent("alchemy-message", html)
2830
const spy = vi.spyOn(component, "dismiss")
@@ -36,9 +38,11 @@ describe("alchemy-message", () => {
3638
it("when type error, does not dismis after delay", () => {
3739
return new Promise((resolve) => {
3840
const html = `
39-
<alchemy-message dismissable type="error" delay=10>
40-
A message
41-
</alchemy-message>
41+
<div id="flash_notices" data-auto-dismiss-delay="10">
42+
<alchemy-message dismissable type="error">
43+
A message
44+
</alchemy-message>
45+
</div>
4246
`
4347
const component = renderComponent("alchemy-message", html)
4448
const spy = vi.spyOn(component, "dismiss")

0 commit comments

Comments
 (0)