Get 2024 Templates Mega Bundle!14 Bootstrap, Vue & React Templates + 3 Vector Sets
Get for 99$

Stacked modal backdrop is not over the first modal


Hello,
I was testing stacked modals, when open stacked modal (level 2) its 'div.modal-backdrop fade show' gets place just above the first modals div.modal-backdrop not betweend the first and second modals.

In your example you showed the modals from small to large which doesn't require the modal-backdrop to fade to black.

So, my question is: Is there a way to make first modal fade into black when its stacked modal opened?
Check the image for the issue, if the question was not clear for you.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (7)


Hi,

Sorry for the late reply.

Could you please clarify your requirements?

When the 2nd modal is shown the page content and the first modal should be invisible behind the 2nd modal's backdrop ?

Regards



Hi,
I just want 'div.modal-backdrop fade show' of stacked modal (which opened above a modal) be between two modals.
When I inspect all 'div.modal-backdrop fade show' are at the end of the HTML just before body closing tag.
Didn't you check the links in my answer?



Hi,

Got it. Great suggestion. We will think about how to implement this. This feature is not supported by the Bootstrap Modal component so we will need to come up with a workaround. We added this task to our to-do list and we will let you know once it's available. Stay tuned!

Regards.



Thank you for response! For now I handled the issue with JS code and 'backdrop-patch' but my js code still cannot remove added '.backdrop-patch' if I used 3 level of modals. Anyways, I decided not to use 2 stacked modal over the first one.

BTW, you said 'stay tuned'. How I'll be informed or do you publish the resolution somehere?

Here is the js code happy
// BS Stacked Modals Backdrop Patch happy
// Store the IDs of first-level modals
var firstModalIDs = ['materialCard', 'anotherModalID'];

// Reference to the first-level modal
var firstLevelModal;

// Event delegation for click events on buttons inside first-level modals with data-bs-stacked-modal attribute
$(document).on('click', '.modal .modal-content button[data-bs-stacked-modal]', function (event) {
var stackingModal = $(this).data('bs-stacked-modal');
var modalContent = $(this).closest('.modal-content');
var modalID = modalContent.closest('.modal').attr('id');

// Check if the clicked button is inside a first-level modal and its ID is in the firstModalIDs list
if (modalID && firstModalIDs.includes(modalID) && stackingModal && modalContent.find('.backdrop-patch').length === 0) {
modalContent.prepend('<div class="backdrop-patch"></div>');
firstLevelModal = modalContent.closest('.modal'); // Store reference to the first-level modal
}
});

// Event listener for when a second modal is shown
$(document).on('shown.bs.modal', function (event) {
var stackedModal = $(event.target);
var modalID = stackedModal.attr('id');

// Check if the stacked modal is inside any modal (first-level, second-level (third level patch has bugs))
// and it has the data-bs-stacked-modal attribute
if (modalID && stackedModal.attr('data-bs-stacked-modal')) {
var firstModalContent = stackedModal.prev('.modal-content');
if (firstModalContent.length > 0) {
firstLevelModal = firstModalContent.closest('.modal');
}
}
});

// Event listener for when a second modal is hidden
$(document).on('hidden.bs.modal', function (event) {
if (firstLevelModal) {
var modalContent = firstLevelModal.find('.modal-content');
modalContent.find('.backdrop-patch').remove();
firstLevelModal = null;
}
});

and CSS:
.backdrop-patch {
position: absolute;
inset: 0;
z-index: 1;
background-color: var(--bs-backdrop-bg);
opacity: var(--bs-backdrop-opacity);
border-radius: var(--bs-modal-border-radius);
}



Hi happy,

Thank you for sharing this. We will have a look at your solution for sure. We will inform you here and provide the hot fix once our solution is available. It's on our todo list.

Regards.



Hi Sean,
I'm surprized that the solution was so easyshocked I found it in stackoverflow.
Patching was so bad happy

/* Adjust z-index for stacked modals and their modal-backdrop */
const zIndex = 1040 + 10 * $('.modal:visible').length;
$(this).css('z-index', zIndex);
setTimeout(() => $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack'));



Hi happy,

Great. Thank you for sharing this. We will check this further.

Regards.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(