Skip to content

An exploration into Datastar's interoperability with third-party JavaScript libraries

biotz/datastar-js-interop-demo

Repository files navigation

Datastar-JavaScript interop demo

In this repository, bidirectional integration between Datastar and third-party JavaScript libraries is explored; namely, Google Maps JavaScript API serves as an example of such a library.

Prerequisites

Running

npm install && make develop-components

and then navigate to http://localhost:5173/.

Documentation

We proceed by wrapping the API in a Web component (see here). Then, achieving bidirectional data and control flow between the API and Datastar is quite straightforward.

Data flow

Datastar → JavaScript

The data flow from Datastar to the API is demonstrated by having the map centered at the coordinates specified in "Map center" (try it out!). The inputs feed the center signal using data-bind and the API web component, in turn, tracks the signal changes through data-attr; all we need to do is declare the center attribute as an observed attribute and implement the attributeChangedCallback.

JavaScript → Datastar

The data flow from the API to Datastar is demonstrated by displaying the latest change to the polygon in "Polygon change" (try dragging and dropping the nodes of the polygon). The API events trigger custom events on the API component and the custom events, in turn, trigger changes to the event signal, which the read-only fields in "Polygon change" bind to using data-bind.

Control flow

Datastar → JavaScript

To see the control flow from Datastar to the API in action, click the "Reset map center" or "Reset polygon" button to reset the map center or the polygon, respectively. We leverage data-ref to store a reference to the API web component in a signal and then use the reference to call a method of the API web component whenever a button is clicked.

JavaScript → Datastar

The control flow from the API to Datastar can be achieved by triggering custom events on the API web component - see above.

About

An exploration into Datastar's interoperability with third-party JavaScript libraries

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published