| 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 context props model action
dataComponentcontext props model action = Component { model :: model , hydrateModel :: Maybe (IO model) , update :: action ->Effectcontext props model action , view :: context -> props -> model ->Viewcontext action , useContext :: Bool , subs :: [Subaction] , styles :: [CSS] , scripts :: [JS] , mountPoint :: MaybeMountPoint, logLevel ::LogLevel, mailbox :: Value -> Maybe action , 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 context 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
Component() () model action 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 () () model action
- data Component context props model action = Component {
- model :: model
- hydrateModel :: Maybe (IO model)
- update :: action -> Effect context props model action
- view :: context -> props -> model -> View context action
- useContext :: Bool
- subs :: [Sub action]
- styles :: [CSS]
- scripts :: [JS]
- mountPoint :: Maybe MountPoint
- logLevel :: LogLevel
- mailbox :: Value -> Maybe action
- eventPropagation :: Bool
- mount :: Maybe action
- unmount :: Maybe action
- onPropsChanged :: Maybe (props -> props -> action)
- type ComponentId = Int
- data SomeComponent context = (Eq context, Eq model, Eq props) => SomeComponent props (Component context props model action)
- data View context 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
- type Events = Map MisoString Phase
- data Phase
- data URI = URI {}
- class ToKey key where
- emptyURI :: URI
- component :: model -> (action -> Effect context props model action) -> (context -> props -> model -> View context action) -> Component context props model action
- vcomp :: model -> (action -> Effect context props model action) -> (context -> props -> model -> View context action) -> Component context props model action
- (+>) :: (Eq context, Eq model) => MisoString -> Component context () model action -> View context a
- mount_ :: (Eq context, Eq model) => Component context () model action -> View context a
- mountUseContext :: (Eq context, Eq model) => Component context () model action -> View context a
- mountWithProps_ :: (Eq context, Eq model, Eq props) => MisoString -> props -> Component context props model action -> View context a
- mountWithProps :: (Eq context, Eq model, Eq props) => props -> Component context props model action -> View context a
- keyed :: MisoString -> View context action -> View context action
- fragment :: [View context action] -> View context action
- fragment_ :: MisoString -> [View context action] -> View context action
- vfrag :: [View context action] -> View context action
- vfrag_ :: MisoString -> [View context action] -> View context action
- getMountPoint :: Maybe MisoString -> MisoString
- optionalAttrs :: ([Attribute action] -> [View context action] -> View context action) -> [Attribute action] -> Bool -> [Attribute action] -> [View context action] -> View context action
- optionalVoidAttrs :: ([Attribute action] -> View context action) -> [Attribute action] -> Bool -> [Attribute action] -> View context action
- optionalChildren :: ([Attribute action] -> [View context action] -> View context action) -> [Attribute action] -> [View context action] -> Bool -> [View context action] -> View context action
- prettyURI :: URI -> MisoString
- prettyQueryString :: URI -> MisoString
- node :: Namespace -> MisoString -> [Attribute action] -> [View context action] -> View context action
- vnode :: Namespace -> MisoString -> [Attribute action] -> [View context action] -> View context action
- text :: MisoString -> View context action
- vtext :: MisoString -> View context action
- text_ :: [MisoString] -> View context action
- textRaw :: MisoString -> View context action
- textKey :: ToKey key => key -> MisoString -> View context action
- textKey_ :: ToKey key => key -> [MisoString] -> View context action
- htmlEncode :: MisoString -> MisoString
- type MisoString = Text
- toMisoString :: ToMisoString str => str -> MisoString
- fromMisoString :: FromMisoString a => MisoString -> a
- ms :: ToMisoString str => str -> MisoString
Types
type App model action = Component () () model action Source #
A miso application is a top-level Component. Its app-global
context defaults to () (see startAppWithContext to supply a
non-trivial context), and its props are fixed to ().
data Component context props model action Source #
Application entry point
Constructors
| Component | |
Fields
| |
type ComponentId = Int Source #
ComponentId of the current Component
data SomeComponent context Source #
Existential wrapper allowing nesting of Component in Component.
The context type parameter is shared with the enclosing View, so every
nested Component participates in the same app-global context.
Constructors
| (Eq context, Eq model, Eq props) => SomeComponent props (Component context props model action) |
data View context action Source #
Core type for constructing a virtual DOM in Haskell
Constructors
| VNode Namespace Tag [Attribute action] [View context action] | |
| VText (Maybe Key) MisoString | |
| VComp (Maybe Key) (SomeComponent context) | |
| VFrag (Maybe Key) [View context action] |
Instances
| Functor (View context) Source # | |
| ToHtml [View m a] Source # | Render a |
Defined in Miso.Html.Render Methods toHtml :: [View m a] -> ByteString Source # | |
| IsString (View context action) Source # |
|
Defined in Miso.Types Methods fromString :: String -> View context 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).
Smart Constructors
Arguments
| :: model | model |
| -> (action -> Effect context props model action) | update |
| -> (context -> props -> model -> View context action) | view |
| -> Component context props model action |
Smart constructor for Component with sane defaults.
Arguments
| :: model | model |
| -> (action -> Effect context props model action) | update |
| -> (context -> props -> model -> View context action) | view |
| -> Component context props model action |
Synonym for component
Component mounting
Arguments
| :: (Eq context, Eq model) | |
| => Component context () model action |
|
| -> View context 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.
mount_ $ component model noop $ \m -> div_ [ id_ "foo" ] [ text (ms m) ]
Since: 1.9.0.0
Arguments
| :: (Eq context, Eq model) | |
| => Component context () model action |
|
| -> View context a |
Component mounting combinator that opts the child into
app-global React-style context updates.
Equivalent to mount_, but sets useContext = True on the mounted
Component so it re-renders whenever the context changes
(see modifyContext). Like mount_, this is unkeyed and so
unsafe when diffing two Component against each other.
mountUseContext $ component model noop $ \ctx m -> div_ [ id_ "foo" ] [ text (ms m) ]
Since: 1.9.0.0
Arguments
| :: (Eq context, Eq model, Eq props) | |
| => MisoString |
|
| -> props |
|
| -> Component context props model action |
|
| -> View context a |
Component mounting combinator.
mountWithProps_ "key" someProps $ component model noop $ \m -> div_ [ id_ "foo" ] [ text (ms m) ]
Since: 1.11.0.0
Arguments
| :: (Eq context, Eq model, Eq props) | |
| => props |
|
| -> Component context props model action |
|
| -> View context 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 context action] -> View context 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 context action] -> View context action Source #
Like fragment, but keyed for efficient diffing.
Since: 1.10.0.0
vfrag :: [View context action] -> View context 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 context action] -> View context 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 context action] -> View context action) | |
| -> [Attribute action] | Attributes to be added unconditionally |
| -> Bool | A condition |
| -> [Attribute action] | Additional attributes to add if the condition is True |
| -> [View context action] | Children |
| -> View context action |
Utility function to make it easy to specify conditional attributes
view :: Bool -> View context action view danger = optionalAttrs div_ [ id_ "some-div" ] danger [ class_ "danger" ] ["child"]
Since: 1.9.0.0
Arguments
| :: ([Attribute action] -> View context action) | |
| -> [Attribute action] | Attributes to be added unconditionally |
| -> Bool | A condition |
| -> [Attribute action] | Additional attributes to add if the condition is True |
| -> View context action |
Utility function to make it easy to specify conditional attributes for void elements.
view :: Bool -> View context action view shouldClear = optionalVoidAttrs textarea_ [ value_ "" ] shouldClear [ id_ "text-area-id" ]
Since: 1.9.0.0
Arguments
| :: ([Attribute action] -> [View context action] -> View context action) | |
| -> [Attribute action] | Attributes to be added unconditionally |
| -> [View context action] | Children to be added unconditionally |
| -> Bool | A condition |
| -> [View context action] | Additional children to add if the condition is True |
| -> View context action |
Conditionally adds children.
view :: Bool -> View context 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 context action] | Child nodes |
| -> View context 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 context action] | Child nodes |
| -> View context action |
text_ :: [MisoString] -> View context action Source #
Create a new VText containing concatenation of the given strings.
view :: View context 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 context action Source #
textKey :: ToKey key => key -> MisoString -> View context action Source #
Like text, but allow the node to be keyed for efficient diffing.
view :: model -> View context action view = x -> div_ [] [ textKey (1 :: Int) "text here" ]
Since: 1.9.0.0
textKey_ :: ToKey key => key -> [MisoString] -> View context action Source #
Like text_, but allow the node to be keyed for efficient diffing.
view :: model -> View context 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.