Skip to content

Instantly share code, notes, and snippets.

@zachdaniel
Created July 14, 2025 15:32
Show Gist options
  • Save zachdaniel/b510c42079e364b843841e4216263bbc to your computer and use it in GitHub Desktop.
Save zachdaniel/b510c42079e364b843841e4216263bbc to your computer and use it in GitHub Desktop.
A utility for conditional classes
# 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