Skip to content

Do not trigger unsaved changes alert for action modals that cannot contain unsaved changes - #20331

Open
grafst wants to merge 1 commit into
filamentphp:5.xfrom
grafst:fix/action-unsaved-changes-alert
Open

Do not trigger unsaved changes alert for action modals that cannot contain unsaved changes#20331
grafst wants to merge 1 commit into
filamentphp:5.xfrom
grafst:fix/action-unsaved-changes-alert

Conversation

@grafst

@grafst grafst commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Description

When unsavedChangesAlerts() is enabled for a panel, the beforeunload guard for action modals fires whenever any action is mounted — setUpUnsavedActionChangesAlert() only checks $wire.mountedActions.length. A read-only modal such as ViewAction's therefore triggers the browser's "unsaved changes" warning when the user closes the tab or navigates away, even though the modal contains no editable inputs and no unsaved state can exist. (Previously reported in #13766, which was closed for an invalid reproduction repository.)

Reproduction: enable ->unsavedChangesAlerts() on a panel, open a ViewAction modal from a table, then close the browser tab — the browser warns that entered information may not be saved (e.g. Firefox: "This page is asking you to confirm that you want to leave — information you've entered may not be saved"), despite the modal being read-only.

With this PR, each mounted action stamps a hasUnsavedChangesAlert flag into its mountedActions entry, and the client-side guard only warns if at least one mounted action has the flag set:

  • By default the flag is true (current behavior), unless the action's schema is disabled. ViewAction calls disabledSchema(), so its modals no longer trigger the alert — and neither do custom actions that use disabledSchema().
  • A new unsavedChangesAlert() method on actions allows opting in or out per action, e.g. ->unsavedChangesAlert(false) for a read-only modal that renders an infolist without disabling its schema. It accepts a closure, consistent with the other modal configuration methods.
  • Mounted action entries without the flag (e.g. state persisted from before an upgrade) keep the current behavior of warning.

Visual changes

None — the only change is that the native browser leave-confirmation dialog is no longer requested while a read-only action modal is open. The dialog itself is browser chrome and cannot be screenshotted meaningfully.

Functional changes

  • Code style has been fixed by running the composer cs command.
  • Changes have been tested to not break existing functionality.
  • Documentation is up-to-date.

New tests cover the default flag for editable schemas, disabled schemas, the unsavedChangesAlert(false) opt-out, the closure/re-opt-in path, and ViewAction mounting (tests/src/Actions/ActionTest.php, tests/src/Actions/ViewActionTest.php). The full tests/src/Actions and tests/src/Tables/Actions suites plus RelationManagerTest pass, and PHPStan reports no errors on the changed files. Documentation was added to packages/actions/docs/02-modals.md and docs/05-panel-configuration.md.

🤖 Generated with Claude Code

…ntain unsaved changes

The panel-level unsaved changes alert hooked `beforeunload` whenever any
action was mounted, so read-only modals such as `ViewAction`'s warned
about unsaved changes that cannot exist. Mounted actions now expose a
`hasUnsavedChangesAlert` flag that the client-side guard checks, which
defaults to `false` for actions with a disabled schema and can be
controlled per action with `unsavedChangesAlert()`.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@danharrin danharrin added enhancement New feature or request pending review labels Aug 5, 2026
@danharrin danharrin added this to the v4 milestone Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request pending review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants