Comments (7)
Make your own container 500px tall, then on click - height auto.
Wrap it with Collapse (always opened + css transitions as standard).
It will work! Collapse would just follow content height with animation.
from react-collapse.
Following what you said:
import { ReactNode, useCallback, useState } from 'react'
import { StyledMosaic, SeeMore, Wrapper } from './styles'
import { Collapse } from 'react-collapse'
interface MosaicProps {
children: ReactNode
limit: boolean
}
export default function Mosaic({ children, limit }: MosaicProps) {
const [isOpened, setIsOpened] = useState(false) // Handles Collapse
const [height, setHeight] = useState('500px') // Handles height changes
const handleClick = useCallback(() => {
setHeight('auto')
setIsOpened(!isOpened)
}, [isOpened])
return (
<Wrapper>
<Collapse isOpened={isOpened}> // Wraps div with 500px height initially
<StyledMosaic height={height}>{children}</StyledMosaic> // changes height from 500px to auto on click
</Collapse>
{limit ? (
<SeeMore onClick={handleClick}>
{isOpened ? 'Veja menos' : 'Veja mais'}
</SeeMore>
) : (
''
)}
</Wrapper>
)
}
I use Styled Components to do CSS:
import styled from 'styled-components'
export const Wrapper = styled.div`
position: relative;
.ReactCollapse--collapse {
transition: height 0.7s;
}
`
export const StyledMosaic = styled.div<{ height: string }>`
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
align-items: center;
justify-content: center;
grid-gap: 20px;
transition: max-height 0.3s;
height: ${p => p.height};
`
export const SeeMore = styled.div`
[ommited]
`
The result isn't what you described. It's like this collapsed:
And, when clicking the Veja mais
button, it opens properly, without issue.
from react-collapse.
from react-collapse.
Sure, should've tried with less variables.
Sadly, it still doesn't work or I'm still getting things wrong.
Below you can see the simplified code. No Styled Components or grid
.
It's a simple component. The Collapse
is opened by watching the state (it starts closed). It's height is initalized as 500
, so it should work as described.
Above the Collapse
component, a simple button handles click, that in time changes height
to auto
and toggles isOpened
.
I even disabled animation to see if it would work, and it doesn't.
export default function Mosaic({ children }) {
const [isOpened, setIsOpened] = useState(false)
const [height, setHeight] = useState<number | string>(500)
const handleClick = useCallback(() => {
setHeight('auto')
setIsOpened(!isOpened)
}, [isOpened])
return (
<div>
<button onClick={handleClick}>
{isOpened ? 'Veja menos' : 'Veja mais'}
</button>
<Collapse isOpened={isOpened}>
<div style={{ height: height }}>{children}</div>
</Collapse>
</div>
)
}
Collapsed:
Opened (image cropped for clarity):
I also tried without {children}
, with simple divs with text inside, and it still doesn't work.
from react-collapse.
you need to make sure collapse is always opened
<Collapse isOpened={true}>
<div style={{ height: height }}>{children}</div>
</Collapse>
from react-collapse.
GOT IT! Styled Components were messing with the updating height.
For reference, the working code is like this:
export default function Mosaic({ children }) {
const [height, setHeight] = useState<number | string>(500)
const handleClick = useCallback(() => {
setHeight(height === 500 ? 'auto' : 500)
}, [height])
return (
<Wrapper>
<Collapse isOpened={true}>
<div
style={{ height: height, overflow: 'hidden', position: 'relative' }}
>
<StyledMosaic>{children}</StyledMosaic>
</div>
</Collapse>
<SeeMore onClick={handleClick}>
{height === 500 ? 'Veja mais' : 'Veja menos'}
</SeeMore>
</Wrapper>
)
}
this div
that holds the StyledMosaic
is what does the magic.
OOF, @nkbt , thanks a lot for the help. I'm happily closing this issue <3
from react-collapse.
Awesome! Glad it worked out.
from react-collapse.
Related Issues (20)
- as-prop for Collapse HOT 1
- Fade in/fade out with opacity style on open/close HOT 1
- hasNestedCollapse no longer necessary in v5 yet is used in Nested example HOT 1
- React-collapse does not work on some zoom levels (devicePixelRatio =1.25) Following up #238 HOT 1
- On collapse, .ReactCollapse--collapse element is being completely removed from the DOM
- Animation not working when the Collapse has a parent that it is in modal or something with position fixed HOT 2
- Financing
- The overflow for 'React-collapse--' is overflow:hidden , not overflow:initial for IE11 HOT 2
- Animation doesn't work HOT 5
- Collapse for table row HOT 3
- initialStyle not working HOT 3
- Collapse horizontally aligned content HOT 3
- Prop passing suggestion HOT 8
- Block stays expenaded with isOpened=false HOT 4
- Nested collapsible shows animation. Help needed HOT 4
- Collapse/Open all on button click? HOT 1
- Position sticky inside collapse HOT 8
- Is the component still maintained + proposed feature additions HOT 1
- CSS modules in React app won't affect the rendered Collapse HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-collapse.