Creating an element in react-draggable with two sides bound and two sides open - here's how!

At the moment, I am facing an issue where the element is restricted from moving outside of the container with the class of card-width-height. My goal is to have the right and bottom sides bounded within the container while allowing the element to move beyond the top and left sides, but still be hidden. Can anyone provide assistance?

import React from "react";
import Draggable from "react-draggable";
import "../css/touch.css";

class Card extends React.Component {

  handleStart = (e, data) => {
    console.log(e, data);
  };

  handleStop = (e, data) => {
    console.log(e, data);
  };

  render() {
     return (  
      <div className="card-width-height">
        <Draggable
          axis="both"
          bounds="parent"
          defaultPosition={{ x: 180, y: 200 }}
          handle=".handle"
          onStart={this.handleStart}
          onStop={this.handleStop}
        >
          <div className="handle child-card" style={{ width: "50%" }}>
            <div>Screen 1</div>
          </div>
        </Draggable>
      </div>
    );
  }
}
export default Card;

Here is the CSS code:

.card-width-height {
  position: relative;
  max-width: 360px;
  min-height: 450px;
  background-color: #fff;
  border: 1px solid #000000;
  margin: 0 auto;
}

.child-card {
  height: 200px;
  font-weight: bold;
  background-color: yellow;
}

Answer №1

To set movement limits, use the bounds property.

Create an object with properties left, top, right, and bottom. These values determine the constraints for draggable movements.

Example:

bounds: {left: number, top: number, right: number, bottom: number}

To implement this, modify your code to something like

bounds= {{left: 0, top: -100, right:360, bottom:350}}
. Adjust the numbers based on your specific width and height parameters.

For a practical demonstration, check out how it works in action by visiting this link.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create a typescript class object

My journey with Typescript is just beginning as I delve into using it alongside Ionic. Coming from a background in Java, I'm finding the syntax and approach quite different and challenging. One area that's giving me trouble is creating new object ...

Can someone explain to me how this AngularJS factory example functions? I have some uncertainty

As a beginner in AngularJS, I am currently studying it through a tutorial and have some questions regarding the use of the factory in Angular. From what I understand, a factory is a pattern that creates objects on request. In the example provided, we see ...

Display the value of a JavaScript variable in a Codeception Acceptance test

Is there a way to view the value of a vanilla JavaScript variable while running an acceptance test? In PHP, you can see the value of a variable in debug using $I->seeMyVar($var), but how can you pass the value of a JavaScript variable to a PHP variable ...

Migrating WordPress Gutenberg to a Separate React Component: Troubleshooting Missing CSS Styles

I am in the process of developing a standalone version of the Gutenberg block editor from Wordpress that can function independently outside of the Wordpress environment. My goal is to integrate the Gutenberg editor as a React component within an existing R ...

Utilizing Next.js routing to accommodate two distinct subdomains on a single website

I am looking to develop a new platform using Next.js (React.js and React-router). The platform will have two distinct spaces - one for users and another for the owner to manage all users. To achieve this, I plan on splitting both areas into two subdomains: ...

Issues encountered with jQuery's $.ajax function when communicating with PHP

I am having trouble creating a simple app that displays data from a MySQL database using PHP and jQuery. The issue I am facing is with retrieving the data using jQuery. While my PHP script successfully returns the data without any problems, I am not receiv ...

Update elements dynamically using JSX

I have an array called data.info that gets updated over time, and my goal is to replace placeholder rendered elements with another set of elements. The default state of app.js is as follows: return ( <Fragment> {data.info.map((index) =&g ...

State of cart is currently empty, local storage fails to reflect product quantity changes, utilizing redux as a potential solution

I am experiencing an issue with my state where the cart appears empty. When I add a new product, the state's cartItem[] is empty and does not display previous items. Similarly, state.cartItems is also an empty array when retrieved from useSelector(). ...

Establishing a connection to a JSON API to retrieve and process

I am trying to extract all instances of names from this page: . For guidance, I have been using this tutorial as a reference: . However, when I run the code provided, it doesn't return anything. What could be going wrong? var request = new XMLHttpRe ...

Issue with Google Inter font not displaying properly in NextJS website

Currently, I am working on a project using NextJS. My goal is to implement the Inter font on my website, but despite setting it up and running 'npm run dev', the font appearing on the site is more reminiscent of Times New Roman rather than Inter. ...

Utilizing custom links for AJAX to showcase targeted pages: a beginner's guide

I am putting the final touches on my website and realized that it may be difficult to promote specific sections of the site because the browser address never changes. When visitors click on links, they open in a div using Ajax coding. However, if I want to ...

Having trouble understanding the differences between Bootstrap 4's .list-inline class and .list-inline-item class?

I am currently utilizing Bootstrap 4 within Wordpress. Strangely, I am facing an issue where I am unable to have list items appear inline (horizontally) solely by using the class .list-inline on the list itself, as shown below: <ul id="dances" class="l ...

Issues with the initial calculator project I built using JavaScript (excluding HTML and CSS)

My first calculator is nearly complete, but I have encountered a challenge. The functionality of my calculator is quite simple; it prompts the user for input using window.prompt and performs addition, subtraction, multiplication, or division based on the u ...

Can you confirm the mobile type, please? Using JavaScript to display a div only once based on the mobile type

Is there a correct way to determine the type of mobile device I'm using? Are there alternative methods to check for the mobile type? Take a look at my approach in the code below. How can I test this using a tool? Does anyone have insights on checki ...

The situation where a Javascript switch case continues to fall through to the default case even when a 'break' statement

I am currently setting up a node.js discord bot that utilizes firebase to save a user's status. My switch statement is functioning smoothly, handling each command effectively. The default case looks like this: default: message.reply("Unknown comm ...

Functionality of Sidebar Navigation malfunctioning with Next.js framework

I'm attempting to implement a sidebar using Next.js, but the code is currently in React format. I tried copying it and converting it to Next.js, but the links in the navigation aren't working. Additionally, I'm having trouble displaying the ...

Strategies for avoiding text wrapping in Bootstrap labels on Firefox

Within my panel and panel-body, there are multiple span elements that display dynamically. These spans behave perfectly in Safari and Chrome, wrapping to the next line when needed. However, in Firefox, they overflow strangely. Here are the comparisons: Ch ...

What is the reason behind the varying display of values?

When trying to set a value using the input tag, I encountered an issue. For example, if I type 1000.0009 into the input text, the valid value should be 1000.0001. However, the value displayed in the input tag is incorrect, while the value outside the tag i ...

What is the process for implementing a click event and accessing the DOM within an iframe using react-frame-component?

I am working on using the react-frame-component to create an iframe. I am trying to bind a click event on the iframe and retrieve the DOM element with the id of "abc" inside the iframe. Can anyone guide me on how to achieve this? The code snippet provided ...

Adjusting the transparency of each segment within a THREE.LineSegments object

I am following up on a question about passing a color array for segments to THREE.LineSegments, but I am looking for a solution that does not involve low-level shaders. I am not familiar with shaders at all, so I would prefer to avoid them if possible. I ...