bokuweb / react-rnd

🖱 A resizable and draggable component for React.
https://bokuweb.github.io/react-rnd/stories
MIT License
3.94k stars 325 forks source link

Resizing the parent while resizing the Rnd #768

Open max-sym opened 3 years ago

max-sym commented 3 years ago

When resizing I need the parent to adjust its height as well. Currently, I'm trying the following and it doesn't work:

const Image = ({ element }) => {
  const [size, setSize] = useState({ width: 100, height: 100 })
  const [containerHeight, setContainerHeight] = useState(100)
  const containerRef = useRef()
  const initialValues = useRef({
    x: 0,
    y: 0,
    width: 100,
    height: 100,
  })

  const onResize = (e, direction, ref, delta, position) => {
    setContainerHeight(`${ref.offsetHeight + 100}px`)
    setSize({
      width: ref.offsetWidth,
      height: ref.offsetHeight,
    })
  }

  return (
    <div ref={containerRef} style={{ position: 'relative', width: '100%' }}>
      <div contentEditable={false} style={{ userSelect: 'none', height: containerHeight }}>
        <Rnd
          bounds="parent"
          default={initialValues}
          disableDragging
          dragGrid={[50, 50]}
          enableResizing={{ bottomRight: true }}
          lockAspectRatio
          onDragStart={onDragStart}
          onResize={onResize}
          onResizeStart={onResizeStart}
          resizeGrid={[50, 50]}
          size={size}
          style={{ position: 'relative', transform: 'none', display: 'block' }}
        >
          <img
            alt=""
            draggable="false"
            src={element.url}
            style={{ width: '100%', height: '100%', userSelect: 'none' }}
          />
        </Rnd>
      </div>
    </div>
  )
}

Is there any workaround for that? Thanks for this amazing library! 🎉🎉🎉

max-sym commented 3 years ago

Ok, it seems that I made it by removing the bounds="parent" prop and adding maxWidth="100%". Here's the code:

const Image = ({ element }) => {
  const [size, setSize] = useState({ width: 100, height: 100 })
  const containerRef = useRef()
  const initialValues = useRef({
    x: 0,
    y: 0,
    width: 100,
    height: 100,
  })

  const onResize = (e, direction, ref, delta, position) => {
    setSize({
      width: ref.offsetWidth,
      height: ref.offsetHeight,
    })
  }

  return (
    <div ref={containerRef} style={{ position: 'relative', width: '100%' }}>
      <div contentEditable={false} style={{ userSelect: 'none' }}>
        <Rnd
          default={initialValues}
          disableDragging
          dragGrid={[50, 50]}
          enableResizing={{ bottomRight: true }}
          lockAspectRatio
          maxWidth="100%"
          onDragStart={onDragStart}
          onResize={onResize}
          resizeGrid={[50, 50]}
          size={size}
          style={{ position: 'relative', transform: 'none', display: 'block' }}
        >
          <img
            alt=""
            draggable="false"
            src={element.url}
            style={{ width: '100%', height: '100%', userSelect: 'none' }}
          />
        </Rnd>
      </div>
    </div>
  )
}