packages feed

nano-ui-0.1.0.0: lib/NanoUI/Widgets/Button.hs

-- | Push buttons.
module NanoUI.Widgets.Button
  ( button
  , button'
  , buttonWith
  , buttonWith'
  )
where

import Data.Text (Text)
import Effectful (Eff, type (:>))
import NanoUI.Monad (Ui)
import NanoUI.Style (Layout, defaultLayout)
import NanoUI.Widgets.Combinators (buttonStyledEx)
import NanoUI.Widgets.Node (Response, respClicked)

-- | Button with a text label. 'True' on the frame it is clicked, by pointer
-- or by Enter or Space while focused.
--
-- @
-- whenM (button "Save") saveDocument
-- @
{-# INLINE button #-}
button :: Ui :> es => Text -> Eff es Bool
button txt = respClicked <$> button' txt

-- | 'button' returning its 'Response', for tooltips, anchored popups, or
-- hover state.
--
-- @
-- help <- button' "Help"
-- tooltip help "Open the manual"
-- when (respClicked help) openManual
-- @
{-# INLINE button' #-}
button' :: Ui :> es => Text -> Eff es Response
button' = buttonWith' id

-- | 'button' with a layout modifier.
--
-- @
-- whenM (buttonWith (fixedW 120) "Submit") submitForm
-- @
{-# INLINE buttonWith #-}
buttonWith :: Ui :> es => (Layout -> Layout) -> Text -> Eff es Bool
buttonWith f txt = respClicked <$> buttonWith' f txt

{-# INLINE buttonWith' #-}
buttonWith' :: Ui :> es => (Layout -> Layout) -> Text -> Eff es Response
buttonWith' f txt = buttonStyledEx True txt 0 (f defaultLayout) 0