Skip to content

Commit

Permalink
Persist Elements Window width in session
Browse files Browse the repository at this point in the history
Makes sure the elements window width is the same across pages.
Uses a cookie, so we can read the value from Rails and put it on
the body tag, so it is immediatly available on page render.
Otherwise we would see the element window resizing on every
page visit. SSR ftw \o/
  • Loading branch information
tvdeyen committed Nov 29, 2024
1 parent 5e510e5 commit 44d3a39
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 17 additions & 1 deletion app/javascript/alchemy_admin/components/elements_window.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ElementsWindow extends HTMLElement {
?.trigger("FocusElementEditor.Alchemy")
}
SortableElements()
this.resize()
}

collapseAllElements() {
Expand All @@ -39,6 +40,7 @@ class ElementsWindow extends HTMLElement {
this.toggleButton
.querySelector("alchemy-icon")
.setAttribute("name", "menu-unfold")
this.resize()
}

hide() {
Expand All @@ -52,7 +54,14 @@ class ElementsWindow extends HTMLElement {
}

resize(width) {
document.body.style.setProperty("--elements-window-width", `${width}px`)
if (width === undefined) {
width = this.widthFromCookie
}

if (width) {
document.body.style.setProperty("--elements-window-width", `${width}px`)
document.cookie = `alchemy-elements-window-width=${width}; SameSite=Lax; Path=/;`
}
}

get collapseButton() {
Expand All @@ -74,6 +83,13 @@ class ElementsWindow extends HTMLElement {
return this.#turboFrame
}

get widthFromCookie() {
return document.cookie
.split("; ")
.find((row) => row.startsWith("alchemy-elements-window-width="))
?.split("=")[1]
}

set isDragged(dragged) {
this.turboFrame.style.transitionProperty = dragged ? "none" : null
this.turboFrame.style.pointerEvents = dragged ? "none" : null
Expand Down
4 changes: 3 additions & 1 deletion app/views/layouts/alchemy/admin.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
<% end %>
<%= yield :javascript_includes %>
</head>
<%= content_tag :body, id: 'alchemy', class: alchemy_body_class + ["alchemy-light"] do %>
<%= content_tag :body, id: 'alchemy',
class: alchemy_body_class + ["alchemy-light"],
style: cookies["alchemy-elements-window-width"] && "--elements-window-width: #{cookies["alchemy-elements-window-width"]}px" do %>
<noscript>
<h1><%= Alchemy.t(:javascript_disabled_headline) %></h1>
<p><%= Alchemy.t(:javascript_disabled_text) %></p>
Expand Down

0 comments on commit 44d3a39

Please sign in to comment.