Skip to content

Instantly share code, notes, and snippets.

@piq9117
Created January 14, 2024 16:25
Show Gist options
  • Select an option

  • Save piq9117/8e711a44cbff5d7e2fa4e6e11e85612b to your computer and use it in GitHub Desktop.

Select an option

Save piq9117/8e711a44cbff5d7e2fa4e6e11e85612b to your computer and use it in GitHub Desktop.

Revisions

  1. piq9117 created this gist Jan 14, 2024.
    38 changes: 38 additions & 0 deletions no-framework.purs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    module Main where

    import Prelude

    import Effect (Effect)
    import Effect.Console (log)
    import Web.HTML as Web
    import Web.HTML.Window as Window
    import Web.HTML.HTMLDocument as HTMLDocument
    import Web.DOM.ParentNode
    ( ParentNode
    , QuerySelector(..)
    , querySelector
    )
    import Data.Maybe (Maybe(..))
    import Web.DOM.Internal.Types (Element)
    import Web.DOM.Document (createElement)
    import Web.DOM.Node (setTextContent, appendChild)
    import Web.DOM.Element as Element

    main :: Effect Unit
    main = do
    window <- Web.window
    document <- Window.document window
    let parentNode = HTMLDocument.toParentNode document
    appElement <- getElement parentNode "#app"
    h1 <- createElement "h1" (HTMLDocument.toDocument document)
    setTextContent "Hello, World" (Element.toNode h1)
    case appElement of
    Nothing -> log "No app element"
    Just app -> appendChild (Element.toNode h1) (Element.toNode app)

    getElement
    :: ParentNode
    -> String
    -> Effect (Maybe Element)
    getElement pNode selector =
    querySelector (QuerySelector selector) pNode