Skip to content

"Save and Activate" saves but does not activate in Safari (button detected via document.activeElement) #606

Open

Description

Environment: Code Snippets 4.0.0, WordPress 7.1.3, Safari on macOS.

What happens: on the Add New and Edit Snippet screens, clicking Save and Activate saves the code but leaves the snippet inactive. After the page reloads we have to switch it on with the Active toggle. The same thing makes Save and Deactivate leave a snippet active.

Likely cause: the form's submit handler works out which button was pressed from the focused element, in js/components/EditMenu/SnippetForm/SnippetForm.tsx (handleSubmit):

const action = Object.values(SubmitSnippetAction).find(actionName =>
    actionName === document.activeElement?.getAttribute('name'))

In Safari (and Firefox on macOS), clicking a button does not give it focus, so focus stays in the code editor, no action matches, and the request goes out as a plain save with "active": false (useSubmitSnippet.tsx only sets active for SAVE_AND_ACTIVATE). In Chrome the click focuses the button, so it works there. The REST endpoint itself honours active correctly.

To reproduce in Chrome: cancel the button's mousedown (so focus stays in the editor), then click Save and Activate: the POST to /wp-json/code-snippets/v1/snippets carries "active": false and the snippet is saved inactive.

Suggested fix: read the form's submitter instead of the focused element, e.g.

const submitter = (event.nativeEvent as SubmitEvent).submitter
const action = Object.values(SubmitSnippetAction).find(a => a === submitter?.getAttribute('name'))

Thank you for the plugin.

Taylor Scott, Valley Sentinel

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions