polaris

    Framework-free MoonBit bindings for Shopify Polaris web components and App Bridge.

    polaris
    shopify
    web-components
    dom
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    57 minutes ago
    Downloads
    2

    No dependencies

    #sennenki/polaris

    Framework-free MoonBit bindings for Shopify's Polaris web components (polaris-1.js) and a small, typed subset of App Bridge. No React, no virtual DOM, no signals, no runtime dependencies.

    Status: alpha. The API is contract-tested against a DOM stub and a local Chromium smoke suite loads the pinned real polaris-1.js. The Shopify Admin checklist and Polaris version matrix are still pending; see docs/browser-checklist.md.

    #Why this package exists

    Polaris components are Lit custom elements. Some state must be written as a JavaScript property (value, checked, error, disabled, loading), while the rest is plain attributes. Writing value as an attribute can look right in HTML and still leave the component's internal state stale. This package makes that distinction explicit and testable.

    • JS backend only (supported_targets = "-all+js").
    • Constructors return @dom.DomNode; there is no wrapper type mirroring Lit internals.
    • No rendering loop and no global state: the caller owns data and re-renders.
    • Package-level tree shaking: import only dom, components, form, etc.

    #Install

    moon add sennenki/polaris

    Requires a MoonBit toolchain with the new moon.mod/moon.pkg DSL.

    #Quick start: a form

    ///|
    import {
    "sennenki/polaris/dom",
    "sennenki/polaris/components",
    "sennenki/polaris/events",
    "sennenki/polaris/form",
    }

    ///|
    fn order_form() -> @dom.DomNode {
    let email = @components.text_field(name="email", label="Email", required=true)
    let country = @components.select(name="country", options=[
    @components.SelectOption("us", "United States"),
    @components.SelectOption("ca", "Canada", selected=true),
    ])
    let terms = @components.checkbox(name="terms", label="Accept terms")
    let submit = @components.button(
    label="Save",
    variant=@components.Primary,
    submit=true,
    )..attr("data-polaris-submit", "")

    let form = @dom.el("form")
    ignore(form..append(email))
    ignore(form..append(country))
    ignore(form..append(terms))
    ignore(form..append(submit))

    @events.on_submit(form, fn(event) {
    event.prevent_default()
    let values = @form.read_form(form)
    match values.get("email") {
    Some(@form.Text(value)) => println("email: " + value)
    _ => println("email missing")
    }
    @form.set_busy(form, true)
    })
    form
    }

    read_form recognises text, colour, select, checkbox, switch, date, money, specialized text fields, native input/select/textarea, and only elements with a name. Unsupported named elements are ignored. Multi-value s-choice-list controls are read separately with @form.read_form_values(form).

    #App Bridge

    Call Bridge::detect() inside an event handler or after mount, never during construction; the CDN may load late or be blocked.

    ///|
    import {
    "sennenki/polaris/appbridge",
    }

    ///|
    async fn load_orders() -> Unit {
    match @appbridge.Bridge::detect() {
    None => println("App Bridge unavailable")
    Some(bridge) =>
    match bridge.fetch("/api/orders", @appbridge.FetchInit()) {
    @appbridge.Ok(response) => println(response.body)
    @appbridge.Unauthorized => bridge.navigate("/login")
    @appbridge.Error(message) => println(message)
    }
    }
    }

    • id_token() caches until shortly before the JWT exp (60 second tokens).
    • fetch() retries once after a 401 with a fresh token, then returns Unauthorized.
    • resource_picker() normalises {id}, {ids}, single objects and arrays.
    • toast, navigate, and locale degrade to safe no-ops / fallbacks.

    #SSR head

    ///|
    import {
    "sennenki/polaris/script",
    }

    ///|
    let head = @script.head("your-public-api-key")

    Output order is significant and fixed:

    <meta name="shopify-api-key" content="your-public-api-key" /> <script src="https://cdn.shopify.com/shopifycloud/app-bridge.js"></script> <script src="https://cdn.shopify.com/shopifycloud/polaris-1.js"></script>

    app-bridge.js must run before every other script. A page with no interaction should not load either bundle.

    #Property-vs-attribute contract

    ComponentMust be a propertyPlain attributes
    s-text-fieldvalue, errorname, label, placeholder, multiline, required, disabled, autocomplete
    s-selectvalue, errorname, label, disabled
    s-checkboxchecked, errorname, label, disabled
    s-buttondisabled, loadingvariant, tone, size, href, target
    s-banner—heading, tone, dismissible
    s-email-field, s-password-field, s-search-field, s-url-field, s-number-field, s-text-areavalue, errorname, label, placeholder, details, autocomplete, min/max/step, ...
    s-switchchecked, errorname, label, details, disabled, required
    s-avatar—initials, src, alt, size
    s-button-group—gap, accessibilitylabel
    s-divider—direction, color
    s-progress—value, max, tone, accessibilitylabel
    s-icon—type, size, color, tone, interestfor
    s-image—src, alt, srcset, aspectratio, objectfit, loading, borders
    s-chip / s-clickable-chip—color, removable, href, disabled
    s-tooltip—id; trigger via interestfor
    s-color-fieldvalue, errorname, label, autocomplete, details, disabled, required
    s-color-pickervaluename, alpha
    s-date-fieldvalue, errorname, label, placeholder, autocomplete, details, disabled, required, readonly
    s-money-fieldvalue, errorname, label, placeholder, currencycode, min, max, disabled, required, readonly
    s-modal— (use modal_show/hide/toggle)heading, accessibilitylabel, size, padding
    s-grid—gridtemplatecolumns, gap, alignitems, ...
    s-option-group—label, disabled
    s-ordered-list——
    s-scroll-box—overflow, snaptype, scrollpadding, scrollmargin, accessibilityrole, accessibilitylabel, padding, blocksize
    s-query-containercontainerName—

    Constructors apply this table for you. If you build elements by hand, use @dom.DomNode::prop for the property column and attr for the attribute column.

    #Packages

    PackagePurpose
    domDomNode, JsValue, attributes/properties, queries, events, stub element for tests
    eventstyped input/change/click/dismiss/submit helpers
    componentsconstructors for Polaris web components
    formread values, apply server errors, busy state
    appbridgewindow.shopify detection, token, fetch, picker, toast, navigation
    scriptSSR meta/script head generation

    See docs/ for per-area API notes and examples/ for compilable builders.

    #Unavailable in the pinned Polaris build

    The pinned polaris-1.js does not define s-pagination, s-tabs/s-tab, or s-app-nav, so these have no wrappers. The rest of the third batch is implemented and browser-smoked. Adding speculative wrappers for nonexistent elements would hide exactly the property/attribute mistakes this package exists to prevent.

    #Shopify Admin smoke app

    shopify-smoke/ is a self-contained Admin app for the remaining real-browser checks: App Bridge detection, ID token, authenticated fetch, locale, picker, toast, and component/form behavior inside Shopify Admin. It is excluded from Mooncake packaging by .moonignore.

    npm run build:smoke npm run start:smoke

    See shopify-smoke/README.md for Dev Dashboard URLs, the OAuth callback, tunnel setup, and the no-account local test (npm run test:smoke:local).

    #Testing

    moon test --target js moon check --target js --deny-warn moon fmt --check npm run test:browser

    The MoonBit suite runs without a browser: dom falls back to a tiny element stub when document is absent. That verifies property/attribute/event contracts, not Lit rendering. npm run test:browser loads the pinned real polaris-1.js in Chromium and covers form payloads, submit, errors, table hierarchy, SSR upgrade, and App Bridge degradation. See the browser checklist before claiming a component works inside Shopify Admin.

    #Version compatibility

    This packagePolaris buildBrowserNotes
    0.1.xpolaris-1.js, SHA-256 912455…818feChromium 159local smoke + Shopify Admin smoke
    0.1.xpolaris-2.0-rc.js, SHA-256 89dd20…0c7bChromium 159same 21-test suite passes; no covered API differences
    0.1.xbothFirefox / WebKitCI projects configured; not yet recorded on a local desktop

    The package version is independent of the Polaris script version. The smoke fixture records the full URL, fetch date, and SHA-256 in e2e/fixtures/README.md; growth of that file is a deliberate prompt to re-run the checklist.

    #License

    Apache-2.0. See LICENSE.