Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure Submitter disabling #1216

Merged
merged 2 commits into from
Aug 14, 2024

Conversation

seanpdoyle
Copy link
Contributor

@seanpdoyle seanpdoyle commented Mar 2, 2024

Depends on #1217

Follow-up to #386

While Turbo's support for disabling a Form Submissions <input type="submit"> or <button> element is rooted in Rails UJS, it
degrades (and has always degraded) the accessibility of those
experiences.

To learn more about the risks involved, read the Don't Disable
Submits
section of Adrian Roselli's Don't Disable Form Controls
along with the additional resources mentioned therein.

The risk of degrading accessibility is especially true for Morph-enabled
Form Submissions. If a form submission will trigger a morphing Page
Refresh with the submitter focused, it's likely that the focus is
intended to
remain on the submitter.

With the current [disabled] behavior, that is not possible without a
bespoke event handler like:

addEventListener("submit", ({ target, submitter }) => {
  if (submitter) {
    target.addEventListener("turbo:submit-start", () => {
      submitter.disabled = false
      submitter.focus()
    }, { once: true })
  }
})

This commit introduces a Turbo.config.forms.submitter object with two
pre-defined keys: "disabled" (the default until we can deprecate it),
and "aria-disabled".

When applications specify either Turbo.config.forms.submitter = "disabled" or Turbo.config.forms.submitter = "aria-disabled", they
will be able to leverage those pre-packed hooks. Otherwise, they can
provide their own object with beforeSubmit(submitter) and
afterSubmit(submitter) functions.

@seanpdoyle
Copy link
Contributor Author

@afcapel @jorgemanrubia this is inspired by #774, although it not mentioned directly.

@seanpdoyle seanpdoyle force-pushed the config-disable-submitter branch 5 times, most recently from 8807db4 to cfb226b Compare March 3, 2024 04:09
@brunoprietog
Copy link
Collaborator

This looks great! As a screen reader user I can confirm that this is a great improvement. Hopefully it can become the default for the next major version.

The initial implementation aims to replace the ad hoc configurations
spread across `window.Turbo` and `Turbo.session`. Those objects
shouldn't be considered part of the public interface, especially for
extension.

This commit exports a single object from `src/core/config` to serve as a
centralized, user-facing repository for configuration values.

The main `src/core/config` object is composed of two constituent parts:

* `src/core/config/drive`
* `src/core/config/forms`

In the future, the directory structure can be expanded upon to suit the
needs of the project.

Any user-facing configuration outside of `Turbo.config` is deprecated.
Follow-up to [hotwired#386][]

While Turbo's support for disabling a Form Submissions `<input
type="submit">` or `<button>` element is rooted in [Rails UJS][], it
degrades (and has always degraded) the accessibility of those
experiences.

To learn more about the risks involved, read the [Don't Disable
Submits][] section of Adrian Roselli's [Don't Disable Form Controls][]
along with the additional resources mentioned therein.

The risk of degrading accessibility is especially true for Morph-enabled
Form Submissions. If a form submission will trigger a morphing Page
Refresh with the submitter focused, it's likely that the focus *is
intended to* remain on the submitter.

With the current `[disabled]` behavior, that is not possible without a
bespoke event handler like:

```js
addEventListener("submit", ({ target, submitter }) => {
  if (submitter) {
    target.addEventListener("turbo:submit-start", () => {
      submitter.disabled = false
      submitter.focus()
    }, { once: true })
  }
})
```

This commit introduces a `Turbo.config.forms.submitter` object with two
pre-defined keys: `"disabled"` (the default until we can deprecate it),
and `"aria-disabled"`.

When applications specify either `Turbo.config.forms.submitter =
"disabled"` or `Turbo.config.forms.submitter = "aria-disabled"`, they
will be able to leverage those pre-packed hooks. Otherwise, they can
provide their own object with `beforeSubmit(submitter)` and
`afterSubmit(submitter)` functions.

[hotwired#386]: hotwired#386
[Rails UJS]: https://guides.rubyonrails.org/v6.0/working_with_javascript_in_rails.html#automatic-disabling
[Don't Disable Form Controls]: https://adrianroselli.com/2024/02/dont-disable-form-controls.html
[Don't Disable Submits]: https://adrianroselli.com/2024/02/dont-disable-form-controls.html#Submit
@seanpdoyle seanpdoyle force-pushed the config-disable-submitter branch from cfb226b to 145faee Compare March 29, 2024 23:36
@seanpdoyle
Copy link
Contributor Author

@jorgemanrubia @brunoprietog are either of you able to review this proposal (as a follow-up to #1217)?

Copy link
Member

@jorgemanrubia jorgemanrubia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @seanpdoyle

@jorgemanrubia jorgemanrubia merged commit 6646957 into hotwired:main Aug 14, 2024
1 check passed
@seanpdoyle seanpdoyle deleted the config-disable-submitter branch August 14, 2024 07:39
seanpdoyle added a commit to seanpdoyle/turbo that referenced this pull request Aug 28, 2024
Follow-up to comment on [238ec26][]

[hotwired#1216][] unintentionally removed support for
the `Turbo.session.drive` boolean property and the
`Turbo.session.formMode` string property.

This commit re-introduces that support by delegating reads and writes to
those properties to their corresponding `Turbo.config` versions.

[hotwired#1216]: hotwired#1216
[238ec26]: hotwired@238ec26#commitcomment-145954235
iainbeeston added a commit to iainbeeston/avo that referenced this pull request Nov 21, 2024
Turbo 8 deprecated `Turbo.setConfirmMethod()` and instead moved it to `Turbo.config.forms.confirm` instead (see hotwired/turbo#1216). Because of that, avo is throwing deprecation messages in the javascript console:

    Please replace `Turbo.setConfirmMethod(confirmMethod)` with `Turbo.config.forms.confirm = confirmMethod`. The top-level function is deprecated and will be removed in a future version of Turbo.

I've updated the code to use the new config object instead, which should resolve the deprecation warning.
Paul-Bob pushed a commit to avo-hq/avo that referenced this pull request Nov 22, 2024
Turbo 8 deprecated `Turbo.setConfirmMethod()` and instead moved it to `Turbo.config.forms.confirm` instead (see hotwired/turbo#1216). Because of that, avo is throwing deprecation messages in the javascript console:

    Please replace `Turbo.setConfirmMethod(confirmMethod)` with `Turbo.config.forms.confirm = confirmMethod`. The top-level function is deprecated and will be removed in a future version of Turbo.

I've updated the code to use the new config object instead, which should resolve the deprecation warning.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants