fix(modal): add turbo-permanent attributes to backdrop to prevent removal during morph #1124
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
data-turbo-permanentanddata-turbo-temporaryattributes to the dynamically created backdrop element to prevent Turbo Morph from removing it during page refreshProblem
When using Flowbite Modal with Turbo (Rails 8) and
turbo_stream.refreshwith Morph, the dynamically created backdrop element is removed during the morph process. This causes the modal to appear broken (no backdrop) or close unexpectedly.The issue is that Turbo Morph compares the current DOM with the server response and removes elements that don't exist in the response. Since the backdrop is dynamically created by JavaScript and not present in the server HTML, it gets removed.
Solution
Add two data attributes to the backdrop element when it's created:
data-turbo-permanent: Tells Turbo to preserve this element during morph/refreshdata-turbo-temporary: Tells Turbo not to cache this element (prevents duplicate backdrops on browser back)Testing
turbo_stream.refreshfrom the serverRelated Issues
Fixes #1123