Code Monkey home page Code Monkey logo

Comments (7)

nkbt avatar nkbt commented on August 16, 2024

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.

angelod1as avatar angelod1as commented on August 16, 2024

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:

image

And, when clicking the Veja mais button, it opens properly, without issue.

from react-collapse.

nkbt avatar nkbt commented on August 16, 2024

from react-collapse.

angelod1as avatar angelod1as commented on August 16, 2024

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:

image

Opened (image cropped for clarity):

image

I also tried without {children}, with simple divs with text inside, and it still doesn't work.

from react-collapse.

nkbt avatar nkbt commented on August 16, 2024

you need to make sure collapse is always opened

      <Collapse isOpened={true}>
        <div style={{ height: height }}>{children}</div>
      </Collapse>

from react-collapse.

angelod1as avatar angelod1as commented on August 16, 2024

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

image

from react-collapse.

nkbt avatar nkbt commented on August 16, 2024

Awesome! Glad it worked out.

from react-collapse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.