DaisyUI Components for Phoenix

repository·main·Indexed 41 days ago

https://github.com/phcurado/daisy_ui_components

A library of ready-to-use Elixir components for Phoenix projects, bringing the DaisyUI CSS component library and Tailwind CSS styling to HEEx templates. It includes a mix archive installer (`daisy_ui_installer`) and a set of components including Actions, Data Display, Navigation, Feedback, Data Input, and Layout, as well as integrated replacements for Phoenix Core Components.

Tokens
10.3K
Snippets
37
Records
47
Agent score
51%

What's inside DaisyUI Components

  1. Use DaisyUIComponents in your Phoenix project

    main

    To make the components available in your templates, you must import the DaisyUIComponents module in your web file (typically lib/my_app_web.ex) within the html_helpers function.

    Option 1: Keep Phoenix Core Components

    Use this if you want to keep your existing CoreComponents and only add DaisyUI components. Note that this may cause conflicts if component names overlap.

    defp html_helpers do
      quote do
        use Gettext, backend: MyAppWeb.Gettext
        import Phoenix.HTML
    
        use DaisyUIComponents, core_components: false
        import YourProjectWeb.CoreComponents
      end
    end

    Option 2: Replace Phoenix Core Components

    Use this to use all DaisyUIComponents as your primary components. You must set core_components: true and remove the import for your project's CoreComponents.

    defp html_helpers do
      quote do
        use Gettext, backend: MyAppWeb.Gettext
        import Phoenix.HTML
    
        use DaisyUIComponents, core_components: true
      end
    end

    Note on Conflicts: If you encounter conflicts in layout.ex, you may need to comment out the flash_group/1 component there.

    # Example of replacing CoreComponents
    defp html_helpers do
      quote do
        use Gettext, backend: MyAppWeb.Gettext
        import Phoenix.HTML
    
        use DaisyUIComponents, core_components: true
      end
    end
  2. Install daisy_ui_installer locally

    main

    If you are developing or testing the installer locally, follow these steps:

    1. Navigate to the installer directory.
    2. Build and install the archive using MIX_ENV=prod.
    3. Return to the project root to run the installer.

    Note: The local installation command assumes you are working within the repository structure.

    cd installer
    MIX_ENV=prod mix do archive.build, archive.install
    
    # Then run in the root of the project:
    mix daisy --dev
  3. Configure Tailwind CSS for DaisyUI Components

    main

    Update your tailwind.config.js to include the daisy_ui_components dependency paths in the content array and add the daisyui plugin to the plugins array.

    module.exports = {
      content: [
        //...
        "../deps/daisy_ui_components/**/*.*ex" // reference DaisyUIComponents as content path
      ],
      //...
      plugins: [
        //...
        require("daisyui")  // add daisyUI plugin
        //...
      ]
    }
  4. Configure error translation and import DaisyUI components

    main

    After installation, you must perform two configuration steps in your Elixir code:

    1. Set the translation function: In config/config.exs, define the translate_function option for :daisy_ui_components. This function is used to translate Ecto changeset errors.
    2. Import components in your Web module: In your web folder's helper module (usually where html_helpers is defined), add use DaisyUIComponents.

    Note on CoreComponents: DaisyUIComponents.CoreComponents acts as a drop-in replacement for the default Phoenix CoreComponents. If you want to use the library's defaults, comment out your project's existing import YourProjectWeb.CoreComponents to avoid conflicts.

    # config/config.exs
    config :daisy_ui_components, translate_function: &MyAppWeb.CoreComponents.translate_error/1
    # lib/my_app_web.ex
    defp html_helpers do
      quote do
        # Translation
        use Gettext, backend: MyAppWeb.Gettext
    
        # HTML escaping functionality
        import Phoenix.HTML
    
        # Import DaisyUI components into your project
        use DaisyUIComponents
    
        # Comment your own CoreComponents to not conflict with the defaults of this library.
        # import YourProjectWeb.CoreComponents
      end
    end
  5. Install daisy_ui_components in Phoenix

    main

    To use daisy_ui_components in your Phoenix application, follow these three steps:

    1. Add the Elixir dependency: Add {:daisy_ui_components, "~> 0.9"} to your mix.exs file.
    2. Install DaisyUI via npm: Navigate to your assets directory and install the daisyui package.
    3. Configure Tailwind CSS: Update your tailwind.config.js to include the library's paths in the content array and add daisyui to the plugins array.
    # mix.exs
    def deps do
      [
        {:daisy_ui_components, "~> 0.9"}
      ]
    end
    # Terminal
    cd assets
    npm i -D daisyui@5
    // tailwind.config.js
    module.exports = {
      content: [
        //...
        "../deps/daisy_ui_components/**/*.*ex",
      ],
      plugins: [
        // require("@tailwindcss/forms"), // Comment this out to avoid conflicts
        require("daisyui"),
      ],
    };
  6. Install daisy_ui_components via mix archive installer

    main

    You can use the daisy_ui_installer archive to quickly add components to your Phoenix project. This method installs the components and their necessary dependencies.

    1. Install the installer archive:
    mix archive.install hex daisy_ui_installer
    1. Run the daisy command in your Phoenix project folder to install all components:
    mix daisy

    Alternatively, you can install a single specific component (e.g., badge and input) using:

    mix daisy badge input
    mix archive.install hex daisy_ui_installer
    mix daisy
  7. Configure NPM asset pipeline and Docker deployment

    main

    Since DaisyUI requires npm, you must ensure your Phoenix asset pipeline is configured to run npm install and that your deployment environment (like Docker) is prepared.

    Local Development Setup

    Update your mix.exs assets.setup to include the npm command:

    Docker Deployment

    1. Ensure npm is installed in your Docker image.
    2. Copy package.json and package-lock.json to the assets directory.
    3. Run npm ci within the assets directory during the build process.
    # mix.exs
    "assets.setup": [
      "tailwind.install --if-missing",
    - "esbuild.install --if-missing"
    + "esbuild.install --if-missing",
    + "cmd npm install --prefix assets"
    ]
    # Dockerfile snippet
    # install build dependencies
    RUN apt-get update -y && apt-get install -y build-essential git npm \
        && apt-get clean && rm -f /var/lib/apt/lists/*_*
    
    # ...
    RUN mix deps.compile
    
    # build assets
    COPY assets/package.json assets/package-lock.json ./assets/
    RUN npm --prefix ./assets ci --progress=false --no-audit --loglevel=error