-----------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE MultilineStrings  #-}
{-# LANGUAGE RecordWildCards   #-}
-----------------------------------------------------------------------------
module Miso.UI.Toast
  ( -- ** Props
    ToastProps (..)
  , defaultToastProps
    -- ** Views
  , toastTrigger_
  , toaster_
    -- ** Samples
  , toastSample
  , toastCodeSample
  , toastPropsApi
  ) where
-----------------------------------------------------------------------------
import           Miso
import qualified Miso.Html.Event as E
import qualified Miso.Html.Element as H
import qualified Miso.Html.Property as P
-----------------------------------------------------------------------------
import           Miso.UI.Button
import           Miso.UI.Types
-----------------------------------------------------------------------------
-- | Props describing a basecoat toast. Dispatched to basecoat's toaster JS
-- (a @basecoat:toast@ custom event) by the application's update function.
data ToastProps
  = ToastProps
  { ToastProps -> MisoString
toastCategory :: MisoString
    -- ^ @success@, @error@, @info@ or @warning@
  , ToastProps -> MisoString
toastTitle :: MisoString
  , ToastProps -> MisoString
toastDescription :: MisoString
  , ToastProps -> MisoString
toastLabel :: MisoString
    -- ^ Label of the dismiss button
  } deriving (Int -> ToastProps -> ShowS
[ToastProps] -> ShowS
ToastProps -> String
(Int -> ToastProps -> ShowS)
-> (ToastProps -> String)
-> ([ToastProps] -> ShowS)
-> Show ToastProps
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ToastProps -> ShowS
showsPrec :: Int -> ToastProps -> ShowS
$cshow :: ToastProps -> String
show :: ToastProps -> String
$cshowList :: [ToastProps] -> ShowS
showList :: [ToastProps] -> ShowS
Show, ToastProps -> ToastProps -> Bool
(ToastProps -> ToastProps -> Bool)
-> (ToastProps -> ToastProps -> Bool) -> Eq ToastProps
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ToastProps -> ToastProps -> Bool
== :: ToastProps -> ToastProps -> Bool
$c/= :: ToastProps -> ToastProps -> Bool
/= :: ToastProps -> ToastProps -> Bool
Eq)
-----------------------------------------------------------------------------
-- | Smart constructor: informational toast
defaultToastProps :: ToastProps
defaultToastProps :: ToastProps
defaultToastProps
  = ToastProps
  { toastCategory :: MisoString
toastCategory = MisoString
"info"
  , toastTitle :: MisoString
toastTitle = MisoString
""
  , toastDescription :: MisoString
toastDescription = MisoString
""
  , toastLabel :: MisoString
toastLabel = MisoString
"Dismiss"
  }
-----------------------------------------------------------------------------
-- | Button that raises a toast; the application maps 'ToastProps' into its
-- own action (which should dispatch the @basecoat:toast@ event)
toastTrigger_
  :: (ToastProps -> action)
  -> ToastProps
  -> [View model action]
  -> View model action
toastTrigger_ :: forall action model.
(ToastProps -> action)
-> ToastProps -> [View model action] -> View model action
toastTrigger_ ToastProps -> action
toAction ToastProps
cfg [View model action]
kids =
  ButtonProps action -> [View model action] -> View model action
forall action model.
ButtonProps action -> [View model action] -> View model action
button_ ButtonProps (ZonkAny 0)
forall action. ButtonProps action
defaultButtonProps
    { buttonVariant = Outline
    , buttonAttrs = [ E.onClick (toAction cfg) ]
    } [View model action]
kids
-----------------------------------------------------------------------------
-- | The toaster container basecoat renders toasts into; place once per page
toaster_ :: [Attribute action] -> View model action
toaster_ :: forall action model. [Attribute action] -> View model action
toaster_ [Attribute action]
attrs = [Attribute action] -> [View model action] -> View model action
forall action model.
[Attribute action] -> [View model action] -> View model action
H.div_ (MisoString -> Attribute action
forall action. MisoString -> Attribute action
P.id_ MisoString
"toaster" Attribute action -> [Attribute action] -> [Attribute action]
forall a. a -> [a] -> [a]
: MisoString -> Attribute action
forall action. MisoString -> Attribute action
P.class_ MisoString
"toaster" Attribute action -> [Attribute action] -> [Attribute action]
forall a. a -> [a] -> [a]
: [Attribute action]
attrs) []
-----------------------------------------------------------------------------
toastSample :: (ToastProps -> action) -> View model action
toastSample :: forall action model. (ToastProps -> action) -> View model action
toastSample ToastProps -> action
toAction =
  [Attribute action] -> [View model action] -> View model action
forall action model.
[Attribute action] -> [View model action] -> View model action
H.div_
  [ MisoString -> Attribute action
forall action. MisoString -> Attribute action
P.class_ MisoString
"flex flex-wrap items-center gap-2" ]
  [ (ToastProps -> action)
-> ToastProps -> [View model action] -> View model action
forall action model.
(ToastProps -> action)
-> ToastProps -> [View model action] -> View model action
toastTrigger_ ToastProps -> action
toAction
      ToastProps
defaultToastProps
        { toastCategory = "success"
        , toastTitle = "Success"
        , toastDescription = "A successful toast !"
        }
      [ View model action
"Success" ]
  , (ToastProps -> action)
-> ToastProps -> [View model action] -> View model action
forall action model.
(ToastProps -> action)
-> ToastProps -> [View model action] -> View model action
toastTrigger_ ToastProps -> action
toAction
      ToastProps
defaultToastProps
        { toastCategory = "error"
        , toastTitle = "Error"
        , toastDescription = "An error toast :("
        }
      [ View model action
"Error" ]
  , (ToastProps -> action)
-> ToastProps -> [View model action] -> View model action
forall action model.
(ToastProps -> action)
-> ToastProps -> [View model action] -> View model action
toastTrigger_ ToastProps -> action
toAction
      ToastProps
defaultToastProps
        { toastCategory = "info"
        , toastTitle = "Info"
        , toastDescription = "An informational toast =]"
        }
      [ View model action
"Info" ]
  , (ToastProps -> action)
-> ToastProps -> [View model action] -> View model action
forall action model.
(ToastProps -> action)
-> ToastProps -> [View model action] -> View model action
toastTrigger_ ToastProps -> action
toAction
      ToastProps
defaultToastProps
        { toastCategory = "warning"
        , toastTitle = "Warning"
        , toastDescription = "A warning toast :/"
        }
      [ View model action
"Warning" ]
  ]
-----------------------------------------------------------------------------
toastCodeSample :: View model action
toastCodeSample :: forall model action. View model action
toastCodeSample =
  View model action
"""
  -----------------------------------------------------------------------------
  module MyToast (toastSample) where
  -----------------------------------------------------------------------------
  import           Miso
  import qualified Miso.Html.Event as E
  import qualified Miso.Html.Element as H
  import qualified Miso.Html.Property as P
  import           Miso.UI.Button
  import           Miso.UI.Types
  import           Miso.UI.Toast
  -----------------------------------------------------------------------------
  toastSample :: (ToastProps -> action) -> View model action
  toastSample toAction =
    H.div_
    [ P.class_ "flex flex-wrap items-center gap-2" ]
    [ toastTrigger_ toAction
        defaultToastProps
          { toastCategory = "success"
          , toastTitle = "Success"
          , toastDescription = "A successful toast !"
          }
        [ "Success" ]
    , toastTrigger_ toAction
        defaultToastProps
          { toastCategory = "error"
          , toastTitle = "Error"
          , toastDescription = "An error toast :("
          }
        [ "Error" ]
    , toastTrigger_ toAction
        defaultToastProps
          { toastCategory = "info"
          , toastTitle = "Info"
          , toastDescription = "An informational toast =]"
          }
        [ "Info" ]
    , toastTrigger_ toAction
        defaultToastProps
          { toastCategory = "warning"
          , toastTitle = "Warning"
          , toastDescription = "A warning toast :/"
          }
        [ "Warning" ]
    ]
  """
-----------------------------------------------------------------------------
toastPropsApi :: View model action
toastPropsApi :: forall model action. View model action
toastPropsApi =
  View model action
"""
  -- | Props describing a basecoat toast. Dispatched to basecoat's toaster JS
  -- (a @basecoat:toast@ custom event) by the application's update function.
  data ToastProps
    = ToastProps
    { toastCategory :: MisoString
      -- ^ @success@, @error@, @info@ or @warning@
    , toastTitle :: MisoString
    , toastDescription :: MisoString
    , toastLabel :: MisoString
      -- ^ Label of the dismiss button
    } deriving (Show, Eq)
  -----------------------------------------------------------------------------
  -- | Smart constructor: informational toast
  defaultToastProps :: ToastProps
  defaultToastProps
    = ToastProps
    { toastCategory = "info"
    , toastTitle = ""
    , toastDescription = ""
    , toastLabel = "Dismiss"
    }
  """
-----------------------------------------------------------------------------