Skip to content

Commit

Permalink
Move overlay into a web component
Browse files Browse the repository at this point in the history
The overlay is now a web component and a bit easier to use. The API is still the same, but the component is using the same mechanic as the Spinner.
  • Loading branch information
sascha-karnatz committed Sep 4, 2023
1 parent 7b7e564 commit b10fbe0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
5 changes: 3 additions & 2 deletions app/javascript/alchemy_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ $.fx.speeds._default = 400

// Web Components
import "alchemy_admin/components/char_counter"
import "alchemy_admin/components/tinymce"
import "alchemy_admin/components/tooltip"
import "alchemy_admin/components/datepicker"
import "alchemy_admin/components/overlay"
import "alchemy_admin/components/spinner"
import "alchemy_admin/components/tinymce"
import "alchemy_admin/components/tooltip"

// Global Alchemy object
if (typeof window.Alchemy === "undefined") {
Expand Down
22 changes: 22 additions & 0 deletions app/javascript/alchemy_admin/components/overlay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AlchemyHTMLElement } from "./alchemy_html_element"

class Overlay extends AlchemyHTMLElement {
static properties = {
show: { default: false },
text: { default: "" }
}

render() {
this.id = `overlay`
this.style.setProperty("display", this.show ? "block" : "none")

return `
<alchemy-spinner></alchemy-spinner>
<div id="overlay_text_box">
<span id="overlay_text">${this.text}</span>
</div>
`
}
}

customElements.define("alchemy-overlay", Overlay)
13 changes: 1 addition & 12 deletions app/javascript/alchemy_admin/please_wait_overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,5 @@
* @param {boolean,null} show
*/
export default function pleaseWaitOverlay(show) {
if (show == null) {
show = true
}
const $overlay = $("#overlay")
if (show) {
const spinner = new Alchemy.Spinner("medium")
spinner.spin($overlay)
$overlay.show()
} else {
$overlay.find(".spinner").remove()
$overlay.hide()
}
customElements.get("alchemy-overlay").show = !!show
}
6 changes: 1 addition & 5 deletions app/views/layouts/alchemy/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@
<h1><%= Alchemy.t(:javascript_disabled_headline) %></h1>
<p><%= Alchemy.t(:javascript_disabled_text) %></p>
</noscript>
<div id="overlay">
<div id="overlay_text_box">
<span id="overlay_text"><%= Alchemy.t(:please_wait) %></span>
</div>
</div>
<alchemy-overlay text="<%= Alchemy.t(:please_wait) %>"></alchemy-overlay>
<div id="left_menu">
<div id="main_navi">
<% sorted_alchemy_modules.each do |alchemy_module| %>
Expand Down
35 changes: 35 additions & 0 deletions spec/javascript/alchemy_admin/components/overlay.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import "../../../../app/javascript/alchemy_admin/components/overlay"

describe("alchemy-overlay", () => {
/**
* @type {HTMLElement | undefined}
*/
let overlay = undefined
let overlayText = undefined

const renderComponent = (html = `<alchemy-overlay></alchemy-overlay>`) => {
document.body.innerHTML = html
overlay = document.querySelector("alchemy-overlay")
overlayText = document.getElementById("overlay_text")
}

it("should render the overlay", () => {
renderComponent()
expect(overlayText).toBeTruthy()
})

it("should be hidden", () => {
renderComponent()
expect(overlay.style.getPropertyValue("display")).toBe("none")
})

it("should have a given text", () => {
renderComponent(`<alchemy-overlay text="Foo Bar"></alchemy-overlay>`)
expect(overlayText.textContent).toBe("Foo Bar")
})

it("should be visible", () => {
renderComponent(`<alchemy-overlay show="true"></alchemy-overlay>`)
expect(overlay.style.getPropertyValue("display")).toBe("block")
})
})

0 comments on commit b10fbe0

Please sign in to comment.