sync editor state with URL for browser back support
Some checks failed
deploy / deploy (push) Has been cancelled

- add ?edit=page/theme/settings param when opening editor or switching tabs
- remove ?edit param when closing editor
- restore .themed class on shop_root for editor panel background tokens
- collapse editor when navigating back to URL without ?edit param

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
jamey 2026-03-09 21:59:48 +00:00
parent 2c3c5050f4
commit 94c6137ab1
2 changed files with 34 additions and 4 deletions

View File

@ -80,8 +80,9 @@
This shop works without JavaScript, but some features work better with it enabled. This shop works without JavaScript, but some features work better with it enabled.
</div> </div>
</noscript> </noscript>
<%!-- Data attributes live on .shop-container inside LiveView for live updates --%> <%!-- .themed here provides fallback theme tokens; .shop-container.themed inside
<div class="shop-root"> LiveView has data attributes for live updates --%>
<div class="shop-root themed">
{@inner_content} {@inner_content}
</div> </div>
</body> </body>

View File

@ -18,7 +18,7 @@ defmodule BerrypodWeb.PageEditorHook do
""" """
import Phoenix.Component, only: [assign: 3] import Phoenix.Component, only: [assign: 3]
import Phoenix.LiveView, only: [attach_hook: 4, push_navigate: 2] import Phoenix.LiveView, only: [attach_hook: 4, push_navigate: 2, push_patch: 2]
alias Berrypod.{Media, Settings} alias Berrypod.{Media, Settings}
alias Berrypod.Pages alias Berrypod.Pages
@ -119,7 +119,12 @@ defmodule BerrypodWeb.PageEditorHook do
|> assign(:editor_active_tab, :settings) |> assign(:editor_active_tab, :settings)
|> maybe_enter_theme_mode() |> maybe_enter_theme_mode()
nil ->
# No edit param - collapse the editor (supports browser back)
assign(socket, :editor_sheet_state, :collapsed)
_ -> _ ->
# Unknown edit value - ignore
socket socket
end end
end end
@ -175,12 +180,30 @@ defmodule BerrypodWeb.PageEditorHook do
defp handle_editor_event("editor_set_sheet_state", %{"state" => state_str}, socket) do defp handle_editor_event("editor_set_sheet_state", %{"state" => state_str}, socket) do
if socket.assigns.is_admin do if socket.assigns.is_admin do
state = if state_str == "open", do: :open, else: :collapsed state = if state_str == "open", do: :open, else: :collapsed
{:halt, assign(socket, :editor_sheet_state, state)}
socket =
socket
|> assign(:editor_sheet_state, state)
|> sync_edit_url_param(state)
{:halt, socket}
else else
{:cont, socket} {:cont, socket}
end end
end end
# Sync URL ?edit param with editor state
defp sync_edit_url_param(socket, :collapsed) do
path = socket.assigns.editor_current_path || "/"
push_patch(socket, to: path)
end
defp sync_edit_url_param(socket, :open) do
path = socket.assigns.editor_current_path || "/"
tab = socket.assigns.editor_active_tab
push_patch(socket, to: "#{path}?edit=#{tab}")
end
# Tab switching for unified editor # Tab switching for unified editor
defp handle_editor_event("editor_set_tab", %{"tab" => tab_str}, socket) do defp handle_editor_event("editor_set_tab", %{"tab" => tab_str}, socket) do
if socket.assigns.is_admin do if socket.assigns.is_admin do
@ -228,6 +251,12 @@ defmodule BerrypodWeb.PageEditorHook do
assign(socket, :editor_active_tab, :settings) assign(socket, :editor_active_tab, :settings)
end end
# Open the sheet and sync URL with new tab
socket =
socket
|> assign(:editor_sheet_state, :open)
|> sync_edit_url_param(:open)
{:halt, socket} {:halt, socket}
else else
{:cont, socket} {:cont, socket}