Created
July 14, 2025 15:32
-
-
Save zachdaniel/b510c42079e364b843841e4216263bbc to your computer and use it in GitHub Desktop.
A utility for conditional classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# class={classes("bar", foo: true)} | |
def classes(string) when is_binary(string) do | |
string | |
end | |
def classes({class, condition}) do | |
if condition do | |
to_string(class) | |
end | |
end | |
def classes(classes) when is_list(classes) do | |
classes | |
|> Enum.map(&classes/1) | |
|> Enum.filter(& &1) | |
|> Enum.join(" ") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment