@@ -7,27 +7,44 @@ defmodule ExCSSModules.View do
77 Use the ExCSSModules.View on a view which defines the JSON for CSS Modules
88 as an external resource.
99
10+ To embed the stylesheet in the file set :embed_stylesheet to true.
11+
1012 If adds the following functions to the View:
1113 - stylesheet/0 - same as ExCSSModules.stylesheet/1 with the stylesheet predefined
1214 - class/1 - same as ExCSSModules.class/2 with the stylesheet predefined
1315 - class_name/1 - same as ExCSSModules.class_name/2 with the stylesheet predefined
1416 - class_name/2 - same as ExCSSModules.class_name/3 with the stylesheet predefined
1517 - class_selector/1 - same as ExCSSModules.class_selector/2 with the stylesheet predefined
1618 """
19+
1720 defmacro __using__ ( opts \\ [ ] ) do
21+ { file , [ file: relative_to ] } =
22+ Code . eval_quoted ( opts [ :stylesheet ] , file: __CALLER__ . file )
23+
24+ file = Path . expand ( file , Path . dirname ( relative_to ) )
25+
1826 quote do
19- @ external_resource unquote ( opts [ :stylesheet ] ) <> ".json"
20- @ stylesheet ExCSSModules . read_stylesheet ( unquote ( opts [ :stylesheet ] ) )
27+ @ stylesheet unquote (
28+ if opts [ :embed_stylesheet ] do
29+ Macro . escape ( ExCSSModules . read_stylesheet ( file ) )
30+ else
31+ Macro . escape ( file )
32+ end
33+ )
34+
35+ def stylesheet_definition , do: @ stylesheet
2136
2237 def stylesheet , do: ExCSSModules . stylesheet ( @ stylesheet )
2338
24- def class ( key ) , do: ExCSSModules . class ( @ stylesheet , key )
39+ def class ( key ) , do: stylesheet ( ) |> ExCSSModules . class ( key )
2540
26- def class_name ( key ) , do: ExCSSModules . class_name ( @ stylesheet , key )
41+ def class_name ( key ) do
42+ ExCSSModules . class_name ( stylesheet ( ) , key )
43+ end
2744 def class_name ( key , value ) , do:
28- ExCSSModules . class_name ( @ stylesheet , key , value )
45+ ExCSSModules . class_name ( stylesheet ( ) , key , value )
2946
30- def class_selector ( key ) , do: ExCSSModules . class_selector ( @ stylesheet , key )
47+ def class_selector ( key ) , do: ExCSSModules . class_selector ( stylesheet ( ) , key )
3148 end
3249 end
3350end
0 commit comments