Table of Contents

Adding controls to the header bar

The header bar is a good place for buttons and menus that should stay visible across views, such as controls for refreshing data or exporting a report. DataMiner renders the standard header bar, and your app adds its controls by posting messages to window.parent. When someone selects an action, DataMiner notifies your app, which is responsible for carrying out the action.

Setting the app controls

type HeaderMessage = { header: { set: HeaderItem[] } };
type HeaderItem = HeaderButton | { type: "spacer" } | { type: "separator" };
type ButtonState = { isLoading?: boolean; isDisabled?: boolean };
type Action = { identifier: string; children?: never };
type Menu = { identifier?: string; children: MenuItem[] };
type LabeledButton = { label: string; icon?: string; title?: string };
type IconOnlyButton = { label?: never; icon: string; title: string };

type HeaderButton = { type?: "button" } & ButtonState &
    (LabeledButton | IconOnlyButton) & (Action | Menu);
type MenuItem = { type?: "button" } & LabeledButton & (
    | (Action & ButtonState)
    | (Menu & { isLoading?: never; isDisabled?: never })
);

set replaces the app's controls and displays them in the order you provide. An empty list clears them.

Text fields cannot be blank. The icon value must be a Material icon name, such as refresh. Unique identifiers keep actions and updates unambiguous, even across nested menus. A menu only needs an identifier if you want to update it.

// Match each action notification to the identifier in the configuration.
window.addEventListener("message", event => {
    if (event.source !== window.parent) return;

    switch (event.data?.header?.identifier) {
        case "refresh":
            console.log("Refresh the current view");
            break;
        case "export-csv":
            console.log("Export the current view as CSV");
            break;
    }
});

// Post the full configuration when the app starts.
window.parent.postMessage({
    header: {
        set: [
            { icon: "refresh", title: "Refresh data", identifier: "refresh" },
            { type: "separator" },
            { label: "Export", children: [{ label: "Export CSV", identifier: "export-csv" }] }
        ]
    }
}, "*");

When someone selects an enabled action that is not loading, DataMiner sends { header: { identifier: string } } as a window message. Opening the menu itself does not send an action message.

Updating an existing control

type HeaderUpdateMessage = {
    header: {
        update: Array<{
            identifier: string;
            label?: string | null;
            title?: string | null;
            icon?: string | null;
            isLoading?: boolean;
            isDisabled?: boolean;
        }>;
    };
};

To update an existing control, specify its identifier and at least one field to change. Omitting label, title, or icon leaves its value unchanged. Passing null explicitly removes the value, as long as the button still meets the requirements above. Changes to the layout, menu structure, or identifiers require a new set message.

// Show the loading state and prevent another activation while data is refreshing.
window.parent.postMessage({
    header: { update: [{ identifier: "refresh", isLoading: true }] }
}, "*");
// Set isLoading back to false when the refresh finishes, even if it fails.

Error messages

The host sends errors as { type: "error", message: string } window messages. A rejected command leaves the existing controls unchanged. Successful commands do not send a reply.

Message Meaning
Invalid header command. The header object must contain exactly one command: set or update.
Invalid header configuration. The set value is not an array of valid header items.
Invalid header update. The update value is not an array of valid updates.
Unknown or ambiguous header button identifier: '<identifier>'. The identifier matches no controls or more than one control.
Invalid header update for '<identifier>'. The change would leave the specified control in an invalid state.
Advanced search syntax