Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
turbo:before-permanent-element-render
While transposing a [permanent element][] from page to page, dispatch a `turbo:before-permanent-element-render` event on the document with a `(currentPermanentElement: Element, newPermanentElement: Element) => void` available as the event's `detail.render` property. The `turbo:before-permanent-element-render` is named to follow the pattern established by its predecessors (`turbo:before-render`, `turbo:before-frame-render`, `turbo:before-stream-render`), and its `CustomEvent.detail.render` property name mirrors the `turbo:before-render` event's `CustomEvent.detail.render` property. This opportunity to modify a permanent element might be useful if it's possible for the preserved element to become partially stale. For example, consider an invalid `<form>` submission with an `<input type="file">`. Since the server's response cannot fully encode the submitted file into the response's element, it might be a useful to mark it with `[data-turbo-permanent]` to preserve whatever client-side state precedes the submission: ```html <label for="profile_image">Profile image</label> <input id="profile_image" type="file" data-turbo-permanent> ``` Suppose the uploaded file is too large, or doesn't match the server's expectations for file type. The server's response might contain a fragment like: ```html <label for="profile_image">Profile image</label> <input id="profile_image" type="file" data-turbo-permanent class="invalid" aria-describedby="profile_image_error"> <p id="profile_image_error">Profile image is too large (5GB). Try a smaller file (<5KB)</p> ``` Prior to this change, the fact that the `<input>` is marked with `[data-turbo-permanent]` would ignore the `[class]` and `[aria-describedby]` attributes from the response, and the `<input>` would remain unchanged (with the attached file still in-memory). With a new `turbo:before-permanent-element-render` available, there's an opportunity to do application-specific merging: ```js addEventListener("turbo:before-permanent-element-render", ({ target, { detail } }) => { detail.render = (currentElement, newElement) => { currentElement.setAttribute("class", newElement.getAttribute("class")) currentElement.setAttribute("aria-describedby", newElement.getAttribute("aria-describedby")) } }) ``` [permanent element]: https://turbo.hotwired.dev/handbook/building#persisting-elements-across-page-loads
- Loading branch information