Learn HalogenGHC loads in the background

14. Your component in a page you do not write

A Halogen component can be used in plain HTML or a site built with React, Vue, Astro or HTMX. Expose it as a custom element such as <x-lesson>. The host page can then set attributes, listen for events and call methods without depending on Halogen.

Custom elements are part of the browser's Web Components APIs. A registered JavaScript class manages each element with that name, whether it came from HTML, a script or a framework. The name must contain a hyphen. A shadow root can isolate the component's markup and styles from the surrounding page.

Defining the element

haskell-halogen-custom-element creates the JavaScript class. The lesson wrapper calls:

CE.define "x-lesson" definition component

Each <x-lesson> then mounts its own component tree in a shadow root and disposes it when removed. Start with CE.defaultDefinition and configure how the page connects to the component. Import the fields with Halogen.CustomElement (Definition (..)).

  • input builds component input from element properties. CE.attr "label" props reads the label attribute; CE.prop would decode a JavaScript property from JSON. Here H.Component Query Text Int m takes a Text label.
  • observed lists names to watch. Changes recompute input and reach H.onReceive, implemented here by receive. Without this handler, state would retain the original label.
  • methods exposes queries. CE.tellMethod "reset" (\() -> Reset) makes element.reset() send the same tell a Halogen parent would send with H.tell. The method returns a promise that resolves after delivery.
  • styles supplies CSS scoped to the shadow root.

The page

The wrapper inserts the custom element, three buttons and a paragraph. Its JavaScript interacts with the element through this interface:

element.setAttribute('label', 'Pears');
element.reset();
element.addEventListener('count', (event) => say(`The page heard a count event: ${event.detail}`));
say(`count() answered ${await element.count()}`);

Click the counter, then "Call it Pears". The label changes without resetting the count because receiving input does not create a new component. "reset()" resets it to 0. The count event and count() method are not connected yet; you will add them below.

Browsers cannot unregister an element name or replace its class. The package instead registers one class and updates the component behind it on each compile. The wrapper saves registration.dispose with HA.keepForNextRun, so the next run first disposes existing instances, then installs the new definition.

Exercise

  1. Expose count outputs as browser events. Import Data.Aeson (toJSON) and add output = \count -> Just ("count", toJSON count) to the definition. H.raise already emits the count; this mapping turns it into a named event with JSON detail. Compile and click: the paragraph now reports it. The event bubbles beyond the shadow root.
  2. Add CE.method "count" (\() -> GetCount) to methods. Compile and press "count()". Its promise resolves with the query answer, like H.request in lesson 4.
Lesson.hs
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}

module Lesson (component, definition) where

import Control.Monad.IO.Class (MonadIO)
import Control.Monad.State (gets, modify)
import Data.Maybe (fromMaybe)
import Data.Row (Empty)
import Data.Text (Text)
import Data.Text qualified as T
import Halogen qualified as H
import Halogen.CustomElement (Definition (..))
import Halogen.CustomElement qualified as CE
import Halogen.HTML qualified as HH
import Halogen.HTML.Events qualified as HE

data Query a = Reset a | GetCount (Int -> a)

data State = State {label :: Text, count :: Int}

data Action = Increment

component :: forall m. (MonadIO m) => H.Component Query Text Int m
component =
  H.component initialState render [H.onAction handleAction, H.onReceive receive, H.onQuery handleQuery]
  where
    initialState label = pure State {label, count = 0}
    render state = HH.button [HE.onClick (\_ -> Increment)] [HH.text (state.label <> ": " <> T.pack (show state.count))]
    handleAction Increment = do
      modify (\s -> s {count = s.count + 1})
      gets (.count) >>= H.raise
    receive label = modify (\s -> s {label})
    handleQuery :: Query a -> H.HalogenM State Action Empty Int m (Maybe a)
    handleQuery (Reset next) = modify (\s -> s {count = 0}) >> pure (Just next)
    handleQuery (GetCount reply) = Just . reply <$> gets (.count)

definition :: Definition Query Text Int
definition =
  CE.defaultDefinition
    { input = \props -> fromMaybe "Clicks" (CE.attr "label" props)
    , observed = ["label"]
    , methods = [CE.tellMethod "reset" (\() -> Reset)]
    , styles = "button { font: inherit; padding: 0.5rem 1rem; border: 2px solid #4338ca; border-radius: 0.5rem; background: #eef2ff; cursor: pointer; }"
    }

The compiler loads once the page has started.

Your component will appear here after compilation.