fix(modal): add turbo-permanent attributes to backdrop to prevent removal during morph#1124
Open
jalen0x wants to merge 1 commit intothemesberg:mainfrom
Open
fix(modal): add turbo-permanent attributes to backdrop to prevent removal during morph#1124jalen0x wants to merge 1 commit intothemesberg:mainfrom
jalen0x wants to merge 1 commit intothemesberg:mainfrom
Conversation
Patch Modal._createBackdrop in index.turbo.ts to add data-turbo-permanent and data-turbo-temporary attributes to the backdrop element. This prevents Turbo Morph from removing the dynamically created backdrop during page refresh, while keeping the core Modal component unchanged for non-Turbo users. Fixes themesberg#1123
21723a7 to
9dbf46d
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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