Skip to content

Conversation

Han5991
Copy link
Contributor

@Han5991 Han5991 commented Jun 26, 2025

Fixes #7951

Problem

The Collapse component had two conflicting requirements that couldn't be satisfied
simultaneously:

  1. Stack layout issue: When a Collapse is closed inside a Stack, it should not
    contribute to the Stack's gap spacing
  2. Nested animation issue: When Collapses are nested, the parent must remain in the
    DOM for child animations to work properly

Root Cause

The core issue was the use of display: 'none' in collapsed state:

  • ✅ With display: 'none': Stack gap issue is resolved, but nested animations break
  • ✅ Without display: 'none': Nested animations work, but Stack gap issue occurs

Solution

Added a keepMounted prop to the Collapse component that allows explicit control over
this behavior:

Default behavior (most common case)

  <Stack>
    <Button>Toggle</Button>
    <Collapse in={isOpen}>  {/* keepMounted defaults to false */}
      <Text>Content</Text>
    </Collapse>
  </Stack>
  • Uses display: 'none' when collapsed
  • Collapsed element doesn't contribute to Stack spacing
  • ✅ Fixes the Stack gap issue

Special case (nested animations)

  <Collapse in={show} keepMounted>  {/* keepMounted={true} */}
    <SegmentedControl />
    <Collapse in={value === 'b'}>
      Content
    </Collapse>
  </Collapse>
  • Doesn't use display: 'none' when collapsed
  • Parent remains in DOM for child height calculations
  • ✅ Enables proper nested animations

Implementation Details

  • Added keepMounted?: boolean prop to CollapseProps interface
  • Modified useCollapse hook to conditionally apply display: 'none'
  • Updated NestedCollapseWithControl story to demonstrate the fix

The new `keepMounted` prop ensures that collapsed elements remain in the DOM, useful for maintaining layout integrity in nested collapses. Updated hooks, types, and stories to support and demonstrate this functionality.
@OliverWales
Copy link
Contributor

This is ideal, thanks!

@rtivital rtivital merged commit 8695030 into mantinedev:master Jun 30, 2025
1 check passed
@rtivital
Copy link
Member

Thanks!

@nwparker
Copy link

nwparker commented Aug 1, 2025

Am I crazy to think by looking at this PR that this is technically still mounted in the DOM (it just uses display: none)?
Docs say: "Keep element in DOM when collapsed, useful for nested collapses"

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.

Collapse inside Stack spacing regression in 7.17.7->7.17.8
4 participants