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

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
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