Skip to content

Conversation

@jalen0x
Copy link

@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
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

1 participant