Skip to content

I just want my Element that can only be moved to viewport,because my element is position of fixed。but I set bounds="body" and cannot work,is still can move in the direction of down while out of viewpoint  #714

Description

@hututuhu
<Draggable
    disabled={!isDraggable}
    handle=".br-modal__header"
    defaultClassNameDragged="br-modal__default-drag"
    positionOffset={unblock ? { x: '-50%', y: '-50%' } : undefined}
    cancel={resizable ? '.react-resizable-handle' : undefined}
    bounds="body"
  >
    {resizeElement}
</Draggable>

image

Activity

  1. changed the title [-]I just want my Element that can only be moved to viewport,because my element is position of fixed。but I set bounds="body" and cannot work,because is still can move in the direction of down while out of viewpoint [/-] [+]I just want my Element that can only be moved to viewport,because my element is position of fixed。but I set bounds="body" and cannot work,is still can move in the direction of down while out of viewpoint [/+] on Mar 21, 2023
  2. tinywaves commented on Apr 17, 2023

    @tinywaves

    Yes, I also encountered this problem. Do you have the solution now? Thank you. @hututuhu

  3. hardik79 commented on Aug 28, 2023

    @hardik79

    Hello @TinyRipple and @hututuhu ,
    I have a solution to your problem. Please add bounds={bounds} into the Draggable and ref={dragRef} into the main draggable div like
    Screenshot_1

  4. hardik79 commented on Aug 28, 2023

    @hardik79
  5. hututuhu commented on Sep 13, 2023

    @hututuhu
    Author

    Yes, I also encountered this problem. Do you have the solution now? Thank you. @hututuhu

    I solve this problem by using onStop and position.
    image

    image

  6. YuLingCheng commented on Feb 21, 2024

    @YuLingCheng

    Check https://github.com/hardik79/react-draggable/blob/main/src/App.js solution example for your problem.

    Setting the computed bounds works well, thanks for the tip 👍
    I think we can manage without a useEffect and rely on the onStart prop to recompute the bounds whenever the users want to start dragging. I just used this strategy to solve similar issue to limit the bounds of a draggable Mui modal.

    function App() {
      const [bounds, setBounds] = useState({});
      const contentRef = useRef(null);
    
      function updateBounds() {
        if (contentRef.current !== null) {
          setBounds({
            left: ..., 
            top: ...,
            right: ...,
            bottom: ..., // Whatever computation works in your case based on contentRef.current
          });
        }
    
      return (
        <Draggable bounds={bounds} onStart={updateBounds}>
          <YourComponent ref={contentRef} />
        </Draggable>
      );
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions