kingdomcoder

kingdomcoder

How to get DaisyUI and Phoenix to work?

I just had my first frustrating experience working in the Elixir ecosystem.

Is there a reason it is so hard to get DaisyUI deployed with my Phoenix app on Fly.io (and is there a way to improve it?)

I’m happy to contribute to solving the problem. I just need to understand what makes adding a plugin to the Tailwind hex package so difficult.

PS: As a side note, I’ll be happy to know that I’m not the only one who has been frustrated by this problem. Please, chime in if you’ve faced this too.

Marked As Solved

kingdomcoder

kingdomcoder

Finding the article you referred to gave me the missing piece.

Here are the relevant steps. I assume you have a Phoenix app setup and npm installed on you device.

  1. Install and setup the Elixir Tailwind library. The README of the library is a great resource.
  2. Publish your app to fly using flyctl. This post is a great guide. Just note that the fly commands in the post might need to be replaced with flyctl commands.
  3. On your local machine, cd into your project’s assets folder and run npm init.
  4. From within your assets folder, follow the DaisyUI installation steps.
  5. Update the Dockerfile generated during the early fly.io deployment. Perform the following
    a. Ensure the COPY lib lib command comes before the RUN mix assets.deploy commnd
    b. Include this command RUN npm --prefix ./assets ci --progress=false --no-audit --loglevel=error in the Dockerfile. This should also come before the RUN mix assets.deploy command

Expectedly, you should be good to go and your app should properly deploy to fly.io :clinking_glasses:

16
Post #9

Also Liked

arcanemachine

arcanemachine

You are overcomplicating things. I just tested this with a fresh Phoenix 1.7.7 project:

Setup DaisyUI

  • Create project:

    • mix phx.new your_project
  • Initialize an npm project in the directory assets/:

    • cd assets && npm init -y
  • Add npm dependency daisyui in the directory assets/:

    • cd assets && npm i -D daisyui@latest
  • Add daisyUI to Tailwind plugins list (in assets/tailwind.config.js):

    • require("daisyUI")
  plugins: [
+    require('daisyui'),
    require("@tailwindcss/forms"),

  • DaisyUI is now installed! Let’s try an example component:

lib/your_project_web/controllers/page_html/home.html.heex

    <h1 class="text-brand mt-10 flex items-center text-sm font-semibold leading-6">
      Phoenix Framework
      <small class="bg-brand/5 text-[0.8125rem] ml-3 rounded-full px-2 font-medium leading-6">
        v<%= Application.spec(:phoenix, :vsn) %>
      </small>
    </h1>

+    <div class="btn btn-primary">
+      Hello daisyUI!
+    </div>

    <p class="text-[2rem] mt-4 font-semibold leading-10 tracking-tighter text-zinc-900">
      Peace of mind from prototype to production.
    </p>

It worked (for me, at least :grin:)!

At this point, daisyUI is installed and everything should be working as expected.


Now for the deployment:

Deploy to Fly.io

  • Create Fly.io app:

    • ‘flyctl launch’
  • Fix issues with the default Dockerfile generated when we ran fly launch:

    • Use apt-get to install nodejs and npm
    • Install our project’s npm dependencies
FROM ${BUILDER_IMAGE} as builder

# install build dependencies
-RUN apt-get update -y && apt-get install -y build-essential git \
+RUN apt-get update -y && apt-get install -y build-essential git nodejs npm \
     && apt-get clean && rm -f /var/lib/apt/lists/*_*
 
# ...

COPY priv priv

COPY lib lib

COPY assets assets
 
+# install npm dependencies
+RUN cd assets && npm install
+
# compile assets
RUN mix assets.deploy

Bonus: Prevent daisyUI from spamming the console when tailwind.config.js is modified

By default, daisyUI will print a message like this to the console whenever tailwind.config.js is modified:

🌼 daisyUI 3.5.0 https://daisyui.com
╰╮
 ╰─ ✔︎ [ 2 ] themes are enabled. You can add more themes or make your own theme:
      https://daisyui.com/docs/themes

    ❤︎ Support daisyUI: https://opencollective.com/daisyui

That’s annoying. Let’s disable that:

assets/tailwind.config.js

module.exports = {
  content: ["./js/**/*.js", "../lib/*_web.ex", "../lib/*_web/**/*.*ex"],
+  daisyui: {
+    logs: false,
+  },
  plugins:  [

Bonus: Remove tailwind/forms to avoid conflicts with daisyUI form components

The Tailwind forms package will cause conflicts with daisyUI forms. Let’s get rid of it:

assets/tailwind.config.js

  plugins: [
    require("daisyui"),
-    require("@tailwindcss/forms"),
    require("@tailwindcss/typography"),
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @kingdomcoder, I’m sorry you’ve had a frustrating experience. While I have not used DaisyUI myself, I imagine others who want to help would find it easier if you described what made this hard for you, or what problems you ran into that require help.

cvkmohan

cvkmohan

I just realized that tailwindcss-cli is working with 3rd party plugins also in the latest version. That would mean that the best way to integrate daisyui into phoenix application is:

  1. create a new phoenix application.
  2. Use phoenixframework/tailwind: An installer for tailwind (github.com) to integrate tailwindcss into the project
  3. cd assets
  4. yarn add daisyui
  5. In your tailwind.config.js file add the line plugins: [require("daisyui")],

That is it. There is no 6th step. :slight_smile:

Last Post!

cvkmohan

cvkmohan

@PJUllrich - I guess npm install daisyui is the missing step - either in development or in production - wherever the error happens.

The above steps are still working for me. For fly deployment the above @arcanemachine steps are sufficient.

Where Next?

Popular in Questions Top

New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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 31494 112
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement