Save bar API
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:
-
Automatic (form attribute): Add the
data-save-barattribute to aformelement. The save bar displays automatically when there are unsaved changes. Thesubmitevent fires when the merchant clicks Save, and theresetevent fires when the merchant clicks Discard. This is the simplest approach for standard form workflows. -
Programmatic (web component): Add a
<ui-save-bar>element with a uniqueidto your page, then useshopify.saveBar.show(id),shopify.saveBar.hide(id), andshopify.saveBar.toggle(id)to control it. The<ui-save-bar>element can contain<button>children for Save (withvariant="primary") and Discard. This approach gives you full control over when the save bar appears and what happens when merchants interact with it.
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.
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.
Anchor to Use casesUse cases
- Standard forms: Use the
data-save-barattribute on aformelement to automatically detect and manage unsaved changes with save and discard actions. - Custom state management: Use the
<ui-save-bar>web component withshopify.saveBarmethods 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().
Anchor to MethodsMethods
The object provides methods to programmatically control save bar visibility.
- Anchor to hidehidehide(id: string) => Promise<void>(id: string) => Promise<void>
Hides the save bar. Call this after the merchant saves or discards their changes.
- Anchor to leaveConfirmationleaveConfirmationleaveConfirmation() => Promise<void>() => 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.locationor custom routing) to prevent accidental data loss.- Anchor to showshowshow(id: string) => Promise<void>(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 toggletoggletoggle(id: string) => Promise<void>(id: string) => Promise<void>
Toggles save bar visibility between shown and hidden states.