Skip to main content

Save Bar API

Info

App Bridge isn't versioned with Polaris. App Bridge APIs and web components are identical in every App Home reference version.

The Save Bar API indicates that a form on the current page has unsaved information. You can implement save bar behavior in one of two ways:

  1. Automatic (form attribute): Add the data-save-bar attribute to a form element. The save bar displays automatically when there are unsaved changes. The submit event fires when the merchant clicks Save, and the reset event fires when the merchant clicks Discard. This is the simplest approach for standard form workflows. App Bridge ignores values that your code sets. To keep the save bar in sync after your code changes form values, refer to How the save bar tracks changes.

  2. Programmatic (web component): Add a <ui-save-bar> element with a unique id to your page, then use shopify.saveBar.show(id), shopify.saveBar.hide(id), and shopify.saveBar.toggle(id) to control it. The <ui-save-bar> element can contain <button> children for Save (with variant="primary") and Discard. This approach gives you full control over when the save bar appears and what happens when merchants interact with it.

Caution

Choose one approach for each form. Don't combine data-save-bar on a form with programmatic shopify.saveBar methods. Each approach manages save bar visibility independently and using both can cause unexpected behavior.

  • Standard forms: Use the data-save-bar attribute on a form element to automatically detect and manage unsaved changes with save and discard actions.
  • Custom state management: Use the <ui-save-bar> web component with shopify.saveBar methods to control the save bar based on application state that isn't tied to a single form.
  • Data protection: Prevent accidental data loss by prompting users when leaving a page with unsaved changes using shopify.saveBar.leaveConfirmation().

The saveBar object provides methods to programmatically control save bar visibility.

(id: string) => Promise<void>

Hides the save bar. Call this after the merchant saves or discards their changes.

Anchor to leaveConfirmation
leaveConfirmation
() => Promise<void>

Prompts the merchant to confirm before leaving the page when there are unsaved changes. The promise resolves when the merchant confirms or when no save bar is visible. Use this before programmatic navigation (for example, using window.location or custom routing) to prevent accidental data loss.

(id: string) => Promise<void>

Displays the save bar to indicate unsaved changes. Call this when you want to prompt the merchant to save.

Anchor to toggle
toggle
(id: string) => Promise<void>

Toggles save bar visibility between shown and hidden states.

Examples

html

<form
data-save-bar
onSubmit="console.log('submit', new FormData(event.target)); event.preventDefault();"
>
<label>
Name:
<input name="username" />
</label>
</form>

Preview

Display a save bar. This example adds the `data-save-bar` attribute to a form element. When the form has unsaved changes, the save bar appears automatically.

Anchor to The ui-save-bar elementThe ui-save-bar element

A <ui-save-bar> element takes these attributes and children:

  • id: The identifier that you pass to shopify.saveBar.show(), shopify.saveBar.hide(), and shopify.saveBar.toggle().
  • discardConfirmation: Shows a confirmation dialog when the merchant clicks Discard.
  • <button variant="primary">: Receives a click when the merchant clicks Save. A <button> without a variant receives a click when the merchant clicks Discard. Add the disabled or loading attribute to a button to disable it or show it as loading in the save bar.

The element has these methods, properties, and events:

  • show(): Shows the save bar. Returns a promise.
  • hide(): Hides the save bar. Returns a promise.
  • toggle(): Shows the save bar when it's hidden, and hides it when it's showing. Returns a promise.
  • showing: A read-only property that's true while the save bar shows.
  • discardConfirmation: A property that gets or sets the discardConfirmation attribute.
  • show and hide events: Fire when the save bar shows and when it hides.

shopify.saveBar.show(id), shopify.saveBar.hide(id), and shopify.saveBar.toggle(id) call the same method on the <ui-save-bar> element with that id. If no element on the page has that id, then the promise rejects.


While the save bar shows, the Shopify admin keeps the merchant on the current page: links in the admin don't open, and the save bar shakes. If your app changes its own route, then your app shows the new page, but the admin's address bar keeps the previous page, and a reload opens that previous page. Your app changes its route when it calls history.pushState(), when it calls a router function such as navigate(), and when its router follows a redirect after a form action. To learn how the address bar follows your app, refer to Address bar sync.

Before your app navigates away from a page with unsaved changes, decide what happens to those changes:

  • The navigation ends the edit: For example, the merchant deletes a resource, and your app redirects to a list. Hide the save bar before you start the navigation. For a <ui-save-bar> element, wait for shopify.saveBar.hide(id). For a form with the data-save-bar attribute, call the form's reset() method, which also runs your reset event handlers.
  • The merchant decides: Wait for shopify.saveBar.leaveConfirmation() before you navigate.

Don't rely on removing the <ui-save-bar> element or the form from the page to hide the save bar. The admin receives your app's route change while the save bar still shows, so the address bar stays on the previous page.

The following example hides the save bar before it submits a delete with React Router. The route's action deletes the QR code and redirects to the app's index page:

React Router

const submit = useSubmit();

async function handleDelete() {
await shopify.saveBar.hide('qr-code-form');
submit({action: 'delete'}, {method: 'post'});
}

Was this page helpful?