Add PrimerLive as a dependency to your Phoenix application's mix.exs
{:primer_live, "~> 0.10"}
Run mix.deps get
.
Add PrimerLive as a dependency in your Phoenix application's mix.exs
by referencing the LiveView 0.20 branch. This branch will get updated with bug fixes and compatible improvements.
{:primer_live, git: "https://github.com/ArthurClemens/primer_live.git", branch: "live-view-0.20"}
Run mix.deps get
.
In endpoint.ex
, create a new Static Plug entry:
# PrimerLive resources
plug(Plug.Static,
at: "/primer_live",
from: {:primer_live, "priv/static"}
)
In <app>_web.ex
, change the Phoenix.VerifiedRoutes
configuration to include the primer_live
directory:
def static_paths, do: ~w(assets fonts images favicon.png robots.txt primer_live)
Add the import link to root.html.heex
.
If you are using verified routes:
<link phx-track-static rel="stylesheet" href={~p"/primer_live/primer-live.min.css"}>
<script defer phx-track-static type="text/javascript" src={~p"/primer_live/primer-live.min.js"}></script>
Otherwise:
<link phx-track-static rel="stylesheet" href="/primer_live/primer-live.min.css">
<script defer phx-track-static type="text/javascript" src={"/primer_live/primer-live.min.js"}></script>
The relatively new @scope
rule enables localized CSS. In practical terms, it allows multiple CSS libraries to coexist within a single codebase without causing style conflicts. For example, use Tailwind components in one section of the page, and use PrimerLive components in another section.
Browser support for the @scope
rule is still limited. For more details, see Can I use: @scope rule. If you're using PrimerLive in a controlled environment where users have access to the latest browsers, you may consider experimenting with this feature.
To use PrimerLive CSS with CSS scope:
- Change the CSS import to
primer-live-scoped.min.css
(orprimer-live-scoped.css
). - Create a container with class
primer-live
where you will be using PrimerLive components. This can be any element, except thehtml
element. - To make theming work properly, the container must be a child of the
theme
component (or equivalent element with attributes created withPrimerLive.Theme.html_attributes/2
).
Example:
<html>
<body>
<section>
Some content
</section>
<section class="primer-live">
PrimerLive components
</section>
</body>
</html>
In assets/js/app.js
, add global Prompt
to the hooks:
let liveSocket = new LiveSocket("/live", Socket, {
params: { _csrf_token: csrfToken },
hooks: {
Prompt: window.Prompt,
// existing hooks ...
},
});