Enhancing React Flow to provide updated selection and hover functionality

After diving into react flow, I found it to be quite user-friendly. However, I've hit a roadblock while attempting to update the styles of a selected node. My current workaround involves using useState to modify the style object for a specific Id. Is there a more efficient way to apply styling to selected/focused nodes using the style prop?

const nodeStyle = {
  padding: '0.7rem',
  height: '7rem',
  width: '12rem',
  borderColor: '#868DA0',
  borderRadius: '3px',
  '&:focus': {                 <----doesn't work
    backgroundColor: 'blue',
    color: 'blue',
    padding: '5rem',
    borderRadius: '7px',
  },
}

export default function Flow({ items, itemConnections }: FlowProps) {
  const nodes = items.map(({ id, x_position, y_position, name }) => ({
    id,
    data: {
      label: name
    },
    position: { x: x_position, y: y_position },
    style: nodeStyle,           <------- Styles passed in here
  }))
  return (
    <Box>
      {items.length === 0 ? (
        <EmptyFlowBox />
      ) : (
        <ReactFlow elements={[...nodes, ...itemConnections]}>
          <Controls />
        </ReactFlow>
      )}
    </Box>
  )
}

Answer №1

A custom style was developed specifically for application in React Flow.

const customReactFlowStyle = {
    background: "#e8ecef",
    "& .react-flow__node": {
      "&:hover": {
        boxShadow: "0 5px 5px rgba(0,0,0,0.25), 0 5px 5px rgba(0,0,0,0.25)",
      },
    },
  };

This style was then utilized on a grid component (using MUI).

            <Grid item xs={12} height="80vh" sx={customReactFlowStyle}>
              <ReactFlowProvider>
                <ReactFlow
                  elements={elements}
                  nodeTypes={nodeTypes}
                  onElementClick={handleElementClick}
                  onConnect={onConnect}
                  onElementsRemove={onElementsRemove}
                  connectionLineType={"smoothstep" as ConnectionLineType}
                />
              </ReactFlowProvider>
            </Grid>

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

Is there a way for me to move a user from one room to another room?

My friend and I both have our own rooms in a session. When I want to send him a message, I need to switch his room to the same one where I am. This is the code snippet for creating my own room with static sessions: socket.on('chat-in', function ...

Will modifying files in the node_modules directory trigger a restart of nodemon?

https://i.stack.imgur.com/3dJLt.png { "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node bin/server.js", "dev":"nodemon bin/server.js" }, "dependencies": { ...

Issue with Font Awesome 5 icons not displaying properly after integrating Bootstrap

Trying to spruce up my buttons by adding some fontawesome icons from bootstrap. The icons I'm using are: fas fa-user fas fa-users They display fine without any styling. However, when I apply the bootstrap button css, the "fa-users" icon appears as ...

"Enhance Your CSS Styling with an Additional Dot for Dotted Under

Is it possible to make the dots at the end and sometimes beginning of a word thicker and more round? https://i.stack.imgur.com/W9WPi.png ' CODE <span style="border-bottom: 3px dotted #111;">Personal Project Magazine Ads Personal Project Magaz ...

Buttons are not aligned with the rest of the row's height

I'm currently working on designing an upload layout that consists of a container with a row inside. Within the row, there are two buttons (each containing input elements) and one h3. For some reason, I am having difficulty getting them to align at the ...

Implement OAuth for user authentication and registration using a customized username feature on an express.js platform

Within my node.js+express.js stack, I have enabled users to register/login using both Twitter and Facebook, as well as through a native registration form. To manage this functionality, I am utilizing everyauth for handling Twitter and Facebook authenticati ...

Get the latest html content and save it as a .html file using javascript or jQuery

Looking for a way to save an HTML page as a .html file? Having some trouble with jQuery modifications not being included in the exported file? Check out the code snippet below and let me know if you can spot what's going wrong! I'm still getting ...

In configuring the print settings, I specified margins to ensure proper formatting. However, I noticed that the margin adjustment only applies to the first page. I need

I have a method that retrieves margin top value from the backend. It works perfectly on the first page of print, but on the second page, the margin top space is missing. initializePrintingSettings() { this.printService.fetchPrintSettings().subscribe(respon ...

callback triggering state change

This particular member function is responsible for populating a folder_structure object with fabricated data asynchronously: fake(folders_: number, progress_callback_: (progress_: number) => void = (progress_: number) => null): Promise<boolean ...

What is the best way to conduct snapshot testing on a component that loads asynchronous data in React using react-testing-library?

Currently, in the projects I'm involved in, my approach to snapshot-testing components that handle asynchronous data loading looks like this: describe('MyComponent component', () =>{ test('Matches snapshot', async () => { ...

Error: The specified property is not found in type 'never' - occurring with the ngFor loop variable

When working with API responses and dynamically sorting my view, I utilize an ngFor loop. Here's the snippet of code in question: <agm-marker *ngFor="let httpResponses of response" [latitude]= "httpResponses.lat" [longitude]=& ...

An issue with setting cookies in JWT when the server (localhost:3001/login) is not directly accessed in a MERN stack environment

Currently, I am in the process of creating a token for authentication. Situation 1: When I access my local server directly using localhost:3001/login, I am able to update/set cookies successfully. Below is a snippet of the code that I used: app.get(' ...

Utilizing $.getJSON to initiate a selection change event

I'm currently working on implementing a feature that involves adding categories to a dropdown list using jQuery Ajax. The goal is to load subcategories when a particular option is selected. However, I've encountered an issue where the addition o ...

Issues with Datepicker functionality in Bootstrap 5 are causing it to malfunction or not display

I am having trouble incorporating a timepicker on my webpage with bootstrap 5. The calendar feature isn't loading properly, preventing me from selecting any dates. I'm unsure if the issue lies with an error on my end or if the plugin isn't c ...

typescript: the modules with relational paths could not be located

As part of a migration process, I am currently converting code from JavaScript to TypeScript. In one of my files 'abc.ts', I need to import the 'xyz.css' file, which is located in the same directory. However, when I try to import it usi ...

Using JavaScript to develop a demonstration of the capabilities of Microsoft's Cognitive

Having trouble getting this basic example of the Microsoft Cognitive Services to work in JavaScript. Need some help or a working example, please! I've attempted to run the code in both node and browser with necessary modifications. Encountering an e ...

What is the method to exhibit the outcome of a promise on a web page within a React component?

In my search for information about promises, I have noticed that most articles provide examples using console.log. However, I am faced with a different scenario. I am working with AWS Athena and I need to display the result on a webpage in my React export. ...

Is there a way to adjust the padding temporarily?

Important Note: Despite the misleading title of my question, it doesn't accurately reflect my actual query (sort of). Below is the code snippet in question: .one{ background-color: gray; } .two{ padding: 20px; } <div class = "one"> < ...

Transmitting JSON information using post method through .ajax(), as well as receiving a JSON reply

Seeking assistance with debugging a registration page I am currently coding. I have hit a roadblock for the past 48 hours and would greatly appreciate any help in resolving the issues. CHALLENGE I am utilizing JavaScript to validate form inputs for error ...

Retrieve a file from a URL using Javascript's AJAX functionality

I have a CSV file uploaded to the server that I need to parse using JavaScript/jQuery. When trying to fetch the file with an AJAX call, I keep getting an error: XMLHttpRequest cannot load . No 'Access-Control-Allow-Origin' header is prese ...