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 componentEach <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 (..)).
inputbuilds component input from element properties.CE.attr "label" propsreads the label attribute;CE.propwould decode a JavaScript property from JSON. HereH.Component Query Text Int mtakes aTextlabel.observedlists names to watch. Changes recompute input and reachH.onReceive, implemented here byreceive. Without this handler, state would retain the original label.methodsexposes queries.CE.tellMethod "reset" (\() -> Reset)makeselement.reset()send the same tell a Halogen parent would send withH.tell. The method returns a promise that resolves after delivery.stylessupplies 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
- Expose count outputs as browser events. Import
Data.Aeson (toJSON)and addoutput = \count -> Just ("count", toJSON count)to the definition.H.raisealready emits the count; this mapping turns it into a named event with JSONdetail. Compile and click: the paragraph now reports it. The event bubbles beyond the shadow root. - Add
CE.method "count" (\() -> GetCount)tomethods. Compile and press "count()". Its promise resolves with the query answer, likeH.requestin lesson 4.