View Source ExCSSModules (ExCSSModules v0.0.10)

CSS Modules helpers

Link to this section Summary

Functions

Reads the class definitions from the definition map and maps them to a class attribute. The keys argument is used to retrieve the class name or multiple class names with class_name/1.

Returns the class name or class names from the definition map, concatenated as one string separated by spaces.

Takes the definition and makes a class selector that can be used in CSS out of the keys given. Takes either a single value or a list of keys.

Reads a valid stylesheet definition. Returns a map if the stylesheet is already a map. Reads the file if the stylesheet is a string. Returns an empty map if the stylesheet does not exist.

Link to this section Types

Specs

key() :: String.t() | atom()

Specs

key_tuple() :: {key(), as_boolean(term())}

Link to this section Functions

Link to this function

class(definition, keys, return_class? \\ true)

View Source

Specs

class(map(), key() | [key(), ...], as_boolean(term())) ::
  {:safe, iodata()} | nil

Reads the class definitions from the definition map and maps them to a class attribute. The keys argument is used to retrieve the class name or multiple class names with class_name/1.

Returns nil if return_class? is falsy. return_class? is optional and truthy by default.

Returns nil for a class_name that does not exist.

Note that this function cannot be used with HEEX templates, as the HTML validation engine does not allow them to be build with arbitrary text. class_name/3 should be used instead.

Examples

iex> class(%{ "hello" => "world"}, "hello")
{:safe, ~s(class="world")}

iex> class(%{ "hello" => "world"}, :hello, true)
{:safe, ~s(class="world")}

iex> class(%{ "hello" => "world"}, "hello", false)
nil

iex> class(%{ "hello" => "world"}, "hello", nil)
nil

iex> class(%{"hello" => "world"}, "foo")
nil
Link to this function

class_name(definition, key, return_class? \\ true)

View Source

Specs

class_name(
  map(),
  key() | key_tuple() | [key(), ...] | [key_tuple(), ...],
  as_boolean(term())
) ::
  String.t() | nil

Returns the class name or class names from the definition map, concatenated as one string separated by spaces.

Second argument key can be a string or atom name of the key, a tuple with {key, boolean} or a list of either keys or tuples.

Returns nil if return_class? is falsy. return_class? is optional and truthy by default.

Returns nil for a key that does not exist.

Examples

iex> class_name(%{"hello" => "world", "foo" => "bar"}, ["hello", "foo"])
"world bar"

iex> class_name(%{"hello" => "world", "foo" => "bar"}, [{"hello", true}, {"foo", true}])
"world bar"

iex> class_name(%{"hello" => "world", "foo" => "bar"}, [{"hello", true}, {"foo", false}])
"world"

iex> class_name(%{"hello" => "world", "foo" => "bar"}, [{:hello, true}, {:foo, false}])
"world"

iex> class_name(%{"hello" => "world"}, "hello")
"world"

iex> class_name(%{"hello" => "world"}, :hello, true)
"world"

iex> class_name(%{"hello" => "world"}, "hello", "anything")
"world"

iex> class_name(%{"hello" => "world"}, {"hello", true})
"world"

iex> class_name(%{"hello" => "world"}, "hello", false)
nil

iex> class_name(%{"hello" => "world"}, {:hello, nil})
nil

iex> class_name(%{"hello" => "world"}, "foo")
nil

iex> class_name(%{}, "hello")
nil
Link to this function

class_selector(definition, keys)

View Source

Specs

class_selector(String.t() | map(), key() | [key(), ...]) :: String.t()

Takes the definition and makes a class selector that can be used in CSS out of the keys given. Takes either a single value or a list of keys.

Examples

iex> class_selector(@example_stylesheet, "title")
"._namespaced_title"

iex> class_selector(@example_stylesheet, ["title", "paragraph"])
"._namespaced_title._namespaced_paragraph"

iex> class_selector(@example_stylesheet, "foo")
nil

iex> class_selector(%{ "hello" => "world"}, "hello")
".world"

iex> class_selector(%{ "hello" => "world"}, :hello)
".world"

iex> class_selector(%{ "hello" => "world", "foo" => "bar"}, ["hello", "foo"])
".world.bar"
Link to this function

stylesheet(definition, build_json_task \\ nil)

View Source

Specs

stylesheet(String.t() | map(), module()) :: map()

Reads a valid stylesheet definition. Returns a map if the stylesheet is already a map. Reads the file if the stylesheet is a string. Returns an empty map if the stylesheet does not exist.

Examples

iex> stylesheet(@example_stylesheet)
%{
  "title" => "_namespaced_title",
  "paragraph" => "_namespaced_paragraph"
}

iex> stylesheet(%{"title" => "_namespaced_title", "paragraph" => "_namespaced_paragraph"})
%{
  "title" => "_namespaced_title",
  "paragraph" => "_namespaced_paragraph"
}

iex> stylesheet("foobar")
%{}