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
Specs
key_tuple() :: {key(), as_boolean(term())}
Link to this section Functions
Specs
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
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
Specs
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"
Specs
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")
%{}