| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Miso.Types
Description
Overview
Miso.Types defines every core type that miso applications are built from. It is re-exported in its entirety by Miso, so most application code never needs to import it directly.
The Component record
is the central record type. It
wires together the MVU loop and all supporting runtime configuration:Component parent props model action
dataComponentparent props model action = Component { model :: model , hydrateModel :: Maybe (IO model) , update :: action ->Effectparent props model action , view :: props -> model ->Viewmodel action , subs :: [Subaction] , styles :: [CSS] , scripts :: [JS] , mountPoint :: MaybeMountPoint, logLevel ::LogLevel, mailbox :: Value -> Maybe action , bindings :: [Bindingparent model] , eventPropagation :: Bool , mount :: Maybe action , unmount :: Maybe action , onPropsChanged :: Maybe (props -> props -> action) }
Use the component smart constructor to build one with sane defaults,
then override only the fields you need:
myApp ::AppModel Action myApp = (componentinitialModel update view) {subs= [ mySub ] ,styles= [Href"style.css" False ] }
The View type
is miso's virtual DOM tree. Its four constructors
map to the four node kinds the runtime handles:View model action
VNode— a regular DOM element (<div>,<svg>, …)VText— a text nodeVComp— an embedded childComponentVFrag— a keyless group of siblings (no wrapper element)
Key types at a glance
Component- full MVU application/component record
App- alias for
ComponentROOT() model action ROOT- phantom type marking a top-level component
View- virtual DOM node
Attribute- DOM property, class list, event handler, or style
NamespaceHTML|SVG|MATHMLKey- reconciliation hint for list diffing
CSS- stylesheet reference (
Href,Style,Sheet) JS- script reference (
Src,Script,Module, …) LogLevel- debug verbosity (
Off,DebugHydrate, …) URI- parsed URL (path + query string + fragment)
Text combinators
text— create a text node (HTML-escaped in SSR mode)textRaw— create a text node without HTML escapingtext_— concatenate a list of strings with a space separatortextKey/textKey_— keyed variants for efficient list diffinghtmlEncode— manually escape& " '
Component mounting
"key"— mount a child component with a key+>compmount_— mount without a key (unsafe in dynamic lists)mountWithProps/mountWithProps_— mount with explicitprops
Fragment and keyed combinators
fragment/vfrag— group siblings without a wrapper elementfragment_/vfrag_— keyed fragmentkeyed— attach a reconciliation key to anyView
Conditional view utilities
optionalAttrs— add attributes conditionallyoptionalVoidAttrs— same for void (no-children) elementsoptionalChildren— add children conditionally
See also
- Miso.Effect —
Effect,Sub,Sink - Miso.Html.Element — element smart constructors built on
node - Miso.Html.Property — attribute constructors built on
Attribute - Miso.Html.Render — SSR serialisation via
ToHtml - Miso.Router —
URIparsing and pretty-printing
Synopsis
- type App model action = Component ROOT () model action
- data Component parent props model action = Component {
- model :: model
- hydrateModel :: Maybe (IO model)
- update :: action -> Effect parent props model action
- view :: props -> model -> View model action
- subs :: [Sub action]
- styles :: [CSS]
- scripts :: [JS]
- mountPoint :: Maybe MountPoint
- logLevel :: LogLevel
- mailbox :: Value -> Maybe action
- bindings :: [Binding parent model]
- eventPropagation :: Bool
- mount :: Maybe action
- unmount :: Maybe action
- onPropsChanged :: Maybe (props -> props -> action)
- type ComponentId = Int
- data SomeComponent parent = (Eq model, Eq props) => SomeComponent props (Component parent props model action)
- data View model action
- newtype Key = Key MisoString
- data Attribute action
- = Property MisoString Value
- | ClassList [MisoString]
- | On (Sink action -> VTree -> LogLevel -> Events -> IO ())
- | Styles (Map MisoString MisoString)
- data Namespace
- data CSS
- data JS
- data LogLevel
- newtype VTree = VTree {}
- data VTreeType
- type Tag = MisoString
- type CacheBust = Bool
- type MountPoint = MisoString
- type DOMRef = JSVal
- data ROOT
- type Events = Map MisoString Phase
- data Phase
- data URI = URI {}
- class ToKey key where
- data Binding parent child
- = ParentToChild (parent -> field) (field -> child -> child)
- | ChildToParent (field -> parent -> parent) (child -> field)
- | Bidirectional Precedence (parent -> field) (field -> parent -> parent) (child -> field) (field -> child -> child)
- emptyURI :: URI
- component :: model -> (action -> Effect parent props model action) -> (props -> model -> View model action) -> Component parent props model action
- vcomp :: model -> (action -> Effect parent props model action) -> (props -> model -> View model action) -> Component parent props model action
- (-->) :: Lens parent a -> Lens model a -> Binding parent model
- (<--) :: Lens parent a -> Lens model a -> Binding parent model
- (<-->) :: Lens parent field -> Lens child field -> Binding parent child
- (<--->) :: Lens' parent field -> Lens' child field -> Binding parent child
- (--->) :: Lens' parent field -> Lens' child field -> Binding parent child
- (<---) :: Lens' parent field -> Lens' child field -> Binding parent child
- (+>) :: forall child childAction model action. Eq child => MisoString -> Component model () child childAction -> View model action
- mount_ :: Eq child => Component parent () child childAction -> View parent action
- mountWithProps_ :: (Eq child, Eq props) => MisoString -> props -> Component parent props child action -> View parent a
- mountWithProps :: (Eq child, Eq props) => props -> Component parent props child action -> View parent a
- keyed :: MisoString -> View model action -> View model action
- fragment :: [View model action] -> View model action
- fragment_ :: MisoString -> [View model action] -> View model action
- vfrag :: [View model action] -> View model action
- vfrag_ :: MisoString -> [View model action] -> View model action
- getMountPoint :: Maybe MisoString -> MisoString
- optionalAttrs :: ([Attribute action] -> [View model action] -> View model action) -> [Attribute action] -> Bool -> [Attribute action] -> [View model action] -> View model action
- optionalVoidAttrs :: ([Attribute action] -> View model action) -> [Attribute action] -> Bool -> [Attribute action] -> View model action
- optionalChildren :: ([Attribute action] -> [View model action] -> View model action) -> [Attribute action] -> [View model action] -> Bool -> [View model action] -> View model action
- prettyURI :: URI -> MisoString
- prettyQueryString :: URI -> MisoString
- node :: Namespace -> MisoString -> [Attribute action] -> [View model action] -> View model action
- vnode :: Namespace -> MisoString -> [Attribute action] -> [View model action] -> View model action
- text :: MisoString -> View model action
- vtext :: MisoString -> View model action
- text_ :: [MisoString] -> View model action
- textRaw :: MisoString -> View model action
- textKey :: ToKey key => key -> MisoString -> View model action
- textKey_ :: ToKey key => key -> [MisoString] -> View model action
- htmlEncode :: MisoString -> MisoString
- type MisoString = Text
- toMisoString :: ToMisoString str => str -> MisoString
- fromMisoString :: FromMisoString a => MisoString -> a
- ms :: ToMisoString str => str -> MisoString
Types
data Component parent props model action Source #
Application entry point
Constructors
| Component | |
Fields
| |
type ComponentId = Int Source #
ComponentId of the current Component
data SomeComponent parent Source #
Constructors
| (Eq model, Eq props) => SomeComponent props (Component parent props model action) |
data View model action Source #
Core type for constructing a virtual DOM in Haskell
Constructors
| VNode Namespace Tag [Attribute action] [View model action] | |
| VText (Maybe Key) MisoString | |
| VComp (Maybe Key) (SomeComponent model) | |
| VFrag (Maybe Key) [View model action] |
Instances
| Functor (View model) Source # | |
| ToHtml [View m a] Source # | Render a |
Defined in Miso.Html.Render Methods toHtml :: [View m a] -> ByteString Source # | |
| IsString (View model action) Source # |
|
Defined in Miso.Types Methods fromString :: String -> View model action # | |
| ToHtml (View m a) Source # | Render a |
Defined in Miso.Html.Render Methods toHtml :: View m a -> ByteString Source # | |
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
| IsString Key Source # | |
Defined in Miso.Types Methods fromString :: String -> Key # | |
| Show Key Source # | |
| Eq Key Source # | |
| ToJSVal Key Source # | ToJSVal instance for |
| ToJSON Key Source # | |
Defined in Miso.Types | |
| ToMisoString Key Source # | |
Defined in Miso.Types Methods toMisoString :: Key -> MisoString Source # | |
| ToKey Key Source # | Identity instance |
data Attribute action Source #
Attribute of a vnode in a View.
Constructors
| Property MisoString Value | |
| ClassList [MisoString] | |
| On (Sink action -> VTree -> LogLevel -> Events -> IO ()) | The |
| Styles (Map MisoString MisoString) |
DOM element namespace.
Allow users to express CSS and append it to <head> before the first draw
'Href' "http://domain.com/style.css" ('True' :: 'CacheBust')
'Style' "body { background-color: red; }"Constructors
| Href MisoString CacheBust |
|
| Style MisoString | |
| Sheet StyleSheet |
Allow users to express JS and append it to <head> before the first draw
This is meant to be useful in development only.
Src"http://example.com/script.js" (False::CacheBust)Script"alert("hi");"ImportMap[ "key"=:"value" ]Module"console.log("hi");"
Since: 1.9.0.0
Constructors
| Src MisoString CacheBust | URL linking to hosted JS |
| Script MisoString | Raw JS content that you would enter in a <script> tag |
| Module MisoString | Raw JS module content that you would enter in a <script type="module"> tag. See script type |
| ImportMap [(MisoString, MisoString)] | Import map content in a <script type="importmap"> tag. See importmap |
Logging configuration for debugging Miso internals (useful to see if prerendering is successful)
Constructors
| Off | No debug logging, the default value used in |
| DebugHydrate | Will warn if the structure or properties of the DOM vs. Virtual DOM differ during prerendering. |
| DebugEvents | Will warn if an event cannot be routed to the Haskell event handler that raised it. Also will warn if an event handler is being used, yet it's not being listened for by the event delegator mount point. |
| DebugAll | Logs on all of the above |
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 | |
VTreeType ADT for matching TypeScript enum
type Tag = MisoString Source #
Tag type, (e.g. div_, p_)
Meant to indicate the type of element being created.
Used as the first argument to document.createElement for the web backend.
type CacheBust = Bool Source #
Parameter used to indicate cache busting logic should be used.
If True this will append a timestamp to the query. This will force cache
invalidation on the browser, causing a fetch of the resources.
type MountPoint = MisoString Source #
mountPoint for Component, e.g "body"
Phase during which event listener is invoked.
Since: 1.9.0.0
URI type. See the official specification
Constructors
| URI | |
Fields
| |
Instances
Classes
class ToKey key where Source #
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 (different inputs must map to different outputs).
Data Bindings
data Binding parent child Source #
A Binding encodes a directed or bidirectional synchronization edge
between a field in a parent model and a field in a child model. The
field is projected via a pair of lenses that must share a common type.
After each update cycle the runtime evaluates all bindings registered
in a Component and propagates field values along each edge according
to its direction. On initial mount the Precedence value determines
which side wins when both fields carry different values.
Construct bindings using the operator combinators rather than the data
constructors directly. For miso Lens:
For van Laarhoven Lens':
Since: 1.9.0.0
Constructors
| ParentToChild (parent -> field) (field -> child -> child) | |
| ChildToParent (field -> parent -> parent) (child -> field) | |
| Bidirectional Precedence (parent -> field) (field -> parent -> parent) (child -> field) (field -> child -> child) |
Smart Constructors
Arguments
| :: model | model |
| -> (action -> Effect parent props model action) | update |
| -> (props -> model -> View model action) | view |
| -> Component parent props model action |
Smart constructor for Component with sane defaults.
Arguments
| :: model | model |
| -> (action -> Effect parent props model action) | update |
| -> (props -> model -> View model action) | view |
| -> Component parent props model action |
Synonym for component
(<-->) :: Lens parent field -> Lens child field -> Binding parent child infix 0 Source #
Constructs a bidirectional Binding between a parent field and a
child field using the miso Lens representation. Changes
to either field are propagated to the other after each update cycle.
On Component mount, the parent field takes Precedence over the
child field. Use to invert this and let the child win, or
<--> to state the parent precedence explicitly.<<-->
For van Laarhoven lenses use .<--->
Since: 1.9.0.0
(<--->) :: Lens' parent field -> Lens' child field -> Binding parent child infix 0 Source #
Constructs a bidirectional Binding between a parent field and a
child field using the van Laarhoven Lens' representation.
Changes to either field are propagated to the other after each update
cycle.
On Component mount, the parent field takes Precedence over the
child field. Use to invert this and let the child win, or
<<---> to state the parent <--->>Precedence explicitly.
For miso lenses use .<-->
Since: 1.9.0.0
Component mounting
Arguments
| :: forall child childAction model action. Eq child | |
| => MisoString |
|
| -> Component model () child childAction | |
| -> View model action |
Arguments
| :: Eq child | |
| => Component parent () child childAction |
|
| -> View parent action |
Component mounting combinator.
Note: only use this if you're certain you won't be diffing two Component
against each other. Otherwise, you will need a key to distinguish between
the two Component, to ensure unmounting and mounting occurs.
mount_ $ component model noop $ \m -> div_ [ id_ "foo" ] [ text (ms m) ]
Since: 1.9.0.0
Arguments
| :: (Eq child, Eq props) | |
| => MisoString |
|
| -> props |
|
| -> Component parent props child action |
|
| -> View parent a |
Component mounting combinator.
mountWithProps_ "key" someProps $ component model noop $ \m -> div_ [ id_ "foo" ] [ text (ms m) ]
Since: 1.11.0.0
Arguments
| :: (Eq child, Eq props) | |
| => props |
|
| -> Component parent props child action |
|
| -> View parent a |
Component mounting combinator.
Note: only use this if you're certain you won't be diffing two Component
against each other. Otherwise, you will need a key to distinguish between
the two Component, to ensure unmounting and mounting occurs.
mountWithProps someProps $ component model noop $ \m -> div_ [ id_ "foo" ] [ text (ms m) ]
Since: 1.11.0.0
Key combinators
Fragment combinators
fragment :: [View model action] -> View model action Source #
Create a fragment (keyless).
A fragment groups multiple sibling View nodes without introducing
an extra DOM element.
Since: 1.10.0.0
fragment_ :: MisoString -> [View model action] -> View model action Source #
Like fragment, but keyed for efficient diffing.
Since: 1.10.0.0
vfrag :: [View model action] -> View model action Source #
Create a fragment (keyless).
A fragment groups multiple sibling View nodes without introducing
an extra DOM element.
Synonym for fragment
Since: 1.10.0.0
vfrag_ :: MisoString -> [View model action] -> View model action Source #
Like fragment, but keyed for efficient diffing.
Since: 1.10.0.0
Utils
getMountPoint :: Maybe MisoString -> MisoString Source #
Convenience for extracting mount point
Arguments
| :: ([Attribute action] -> [View model action] -> View model action) | |
| -> [Attribute action] | Attributes to be added unconditionally |
| -> Bool | A condition |
| -> [Attribute action] | Additional attributes to add if the condition is True |
| -> [View model action] | Children |
| -> View model action |
Utility function to make it easy to specify conditional attributes
view :: Bool -> View model action view danger = optionalAttrs div_ [ id_ "some-div" ] danger [ class_ "danger" ] ["child"]
Since: 1.9.0.0
Arguments
| :: ([Attribute action] -> View model action) | |
| -> [Attribute action] | Attributes to be added unconditionally |
| -> Bool | A condition |
| -> [Attribute action] | Additional attributes to add if the condition is True |
| -> View model action |
Utility function to make it easy to specify conditional attributes for void elements.
view :: Bool -> View model action view shouldClear = optionalVoidAttrs textarea_ [ value_ "" ] shouldClear [ id_ "text-area-id" ]
Since: 1.9.0.0
Arguments
| :: ([Attribute action] -> [View model action] -> View model action) | |
| -> [Attribute action] | Attributes to be added unconditionally |
| -> [View model action] | Children to be added unconditionally |
| -> Bool | A condition |
| -> [View model action] | Additional children to add if the condition is True |
| -> View model action |
Conditionally adds children.
view :: Bool -> View model action view withChild = optionalChildren div_ [ id_ "txt" ] [] withChild [ "foo" ]
Since: 1.9.0.0
prettyQueryString :: URI -> MisoString Source #
Pretty-prints a URI query string.
Combinators
Arguments
| :: Namespace | Element namespace ( |
| -> MisoString | Tag name (e.g. |
| -> [Attribute action] | Attributes, properties, and event handlers |
| -> [View model action] | Child nodes |
| -> View model action |
Create a new VNode.
node ns tag attrs children creates a new node with tag tag
in the namespace ns. All attrs are called when
the node is created and its children are initialized to children.
Arguments
| :: Namespace | Element namespace ( |
| -> MisoString | Tag name (e.g. |
| -> [Attribute action] | Attributes, properties, and event handlers |
| -> [View model action] | Child nodes |
| -> View model action |
text_ :: [MisoString] -> View model action Source #
Create a new VText containing concatenation of the given strings.
view :: View model action
view = div_
[ className "container" ]
[ text_
[ "foo"
, "bar"
]
]
Renders as class="container"foo bar/div
A single additional space is added between elements.
textRaw :: MisoString -> View model action Source #
textKey :: ToKey key => key -> MisoString -> View model action Source #
Like text, but allow the node to be keyed for efficient diffing.
view :: model -> View model action view = x -> div_ [] [ textKey (1 :: Int) "text here" ]
Since: 1.9.0.0
textKey_ :: ToKey key => key -> [MisoString] -> View model action Source #
Like text_, but allow the node to be keyed for efficient diffing.
view :: model -> View model action view = x -> div_ [] [ textKey_ (1 :: Int) [ "text", "goes", "here" ] ]
Since: 1.9.0.0
htmlEncode :: MisoString -> MisoString Source #
HTML-encodes text.
Useful for escaping HTML when delivering on the server. Naive usage
of text will ensure this as well.
>>>Data.Text.IO.putStrLn $ text "<a href=\"\">"<a href="">
MisoString
type MisoString = Text Source #
The primary string type in Miso applications.
1(server/SSR build): alias forText- WASM / GHC JS backend: alias for
JSString— a zero-copy wrapper around a native JavaScript string, giving optimal interop with the DOM and JSON APIs
toMisoString :: ToMisoString str => str -> MisoString Source #
Convert a value to MisoString.
fromMisoString :: FromMisoString a => MisoString -> a Source #
Parse a MisoString, throwing an error on failure.
Use fromMisoStringEither as a safe alternative.
ms :: ToMisoString str => str -> MisoString Source #
Short alias for toMisoString. The idiomatic way to construct a MisoString.