miso
Copyright(C) 2016-2025 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Miso.FFI

Description

Foreign Function Interface (FFI) utilities for interacting with JavaScript.

Synopsis

Object

set :: ToJSVal v => MisoString -> v -> Object -> IO () Source #

Set property on object

Performance

Logging

consoleLog :: MisoString -> IO () Source #

Outputs a message to the web console

See https://developer.mozilla.org/en-US/docs/Web/API/Console/log

Console logging of JavaScript strings.

consoleLog' :: ToArgs a => a -> IO () Source #

Console-logging of JSVal

consoleError :: MisoString -> IO () Source #

Outputs an error message to the web console

See https://developer.mozilla.org/en-US/docs/Web/API/Console/error

Console logging of JavaScript strings.

consoleWarn :: MisoString -> IO () Source #

Outputs a warning message to the web console

See https://developer.mozilla.org/en-US/docs/Web/API/Console/warn

Console logging of JavaScript strings.

DOM

getElementById :: MisoString -> IO JSVal Source #

Returns an Element object representing the element whose id property matches the specified string.

See https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById

focus :: MisoString -> IO () Source #

Fails silently if the element is not found.

Analogous to document.getElementById(id).focus().

blur :: MisoString -> IO () Source #

Fails silently if the element is not found.

Analogous to document.getElementById(id).blur()

select :: MisoString -> IO () Source #

Fails silently if the element is not found.

Analogous to document.querySelector(# + id).select().

setSelectionRange :: MisoString -> Int -> Int -> IO () Source #

Fails silently if the element is not found.

Analogous to document.querySelector(# + id).setSelectionRange(start, end, 'none').

alert :: MisoString -> IO () Source #

Calls the alert() function.

getProperty :: JSVal -> MisoString -> IO JSVal Source #

Get a property of a JSVal

Example usage:

Just (value :: String) <- fromJSVal =<< getProperty domRef "value"

callFunction :: ToArgs args => JSVal -> MisoString -> args -> IO JSVal Source #

Calls a function on a JSVal

Example usage:

callFunction domRef "focus" ()
callFunction domRef "setSelectionRange" (0, 3, "none")

castJSVal :: FromJSVal a => JSVal -> IO (Maybe a) Source #

Marshalling of JSVal, useful for getProperty

removeChild :: DOMRef -> DOMRef -> IO () Source #

Removes a child node from a parent node.

Calls miso.drawingContext.removeChild(parent, child).

Since: 1.9.0.0

Styles

addStyle :: MisoString -> IO JSVal Source #

Appends a style_ element containing CSS to head_

addStyle "body { background-color: green; }"
<head><style>body { background-color: green; }</style></head>

addStyleSheet :: MisoString -> IO JSVal Source #

Appends a StyleSheet link_ element to head_ The link_ tag will contain a URL to a CSS file.

addStyleSheet "https://cdn.jsdelivr.net/npm/todomvc-common@1.0.5/base.min.css"
<head><link href="https://cdn.jsdelivr.net/npm/todomvc-common@1.0.5/base.min.css" ref="stylesheet"></head>

Callbacks

syncCallback :: IO () -> IO JSVal Source #

A synchronous callback

syncCallback1 :: (JSVal -> IO ()) -> IO JSVal Source #

A synchronous callback with a single argument

asyncCallback :: IO () -> IO JSVal Source #

A asynchronous callback

asyncCallback1 :: (JSVal -> IO ()) -> IO JSVal Source #

A asynchronous callback with one argument

asyncCallback2 :: (JSVal -> JSVal -> IO ()) -> IO JSVal Source #

A asynchronous callback with two arguments

Drawing

flush :: IO () Source #

Flush is used to force a draw of the render tree. This is currently only used when targeting platforms other than the browser (like mobile).

setDrawingContext :: MisoString -> IO () Source #

Used to select a drawing context. Users can override the default DOM renderer by implementing their own Context, and exporting it to the global scope. This opens the door to different rendering engines, ala miso-lynx.

Window

windowInnerWidth :: IO Int Source #

Retrieves the width (in pixels) of the browser window viewport including if rendered, the vertical scrollbar.

See https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth

windowInnerHeight :: IO Int Source #

Retrieves the height (in pixels) of the browser window viewport including, if rendered, the horizontal scrollbar.

See https://developer.mozilla.org/en-US/docs/Web/API/Window/innerHeight

locationReload :: IO () Source #

Calls the location.reload() function.

Image

newtype Image Source #

Type that holds an Image.

Constructors

Image JSVal 

Instances

Instances details
FromJSVal Image Source # 
Instance details

Defined in Miso.FFI.Internal

ToJSVal Image Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

toJSVal :: Image -> IO JSVal Source #

ToObject Image Source # 
Instance details

Defined in Miso.FFI.Internal

newImage :: MisoString -> IO Image Source #

Smart constructor for building a Image w/ src_ Attribute.

Date

newtype Date Source #

The Date type

Constructors

Date JSVal 

Instances

Instances details
Eq Date Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

(==) :: Date -> Date -> Bool #

(/=) :: Date -> Date -> Bool #

ToJSVal Date Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

toJSVal :: Date -> IO JSVal Source #

ToObject Date Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

toObject :: Date -> IO Object Source #

newDate :: IO Date Source #

Smart constructor for a Date

toLocaleString :: Date -> IO MisoString Source #

Date conversion function to produce a locale

getSeconds :: Date -> IO Double Source #

Retrieves current seconds from Date

getMilliseconds :: Date -> IO Double Source #

Retrieves current milliseconds from Date

DOM Traversal

nextSibling :: JSVal -> IO JSVal Source #

Fetch next sibling DOM node

Since: 1.9.0.0

previousSibling :: JSVal -> IO JSVal Source #

Fetch previous sibling DOM node

Since: 1.9.0.0

Element

click :: () -> JSVal -> IO () Source #

Simulates a click event

button & click ()

Since: 1.9.0.0

setValue :: DOMRef -> MisoString -> IO () Source #

Sets the .value property on a DOMRef.

Useful for resetting the value property on an input element.

  setValue domRef ("" :: MisoString)

File Input

files :: JSVal -> IO [JSVal] Source #

When working with <input type="file">, this is useful for extracting out the selected files.

  update (InputClicked inputElement) = withSink $ \sink -> do
     files_ <- files inputElement
     forM_ files_ $ \file -> sink (Upload file)
  update (Upload file) = do
     fetch "https://localhost:8080/upload" "POST" (Just file) []
       Successful Errorful

Since: 1.9.0.0

Navigator

isOnLine :: IO Bool Source #

Navigator function to query the current online status of the user's computer

See navigator.onLine

ArrayBuffer

newtype ArrayBuffer Source #

Constructors

ArrayBuffer JSVal 

Instances

Instances details
Eq ArrayBuffer Source # 
Instance details

Defined in Miso.FFI.Internal

FromJSVal ArrayBuffer Source # 
Instance details

Defined in Miso.FFI.Internal

ToJSVal ArrayBuffer Source # 
Instance details

Defined in Miso.FFI.Internal

Blob

newtype Blob Source #

Constructors

Blob JSVal 

Instances

Instances details
Eq Blob Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

(==) :: Blob -> Blob -> Bool #

(/=) :: Blob -> Blob -> Bool #

FromJSVal Blob Source # 
Instance details

Defined in Miso.FFI.Internal

ToJSVal Blob Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

toJSVal :: Blob -> IO JSVal Source #

Uint8Array

newtype Uint8Array Source #

Constructors

Uint8Array JSVal 

Instances

Instances details
FromJSVal Uint8Array Source # 
Instance details

Defined in Miso.FFI.Internal

ToJSVal Uint8Array Source # 
Instance details

Defined in Miso.FFI.Internal

FormData

newtype FormData Source #

Constructors

FormData JSVal 

Instances

Instances details
Eq FormData Source # 
Instance details

Defined in Miso.FFI.Internal

FromJSVal FormData Source # 
Instance details

Defined in Miso.FFI.Internal

ToJSVal FormData Source # 
Instance details

Defined in Miso.FFI.Internal

File

newtype File Source #

Constructors

File JSVal 

Instances

Instances details
Eq File Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

(==) :: File -> File -> Bool #

(/=) :: File -> File -> Bool #

FromJSVal File Source # 
Instance details

Defined in Miso.FFI.Internal

ToJSVal File Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

toJSVal :: File -> IO JSVal Source #

ToObject File Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

toObject :: File -> IO Object Source #

URLSearchParams

FileReader

newtype FileReader Source #

Constructors

FileReader JSVal 

Instances

Instances details
Eq FileReader Source # 
Instance details

Defined in Miso.FFI.Internal

FromJSVal FileReader Source # 
Instance details

Defined in Miso.FFI.Internal

ToJSVal FileReader Source # 
Instance details

Defined in Miso.FFI.Internal

ToObject FileReader Source # 
Instance details

Defined in Miso.FFI.Internal

newFileReader :: IO FileReader Source #

Smart constructor for building a FileReader

Fetch API

fetch Source #

Arguments

:: (FromJSVal success, FromJSVal error) 
=> MisoString

url

-> MisoString

method

-> Maybe JSVal

body

-> [(MisoString, MisoString)]

headers

-> (Response success -> IO ())

successful callback

-> (Response error -> IO ())

errorful callback

-> CONTENT_TYPE

content type

-> IO () 

Retrieve JSON via Fetch API

Basic GET of JSON using Fetch API, will be expanded upon.

See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

data Response body Source #

Type returned from a fetch request

Constructors

Response 

Fields

Instances

Instances details
Functor Response Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

fmap :: (a -> b) -> Response a -> Response b #

(<$) :: a -> Response b -> Response a #

FromJSVal body => FromJSVal (Response body) Source # 
Instance details

Defined in Miso.FFI.Internal

Event

addEventListener Source #

Arguments

:: JSVal

Event target on which we want to register event listener

-> MisoString

Type of event to listen to (e.g. "click")

-> (JSVal -> IO ())

Callback which will be called when the event occurs, the event will be passed to it as a parameter.

-> IO Function 

Register an event listener on given target.

removeEventListener Source #

Arguments

:: JSVal

Event target from which we want to remove event listener

-> MisoString

Type of event to listen to (e.g. "click")

-> Function

Callback which will be called when the event occurs, the event will be passed to it as a parameter.

-> IO () 

Removes an event listener from given target.

dispatchEvent :: Event -> IO () Source #

Invokes document.dispatchEvent

  update ChangeTheme = io_ $ do
    themeEvent <- newEvent "basecoat:theme"
    dispatchEvent themeEvent

newEvent :: ToArgs args => args -> IO Event Source #

Creates a new Event

  update ChangeTheme = io_ $ do
    themeEvent <- newEvent "basecoat:theme"
    dispatchEvent themeEvent

newCustomEvent :: ToArgs args => args -> IO Event Source #

Creates a new Event

  update ToggleSidebar = io_ $ do
    themeEvent <- newCustomEvent "basecoat:sidebar"
    dispatchEvent themeEvent

newtype Event Source #

Constructors

Event JSVal 

Instances

Instances details
Eq Event Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

(==) :: Event -> Event -> Bool #

(/=) :: Event -> Event -> Bool #

FromJSVal Event Source # 
Instance details

Defined in Miso.FFI.Internal

ToJSVal Event Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

toJSVal :: Event -> IO JSVal Source #

eventPreventDefault :: JSVal -> IO () Source #

Prevent default event behavior

eventStopPropagation :: JSVal -> IO () Source #

Stop propagation of events

Inline JS

inline :: (FromJSVal return, ToObject object) => MisoString -> object -> IO return Source #

Convenience function to write inline javascript.

Prefer this function over the use of eval.

This function takes as arguments a JavaScript object and makes the keys available in the function body.

 data Person = Person { name :: MisoString, age :: Int }
   deriving stock (Generic)
   deriving anyclass (ToJSVal, ToObject)

logNameGetAge :: Person -> IO Int
logNameGetAge = inline
  """
  console.log(name, name);
  return age;
  """