Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default cropsize is {width: 0, height: 0} #267

Closed
nickisyourfan opened this issue Jun 16, 2021 · 10 comments
Closed

Default cropsize is {width: 0, height: 0} #267

nickisyourfan opened this issue Jun 16, 2021 · 10 comments

Comments

@nickisyourfan
Copy link

Describe the bug
Hello! I think maybe I found a bug? I have been messing with this for a few hours and am stumped... The first time I used this inside a modal it worked great! This time, I am using it inside a modal with a collapse and am getting a default cropSize of {width: 0, height: 0} -- I have checked the docs a ton and read through the issues but can't seem to find one that matches what I am experiencing.

Either way - I am loving the package and hope that this is just me missing something crazy obvious - I have tried to set the default crop size manually - but that makes it so the image does not get centered by default.

I have attached a quick video to show my problem... and some code below.

Funny part is, if I resize the window, it recalculates the cropSize and it works.

  <CCollapse show={showCropper} className="my-2">
      <CContainer
        className="position-relative"
        style={{ height: "60vh", width: "75%", position: "relative" }}
      >
        <Cropper
          image={inputImg}
          crop={crop}
          zoom={zoom}
          aspect={aspect}
          onCropChange={onCropChange}
          onCropComplete={onCropComplete}
          onZoomChange={onZoomChange}
        />
      </CContainer>
      <CButton
        color="success"
        className="w-75"
        style={{ borderTopRightRadius: 0, borderTopLeftRadius: 0 }}
      >
        Save New Version
      </CButton>
    </CCollapse>

I have also forced this not to load unless the image is loaded, without luck.

Thanks for the help!

Quick Video of Problem:
https://user-images.githubusercontent.com/62756900/122150245-f59f4d80-ce22-11eb-935f-e12c75b1fc46.mp4

@ValentinH
Copy link
Owner

This is a classic issue of showing an element that needs to know it's side in a modal: you should only instantiate it when the modal is open.

You can add a condition to only render the cropper once the modal is open.

@nickisyourfan
Copy link
Author

Thanks so much for your response! Sorry that it might have been a dumb question! Condition added and it works great - I wish you the best of luck with the future of this package!

@ValentinH
Copy link
Owner

There is no dumb question! Thanks for the kind words.

@AndreiTS
Copy link

Hi, I'm having the same problem, how do I wait for the modal to show?

@ValentinH
Copy link
Owner

It depends on what modal you use. If you provide an example on CodeSandbox, I can help you 😉

@AndreiTS
Copy link

@ValentinH
Copy link
Owner

Here's a fixed version: https://codesandbox.io/s/basic-antd-4-17-3-forked-wxf4b?file=/index.js

There were a few things to do:

  • set the bodyStyle to have position: 'relative'
  • disable opening animation (otherwise, the container size is not correct at the moment where the Cropper is rendered)
  • only render the Cropper element when isModalVisible is shown

@AndreiTS
Copy link

Thank you

@YuJian920
Copy link

I happened to have this problem, thanks a lot!

@ValentinH
Copy link
Owner

I'm actually planning to try using a ResizeObserver to automatically reset the internal cropper state when the parent component size changes. This should fix the cases where the cropper is inside the Modal and remove the need to do custom logic from developers using it. 🤞

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

No branches or pull requests

4 participants