JavaScript & Interactivity

How StellarAdmin.UI adds interactivity to the components with JavaScript

StellarAdmin.UI uses HTML and CSS features of the browser wherever it can. Where a component needs more, such as keyboard handling or state that changes in the browser, StellarAdmin.UI adds it with small web components.

Adding the script

The script is part of the installation. Add it to the <head> of your layout:

<script type="module" src="/_content/StellarAdmin.UI/stellar-admin.js" asp-append-version="true"></script>

The script contains:

Web components

Some Tag Helpers render a custom element, such as <sel-tabs>, around their markup or in place of it. The script registers these custom elements, which add behavior to the HTML that the server renders.

The table below lists the custom element each Tag Helper renders, and what it adds:

Tag HelperCustom elementWhat the script adds
<sa-carousel><sel-carousel>Scrolling between the slides, enabling and disabling the previous and next buttons, and the position indicators.
<sa-collapsible><sel-collapsible>The --toggle, --show and --hide commands, and the aria-expanded attribute of the buttons that open it.
<sa-list-editor>, <sa-dictionary-editor><sel-collection-editor>Adding, removing and reordering rows, with the keyboard, pasting and dragging, and renumbering the names that the rows post. Checking the rows, blocking the submit while a row is invalid, and dropping blank rows when the form is submitted.
<sa-command><sel-command>Filtering the items as you type, and keyboard navigation.
<sa-dialog>, <sa-alert-dialog>, <sa-sheet><sel-dialog>Locking the page's scrolling while a modal dialog is open, and the data-open attribute, which reflects whether the dialog is open.
<sa-dropdown-menu-content>, <sa-dropdown-menu-sub-content><sel-dropdown-menu>Menu keyboard navigation, with the arrow keys, Home, End and typing an item's first letters. Selecting an item with Enter or Space, closing the menu when an item is selected, checkbox and radio items, and opening and closing submenus.
<sa-icon-picker><sel-icon-picker>Opening the popover, loading the icons from icons-url the first time it opens, searching the icons, moving through the grid with the arrow keys, and putting the selected icon's name in the hidden input.
<sa-image-upload><sel-image-upload>Opening the file picker, displaying the selected image and posting its value.
<sa-input-otp><sel-input-otp>Displaying the value of the <input> in the slots, with the active slot and the caret, and enforcing the input's pattern.
<sa-message-scroller><sel-message-scroller>Scrolling to new messages, and the scrollToStart() and scrollToEnd() methods and the following property.
<sa-rich-text-editor><sel-rich-text-editor>The whole editor: the toolbar, the selection bubble, the / menu, links, tables and image uploads, and writing the HTML to the <textarea> that the form posts. The editor has its own script.
<sa-questionnaire><sel-questionnaire>The shortcut keys of <sa-questionnaire-choices>, and the arrow keys, which move between a question's answers and wrap at either end. Posting either the free-text answer or the selected choices, but not both.
<sa-sidebar-wrapper><sel-sidebar>Expanding and collapsing the sidebar on desktop, opening and closing the drawer on mobile, and the --toggle-sidebar, --open-mobile and --close-mobile commands.
<sa-slider><sel-slider>Moving the thumbs with the pointer and the keyboard, and updating the range fill, aria-valuenow and the hidden inputs.
<sa-table> with selection or row-details<sel-table>The checkbox that selects all rows, the highlight of selected rows, and expanding and collapsing row details.
<sa-tabs><sel-tabs>Displaying the selected tab's panel, moving between the tabs with the arrow keys, and the tabhide and tabshow events.
<sa-toaster><sel-toaster>Displaying, stacking and closing toasts, pausing their timers on hover and focus, and displaying the toasts the server queued for the page.

The names of the custom elements are internal and can change in any version, so don't select or style them by name. To call a component's JavaScript API, give its Tag Helper an id, and get the element with document.getElementById(). The id is rendered on the element that has the API.

The custom elements don't use a shadow root, so your CSS, Tailwind CSS and your own scripts can reach the markup inside them. Some components also work without the script. For example, a <sa-table selection="true"> still posts its checked rows, and a <sa-dialog> still opens with a command="show-modal" button.

Waiting for the custom elements

The script is a module, so it runs after the browser has parsed the page, and only then adds the behavior to the custom elements. If your own code uses one of them, such as to read selectedValues from a table with selection, run your code in a DOMContentLoaded listener, or in a module or deferred script after stellar-admin.js.

Scripts of large components

Components that are large and only used on a few pages have their own script, which isn't part of stellar-admin.js. The rich text editor is one of them. You don't need to add its script to your layout: the editor adds it to the page, and the browser loads it once, however many editors the page has. This also works when htmx adds an editor to the page.

Invoker commands

StellarAdmin.UI uses the Invoker Commands API to open and close components, such as dialogs, without JavaScript. To open a component, give a button a commandfor attribute with the component's id, and a command attribute with the command:

<sa-button commandfor="booking-dialog" command="show-modal">Edit booking</sa-button>

<sa-dialog id="booking-dialog">
    ...
    <sa-button commandfor="booking-dialog" command="close">Cancel</sa-button>
</sa-dialog>

<sa-dialog>, <sa-alert-dialog> and <sa-sheet> use the browser's built-in commands, such as show-modal, show and close. Other components add their own commands, such as --toggle, --show and --hide on <sa-collapsible>.

The element with commandfor and command must be a <button>. <sa-button> renders one. On any other element, the attributes do nothing.

StellarAdmin.UI doesn't include a polyfill for invoker commands. Check the browser support against the browsers your users have.

Interest invokers

Tooltips and popovers that open on hover use interest invokers. The element that opens the tooltip or popover has an interestfor attribute, which opens it on hover, focus and long-press.

The script includes a polyfill for interest invokers, so they work in all browsers that StellarAdmin.UI supports. Browsers that support interest invokers only support them on links and buttons, so the polyfill also adds them to other elements, such as inputs, selects and toggles, with the same delays and the same interest and loseinterest events. For a control inside a label, such as a toggle's checkbox, hovering over the label opens the tooltip, which is aligned with the label.

The window.stellarAdmin object

The script adds a window.stellarAdmin object, which you use to open dialogs from your own JavaScript and wait for the result. The example below opens a dialog, and reads the dialog's form values when the dialog is confirmed:

const result = await window.stellarAdmin.dialog("#booking-dialog").showAsync();
if (result.confirmed) {
    // result.data contains the dialog's form values
}

window.stellarAdmin has the following members:

MemberUsed to
dialog()Open a <sa-dialog> or <sa-sheet>. Its showAsync() method opens the dialog, and returns whether it was confirmed, its returnValue and its form values.
alertDialog()Open a <sa-alert-dialog>, the same way as dialog().
toastDisplay toasts. Its fromResponse() method displays the toasts that the server sent with a fetch or XHR response.

If your page already has a window.stellarAdmin object, the script replaces it, and logs a warning in the browser console.

Stability

The following only change in a breaking release:

The following are internal, and can change in any version:

  • The names of the custom elements.
  • The data-* attributes the custom elements set.
  • The JSON in the SA-Toasts header. To read it, use toast.fromResponse().

On this page