Avoid CSS conflicts with DaisyUI
mainTo prevent default styles from conflicting with DaisyUI, remove the bg-white class from the <body> tag in your root.html.heex file.
## Change from this
<body class="bg-white">
## To this
<body>repository·main·Indexed 41 days ago
https://github.com/phcurado/daisy_ui_componentsA 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.
To prevent default styles from conflicting with DaisyUI, remove the bg-white class from the <body> tag in your root.html.heex file.
## Change from this
<body class="bg-white">
## To this
<body>To use this library, add it to your mix.exs dependencies and install the daisyui npm package in your Phoenix assets directory.
# In mix.exs
def deps do
[
{:daisy_ui_components, "~> 0.1"}
]
end# In your phoenix application
cd assets
npm i -D daisyui@latestTo 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.
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
endUse 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
endNote 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
endIf you are developing or testing the installer locally, follow these steps:
installer directory.MIX_ENV=prod.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 --devUpdate 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
//...
]
}To add the DaisyUIComponents installer to your Elixir project using Hex, run the following command:
mix archive.install hex daisy_ui_installerAfter installation, you must perform two configuration steps in your Elixir code:
config/config.exs, define the translate_function option for :daisy_ui_components. This function is used to translate Ecto changeset errors.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
endTo use daisy_ui_components in your Phoenix application, follow these three steps:
{:daisy_ui_components, "~> 0.9"} to your mix.exs file.assets directory and install the daisyui package.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"),
],
};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.
mix archive.install hex daisy_ui_installermix daisyAlternatively, you can install a single specific component (e.g., badge and input) using:
mix daisy badge inputmix archive.install hex daisy_ui_installer
mix daisySince 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.
Update your mix.exs assets.setup to include the npm command:
npm is installed in your Docker image.package.json and package-lock.json to the assets directory.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=errorTo use the library as a standard dependency, add it to your mix.exs file:
defp deps do
[
{:daisy_ui_components, "~> 0.9"}
]
endTo remove the daisy_ui_installer archive from your system, run:
mix archive.uninstall daisy_ui_installer