miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.CSS

Description

Overview

Miso.CSS is a typed DSL for constructing CSS properties, stylesheets, animations, and media queries in miso applications. Two styling modes are available:

  • Structured styles (style_): CSS properties are stored in a Map and diffed by the virtual DOM, so only changed properties are written to the DOM node on each render. Prefer this for dynamic styles.
  • Inline string styles (styleInline_): a raw CSS string is set on the style attribute verbatim and is not diffed. Useful for static one-liners or values that the structured combinators do not yet cover.

Quick start

import qualified Miso.CSS       as CSS
import           Miso.CSS.Color (red, rgba)

myView :: View Model Action
myView =
  div_
    [ CSS.style_
        [ CSS.display        "flex"
        , CSS.flexDirection  "column"
        , CSS.gap            (CSS.px 8)
        , CSS.padding        (CSS.px 16)
        , CSS.backgroundColor red
        , CSS.borderRadius   (CSS.px 4)
        ]
    ] [ text "Hello, miso!" ]

Global Stylesheets

Construct a StyleSheet with sheet_ and selector_, then render it to a MisoString with renderStyleSheet for injection into a <style> tag:

mySheet :: StyleSheet
mySheet = sheet_
  [ selector_ "body"
      [ CSS.margin     (CSS.px 0)
      , CSS.fontFamily "sans-serif"
      ]
  , selector_ ".card"
      [ CSS.backgroundColor (rgba 255 255 255 0.9)
      , CSS.borderRadius    (CSS.px 4)
      ]
  ]

Animations and Media Queries

myAnimation :: Styles
myAnimation = keyframes_ "slide-in"
  [ from_ [ CSS.transform "translateX(-100%)" ]
  , to_   [ CSS.transform "translateX(0)" ]
  ]

myMedia :: Styles
myMedia = media_ (screen_ `and_` minWidth_ (CSS.px 480))
  [ rule_ "header" [ CSS.height "auto" ]
  , rule_ "nav"    [ CSS.display "flex" ]
  ]

CSS Units

Use the unit helpers to build length and time values: px, pt, em, rem, vh, vw, pct, ms, s, deg, rad, turn.

CSS.style_
  [ CSS.width      (CSS.pct 100)
  , CSS.fontSize   (CSS.rem 1.5)
  , CSS.transition ("opacity " <> CSS.ms 300 <> " ease")
  ]

Colors

The Color type and named colors are re-exported from Miso.CSS.Color:

CSS.backgroundColor (CSS.rgb 30 144 255)
CSS.color           CSS.white
CSS.borderColor     (CSS.rgba 0 0 0 0.2)

Note: color in this module and color share the same name but have different types. Always qualify import qualified Miso.CSS as CSS when also importing Miso.Canvas.

Synopsis

Types

Smart Constructor

style_ :: [Style] -> Attribute action Source #

Constructs a structured style attribute from a list of CSS properties.

Each Style is a (property, value) pair produced by the combinators in this module. Miso tracks the properties as a Map, diffs them on each render, and applies only the changed properties to the DOM. Properties absent from the list are removed from the node.

div_
  [ style_
      [ display       "flex"
      , flexDirection "column"
      , gap           (px 8)
      , backgroundColor red
      ]
  ] []

See also styleInline_ for setting raw CSS strings.

https://developer.mozilla.org/en-US/docs/Web/CSS

styleInline_ :: MisoString -> Attribute action Source #

Sets the style attribute to a raw CSS string.

Unlike style_, the string is applied verbatim and is not tracked or diffed by the virtual DOM. Suitable for static styles or CSS values that the structured combinators do not yet cover.

div_ [ styleInline_ "background-color:red; color:blue;" ] [ "foo" ]

https://developer.mozilla.org/en-US/docs/Web/CSS

sheet_ :: [Styles] -> StyleSheet Source #

Constructs a StyleSheet from a list of Styles entries.

Combine with selector_, keyframes_, and media_ to build a full stylesheet, then render it to a MisoString with renderStyleSheet.

mySheet :: StyleSheet
mySheet = sheet_
  [ selector_ "body"   [ margin (px 0), fontFamily "sans-serif" ]
  , selector_ "button" [ cursor "pointer", borderRadius (px 4) ]
  ]

selector_ :: MisoString -> [Style] -> Styles Source #

Constructs a Styles entry pairing a CSS selector with a list of properties. Combine multiple entries with sheet_.

sheet_
  [ selector_ ".card"  [ backgroundColor white, borderRadius (px 4) ]
  , selector_ ".title" [ fontSize (rem 1.5), fontWeight "bold" ]
  ]

Render

renderStyleSheet :: StyleSheet -> MisoString Source #

Renders a StyleSheet to a MisoString suitable for injection into a <style> tag.

view_ :: View model action
view_ = style [] [ text (renderStyleSheet mySheet) ]

Combinators

fill :: Color -> Style Source #

SVG fill color.

fill red

stroke :: Color -> Style Source #

SVG stroke color.

stroke black

transforms :: [TransformFn] -> Style Source #

Apply a list of TransformFn values as a transform style.

transforms [ translate (px 10) (pct 50), rotate (deg 45), scaleX 1.5 ]

Transform functions

translate :: MisoString -> MisoString -> TransformFn Source #

https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translate

>>> renderTransformFn (translate (px 10) (pct 50))
"translate(10px,50.0%)"

rotate3d :: Double -> Double -> Double -> MisoString -> TransformFn Source #

https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/rotate3d x, y, z are unitless direction vector components; angle uses a unit constructor like deg.

scale :: Double -> TransformFn Source #

https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scale Uniform scale on both axes.

>>> renderTransformFn (scale 1.5)
"scale(1.5)"

perspectiveFn :: MisoString -> TransformFn Source #

https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/perspective The perspective() transform function, distinct from the perspective CSS property.

>>> renderTransformFn (perspectiveFn (px 500))
"perspective(500px)"

matrix3d :: (Double, Double, Double, Double) -> (Double, Double, Double, Double) -> (Double, Double, Double, Double) -> (Double, Double, Double, Double) -> TransformFn Source #

https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix3d 4x4 homogeneous matrix in column-major order; each tuple is one column.

matrix3d (1,0,0,0) (0,1,0,0) (0,0,1,0) (10,20,0,1)

Colors

Units

px :: Int -> MisoString Source #

Font sizing in terms of *px*

>>> px 10
"10px"

ppx :: Double -> MisoString Source #

Physical pixel unit (ppx), used in some native/mobile rendering contexts.

>>> ppx 2.0
"2.0ppx"

pt :: Int -> MisoString Source #

Font sizing in terms of *pt*

>>> pt 10
"10pt"

vw :: Double -> MisoString Source #

Viewport width

>>> vw 10.0
"10.0vw"

vh :: Double -> MisoString Source #

Viewport height

>>> vh 10.0
"10.0vh"

deg :: Double -> MisoString Source #

Degree specification

>>> deg 10
"10deg"

turn :: Double -> MisoString Source #

Turn constructor, useful for specifying rotations

>>> turn 10.0
"10.0turn"

rad :: Double -> MisoString Source #

Radial constructor

>>> rad 10.0
"10.0rad"

rpx :: Double -> MisoString Source #

Responsive pixel sizing, *rpx*

>>> rpx 10.0
"10.0rpx"

rem :: Double -> MisoString Source #

Relative *em* sizing

>>> rem 10.0
"10.0rem"

em :: Double -> MisoString Source #

  • em* sizing
>>> em 10.0
"10.0em"

s :: Double -> MisoString Source #

Duration in seconds

>>> s 10.0
"10.0s"

ms :: Double -> MisoString Source #

Duration in milliseconds

>>> ms 10.0
"10.0ms"

Misc

url :: MisoString -> MisoString Source #

Wraps a value in the CSS url() function, used for background images and similar.

>>> url "dog.png"
"url(dog.png)"
backgroundImage (url "banner.png")

https://developer.mozilla.org/en-US/docs/Web/CSS/url

matrix Source #

Arguments

:: Double

a — scale x

-> Double

b — skew y

-> Double

c — skew x

-> Double

d — scale y

-> Double

tx — translate x

-> Double

ty — translate y

-> MisoString 

Constructs a 2D CSS transformation matrix string: matrix(a, b, c, d, tx, ty).

The six parameters define a 2D affine transformation: a and d scale, b and c skew, and tx/ty translate.

transform (matrix 1 0 0 1 50 100)  -- translate by (50, 100)

https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix

Animation

keyframes_ :: MisoString -> [KeyframeStop] -> Styles Source #

Constructs a CSS @keyframes animation rule.

The first argument is the animation name; the second is a list of (keyframe-selector, [Style]) pairs. Keyframe selectors are either "from"/"to" or a percentage string produced by pct.

slideIn :: Styles
slideIn = keyframes_ "slide-in"
  [ from_ [ transform "translateX(-100%)" ]
  , at (pct 50) [ opacity 0.5 ]
  , to_   [ transform "translateX(0)" ]
  ]

https://developer.mozilla.org/en-US/docs/Web/CSS/@keyframes

from_ :: [Style] -> KeyframeStop Source #

The from stop in a 'keyframes' rule (equivalent to 0%@).

to_ :: [Style] -> KeyframeStop Source #

The to stop in a 'keyframes' rule (equivalent to 100%@).

at :: MisoString -> [Style] -> KeyframeStop Source #

A keyframe stop at a given position, typically built with pct.

at (pct 50) [ opacity 0.5 ]

Media Queries

media_ :: MediaQuery -> [MediaRule] -> Styles Source #

Constructs a CSS @media query rule.

The first argument is the media condition string; the second is a list of (selector, [Style]) pairs scoped to that query.

responsive :: Styles
responsive = media_ (screen_ `and_` minWidth_ (px 480))
  [ rule_ "header" [ height "auto" ]
  , rule_ "ul"     [ display "block" ]
  ]

https://developer.mozilla.org/en-US/docs/Web/CSS/@media

rule_ :: MisoString -> [Style] -> MediaRule Source #

A selector rule inside a media_ block.

rule_ "header" [ height "auto" ]

Media query combinators

screen_ :: MediaQuery Source #

The screen media type.

print_ :: MediaQuery Source #

The print media type.

all_ :: MediaQuery Source #

The all media type (matches all devices).

and_ :: MediaQuery -> MediaQuery -> MediaQuery Source #

Logical and for media queries.

screen_ \`and_\` minWidth_ (px 480)

or_ :: MediaQuery -> MediaQuery -> MediaQuery Source #

Logical or for media queries (comma-separated).

screen_ \`or_\` print_

not_ :: MediaQuery -> MediaQuery Source #

Logical not for media queries.

not_ print_

minWidth_ :: MisoString -> MediaQuery Source #

min-width media feature. Use unit constructors like px or em.

maxWidth_ :: MisoString -> MediaQuery Source #

max-width media feature.

minHeight_ :: MisoString -> MediaQuery Source #

min-height media feature.

maxHeight_ :: MisoString -> MediaQuery Source #

max-height media feature.

orientation_ :: MisoString -> MediaQuery Source #

orientation media feature. Use "portrait" or "landscape".

prefersColorScheme_ :: MisoString -> MediaQuery Source #

prefers-color-scheme media feature. Use "light" or "dark".

prefersReducedMotion_ :: MisoString -> MediaQuery Source #

prefers-reduced-motion media feature. Use "reduce" or "no-preference".

hover_ :: MisoString -> MediaQuery Source #

hover media feature. Use "hover" or "none".