Allow defining layout in callback function#1808
Conversation
- Default layout/1 returns config(:layout) instead of nil for backwards compatibility - Simplify layout.ex to a single code path through the callback (no if/else branching) - Fix callback typespec to match actual return type (layout spec, not rendered HTML)
|
Hey, thanks for your contribution! |
There was a problem hiding this comment.
Pull request overview
Adds a new layout/1 callback to Backpex.LiveResource so consumers can define the LiveView layout via a function (instead of a compile-time layout: option), aiming to reduce compile-time dependencies (and related compile cycles).
Changes:
- Make the
layoutoption inuse Backpex.LiveResourceoptional and allow alayout/1callback. - Add
c:layout/1behavior + a default implementation returningconfig(:layout)and mark it overridable. - Update
Backpex.HTML.Layout.layout/1to callassigns.live_resource.layout(assigns)instead of readingconfig(:layout)directly.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| lib/backpex/live_resource.ex | Adds layout/1 callback + default implementation; makes layout option optional. |
| lib/backpex/html/layout.ex | Switches layout resolution to call the new layout/1 callback. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The default layout/1 callback now raises an ArgumentError with an actionable message when neither the layout option nor a layout/1 callback override is provided, instead of letting nil propagate to a CaseClauseError at render time.
Of course, thanks for the very helpful library! Happy to help with getting this merged, just let me know 🙂 |
pehbehbeh
left a comment
There was a problem hiding this comment.
LGTM
Maybe even soft deprecate the old way via NimbleOptions (see :deprecated option)?
Hello!
I was debugging some compile cycles in a Phoenix application, and noticed that
backpexwas part of the issue.The demo application has a good example of what I'm running into. If you run
mix xref graph --format cycles --label compile-connected, it reports that there is a compile cycle:This results in a change in any of the files recompiling all of the files, which usually means slower compile times during development in apps that use Backpex.
I'm not deeply familiar with Elixir compiler functionality, but my understanding is the reason for it is the
DemoWeb.Layoutsbecomes a compile dependency of the LiveViews when specified inuse Backpex.LiveResource.DemoWeb.Layoutsincludesuse DemoWeb, :htmlwhich ends up pulling in the router, etc.To continue the above example using the demo app, if you apply this patch to replace the layout option with the callback, the demo app no longer has a compile cycle:
I'm proposing allowing specifying the layout as a callback function to avoid the specific compile dependency between layouts and the LiveView. To avoid a breaking change, I've not removed the current option. I also haven't tried updating any guides, documentation, or similar, but am happy to if you're open to this change.