steffend

steffend

Phoenix Core Team

LiveView 1.2.0 is out now!

Colocated CSS

The biggest new feature is support for Colocated CSS, which is built on the work we did in 1.1 for Colocated Hooks and Colocated JS.

def table(assigns) do
  ~H"""
  <style :type={MyAppWeb.ColocatedCSS}>
    thead color: {
      ...;
    }
    tbody, tr:hover {
      ...
    }
  </style>
  <table>...</table>
  """
end

You can optionally scope colocated CSS to only the component it is defined in. We don’t ship a built in scoping mechanism for now, but the docs include an example using CSS @scope.

To implement this, we did a big refactor of how we compile HEEx templates, which we explain in a separate blog post.

tl;dr: To make colocated CSS work, we had to change how we compile HEEx templates by splitting compilation into a separate tokenization and parsing step, allowing us to handle macro components (colocated CSS and JS) without making the rest of the compilation more complex. It also allowed us to reuse code we previously had to duplicate between template compilation and formatting.

Formatters for <script> and <style> tags

The Phoenix.LiveView.HTMLFormatter.TagFormatter behaviour allows you to format <script> and <style> tags with third party tooling when running mix format, especially useful if your project uses colocated assets like Phoenix.LiveView.ColocatedHook or the new ColocatedCSS.

The documentation contains an example using prettier, which we also use in the LiveView repository itself.

Encoding JS commands to JSON

Phoenix.LiveView.JS structs can now be encoded to JSON for usage in push_event. So now you can do

push_event(socket, "highlight", %{toggle: JS.toggle_class(...)})
// some hook
this.handleEvent("highlight", ({ toggle }) => { this.js().execJS(this.el, toggle); });

while in the past you’d have to render the command in an element attribute and then refer back to it in your hook.

LiveView implements JSON.Encoder and Jason.Encoder automatically. If you use a different library, you can invoke JS.to_encodable/1 manually.

Thank you to @rhcarvalho for contributing this in response to Make `JS.t()` a public data structure, or json serializable.

Opting out of debug annotations

You can now opt-out of the HEEx debug annotations (HTML comments and attributes) per module with

@debug_heex_annotations false
@debug_attributes false

This was done in response to Disable HEEx debug annotations conditionnaly.

Granular configuration for test warnings

In previous releases, we started to add some checks when running LiveView tests, for example to warn you if there’s a duplicated ID on the page (that will cause problems in the browser). In LiveView 1.2, we added a new check to warn about forms with a missing ID, which prevents [form recovery] from working. Because there are cases where an ID is deliberately omitted and form recovery is not needed, all of those checks can now be configured on their own:

config :phoenix_live_view, :test_warnings,
 duplicate_id: :warn, # one of :warn, :raise, :ignore
 ...

See the documentation for more details.

Documentation for the JS client

While many people like that they’re able to avoid touching JavaScript when using LiveView, there are cases where creating hooks and interacting with the LiveSocket API are needed. We now have dedicated documentation on the JS client that shows the public API, based on typedoc.

Links

See the official announcement for more details:

Also have a look at the HEEx compilation deep dive if you’re interested:

Finally, have a look at the changelog for more details on bugs and other small enhancements.

Happy coding!

Showing Posts 1 to 4

LostKobrakai

LostKobrakai

It’s great to see this part of the codebase become less of a black box by such blog posts. The MacroComponent still seems to be private API in 1.2. Does the mentioned rework of the template compilation flow move us closer to MacroComponent becoming available to LV users / the token AST used becoming public / stable?

steffend

steffend OP

Phoenix Core Team

It moves us closer for sure. I’d even say doing this was kind of a requirement for it. Now, it’s mostly evaluating if we can commit to the node format or if there are problems we’d run into. That said, there’s no ETA right now.

vicb335

vicb335

Amazing work, thank you!! I saw this in rc a few weeks back, nice to it stabilized!

rhcarvalho

rhcarvalho

Yay! Thanks @steffend and the Phoenix team for the release, looking forward to upgrading!

Colocated CSS is going to be very handy for an upcoming project :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Phoenix News Top

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews