简体   繁体   English

使用水平 MUI 折叠组件和 TransitionGroup 过渡时,如何使 Flexbox 与儿童一起使用?

[英]How to make Flexbox work with children when using horizontal MUI Collapse Component and TransitionGroup transitions?

I am using MUI and have a hard time using the Collapse + TransitionGroup Component and getting the containers to expand with flex box.我正在使用 MUI,并且很难使用 Collapse + TransitionGroup 组件并使用弹性框来扩展容器。

<Box sx={{display: 'flex', height: '100vh', width: '100vw'}}>
  <Box sx={{flex: 1}}>Content-A</Box>
  <Box sx={{flex: 1}}>Content-B</Box>
</Box>

This is a simple example of what I want.这是我想要的一个简单示例。 Both Boxes next to each other and splitting the whole page in a left side and right side.两个盒子彼此相邻,将整个页面分成左右两边。 However when you introduce the Collapse Component it will break:但是,当您引入 Collapse 组件时,它会中断:

<TransitionGroup sx={{display: 'flex', height: '100vh', width: '100vw}}>
  <Collapse orientation='horizontal'>
    <Box sx={{flex: 1}}>Content-A</Box>
  </Collapse>
  <Collapse orientation='horizontal'>
    <Box sx={{flex: 1}}>Content-B</Box>
  </Collapse>
</TransitionGroup>

The Collapse component is now interfering directly with the Flexbox parent-child relationship. Collapse 组件现在直接干扰 Flexbox 父子关系。 If I look into the DOM I notice that the Collapse Component inserts 3 wrappers around the child component (root, outer, inner wrap).如果我查看 DOM,我注意到 Collapse 组件在子组件周围插入了 3 个包装器(根、外部、内部包装)。 How do I pass the flexbox properties down to the original child?如何将 flexbox 属性传递给原始子项?
I tried overriding the style of each of those wrapping components in the theme and giving them display: flex, flex: 1 to force it all the way down the chain to the child but that breaks the transition and it looks super choppy.我尝试覆盖主题中每个包装组件的样式并给它们display: flex, flex: 1以强制它一直沿着链向下传递到子级,但这会破坏过渡,并且看起来非常不稳定。

EDIT: For clarification: I need the transition group because I want a specific transition to happen.编辑:澄清:我需要过渡组,因为我希望发生特定的过渡。 Basically I have the page split in left (A) and right (B).基本上我的页面分为左(A)和右(B)。 On an event I want (A) to transition out the screen (to the left, hence horizontal).在一个事件中,我希望 (A) 转换出屏幕(向左,因此是水平的)。 (B) should transition to the place where (A) was and a new element (C) comes in from the right and transitions to where (B) was. (B) 应该过渡到 (A) 所在的位置,并且一个新元素 (C) 从右侧进入并过渡到 (B) 所在的位置。 Imagine it is like a Sliding Queue.想象一下它就像一个滑动队列。 Then it is rinse and repeat (B out -> C goes to where B was -> D transitions in to where C was).然后冲洗并重复(B out -> C 转到 B 所在的位置 -> D 过渡到 C 所在的位置)。 The problem is that using Collapse horizontal it wont flex the remaining space.问题是使用 Collapse Horizontal 它不会弯曲剩余空间。

Its a bit unclear what you're actually trying to achieve.它有点不清楚你实际上想要实现什么。

why use transition group?为什么要使用过渡组?

Usually a <TransistionGroup /> is used to set the in property on a <Collapse/> automatically.通常使用<TransistionGroup /> <Collapse/>in属性。 For example when adding a bunch of items in a list.例如,在列表中添加一堆项目时。

You would not need to have the <TransistionGroup /> if you just want to render 2 columns.如果您只想渲染 2 列,则不需要<TransistionGroup />
You can pass the in boolean manually.您可以手动传递in

Styling the collapse.造型倒塌。

you can pass styling to the collapse wrapper and set any css you need.您可以将样式传递给折叠包装器并设置您需要的任何 css。 IE: IE:

       <Collapse
          in={isOn}
          orientation="horizontal"
          style={{ flex: "1", display: "inline-block" }}
        >

Example例子

have a look at this Code sandbox看看这个代码沙箱

It has 2 examples:它有两个例子:

  • version 1 that just open/close the columns仅打开/关闭列的版本 1
  • version 2 that adds columns and uses transitiongroup to expand them版本 2 添加列并使用转换组来扩展它们

I think version 1 is what you're really looking for:我认为第 1 版是您真正想要的:

const Example1 = () => {
  const [isOn, setIsOn] = React.useState(false);

  const toggle = () => {
    setIsOn((prev) => !prev);
  };

  return (
    <>
      <Button variant="contained" onClick={toggle}>
        {isOn ? "collapse" : "expand"}
      </Button>

      <Box display="flex">
        <Collapse
          in={isOn}
          orientation="horizontal"
          style={{ flex: "1", display: "inline-block" }}
        >
          <Box>Content-A</Box>
        </Collapse>
        <Collapse
          in={isOn}
          orientation="horizontal"
          style={{ flex: "1", display: "inline-block" }}
        >
          <Box>Content-B</Box>
        </Collapse>
      </Box>
    </>
  );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM