-----------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Miso.Native.X.Element.Svg.Event
-- 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
----------------------------------------------------------------------------
module Miso.Native.X.Element.Svg.Event
  ( -- *** Events
    onLoad
  , onLoadWith
  , onLoadMain
  , onLoadMainWith
    -- *** Event Map
  , svgEvents
  ) where
-----------------------------------------------------------------------------
import qualified Data.Map as M
-----------------------------------------------------------------------------
import           Miso.Event
import           Miso.Types (Attribute, EventHandler, DOMRef)
-----------------------------------------------------------------------------
svgEvents :: Events
svgEvents :: Events
svgEvents = [(MisoString, Phase)] -> Events
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [ (MisoString
"load", Phase
BUBBLE) ]
-----------------------------------------------------------------------------
-- | https://lynxjs.org/api/elements/built-in/svg.html#bindload
--
-- Triggered when the SVG finishes loading.
--
onLoad :: action -> Attribute model action
onLoad :: forall action model. action -> Attribute model action
onLoad action
action = MisoString
-> Decoder ()
-> (() -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"load" Decoder ()
emptyDecoder (\() model
_ DOMRef
_ -> action
action)
-----------------------------------------------------------------------------
-- | Like 'onLoad', but dispatched on the Lynx __main thread__ ('MTS').
--
-- Runs imperatively on the MTS (no VDOM diff). Meant to be used with
-- @-XStaticPointers@.
--
-- @
-- data Action = Loaded
--
-- view_ [ event (static (onLoadMain Loaded)) ] [ "some view" ]
-- @
--
onLoadMain :: action -> EventHandler model action
onLoadMain :: forall action model. action -> EventHandler model action
onLoadMain action
action = MisoString
-> Decoder ()
-> (() -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"load" Decoder ()
emptyDecoder (\() model
_ DOMRef
_ -> action
action)
-----------------------------------------------------------------------------
-- | Like 'onLoadMain', but the handler also receives read-only access to the
-- @model@ and the target element's 'DOMRef' (for imperative MTS mutation).
--
-- @
-- data Action = Loaded Model DOMRef
--
-- view_ [ event (static (onLoadMainWith Loaded)) ] [ "some view" ]
-- @
--
onLoadMainWith :: (model -> DOMRef -> action) -> EventHandler model action
onLoadMainWith :: forall model action.
(model -> DOMRef -> action) -> EventHandler model action
onLoadMainWith model -> DOMRef -> action
action = MisoString
-> Decoder ()
-> (() -> model -> DOMRef -> action)
-> EventHandler model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> EventHandler model action
onMain MisoString
"load" Decoder ()
emptyDecoder (\() model
m DOMRef
ref -> model -> DOMRef -> action
action model
m DOMRef
ref)
-----------------------------------------------------------------------------
-- | Like 'onLoad', but the handler also receives the target element's 'DOMRef'.
-- Use for main-thread ('MTS') handlers that imperatively mutate the element.
onLoadWith :: (DOMRef -> action) -> Attribute model action
onLoadWith :: forall action model. (DOMRef -> action) -> Attribute model action
onLoadWith DOMRef -> action
action = MisoString
-> Decoder ()
-> (() -> model -> DOMRef -> action)
-> Attribute model action
forall result model action.
MisoString
-> Decoder result
-> (result -> model -> DOMRef -> action)
-> Attribute model action
on MisoString
"load" Decoder ()
emptyDecoder (\() model
_ DOMRef
ref -> DOMRef -> action
action DOMRef
ref)
-----------------------------------------------------------------------------