-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And use this for Rails `flash` messages, `render_message` helper and `Alchemy.growl` notices.
- Loading branch information
Showing
19 changed files
with
513 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Alchemy | ||
module Admin | ||
class Message < ViewComponent::Base | ||
attr_reader :message, :type, :dismissable | ||
|
||
erb_template <<~ERB | ||
<alchemy-message type="<%= type %>"<%= dismissable ? ' dismissable' : '' %>> | ||
<%= message || content %> | ||
</alchemy-message> | ||
ERB | ||
|
||
def initialize(message = nil, type: :info, dismissable: false) | ||
@message = message | ||
@dismissable = dismissable | ||
@type = type | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const DISMISS_DELAY = 5000 | ||
|
||
class Message extends HTMLElement { | ||
#message | ||
|
||
constructor() { | ||
super() | ||
this.#message = this.innerHTML | ||
if (this.dismissable || this.type === "error") { | ||
this.addEventListener("click", this) | ||
} | ||
} | ||
|
||
handleEvent(event) { | ||
if (event.type === "click") { | ||
this.dismiss() | ||
} | ||
} | ||
|
||
connectedCallback() { | ||
this.innerHTML = ` | ||
<alchemy-icon name="${this.iconName}"></alchemy-icon> | ||
${this.dismissable && this.type === "error" ? '<alchemy-icon name="close"></alchemy-icon>' : ""} | ||
${this.#message} | ||
` | ||
if (this.dismissable && this.type !== "error") { | ||
setTimeout(() => { | ||
this.dismiss() | ||
}, this.delay) | ||
} | ||
} | ||
|
||
dismiss() { | ||
this.addEventListener("transitionend", () => this.remove()) | ||
this.classList.add("dismissed") | ||
} | ||
|
||
get dismissable() { | ||
return this.hasAttribute("dismissable") | ||
} | ||
|
||
get type() { | ||
return this.getAttribute("type") || "notice" | ||
} | ||
|
||
get delay() { | ||
return parseInt(this.getAttribute("delay") || DISMISS_DELAY) | ||
} | ||
|
||
get iconName() { | ||
switch (this.type) { | ||
case "warning": | ||
case "warn": | ||
case "alert": | ||
return "alert" | ||
case "notice": | ||
return "check" | ||
case "info": | ||
case "hint": | ||
return "information" | ||
case "error": | ||
return "bug" | ||
default: | ||
return this.type | ||
} | ||
} | ||
} | ||
|
||
customElements.define("alchemy-message", Message) |
Oops, something went wrong.