miso-1.3.0.0: A tasty Haskell front-end framework

Copyright(C) 2016-2018 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <djohnson.m@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.Html

Contents

Description

Example usage:

import Miso

data IntAction = Add | Subtract

intView :: Int -> View IntAction
intView n = div_ [ class_ "main" ] [
   btn_ [ onClick Add ] [ text_ "+" ]
 , text_ $ pack (show n)
 , btn_ [ onClick Subtract ] [ text_ "-" ]
 ]

More information on how to use miso is available on GitHub

http://github.com/dmjio/miso

Synopsis

Documentation

Core types and interface

newtype VTree #

Virtual DOM implemented as a JavaScript Object. Used for diffing, patching and event delegation. Not meant to be constructed directly, see View instead.

Constructors

VTree 

Fields

newtype View action #

Core type for constructing a VTree, use this instead of VTree directly.

Constructors

View 

Fields

Instances
Functor View # 
Instance details

Defined in Miso.Html.Internal

Methods

fmap :: (a -> b) -> View a -> View b #

(<$) :: a -> View b -> View a #

IsString (View a) #

IsString instance

Instance details

Defined in Miso.Html.Internal

Methods

fromString :: String -> View a #

HasLink (View a :: Type) #

For constructing type-safe links

Instance details

Defined in Miso.Html.Internal

Associated Types

type MkLink (View a) a :: Type #

Methods

toLink :: (Link -> a0) -> Proxy (View a) -> Link -> MkLink (View a) a0 #

HasRouter (View a :: Type) #

View

Instance details

Defined in Miso.Router

Associated Types

type RouteT (View a) a :: Type #

Methods

mkRouter :: Proxy (View a) -> Proxy a0 -> RouteT (View a) a0 -> Router a0

type MkLink (View a :: Type) b # 
Instance details

Defined in Miso.Html.Internal

type MkLink (View a :: Type) b = MkLink (Get ([] :: [Type]) ()) b
type RouteT (View a :: Type) x # 
Instance details

Defined in Miso.Router

type RouteT (View a :: Type) x = x

class ToView v where #

Convenience class for using View

Methods

toView :: v -> View m #

newtype Attribute action #

Attribute of a vnode in a View.

The Sink callback can be used to dispatch actions which are fed back to the update function. This is especially useful for event handlers like the onclick attribute. The second argument represents the vnode the attribute is attached to.

Constructors

Attribute (Sink action -> Object -> JSM ()) 

Smart View constructors

node :: NS -> MisoString -> Maybe Key -> [Attribute m] -> [View m] -> View m #

Create a new VNode.

node ns tag key attrs children creates a new node with tag tag and Key key in the namespace ns. All attrs are called when the node is created and its children are initialized to children.

text :: MisoString -> View m #

Create a new VText with the given content.

Key patch internals

newtype Key #

A unique key for a dom node.

This key is only used to speed up diffing the children of a DOM node, the actual content is not important. The keys of the children of a given DOM node must be unique. Failure to satisfy this invariant gives undefined behavior at runtime.

Constructors

Key MisoString 
Instances
ToJSVal Key # 
Instance details

Defined in Miso.Html.Internal

Methods

toJSVal :: Key -> IO JSVal #

toJSValListOf :: [Key] -> IO JSVal #

ToKey Key #

Identity instance

Instance details

Defined in Miso.Html.Internal

Methods

toKey :: Key -> Key #

class ToKey key where #

Convert custom key types to Key.

Instances of this class do not have to guarantee uniqueness of the generated keys, it is up to the user to do so. toKey must be an injective function.

Methods

toKey :: key -> Key #

Instances
ToKey Double #

Convert Double to Key

Instance details

Defined in Miso.Html.Internal

Methods

toKey :: Double -> Key #

ToKey Float #

Convert Float to Key

Instance details

Defined in Miso.Html.Internal

Methods

toKey :: Float -> Key #

ToKey Int #

Convert Int to Key

Instance details

Defined in Miso.Html.Internal

Methods

toKey :: Int -> Key #

ToKey Word #

Convert Word to Key

Instance details

Defined in Miso.Html.Internal

Methods

toKey :: Word -> Key #

ToKey String #

Convert String to Key

Instance details

Defined in Miso.Html.Internal

Methods

toKey :: String -> Key #

ToKey Text #

Convert Text to Key

Instance details

Defined in Miso.Html.Internal

Methods

toKey :: Text -> Key #

ToKey MisoString #

Convert MisoString to Key

Instance details

Defined in Miso.Html.Internal

Methods

toKey :: MisoString -> Key #

ToKey Key #

Identity instance

Instance details

Defined in Miso.Html.Internal

Methods

toKey :: Key -> Key #

Namespace

data NS #

Namespace of DOM elements.

Constructors

HTML

HTML Namespace

SVG

SVG Namespace

MATHML

MATHML Namespace

Instances
Eq NS # 
Instance details

Defined in Miso.Html.Internal

Methods

(==) :: NS -> NS -> Bool #

(/=) :: NS -> NS -> Bool #

Show NS # 
Instance details

Defined in Miso.Html.Internal

Methods

showsPrec :: Int -> NS -> ShowS #

show :: NS -> String #

showList :: [NS] -> ShowS #

ToJSVal NS # 
Instance details

Defined in Miso.Html.Internal

Methods

toJSVal :: NS -> IO JSVal #

toJSValListOf :: [NS] -> IO JSVal #

Setting properties on virtual DOM nodes

prop :: ToJSVal a => MisoString -> a -> Attribute action #

prop k v is an attribute that will set the attribute k of the DOM node associated with the vnode to v.

Setting css

style_ :: Map MisoString MisoString -> Attribute action #

style_ attrs is an attribute that will set the style attribute of the associated DOM node to attrs.

style attributes not contained in attrs will be deleted.

import qualified Data.Map as M
div_ [ style_  $ M.singleton "background" "red" ] [ ]

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

Handling events

on :: MisoString -> Decoder r -> (r -> action) -> Attribute action #

Convenience wrapper for onWithOptions defaultOptions.

let clickHandler = on "click" emptyDecoder $ \() -> Action
in button_ [ clickHandler, class_ "add" ] [ text_ "+" ]

onWithOptions :: Options -> MisoString -> Decoder r -> (r -> action) -> Attribute action #

onWithOptions opts eventName decoder toAction is an attribute that will set the event handler of the associated DOM node to a function that decodes its argument using decoder, converts it to an action using toAction and then feeds that action back to the update function.

opts can be used to disable further event propagation.

let clickHandler = onWithOptions defaultOptions "click" emptyDecoder $ \() -> Action
in button_ [ clickHandler, class_ "add" ] [ text_ "+" ]

Life cycle events

onCreated :: action -> Attribute action #

onCreated action is an event that gets called after the actual DOM element is created.

Important note: Any node that uses this event MUST have a unique Key, otherwise the event may not be reliably called!

onDestroyed :: action -> Attribute action #

onDestroyed action is an event that gets called after the DOM element is removed from the DOM. The action is given the DOM element that was removed from the DOM tree.

Important note: Any node that uses this event MUST have a unique Key, otherwise the event may not be reliably called!

onBeforeDestroyed :: action -> Attribute action #

onBeforeDestroyed action is an event that gets called before the DOM element is removed from the DOM. The action is given the DOM element that was removed from the DOM tree.

Important note: Any node that uses this event MUST have a unique Key, otherwise the event may not be reliably called!

Events

defaultEvents :: Map MisoString Bool #

Default delegated events

Subscription type

type Sub action = Sink action -> JSM () #

Type synonym for constructing event subscriptions.

The Sink callback is used to dispatch actions which are then fed back to the update function.

boolProp :: MisoString -> Bool -> Attribute action #

Set field to Bool value

stringProp :: MisoString -> String -> Attribute action #

Set field to String value

textProp :: MisoString -> MisoString -> Attribute action #

Set field to Text value

intProp :: MisoString -> Int -> Attribute action #

Set field to Int value

integerProp :: MisoString -> Int -> Attribute action #

Set field to Integer value

doubleProp :: MisoString -> Double -> Attribute action #

Set field to Double value

classList_ :: [(MisoString, Bool)] -> Attribute action #

Define multiple classes conditionally

div_ [ classList_ [ ("empty", null items) ] [ ]