@@ -7,27 +7,43 @@ 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 ] } = Code . eval_quoted ( opts [ :stylesheet ] , file: __CALLER__ . file )
22+
23+ file = Path . expand ( file , Path . dirname ( relative_to ) )
24+
1825 quote do
19- @ external_resource unquote ( opts [ :stylesheet ] ) <> ".json"
20- @ stylesheet ExCSSModules . read_stylesheet ( unquote ( opts [ :stylesheet ] ) )
26+ @ stylesheet unquote (
27+ if opts [ :embed_stylesheet ] do
28+ Macro . escape ( ExCSSModules . read_stylesheet ( file ) )
29+ else
30+ Macro . escape ( file )
31+ end
32+ )
33+
34+ def stylesheet_definition , do: @ stylesheet
2135
2236 def stylesheet , do: ExCSSModules . stylesheet ( @ stylesheet )
2337
24- def class ( key ) , do: ExCSSModules . class ( @ stylesheet , key )
38+ def class ( key ) , do: stylesheet ( ) |> ExCSSModules . class ( key )
2539
26- def class_name ( key ) , do: ExCSSModules . class_name ( @ stylesheet , key )
40+ def class_name ( key ) do
41+ ExCSSModules . class_name ( stylesheet ( ) , key )
42+ end
2743 def class_name ( key , value ) , do:
28- ExCSSModules . class_name ( @ stylesheet , key , value )
44+ ExCSSModules . class_name ( stylesheet ( ) , key , value )
2945
30- def class_selector ( key ) , do: ExCSSModules . class_selector ( @ stylesheet , key )
46+ def class_selector ( key ) , do: ExCSSModules . class_selector ( stylesheet ( ) , key )
3147 end
3248 end
3349end
0 commit comments