From 1b025ef089dd64ee97c0ca6c8b1d0f973a152869 Mon Sep 17 00:00:00 2001 From: Kevin Wilson Date: Mon, 13 Nov 2023 07:09:04 -0700 Subject: [PATCH] added the ability to use custom colors by extending the default color map in an application's config file --- README.md | 8 ++++++++ lib/twix/config_utils.ex | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/README.md b/README.md index 568e3ad..403376f 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,14 @@ with the `hover` variant will remain while all previous ones are dropped. Now we have a red button, as we intended! +#### Using Custom Colors + +If you are defining custom colors in you tailwind.config.js file, you can extend the colors that Twix will recognize by adding the following to your config.exs file: +```elixir +config :twix, colors: ["my_first_custom_color", "my_second_custom_color", "etc"] +`` +``` + ## Installation This package can be installed by adding `twix` to your diff --git a/lib/twix/config_utils.ex b/lib/twix/config_utils.ex index a47cef6..7b8c331 100644 --- a/lib/twix/config_utils.ex +++ b/lib/twix/config_utils.ex @@ -46,6 +46,19 @@ defmodule Twix.ConfigUtils do pink: [{Twix.Validate, :is_integer?}, {Twix.Validate, :is_opacity?}], rose: [{Twix.Validate, :is_integer?}, {Twix.Validate, :is_opacity?}] } + |> extend(Application.get_env(:twix, :colors)) ] end + + defp extend(color_map, nil), do: color_map + + defp extend(color_map, config) do + config + |> Enum.reduce(color_map, fn color, acc -> + Map.put(acc, String.to_atom(color), [ + {Twix.Validate, :is_integer?}, + {Twix.Validate, :is_opacity?} + ]) + end) + end end