0 | module Text.CSS.Declaration
  1 |
  2 | import Data.List
  3 | import Data.Vect
  4 | import Text.CSS.Color
  5 | import Text.CSS.Cursor
  6 | import Text.CSS.Dir
  7 | import Text.CSS.Flexbox
  8 | import Text.CSS.Gradient
  9 | import Text.CSS.Grid
 10 | import Text.CSS.Length
 11 | import Text.CSS.ListStyleType
 12 | import Text.CSS.Percentage
 13 | import Text.CSS.Property
 14 |
 15 | %default total
 16 |
 17 | public export
 18 | data Declaration : Type where
 19 |   Decl : (property, value : String) -> Declaration
 20 |   Display : Display -> Declaration
 21 |
 22 | export
 23 | Interpolation Declaration where
 24 |   interpolate (Decl p v) = "\{p}: \{v};"
 25 |   interpolate (Display None) = "display: none;"
 26 |   interpolate (Display Flex) = "display: flex;"
 27 |   interpolate (Display Grid) = "display: grid;"
 28 |   interpolate (Display $ Area rs cs a) = "\{renderArea rs cs a};"
 29 |
 30 | export %inline
 31 | decl : Interpolation a => String -> a -> Declaration
 32 | decl s = Decl s . interpolate
 33 |
 34 | -- prefix
 35 | prfx : Dir a -> String
 36 | prfx (Left _)   = "-left"
 37 | prfx (Right _)  = "-right"
 38 | prfx (Top _)    = "-top"
 39 | prfx (Bottom _) = "-bottom"
 40 | prfx _          = ""
 41 |
 42 | export
 43 | dirDecl : (prop : String) -> (a -> String) -> Dir a -> Declaration
 44 | dirDecl prop f d =
 45 |   let vs := concat . intersperse " " . map f $ vals d
 46 |    in Decl "\{prop}\{prfx d}" vs
 47 |
 48 | export
 49 | dirDecl2 : (prop,suffix : String) -> (a -> String) -> Dir a -> Declaration
 50 | dirDecl2 prop suffix f d =
 51 |   let vs := concat . intersperse " " . map f $ vals d
 52 |    in Decl "\{prop}\{prfx d}-\{suffix}" vs
 53 |
 54 | --------------------------------------------------------------------------------
 55 | --          Predefined Properties
 56 | --------------------------------------------------------------------------------
 57 |
 58 | export %inline
 59 | alignItems : FlexAlign -> Declaration
 60 | alignItems = decl "align-items"
 61 |
 62 | export %inline
 63 | alignSelf : FlexAlign -> Declaration
 64 | alignSelf = decl "align-self"
 65 |
 66 | export %inline
 67 | aspectRatio : Ratio -> Declaration
 68 | aspectRatio = decl "aspect-ratio"
 69 |
 70 | export %inline
 71 | backgroundColor : Color -> Declaration
 72 | backgroundColor = decl "background-color"
 73 |
 74 | export %inline
 75 | backgroundImage : (url : String) -> Declaration
 76 | backgroundImage = Decl "background-image"
 77 |
 78 | export %inline
 79 | backgroundImageGradient : Gradient -> Declaration
 80 | backgroundImageGradient = decl "background-image"
 81 |
 82 | export %inline
 83 | backgroundSize : Width -> Declaration
 84 | backgroundSize = decl "background-size"
 85 |
 86 | export %inline
 87 | border : String -> Declaration
 88 | border = Decl "border"
 89 |
 90 | export %inline
 91 | borderColor : Dir Color -> Declaration
 92 | borderColor = dirDecl2 "border" "color" interpolate
 93 |
 94 | export %inline
 95 | borderRadius : BorderRadius -> Declaration
 96 | borderRadius = decl "border-radius"
 97 |
 98 | export %inline
 99 | borderStyle : Dir BorderStyle -> Declaration
100 | borderStyle = dirDecl2 "border" "style" interpolate
101 |
102 | export %inline
103 | borderWidth : Dir BorderWidth -> Declaration
104 | borderWidth = dirDecl2 "border" "width" interpolate
105 |
106 | export %inline
107 | boxSizing : BoxSizing -> Declaration
108 | boxSizing = decl "box-sizing"
109 |
110 | export %inline
111 | color : Color -> Declaration
112 | color = decl "color"
113 |
114 | export %inline
115 | columnGap : Length -> Declaration
116 | columnGap = decl "column-gap"
117 |
118 | export %inline
119 | containerType : ContainerType -> Declaration
120 | containerType = decl "container-type"
121 |
122 | export
123 | cursor : List Cursor -> Declaration
124 | cursor = decl "cursor" . fastConcat . intersperse "," . map interpolate
125 |
126 | export %inline
127 | direction : Direction -> Declaration
128 | direction = decl "direction"
129 |
130 | export %inline
131 | display : Display -> Declaration
132 | display = Display
133 |
134 | export %inline
135 | area :
136 |      {0 n,m : Nat}
137 |   -> {0 a : Type}
138 |   -> {auto as : Show a}
139 |   -> (rows    : Vect (S m) GridValue)
140 |   -> (columns : Vect (S n) GridValue)
141 |   -> (area    : Vect (S m) (Vect (S n) a))
142 |   -> Declaration
143 | area rs cs a = display (Area rs cs a)
144 |
145 | export
146 | fill : Maybe Color -> Declaration
147 | fill Nothing  = decl "fill" "none"
148 | fill (Just c) = decl "fill" c
149 |
150 | export %inline
151 | flex : String -> Declaration
152 | flex = Decl "flex"
153 |
154 | export %inline
155 | flexBasis : FlexBasis -> Declaration
156 | flexBasis = decl "flex-basis"
157 |
158 | export %inline
159 | flexDirection : FlexDirection -> Declaration
160 | flexDirection = decl "flex-direction"
161 |
162 | export %inline
163 | flexWrap : String -> Declaration
164 | flexWrap = Decl "flex-wrap"
165 |
166 | export %inline
167 | flexGrow : Nat -> Declaration
168 | flexGrow = Decl "flex-grow" . show
169 |
170 | export %inline
171 | flexFlow : List FlexFlow -> Declaration
172 | flexFlow = decl "flex-flow"
173 |
174 | export %inline
175 | fontFamily : String -> Declaration
176 | fontFamily = Decl "font-family"
177 |
178 | export %inline
179 | fontSize : FontSize -> Declaration
180 | fontSize = decl "font-size"
181 |
182 | export %inline
183 | fontStyle : FontStyle -> Declaration
184 | fontStyle = decl "font-style"
185 |
186 | export %inline
187 | fontWeight : FontWeight -> Declaration
188 | fontWeight = decl "font-weight"
189 |
190 | export %inline
191 | gridArea : Show a => a -> Declaration
192 | gridArea = Decl "grid-area" . showTag
193 |
194 | export %inline
195 | gridColumn : GridPosition -> Declaration
196 | gridColumn = decl "grid-column"
197 |
198 | export %inline
199 | gridRow : GridPosition -> Declaration
200 | gridRow = decl "grid-row"
201 |
202 | export %inline
203 | gridTemplateColumns : List GridValue -> Declaration
204 | gridTemplateColumns = decl "grid-template-columns"
205 |
206 | export %inline
207 | gridTemplateRows : List GridValue -> Declaration
208 | gridTemplateRows = decl "grid-template-rows"
209 |
210 | export %inline
211 | height : Width -> Declaration
212 | height = decl "height"
213 |
214 | export %inline
215 | justifyContent : FlexJustify -> Declaration
216 | justifyContent = decl "justify-content"
217 |
218 | export %inline
219 | justifySelf : FlexJustify -> Declaration
220 | justifySelf = decl "justify-self"
221 |
222 | export %inline
223 | lineHeight : LineHeight -> Declaration
224 | lineHeight = decl "line-height"
225 |
226 | export %inline
227 | listStyleType : ListStyleType -> Declaration
228 | listStyleType = decl "list-style-type"
229 |
230 | export %inline
231 | margin : Dir Length -> Declaration
232 | margin = dirDecl "margin" interpolate
233 |
234 | export %inline
235 | maxHeight : Width -> Declaration
236 | maxHeight = decl "max-height"
237 |
238 | export %inline
239 | maxWidth : Width -> Declaration
240 | maxWidth = decl "max-width"
241 |
242 | export %inline
243 | minHeight : Width -> Declaration
244 | minHeight = decl "min-height"
245 |
246 | export %inline
247 | minWidth : Width -> Declaration
248 | minWidth = decl "min-width"
249 |
250 | export %inline
251 | outlineColor : Color -> Declaration
252 | outlineColor = decl "outline-color"
253 |
254 | export %inline
255 | outlineStyle : BorderStyle -> Declaration
256 | outlineStyle = decl "outline-style"
257 |
258 | export %inline
259 | outlineWidth : BorderWidth -> Declaration
260 | outlineWidth = decl "outline-width"
261 |
262 | export %inline
263 | overflowX : Overflow -> Declaration
264 | overflowX = decl "overflow-x"
265 |
266 | export %inline
267 | overflowY : Overflow -> Declaration
268 | overflowY = decl "overflow-y"
269 |
270 | export %inline
271 | padding : Dir Length -> Declaration
272 | padding = dirDecl "padding" interpolate
273 |
274 | export %inline
275 | rowGap : Length -> Declaration
276 | rowGap = decl "row-gap"
277 |
278 | export
279 | stroke : Maybe Color -> Declaration
280 | stroke Nothing  = decl "stroke" "none"
281 | stroke (Just c) = decl "stroke" c
282 |
283 | export %inline
284 | strokeWidth : Width -> Declaration
285 | strokeWidth = decl "stroke-width"
286 |
287 | export %inline
288 | strokeLinecap : Linecap -> Declaration
289 | strokeLinecap = decl "stroke-linecap"
290 |
291 | export %inline
292 | strokeLinejoin : Linejoin -> Declaration
293 | strokeLinejoin = decl "stroke-linejoin"
294 |
295 | export %inline
296 | strokeMiterlimit : Double -> Declaration
297 | strokeMiterlimit = decl "stroke-miterlimit" . show
298 |
299 | export %inline
300 | textAlign : TextAlign -> Declaration
301 | textAlign = decl "text-align"
302 |
303 | export %inline
304 | textDecoration : String -> Declaration
305 | textDecoration = Decl "text-decoration"
306 |
307 | export %inline
308 | textDecorationColor : Color -> Declaration
309 | textDecorationColor = decl "text-decoration-color"
310 |
311 | export %inline
312 | textDecorationLine : TextDecorationLine -> Declaration
313 | textDecorationLine = decl "text-decoration-line"
314 |
315 | export %inline
316 | textDecorationStyle : TextDecorationStyle -> Declaration
317 | textDecorationStyle = decl "text-decoration-style"
318 |
319 | export %inline
320 | textOverflow : TextOverflow -> Declaration
321 | textOverflow = decl "text-overflow"
322 |
323 | export %inline
324 | textOverflow2 : TextOverflow -> TextOverflow -> Declaration
325 | textOverflow2 x y = Decl "text-overflow" "\{x} \{y}"
326 |
327 | export %inline
328 | width : Width -> Declaration
329 | width = decl "width"
330 |
331 | export %inline
332 | whitespace : WhiteSpace -> Declaration
333 | whitespace = decl "white-space"
334 |