| 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 :: model ->Viewcontext props model 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 ::CacheBust) ] }
The View type
is miso's virtual DOM tree. Five of
its constructors are the node kinds the runtime handles:View context props model action
VNode— a regular DOM element (<div>,<svg>, …)VText— a text nodeVComp— an embedded childComponentVCompStatic— an embedded childComponentbehind aStaticPtr, so it can cross the Lynx dual-thread boundary (seevcomp/mountStatic)VFrag— a group of siblings with no wrapper element, optionally keyed
The remaining three are ambient accessors, not nodes. Each wraps a function that is applied — and the wrapper discarded — when the tree is built or rendered, so none of them ever appears in the virtual DOM:
VContext— reads the app-globalcontextVProps— reads the enclosingComponent'spropsVModel— reads the enclosingComponent'smodel
(ImplicitParams or a Reader could play the same role, at the cost of a
GHC-specific extension or a monadic style for view code; see the
VProps section of the Miso module docs.)
The props parameter is the props type of the Component whose
view produced the tree, exactly as model is that component's model
type. Both are forgotten at a mount boundary (VComp / VCompStatic),
so a parent with props ~ P can freely mount a child with props ~ Q.
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 explicitpropsmountUseContext— mount without a key, subscribed tocontextupdatesvcomp/vcomp_(withmountStatic/mountStaticWithProps) — static-key mounting; the compile-timeStaticKeyalready provides identity, so unlike the non-static combinators there is no keyed variant
__Under the Lynx dual-thread (1) backend, always use vcomp /
vcomp_ — never +> / mount_ / mountWithProps / mountWithProps_ /
mountUseContext.__ The non-static combinators build a component with no
StaticKey; anything mounted with them after the initial
frame (e.g. inside a list or behind a conditional) never registers a
main-thread mirror on the MTS, which silently drops every OnStatic
(main-thread) event handler inside that subtree for the component's whole
lifetime. This is invisible outside of a console error — there is no type
error and no runtime crash. GHC emits a warning at every use site of the
non-static combinators when built with 1 as a reminder.
Fragment combinators
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 :: model -> View context props model action
- useContext :: Bool
- subs :: [Sub model 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 = MountConstraints context props model action => SomeComponent (Maybe Key) props (Component context props model action)
- data SomeStaticComponent props context = MountConstraints context props model action => SomeStaticComponent (Component context props model action)
- type MountConstraints context props model action = (Eq context, Eq props, Eq model, FromJSON props, ToJSON props, FromJSON model, ToJSON model, FromJSON action, ToJSON action)
- data EventHandler model action = EventHandler {
- eventHandlerInstall :: model -> Sink action -> VTree -> LogLevel -> Events -> IO ()
- eventHandlerDecoder :: Decoder result
- eventHandlerConvert :: result -> model -> DOMRef -> action
- data View context props model action
- = VNode Namespace Tag [Attribute model action] [View context props model action] DirectEvents
- | VText (Maybe Key) MisoString
- | VComp (SomeComponent context)
- | VCompStatic (StaticPtr (SomeStaticComponent childProps context)) childProps
- | VFrag (Maybe Key) [View context props model action]
- | VContext (context -> View context props model action)
- | VProps (props -> View context props model action)
- | VModel (model -> View context props model action)
- newtype Key = Key MisoString
- data Attribute model action
- = Property MisoString Value
- | ClassList [MisoString]
- | On (model -> Sink action -> VTree -> LogLevel -> Events -> IO ())
- | OnStatic (StaticPtr (EventHandler model action))
- | Styles (Map MisoString MisoString)
- data Namespace
- data CSS
- data JS
- data LogLevel
- newtype VTree = VTree {}
- data VTreeType
- data Hydrate
- type Tag = MisoString
- type DirectEvents = Set 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) -> (model -> View context props model action) -> Component context props model action
- event :: StaticPtr (EventHandler model action) -> Attribute model action
- vcomp :: childProps -> StaticPtr (SomeStaticComponent childProps context) -> View context props model action
- vcomp_ :: StaticPtr (SomeStaticComponent () context) -> View context props model action
- (+>) :: forall context childModel childAction model action props. (Eq context, Eq childModel, FromJSON childModel, ToJSON childModel, FromJSON childAction, ToJSON childAction) => MisoString -> Component context () childModel childAction -> View context props model action
- mount_ :: forall context childModel childAction model action props. (Eq context, Eq childModel, FromJSON childModel, ToJSON childModel, FromJSON childAction, ToJSON childAction) => Component context () childModel childAction -> View context props model action
- mountUseContext :: forall context childModel childAction model action props. (Eq context, Eq childModel, FromJSON childModel, ToJSON childModel, FromJSON childAction, ToJSON childAction) => Component context () childModel childAction -> View context props model action
- mountWithProps_ :: forall context childProps childModel childAction model action props. (Eq context, Eq childProps, Eq childModel, FromJSON childAction, FromJSON childModel, ToJSON childModel, ToJSON childAction, FromJSON childProps, ToJSON childProps) => MisoString -> childProps -> Component context childProps childModel childAction -> View context props model action
- mountWithProps :: forall context childProps childModel childAction model action props. (Eq context, Eq childProps, Eq childModel, FromJSON childModel, ToJSON childModel, FromJSON childAction, ToJSON childAction, FromJSON childProps, ToJSON childProps) => childProps -> Component context childProps childModel childAction -> View context props model action
- mountStatic :: MountConstraints context props model action => Component context props model action -> SomeStaticComponent props context
- mountStaticWithProps :: MountConstraints context props model action => Component context props model action -> SomeStaticComponent props context
- fragment :: [View context props model action] -> View context props model action
- fragment_ :: MisoString -> [View context props model action] -> View context props model action
- vfrag :: [View context props model action] -> View context props model action
- vfrag_ :: MisoString -> [View context props model action] -> View context props model action
- vcontext :: (context -> View context props model action) -> View context props model action
- withContext :: (context -> View context props model action) -> View context props model action
- vprops :: (props -> View context props model action) -> View context props model action
- withProps :: (props -> View context props model action) -> View context props model action
- vmodel :: (model -> View context props model action) -> View context props model action
- withModel :: (model -> View context props model action) -> View context props model action
- getMountPoint :: Maybe MisoString -> MisoString
- optionalAttrs :: ([Attribute model action] -> [View context props model action] -> View context props model action) -> [Attribute model action] -> Bool -> [Attribute model action] -> [View context props model action] -> View context props model action
- optionalVoidAttrs :: ([Attribute model action] -> View context props model action) -> [Attribute model action] -> Bool -> [Attribute model action] -> View context props model action
- optionalChildren :: ([Attribute model action] -> [View context props model action] -> View context props model action) -> [Attribute model action] -> [View context props model action] -> Bool -> [View context props model action] -> View context props model action
- prettyURI :: URI -> MisoString
- prettyQueryString :: URI -> MisoString
- node :: Namespace -> MisoString -> [Attribute model action] -> [View context props model action] -> View context props model action
- nodeDirectEvents :: Namespace -> MisoString -> [Attribute model action] -> [MisoString] -> [View context props model action] -> View context props model action
- vnode :: Namespace -> MisoString -> [Attribute model action] -> [View context props model action] -> View context props model action
- text :: MisoString -> View context props model action
- vtext :: MisoString -> View context props model action
- text_ :: [MisoString] -> View context props model action
- textRaw :: MisoString -> View context props model action
- textKey :: ToKey key => key -> MisoString -> View context props model action
- textKey_ :: ToKey key => key -> [MisoString] -> View context props model 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
| MountConstraints context props model action => SomeComponent (Maybe Key) props (Component context props model action) |
data SomeStaticComponent props context Source #
A closed Component bundled with its MountConstraints dictionaries,
ready to be placed behind static and mounted with vcomp.
Unlike SomeComponent, the props type parameter is preserved (not
existential) so vcomp can statically require the runtime props value
to match the component. Only model and action are hidden. Because the
dictionaries sit here, in the value a StaticKey resolves to, the Lynx
main thread can decode a wire props payload or an action at the right
type from the key alone — no props value is needed first.
Built with mountStatic; consumed by vcomp / vcomp_.
Up to 1.13 this held a props -> function instead
of the component itself, which forced the main thread to apply it to a
placeholder just to reach the dictionaries; see SomeComponent contextmountStatic.
Since: 1.13.0.0
Constructors
| MountConstraints context props model action => SomeStaticComponent (Component context props model action) |
type MountConstraints context props model action = (Eq context, Eq props, Eq model, FromJSON props, ToJSON props, FromJSON model, ToJSON model, FromJSON action, ToJSON action) Source #
The dictionaries a Component must carry to be mounted as a child:
equality for dirty-checking, plus (under the native flag) JSON for
shipping model, props and action across the Lynx dual-thread
boundary. Shared by SomeComponent and SomeStaticComponent so the two
can never drift apart.
Since: 1.14.0.0
data EventHandler model action Source #
Wrapper for event handler callbacks, used for cross-thread communication.
Carries two independent things built from the same (decoder, convert)
pair:
eventHandlerInstall— attaches a real JS listener to a live vnode. Used by the runtime's attribute diffing, on both threads.eventHandlerDecoder/eventHandlerConvert— the decode step exposed directly, with no JS installer round-trip. Used by the MTS'sdispatchMainThreadEventto decode + dispatch a main-thread event synchronously, without reconstructing (and discarding) a JS callback via a throwaway scratch node on every single event.
Since: 1.13.0.0
Constructors
| EventHandler | |
Fields
| |
data View context props model action Source #
Core type for constructing a virtual DOM in Haskell
Constructors
| VNode Namespace Tag [Attribute model action] [View context props model action] DirectEvents | The final |
| VText (Maybe Key) MisoString | |
| VComp (SomeComponent context) | |
| VCompStatic (StaticPtr (SomeStaticComponent childProps context)) childProps | An embedded child |
| VFrag (Maybe Key) [View context props model action] | |
| VContext (context -> View context props model action) | Ambient accessor for the app-global |
| VProps (props -> View context props model action) | Ambient accessor for the enclosing |
| VModel (model -> View context props model action) | Ambient accessor for the enclosing |
Instances
| (context ~ (), props ~ (), model ~ ()) => ToHtml [View context props model action] Source # | Render a |
Defined in Miso.Html.Render Methods toHtml :: [View context props model action] -> ByteString Source # | |
| IsString (View context props model action) Source # |
|
Defined in Miso.Types Methods fromString :: String -> View context props model action # | |
| (context ~ (), props ~ (), model ~ ()) => ToHtml (View context props model action) Source # | Render a Rendering never starts the runtime, so a bare |
Defined in Miso.Html.Render Methods toHtml :: View context props model action -> 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 # | |
| ToMisoString Key Source # | |
Defined in Miso.Types Methods toMisoString :: Key -> MisoString Source # | |
| ToKey Key Source # | Identity instance |
data Attribute model action Source #
Attribute of a vnode in a View.
Constructors
| Property MisoString Value | |
| ClassList [MisoString] | |
| On (model -> Sink action -> VTree -> LogLevel -> Events -> IO ()) | A fully-applied |
| OnStatic (StaticPtr (EventHandler model action)) | A |
| 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
Hydrate avoids calling diff, and instead calls hydrate
Draw invokes the virtual-DOM diff
type Tag = MisoString Source #
type DirectEvents = Set MisoString Source #
The set of events an element dispatches directly on itself rather than
by bubbling to the delegated mount listener. Empty for HTML/SVG/MathML;
populated for Lynx native elements (see nodeDirectEvents).
Since: 1.13.0.0
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"
type Events = Map MisoString Phase Source #
Convenience type for Events
The map declares which DOM events are delegated and at which Phase. Whether
an individual handler runs on the Lynx main thread (MTS) or background
thread (BTS) is decided per handler (see Miso.Event.mainThread), not
per event name — mirroring Lynx's main-thread:bind vs bind prefix.
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 |
| -> (model -> View context props model action) | view |
| -> Component context props model action |
Smart constructor for Component with sane defaults.
Event handler smart constructor
event :: StaticPtr (EventHandler model action) -> Attribute model action Source #
Embed a fully-applied static event handler.
The handler is baked into the StaticPtr, so the main thread can rebuild it
from the StaticKey alone (no payload to forward). Use this for handlers that
take no injected model data — including decoder handlers whose argument is a
function (e.g. onScroll HandleScroll).
button_ [ event (static (onClick AddOne)) ] div_ [ event (static (onScroll HandleScroll)) ]
Since: 1.13.0.0
Component mounting
vcomp :: childProps -> StaticPtr (SomeStaticComponent childProps context) -> View context props model action Source #
Embed a child Component as a View.
Smart constructor for VComp, mirroring vnode / vtext / vfrag.
The StaticPtr wraps only the closed SomeStaticComponent (built with
mountStatic); it must use the static keyword and refer to a closed,
top-level binding. The props value is supplied separately — so it may
depend on the parent's model — and is serialized across the dual-thread
boundary. The no-props case passes ().
No class constraints appear here: the dictionaries are discharged at the
static (mountStatic child) site and travel inside the SomeStaticComponent,
which is where the MTS recovers them from the StaticKey.
The StaticKey also serves as the node's diff key: two
different static sites at the same position are replaced, not reused,
while siblings built from one site still diff positionally.
div_ [] [ vcomp_ (static (mountStatic myComp)) ]
Since: 1.12.0.0
vcomp_ :: StaticPtr (SomeStaticComponent () context) -> View context props model action Source #
Like vcomp, but for a Component that takes no props.
— pair it with vcomp_ = vcomp ()mountStatic on a component whose
props are (), which produces a .SomeStaticComponent () context
div_ [] [ vcomp_ (static (mountStatic myComp)) ]
Since: 1.13.0.0
Arguments
| :: forall context childModel childAction model action props. (Eq context, Eq childModel, FromJSON childModel, ToJSON childModel, FromJSON childAction, ToJSON childAction) | |
| => MisoString |
|
| -> Component context () childModel childAction | |
| -> View context props model action |
Warning: [NATIVE] +> has no StaticKey; a component mounted with it after the initial frame silently drops OnStatic handlers inside it. Use vcomp with mountStaticWithProps instead.
Component mounting combinator
Used in the view function to mount a Component on any VNode.
"component-id" +> component model noop $ \m -> div_ [ id_ "foo" ] [ text (ms m) ]
Warning (Lynx dual-thread / 1): this builds a VComp with no
StaticKey. A component mounted this way as part of the
initial frame is fine (the MTS independently reconstructs it while
painting its own first frame). But if it is mounted later — e.g. inside
a list or behind a conditional, appearing only after the first frame — the
MTS has no other way to learn of it, so it never registers a mirror
ComponentState for it, and any main-thread (OnStatic)
event handler inside that subtree silently fails to dispatch for the
component's whole lifetime, with only a console error as a clue. Use
vcomp with mountStaticWithProps instead for anything that may mount
after the initial frame under 1 — the compile-time
StaticKey already supplies the identity a manual key would,
no explicit key needed.
Since: 1.9.0.0
Arguments
| :: forall context childModel childAction model action props. (Eq context, Eq childModel, FromJSON childModel, ToJSON childModel, FromJSON childAction, ToJSON childAction) | |
| => Component context () childModel childAction |
|
| -> View context props model action |
Warning: [NATIVE] mount_ has no StaticKey; a component mounted with it after the initial frame silently drops OnStatic handlers inside it. Use vcomp_ with mountStatic instead.
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) ]
Warning (Lynx dual-thread / 1): see the note on (+>) — this
also builds a VComp with no StaticKey, so the same
caveat applies: mounted after the initial frame, it never gets a
main-thread mirror registered, silently breaking OnStatic handlers
inside it. Use vcomp_ with mountStatic instead for anything that may
mount dynamically under 1.
Since: 1.9.0.0
Arguments
| :: forall context childModel childAction model action props. (Eq context, Eq childModel, FromJSON childModel, ToJSON childModel, FromJSON childAction, ToJSON childAction) | |
| => Component context () childModel childAction |
|
| -> View context props model action |
Warning: [NATIVE] mountUseContext has no StaticKey; a component mounted with it after the initial frame silently drops OnStatic handlers inside it. Use vcomp_ with mountStatic on a component with useContext = True instead.
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) ]
Warning (Lynx dual-thread / 1): see the note on mount_ —
this also builds a VComp with no StaticKey, so the same
caveat applies: mounted after the initial frame, it never gets a
main-thread mirror registered, silently breaking OnStatic handlers
inside it. Use
instead under vcomp_ (static (mountStatic comp { useContext = True }))1.
Since: 1.13.0.0
Arguments
| :: forall context childProps childModel childAction model action props. (Eq context, Eq childProps, Eq childModel, FromJSON childAction, FromJSON childModel, ToJSON childModel, ToJSON childAction, FromJSON childProps, ToJSON childProps) | |
| => MisoString | |
| -> childProps | |
| -> Component context childProps childModel childAction |
|
| -> View context props model action |
Warning: [NATIVE] mountWithProps_ has no StaticKey; a component mounted with it after the initial frame silently drops OnStatic handlers inside it. Use vcomp with mountStaticWithProps instead.
Component mounting combinator, keyed, with props supplied directly.
Warning (Lynx dual-thread / 1): see the note on (+>) — this
also builds a VComp with no StaticKey (the key here is
just the diffing Key, unrelated), so the same caveat applies: mounted
after the initial frame, it never gets a main-thread mirror registered,
silently breaking OnStatic handlers inside it. Use vcomp with
mountStaticWithProps instead for anything that may mount dynamically
under 1 — the compile-time StaticKey already
supplies the identity a manual key would, no explicit key needed.
Arguments
| :: forall context childProps childModel childAction model action props. (Eq context, Eq childProps, Eq childModel, FromJSON childModel, ToJSON childModel, FromJSON childAction, ToJSON childAction, FromJSON childProps, ToJSON childProps) | |
| => childProps | |
| -> Component context childProps childModel childAction |
|
| -> View context props model action |
Warning: [NATIVE] mountWithProps has no StaticKey; a component mounted with it after the initial frame silently drops OnStatic handlers inside it. Use vcomp with mountStaticWithProps instead.
Component mounting combinator, with props supplied directly.
Warning (Lynx dual-thread / 1): see the note on (+>) — this
also builds an unkeyed VComp with no StaticKey, so the
same caveat applies: components mounted with this after the initial
frame never get a main-thread mirror registered, silently breaking
OnStatic handlers inside them. Use vcomp with mountStaticWithProps
instead for anything that may mount dynamically under 1.
Arguments
| :: MountConstraints context props model action | |
| => Component context props model action |
|
| -> SomeStaticComponent props context |
Static Component mounting combinator.
Wraps the component in a SomeStaticComponent — the closed value to place
behind static — and discharges its MountConstraints dictionaries
there. The props value is not supplied here but later, at the vcomp
site, so a parent can pass runtime props (e.g. derived from its own
model) without an explicit lambda; a component with props ~ () pairs
with vcomp_. Unlike mount_, no key is needed: the compile-time
StaticKey already supplies identity, so this is safe to
diff against another Component.
To opt the child into app-global context updates, set the field directly:
mountStatic comp { useContext = True }.
div_ [] [ vcomp_ (static (mountStatic myComp)) ] div_ [] [ vcomp (model ^. field) (static (mountStatic child)) ]
Since: 1.13.0.0
Arguments
| :: MountConstraints context props model action | |
| => Component context props model action |
|
| -> SomeStaticComponent props context |
Deprecated: Use mountStatic; it now accepts components with props. This alias will be removed in 1.15.
Deprecated. Synonym for mountStatic, which now handles components
with and without props alike (the props value is supplied at the
vcomp site either way). Will be removed in 1.15.
Since: 1.13.0.0
Fragment combinators
fragment :: [View context props model action] -> View context props 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 context props model action] -> View context props model action Source #
Like fragment, but keyed for efficient diffing.
Since: 1.10.0.0
vfrag :: [View context props model action] -> View context props 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 context props model action] -> View context props model action Source #
Like fragment, but keyed for efficient diffing.
Since: 1.10.0.0
Context combinator
vcontext :: (context -> View context props model action) -> View context props model action Source #
Create a new VContext.
Embeds a subtree that is resolved against the app-global context at the
point the enclosing View is built or rendered, so any part of a view tree
can read context without needing it threaded through as an explicit
argument. Since view takes only the model, this is the way to read
the context during render.
vcontext $ \theme -> div_ [] [ text (themeLabel theme) ]
Note: this does not opt the enclosing Component into context-driven
redraws — that is still governed solely by useContext. A
VContext only ever sees a fresh context when the surrounding View is
(re)built for some other, already-scheduled reason.
Since: 1.14.0.0
withContext :: (context -> View context props model action) -> View context props model action Source #
Synonym for vcontext.
Since: 1.14.0.0
Props combinator
vprops :: (props -> View context props model action) -> View context props model action Source #
Create a new VProps.
Embeds a subtree that is resolved against the enclosing Component's
props at the point the enclosing View is built or rendered, so any part
of a view tree can read props without needing it threaded through as an
explicit argument. Since view takes only the model, this is the way
to read the props during render.
vprops $ \Props { title } -> h1_ [] [ text title ]
Because props is a type parameter of View, the props seen here is
statically the props of the Component whose view contains this
node — a mismatch is a compile-time error. A child mounted with
mountWithProps / vcomp sees its own props, not its parent's.
Note: a VProps node adds no redraw logic of its own. A component is
redrawn when its parent passes it different props (the props phase), and
the node is re-resolved against the new props as part of that redraw.
Since: 1.14.0.0
withProps :: (props -> View context props model action) -> View context props model action Source #
Synonym for vprops.
Since: 1.14.0.0
Model combinator
vmodel :: (model -> View context props model action) -> View context props model action Source #
Create a new VModel.
Embeds a subtree that is resolved against the enclosing Component's
model at the point the enclosing View is built or rendered, so a helper
deep in a view tree can read model without needing it threaded through as
an explicit argument. Unlike context and props, the model is also
handed to view as its only parameter — that is a convenience, not a
restriction: view _ = vmodel $ \m -> … is equivalent, and vmodel is
the better choice for a helper the model is not otherwise passed to.
vmodel $ \Model { count } -> span_ [] [ text (ms count) ]
Because model is a type parameter of View, the model seen here is
statically the model of the Component whose view contains this
node — a mismatch is a compile-time error. A child mounted with mount_ /
vcomp sees its own model, not its parent's.
Note: a VModel node adds no redraw logic of its own. A component is
redrawn when its model changes after an update, and the node is
re-resolved against the new model as part of that redraw.
When serialising, a bare View under toHtml has no
enclosing component to supply a model, so its model is fixed to ();
pass a real model with toHtmlWith instead. A VModel
nested inside a mounted component always sees that component's initial
(or hydrated) model.
Since: 1.14.0.0
withModel :: (model -> View context props model action) -> View context props model action Source #
Synonym for vmodel.
Since: 1.14.0.0
Utils
getMountPoint :: Maybe MisoString -> MisoString Source #
Convenience for extracting mount point
Arguments
| :: ([Attribute model action] -> [View context props model action] -> View context props model action) | |
| -> [Attribute model action] | Attributes to be added unconditionally |
| -> Bool | A condition |
| -> [Attribute model action] | Additional attributes to add if the condition is True |
| -> [View context props model action] | Children |
| -> View context props model action |
Utility function to make it easy to specify conditional attributes
view :: Bool -> View context props model action view danger = optionalAttrs div_ [ id_ "some-div" ] danger [ class_ "danger" ] ["child"]
Since: 1.9.0.0
Arguments
| :: ([Attribute model action] -> View context props model action) | |
| -> [Attribute model action] | Attributes to be added unconditionally |
| -> Bool | A condition |
| -> [Attribute model action] | Additional attributes to add if the condition is True |
| -> View context props model action |
Utility function to make it easy to specify conditional attributes for void elements.
view :: Bool -> View context props model action view shouldClear = optionalVoidAttrs textarea_ [ value_ "" ] shouldClear [ id_ "text-area-id" ]
Since: 1.9.0.0
Arguments
| :: ([Attribute model action] -> [View context props model action] -> View context props model action) | |
| -> [Attribute model action] | Attributes to be added unconditionally |
| -> [View context props model action] | Children to be added unconditionally |
| -> Bool | A condition |
| -> [View context props model action] | Additional children to add if the condition is True |
| -> View context props model action |
Conditionally adds children.
view :: Bool -> View context props 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 model action] | Attributes, properties, and event handlers |
| -> [View context props model action] | Child nodes |
| -> View context props 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 model action] | Attributes, properties, and event handlers |
| -> [MisoString] | Events dispatched directly on this element |
| -> [View context props model action] | Child nodes |
| -> View context props model action |
Like node, but declares the set of events this element dispatches
directly on itself instead of by bubbling to the delegated mount listener.
Only relevant to the Lynx native runtime, where component-emitted events
(input, scroll, load, …) do not bubble and must be bound on the
element. The set is a capability: a listener is bound only for events the
element actually handles. Empty on the browser/WASM runtime.
Since: 1.13.0.0
Arguments
| :: Namespace | Element namespace ( |
| -> MisoString | Tag name (e.g. |
| -> [Attribute model action] | Attributes, properties, and event handlers |
| -> [View context props model action] | Child nodes |
| -> View context props model action |
text :: MisoString -> View context props model action Source #
Create a new VText with the given content.
text_ :: [MisoString] -> View context props model action Source #
Create a new VText containing concatenation of the given strings.
view :: View context props 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 context props model action Source #
textKey :: ToKey key => key -> MisoString -> View context props model action Source #
Like text, but allow the node to be keyed for efficient diffing.
view :: model -> View context props model action view = x -> div_ [] [ textKey (1 :: Int) "text here" ]
Since: 1.9.0.0
textKey_ :: ToKey key => key -> [MisoString] -> View context props model action Source #
Like text_, but allow the node to be keyed for efficient diffing.
view :: model -> View context props 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.