Create a gist now

Instantly share code, notes, and snippets.

Idris JavaScript FFI
module Main
data HTMLElement : Type where
Elem : Ptr -> HTMLElement
data NodeList : Type where
Nodes : Ptr -> NodeList
query : String -> IO NodeList
query q = do
e <- mkForeign (FFun "document.querySelectorAll" [FString] FPtr) q
return (Nodes e)
item : NodeList -> Int -> IO HTMLElement
item (Nodes p) i = do
i <- mkForeign (FFun ".item" [FPtr, FInt] FPtr) p i
return (Elem i)
setOnClick : HTMLElement -> (() -> IO()) -> IO ()
setOnClick (Elem p) f =
mkForeign (FFun "['onclick']=" [FPtr, FFunction FUnit (FAny (IO ()))] FUnit) p f
setText : HTMLElement -> String -> IO ()
setText (Elem p) s =
mkForeign (FFun ".textContent=" [FPtr, FString] FUnit) p s
test : HTMLElement -> () -> IO ()
test e i = do
setText e "SUPERFOO"
putStrLn "testfoo"
main : IO ()
main = do
e <- query "#test"
i <- item e 0
setOnClick i (test i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment