forked from hotwired/turbo
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
6e28e7a
commit cfb226b
Showing
4 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,41 @@ | ||
export const forms = { | ||
mode: "on" | ||
import { cancelEvent } from "../../util" | ||
|
||
const submitter = { | ||
"aria-disabled": { | ||
beforeSubmit: submitter => { | ||
submitter.setAttribute("aria-disabled", "true") | ||
submitter.addEventListener("click", cancelEvent) | ||
}, | ||
|
||
afterSubmit: submitter => { | ||
submitter.removeAttribute("aria-disabled") | ||
submitter.removeEventListener("click", cancelEvent) | ||
} | ||
}, | ||
|
||
"disabled": { | ||
beforeSubmit: submitter => submitter.disabled = true, | ||
afterSubmit: submitter => submitter.disabled = false | ||
} | ||
} | ||
|
||
class Config { | ||
#submitter = null | ||
|
||
constructor(config) { | ||
Object.assign(this, config) | ||
} | ||
|
||
get submitter() { | ||
return this.#submitter | ||
} | ||
|
||
set submitter(value) { | ||
this.#submitter = submitter[value] || value | ||
} | ||
} | ||
|
||
export const forms = new Config({ | ||
mode: "on", | ||
submitter: "disabled" | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters