steffend

steffend

Phoenix Core Team

Phoenix LiveView - 1.2.0 released

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!

Most Liked

steffend

steffend

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.

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:

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?

Where Next?

Popular in Phoenix News Top

chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
chrismccord
A minor vulnerability has been disclosed for applications redirecting to URLs provided by user input. Only applications passing user inpu...
New
chrismccord
Hey folks, Phoenix 1.3.0 is out! The final release please brings tweaks to web directory and alias conventions that were estabished in t...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
chrismccord
I’m pleased to announce the first release candidate of Phoenix 1.6.0 has landed on the heels of a fresh LiveView 0.16 release! This relea...
New
chrismccord
Phoenix 1.3.5, 1.4.18, 1.5.14, and 1.6.14 have been released to resolve a vulnerability in wildcard check_origin configurations. Previou...
New
chrismccord
The final release of Phoenix 1.7 is out! Most of the new features have been outlined in the 1.7 RC thread, but it has been a few months s...
New
chrismccord
LiveView 1.0.0-rc.0 is out! This 1.0 milestone comes almost six years after the first LiveView commit. I published a Phoenix blog highli...
New
chrismccord
Check the announcement blog for details! Blog duped here for convenience: Phoenix 1.8.0-rc released! The first release candidate of P...
418 13345 167
New

Other popular topics Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

We're in Beta

About us Mission Statement