Skip to content

fix(modal): add turbo-permanent attributes to backdrop to prevent removal during morph#1124

Open
jalen0x wants to merge 1 commit intothemesberg:mainfrom
jalen0x:fix/modal-turbo-morph-backdrop
Open

fix(modal): add turbo-permanent attributes to backdrop to prevent removal during morph#1124
jalen0x wants to merge 1 commit intothemesberg:mainfrom
jalen0x:fix/modal-turbo-morph-backdrop

Conversation

@jalen0x
Copy link
Copy Markdown

@jalen0x jalen0x commented Jan 8, 2026

Summary

  • Add data-turbo-permanent and data-turbo-temporary attributes to the dynamically created backdrop element to prevent Turbo Morph from removing it during page refresh

Problem

When using Flowbite Modal with Turbo (Rails 8) and turbo_stream.refresh with 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/refresh
  • data-turbo-temporary: Tells Turbo not to cache this element (prevents duplicate backdrops on browser back)

Testing

  1. Create a Rails 8 app with Turbo and Flowbite
  2. Open a Flowbite modal
  3. Trigger turbo_stream.refresh from the server
  4. Before: backdrop disappears, modal breaks
  5. After: modal and backdrop remain visible and functional

Related Issues

Fixes #1123

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
@jalen0x jalen0x force-pushed the fix/modal-turbo-morph-backdrop branch from 21723a7 to 9dbf46d Compare January 8, 2026 15:29
Copy link
Copy Markdown

@codeCraft-Ritik codeCraft-Ritik left a comment

Choose a reason for hiding this comment

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

Great fix! Adding Turbo-specific attributes to preserve the backdrop resolves a tricky integration issue.
Nice handling of morph behavior while avoiding caching side effects 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Modal backdrop removed during Turbo Morph refresh

2 participants