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 two ways:
-
Form attribute: Add the
data-save-barattribute to aformelement. The save bar displays automatically when there are unsaved changes. Thesubmitevent fires when the Save button is pressed, and theresetevent fires when the Discard button is pressed. -
Programmatic control: Use
shopify.saveBarmethods (show(),hide(),toggle(),leaveConfirmation()) to control the save bar based on your application state.
Anchor to Use casesUse cases
- Form management: Automatically detect and manage unsaved form changes with save and discard actions.
- Data protection: Prevent accidental data loss by prompting users when leaving a page with unsaved changes.
- Programmatic control: Show, hide, and toggle the save bar programmatically for custom form workflows.
- Form integration: Integrate with HTML form elements using the
data-save-barattribute for automatic change tracking.
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.