Although react-moveable and css modules work well together, implementing CSS styling can be challenging

I'm currently utilizing react-moveable to incorporate drag and drop functionality for a specific component. My goal is to hide the control box until the user interacts with the target.

Initially, I attempted to apply custom CSS to the control box, but it seems to be ineffective. Despite the documentation indicating that a className can be assigned, it's not working as expected for me.

Below is a simplified version of the component structure:


import React from "react";
import Moveable from "react-moveable";

import { makeMoveable, DraggableProps, ResizableProps, RotatableProps, Rotatable, Draggable, Resizable } from "react-moveable";
import MoveableHelper from "moveable-helper";
import styles from './label.module.css';

const Label = () => {
  const [helper] = React.useState(() => {
    return new MoveableHelper();
})
const targetRef = React.useRef<HTMLDivElement>(null);
  return (
    <div className="container">
     <div>
        name
      </div>
      <Moveable
        className={styles.moveable1}
        target={targetRef}
        draggable={true}
        resizable={true}
        rotatable={true}
        origin={false}
        onDragStart={helper.onDragStart}
        onDrag={helper.onDrag}
        onResizeStart={helper.onResizeStart}
        onResize={helper.onResize}
        onRotateStart={helper.onRotateStart}
        onRotate={helper.onRotate}
      />
    </div>
  );
};

export default Label;

Here is the content of the CSS module file label.module.css:

.moveable1 .moveable-line .moveable-direction  {
    background: red !important;

}

Despite my attempts to specify the className as a string and make adjustments to the class names in the CSS files, the appearance of the control box remains unchanged. Any insights or suggestions?

Any thoughts on how to tackle this issue?

Answer №1

  1. The control box is determined by the zoom and renderDirection settings.

We have the option to hide it by setting zoom to 0 (default is 1).

zoom={0}

We can also customize or hide the border controls using renderDirections. (Default settings provided)

 renderDirections={["nw", "n", "ne", "w", "e", "sw", "s", "se"]}

Furthermore, you can add styles to the target div using a normal onClick function along with the useState hook.

import React from "react";
import Moveable from "react-moveable";

import { makeMoveable, DraggableProps, ResizableProps, RotatableProps, Rotatable, Draggable, Resizable } from "react-moveable";
import MoveableHelper from "moveable-helper";
import styles from './label.module.css';

const Label = () => {
  const [helper] = React.useState(() => {
    return new MoveableHelper();
})
const targetRef = React.useRef<HTMLDivElement>(null);

const [customStyles, setCustomStyles] = useState( {border: "1px solid green";} ) 

const handleStyles = e => { 
// Your style logic... 
// Update the state using setState
setCustomStyles({border: "1px solid blue"})

}
  return (
    <div className="container">
     <div onclick={handleStyles} style={customStyles}>
        name
      </div>
      <Moveable
        className={styles.moveable1}
        target={targetRef}
        draggable={true}
        resizable={true}
        rotatable={true}
        origin={false}
        onDragStart={helper.onDragStart}
        onDrag={helper.onDrag}
        onResizeStart={helper.onResizeStart}
        onResize={helper.onResize}
        onRotateStart={helper.onRotateStart}
        onRotate={helper.onRotate}

        { /** Hide the control box * /  }
        zoom={0}
        renderDirections={["nw", "n", "ne"]}
      />
    </div>
  );
};

export default Label;

Additional Resources:

Zoom Types:

Render Directions:

CSS style Box: https://github.com/daybrush/moveable/blob/master/handbook/handbook.md#toc-directions

👀 style={{ background: 'yellow' }}

Hope this information proves helpful. Best regards, M. 💀

Answer №2

For confirmation, have you attempted using the .moveable-control css class name like this:

.moveable-control {
    background: red !important;
}

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

Ways to center Bootstrap v4 modal pop-ups vertically

How to properly center Bootstrap 4 modal dialogues vertically? Please Note: I am seeking a precise method to vertically center a Bootstrap modal that covers all scenarios, on all devices, and in all browsers. I needed this for a large single-page applicat ...

Having trouble aligning page in the center with flexbox and styled components

Hey there, I'm having some trouble figuring out how to center my page using flexbox within styled components in a Next.js app. Any suggestions or tips? Let me share with you my Blog Component import ReactMarkdown from 'react-markdown' impor ...

Creating manageable CSS styles for a wide variety of themes

I need to add themes to a web application that allows users to switch between them seamlessly. The designers want a variety of font colors and background colors, around 20 in total. Is there a way to achieve this without creating multiple .css files, which ...

In a Next JS project, the CSS Transition fails to activate when a class is dynamically added

In my Next.js project with Tailwind styling, I encountered an issue when using the globals.css file for more complex non-Tailwind CSS styling on a component. To better understand the problem, you can view the Code Sandbox here: https://codesandbox.io/p/sa ...

The correct way to write media queries in CSS for the iPhone

After successfully formatting a website for various devices using an online responsive design tester and CSS declarations like @media only screen and (max-width: 480px), I encountered an issue during testing on an actual iPhone. The browser was not computi ...

Employ the useEffect hook to make a constant post request

I have encountered a perplexing issue while working on a basic To-Do list web application that utilizes a MongoDB database. Despite having the functionality in place, I noticed that my useEffect hook is continuously sending an excessive number of post requ ...

Prevent event propagation when clicking on an Autocomplete material UI component within an accordion section

I am facing an issue with adding an Autocomplete component inside an AccordionSummary. Whenever I click on it, the accordion expands and enters a focus state. I have tried to stop the propagation of events, but unfortunately, it has not been successful. ...

Parallax scrolling in all directions

Is there a resource available for learning how to program a website similar to the one at ? I am familiar with parallax but can't seem to find any examples that resemble what they have done on that site. ...

How can I ensure the text remains centered within a div, even when there are buttons aligned to the left or

I have a CSS class called .text-align-center that I used to center text. .text-align-center { text-align:center; } However, I noticed that when there is a left or right arrow icon within the <div>, the text does not appear perfectly centered. Is ...

Styling a div element in CSS so that it adjusts its size to fit its

How can I make a div element adjust to its content and also break the line so that the next element appears below it? I attempted setting the display property to inline-block, but then the div no longer breaks the line... Normally, it breaks the line, but ...

What could be causing this massive error during the npm install process in this particular repository?

I am trying to implement the react-file-viewer library, but I am encountering issues running it. After running npm install, I am faced with a multitude of errors that I am unfamiliar with since I am relatively new to this. My current Node version is v16.9 ...

Ways to conceal CSS on the page when triggering a different element

I am trying to achieve the functionality of hiding the black arrow when clicking on the green arrow, all without using jQuery. Here is my fiddle: http://jsfiddle.net/t5Nf8/195/ html: <div class="arrow-down"></div> <div class="arrow-up"> ...

Encountering a duplicate key error with MaterialUI React components

During the creation of a navigation bar, I incorporated multiple collapse components from MUI to generate menu items. Although it's not a critical error in the code, it is quite annoying. The console displays the following warning: Warning: Encounter ...

Incorporating external CSS stylesheets into a custom LESS framework

Is there a reliable method for integrating a third-party CSS file obtained from an external source into my own LESS compile structure without making many changes to it? I anticipate that the external CSS file may be updated periodically, so I would prefer ...

What causes Next.js to struggle with recognizing TypeScript code in .tsx and .ts files?

Webpages lacking a declared interface load correctly https://i.stack.imgur.com/DJZhy.png https://i.stack.imgur.com/r1XhE.png https://i.stack.imgur.com/zXLqz.png https://i.stack.imgur.com/Z1P3o.png ...

Encountering errors while working with React props in typing

In my application, I am utilizing ANT Design and React with 2 components in the mix: //PARENT const Test = () => { const [state, setState] = useState([]); function onChange( pagination: TablePaginationConfig, filters: Record<string, ...

When the screen is tapped, the background color transforms into a different shade

Is there a way to change the background color of a specific div when touching it? I have tried using the following code: .x-list-item:active { background: #870000 !important; /* fallback for old browsers */ background: -webkit-linear-gradient(to left, # ...

next-redux-wrapper is being invoked repeatedly and experiencing multiple calls to HYDRATE

I'm embarking on a new Next.js project, transitioning from a standard React app to a Next.js application. Our plan is to utilize Redux Toolkit for global state management and incorporate server-side rendering. During this process, we discovered the ne ...

Can you show me how to achieve a smooth background color transition for a div that takes 2 seconds to complete when it is inserted into the DOM?

On my webpage, I have a list of items that are constantly being updated via a WebSocket connection to the backend. Each time a new item is added to the list, I would like it to be displayed with a background color transition lasting only a brief moment. ...

Establish a background image for a particular item within a list

Employing Ionic 2 with Typescript. I am seeking a way to empower users to choose the background color for each item in my list. ISSUE: How can I retrieve the reference to the i-th element? Whenever I select an item, it only changes the background of the ...