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

Transferring information from an online platform onto pre-arranged sheets for hard copy

Has anyone had success printing from a website? I have some papers that are already printed with checkboxes. These checkboxes need to be filled in based on information entered through a web form or retrieved from a MySQL database. All of this information ...

What is the process for retrieving data on the server side using node.js that has been sent from the frontend via the post method

After diving into learning node.js with the express framework, I encountered a roadblock. I'm experimenting with a basic query search webapp and trying to send some data (a boolean named all) from front-end plain javascript via ajax to the server sid ...

Utilizing LessCss for color transparency and variable declarations with @vars

I'm attempting to create a function using LessCss, but I am encountering an issue: .transparent-border (@alpha:.15, @color:'0,0,0', @type: solid, @size:1px) { @val: @size @type rgba(@color, @alpha); border: @val; } The problem er ...

React-bootstrap's Modal is glitching and only partially appearing on the screen

I am a beginner in frontend development and I've been struggling to position the modal at the center of the screen; it keeps appearing on the right side. I am currently using "bootstrap/dist/css/bootstrap.min.css" for my CSS. Should I create a new CSS ...

Encountering an error with Next.js installation and useContext when no files have been modified since the initial installation

After freshly installing either npx create-next-app@latest or npx create-next-app@latest --typescript, I complete the installation and then attempt to run next dev, only to encounter a useContext error. Despite not making any changes to the files and tryin ...

Displaying Material UI textboxes in multiple columns for dynamically created forms using ReactJs from an array

I am working with an API to retrieve a list of cars, and I have come up with the following code snippet to generate labels and textboxes for editing the car codes. {!carQuery.loading && carDefinitions?.map((car: ICarType, idx: number ...

AngularJS Firebase Login Scope Value Not Retained After Refresh

I have stored my unique username in the variable "$scope" and I am trying to access it from different views once the user logs in, using: However, when I refresh the page immediately after the user successfully signs in, the value of "$scope" goes bac ...

Are there any creative methods for structuring Node.js Alexa code efficiently?

I'm looking for a more organized approach to managing my Node.js Alexa code. With the increasing number of intents in my interaction model, the lines of code in my lambda's index.js have become difficult to manage. Can someone provide an example ...

Managing stream pauses and resumes in Node.js using setTimeout()

After some experimentation with Node.js (v4.4.7), I managed to create a simple program to play a sound... const Speaker = require('audio-speaker/stream'); const Generator = require('audio-generator/stream'); const speaker = new Speake ...

How can I utilize a filter or pipe to populate product categories onto screens within Ionic 2?

I am considering creating an Ionic 2 app with 6 pages, but I'm unsure whether to utilize a Pipe or a Filter for the individual category pages and how to implement the necessary code. Each category page should be able to display products from the "app ...

An HTML button generated by a .js file is able to execute a .js function without any issues, but it inadvertently removes all .css styling

Currently grappling with an issue in my JavaScript self-teaching journey that I can't seem to resolve or find examples for. The problem at hand: When the HTML button calls a .js function, it successfully executes the function but also wipes out all C ...

Utilizing Javascript Regular Expressions to extract specified parameters from URLs with specific patterns

I am facing a specific issue with pure vanilla javascript. My task is to extract the values of A, B & C from various URL patterns, excluding any instances where the value is "XX". A, B, and C are static words that can appear in different positions wit ...

Create a new Chart.js Chart by using the data retrieved from an AJAX request and encoded in JSON

I am currently working on drawing a chart using the chart.js library. The initial draw works perfectly fine, but I am facing issues when trying to redraw the doughnut chart with new data retrieved from an ajax call. My approach involves PHP and Codeignite ...

Encountering a node module issue when implementing graphql in a TypeScript project

I encountered issues when attempting to utilize the @types/graphql package alongside TypeScript Node Starter node_modules//subscription/subscribe.d.ts(17,4): error TS2314: Generic type AsyncIterator<T, E>' requires 2 type argument(s). node_modu ...

What methods can I use to create a navigation bar similar to this design?

Lately, I've noticed a growing trend in navigation bars that are designed to resemble waves. Wavey Navbar I'm eager to incorporate a similar navbar into my website, but I'm struggling to figure out how to do it. I'm working with react ...

serving the frontend and backend from separate ports

Greetings! I currently have my frontend (create-react-app) and backend server (express.js) running on separate ports but on the same host. Specifically, my frontend is located at 127.0.0.1:3000 while the backend is at 127.0.0.1:3003. In my package.json fi ...

A guide on utilizing AngularJS to extract data from a webpage

I'm attempting to transfer the information from a specific page on my website and paste it into a file. I know how to post a sample text stored in a variable from Angular and save it in a file in the backend using Node Express, so writing a file isn&a ...

Failure to detect the Event Object upon submission of the Form featuring Radio Buttons

I am currently working on a react component that contains a form with radio buttons. I've been attempting to implement onChange() and handleSubmit() functions to capture the value of the selected button and display it using console.log(). However, I&a ...

What is the best way to incorporate Ajax into a Rails application?

Check out the Webpage Design I am currently working on a Todo List that includes various Todo Items. Each incomplete task has a button called "Mark Item as Done" next to it, which triggers the button_to method when clicked. I am facing challenges in imple ...

The align-items property in Flexbox is not functioning as expected when applied to Link components contained within

I'm in the process of creating a simple navigation bar using flexbox, but I'm encountering an issue where the content within the unordered list is not vertically centered. Below is the HTML code snippet: *,*::before,*::after{ margin: 0; ...