11@ target ( erlang )
22import gleam/result
3- import sketch/css . { type AtRule , type Class , type Global }
3+ import sketch/css . { type AtRule , type Class }
44import sketch/error
55@ target ( erlang )
66import sketch/internals/cache/actor
@@ -10,12 +10,12 @@ import sketch/internals/cache/cache
1010@ target ( javascript )
1111/// Manages the styles. Can be instanciated with [`stylesheet`](#stylesheet).
1212pub opaque type StyleSheet {
13- StyleSheet ( cache : cache . Cache , is_persistent : Bool )
13+ StyleSheet ( cache : cache . Cache , id : Int , is_persistent : Bool )
1414}
1515
1616@ target ( erlang )
1717pub opaque type StyleSheet {
18- StyleSheet ( cache : actor . Cache )
18+ StyleSheet ( cache : actor . Cache , id : Int )
1919}
2020
2121@ target ( javascript )
@@ -41,62 +41,35 @@ pub fn class_name(class: Class, stylesheet: StyleSheet) -> #(StyleSheet, String)
4141@ target ( erlang )
4242pub fn class_name ( class : Class , stylesheet : StyleSheet ) -> # ( StyleSheet , String ) {
4343 let # ( cache , class_name ) = actor . class_name ( class , stylesheet . cache )
44- # ( StyleSheet ( cache : ) , class_name )
44+ # ( StyleSheet ( .. stylesheet , cache : ) , class_name )
4545}
4646
4747@ target ( javascript )
4848/// Pushes an `@rule` in the StyleSheet, to get it bundled in the outputted CSS.
4949/// It returns the StyleSheet with the rule added.
50- @ deprecated ( "Use `at_rule_` instead. `at_rule` will be replaced in the next version." )
5150pub fn at_rule ( rule : AtRule , stylesheet : StyleSheet ) -> StyleSheet {
5251 let cache = cache . at_rule ( rule , stylesheet . cache )
5352 StyleSheet ( .. stylesheet , cache : )
5453}
5554
5655@ target ( erlang )
57- @ deprecated ( "Use `at_rule_` instead. `at_rule` will be replaced in the next version." )
5856pub fn at_rule ( rule : AtRule , stylesheet : StyleSheet ) -> StyleSheet {
5957 let cache = actor . at_rule ( rule , stylesheet . cache )
60- StyleSheet ( cache : )
58+ StyleSheet ( .. stylesheet , cache : )
6159}
6260
6361@ target ( javascript )
6462/// Pushes an `@rule` in the StyleSheet, to get it bundled in the outputted CSS.
6563/// It returns the StyleSheet with the rule added.
66- pub fn at_rule_ ( stylesheet : StyleSheet , rule : AtRule ) -> StyleSheet {
67- let cache = cache . at_rule ( rule , stylesheet . cache )
68- StyleSheet ( .. stylesheet , cache : )
69- }
70-
71- @ target ( erlang )
72- pub fn at_rule_ ( stylesheet : StyleSheet , rule : AtRule ) -> StyleSheet {
73- let cache = actor . at_rule ( rule , stylesheet . cache )
74- StyleSheet ( cache : )
75- }
76-
77- @ target ( javascript )
78- /// Injects a `Global` class in the StyleSheet. Use it to apply some style on
79- /// `body` for example.
80- ///
81- /// ```gleam
82- /// let assert Ok(stylesheet) = sketch.stylesheet(sketch.Persistent)
83- /// stylesheet
84- /// |> sketch.global({
85- /// css.global("body", [
86- /// css.margin(px(0)),
87- /// css.backkground("red"),
88- /// ])
89- /// })
90- /// ```
91- pub fn global ( stylesheet : StyleSheet , global : Global ) -> StyleSheet {
64+ pub fn global ( stylesheet : StyleSheet , global : css . Global ) -> StyleSheet {
9265 let # ( cache , _ ) = cache . class_name ( global . class , stylesheet . cache )
9366 StyleSheet ( .. stylesheet , cache : )
9467}
9568
9669@ target ( erlang )
97- pub fn global ( stylesheet : StyleSheet , global : Global ) -> StyleSheet {
70+ pub fn global ( stylesheet : StyleSheet , global : css . Global ) -> StyleSheet {
9871 let # ( cache , _ ) = actor . class_name ( global . class , stylesheet . cache )
99- StyleSheet ( cache : )
72+ StyleSheet ( .. stylesheet , cache : )
10073}
10174
10275/// Strategy for the StyleSheet. Two strategies are available as of now: ephemeral
@@ -118,19 +91,25 @@ pub type Strategy {
11891pub fn stylesheet (
11992 strategy strategy : Strategy ,
12093) -> Result ( StyleSheet , error . SketchError ) {
94+ let id = unique_id ( )
12195 Ok ( case strategy {
122- Ephemeral -> StyleSheet ( cache : cache . new ( ) , is_persistent : False )
123- Persistent -> StyleSheet ( cache : cache . new ( ) , is_persistent : True )
96+ Ephemeral -> StyleSheet ( cache : cache . new ( ) , id : , is_persistent : False )
97+ Persistent -> StyleSheet ( cache : cache . new ( ) , id : , is_persistent : True )
12498 } )
12599}
126100
127101@ target ( erlang )
128102pub fn stylesheet (
129103 strategy strategy : Strategy ,
130104) -> Result ( StyleSheet , error . SketchError ) {
131- case strategy {
105+ let id = unique_id ( )
106+ use cache <- result . map ( case strategy {
132107 Ephemeral -> Ok ( actor . ephemeral ( ) )
133108 Persistent -> actor . persistent ( )
134- }
135- |> result . map ( StyleSheet )
109+ } )
110+ StyleSheet ( cache : , id : )
136111}
112+
113+ @ external ( erlang , "erlang" , "unique_integer" )
114+ @ external ( javascript , "./sketch.ffi.mjs" , "uniqueId" )
115+ fn unique_id ( ) -> Int
0 commit comments