react-rnd icon indicating copy to clipboard operation
react-rnd copied to clipboard

Initial position of Rnd component from bottom and right side

Open iamtekson opened this issue 4 years ago • 2 comments

Overview of the problem

I'm using react-rnd latest version and I am looking for settting the initial postion of the Rnd component from right and bottom direction. As explained in the documentation, the position of Rnd is always from left (x) and top (y).

My browser is: Chrome

Reproduced project

https://codesandbox.io/s/y3997qply9

Description

Is there any way to set the position of the Rnd component from the right and bottom direction?

I tried to set the custom style in the Rnd component as below,

const rndStyle = {
      zIndex: "1000",
      left: "auto",
      right: "10px",
      bottom: "10px",
      top: "auto",
    };

<Rnd
    style={style}
    default={{
      x: 0,
      y: 0,
      width: 320,
      height: 200
    }}
  >
    Rnd
  </Rnd>

When I inspect the element, the component css was updated according to my rndStyle, but it doesn't update the actual position. Any help?

iamtekson avatar Apr 29 '21 03:04 iamtekson

This is 100% not the way to do it but it works at the moment. Simply wrap your <Rnd/> in a div with the offsets of your initial height and width like this:

 <div style={{ position: "absolute", bottom: `${initialPosition.height}px`, right: `${initialPosition.width}px` }}>
      <Rnd>
          ...
      </Rnd>
</div>

I am using redux so to me it looks something like this:

const EXTRA_BOTTOM_OFFSET = 8
<div
      style={{
        position: "absolute",
        bottom: `${INITIAL_UI_STATE.notes.notesWindowDimensions.height + EXTRA_BOTTOM_OFFSET}px`,
        right: `${INITIAL_UI_STATE.notes.notesWindowDimensions.width}px`,
      }}
    >
      <Rnd>
          ...

wmonecke avatar Jul 16 '21 12:07 wmonecke

@wmonecke 's solution didn't work for me. My <Rnd> - wrapped component was no longer visible.

What did work is setting the default.x prop to:

document.getElementById('MyWindow')?.getBoundingClientRect().width; 

e.g.

        <Rnd
            default={{
                x: document.getElementById('MyWindow')?.getBoundingClientRect().width - 350 ,

mpelzsherman avatar Aug 09 '23 20:08 mpelzsherman