Adjusting the size of a React element containing an SVG (d3)

In my simple react app, I have the following component structure:

<div id='app'>
 <Navbar />
 <Graph />
 <Footer />
</div>

The Navbar has a fixed height of 80px.

The Footer has a fixed height of 40px.

The Graph is a d3-wrapper div that contains an SVG with graph elements.

  1. How can I make sure the Graph always occupies the remaining height of the screen?

  2. What is the best way to update the react component containing the d3-wrapper/SVG on a resize event?

Note: To ensure the graph is responsive, it is important not to hardcode the width and height of the SVG element.

Here is a codepen snippet where I attempted to address this issue.

Answer №1

After some exploration, I have discovered a solution to this issue. The most efficient approach is to delegate the majority of the work to CSS:

.d3-wrapper {
  width: 100%;
  height: calc(100vh - #{$footerHeight + $navbarHeight}); 
}

svg {
 width: 100%;
 height: 100%;
}

In the d3-wrapper component, if necessary, establish a reference to the div and send clientWidth and clientHeight to the svg component as props.

To handle resizing updates, include a resize listener and ensure the svg component can update itself.

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

What is the optimal approach for managing multiple languages using React Router version 5?

I am exploring the possibility of incorporating multiple languages into my website using React and React Router v5. Can you provide guidance on the most effective approach to achieve this? Below is a snippet of the current routing code I am working with: ...

Discover the steps for utilizing Codemod with Next.js. Avoid selecting an invalid transform option

I recently attempted to use a codemod on my pages (specifically inside index.js). npx @next/codemod pages npx: installed 440 in 15.252s Invalid transform choice, must choose one of the following: - name-default-component - withamp-to-config - url-to-with ...

Is it possible to modify a website's CSS permanently using just the browser?

Is there a way to make changes to CSS in a large project using Firefox's built-in developer tools and have them saved permanently? I've been trying to edit the style sheets, but couldn't locate the specific property I needed. Since I can&apo ...

Space between an image and a div element

While creating a website in Dreamweaver CC, I noticed a significant gap between my image and a div tag. Here is a screenshot for reference: Below is the code from my index file: <!doctype html> <html> ... </div> Additionally, here is a ...

Unique hover tags such as those provided by Thinglink

Looking for a way to replicate the functionality of Thinglink? Imagine a dot on an image that, when hovered over, displays a text box - that's what I'm trying to achieve. I thought about using tooltips in Bootstrap, but I'm not sure if it ...

``I'm having trouble with TablePagination styling on a material-table component. Anyone have

I am trying to customize the appearance of rows numbered from-to of x on a Material-Table, using the following code: import MaterialTable from 'material-table' import { TablePagination, withStyles } from '@material-ui/core' const Style ...

Can divs be arranged in a similar fashion as images?

My goal is to create a navigation bar (#nav) with each nav item being a div (#nav div). However, my current approach isn't working as expected. Here is the code I'm using: #nav { background: url("navbg.png"); /* navbg.png is 1x40 pixels */ ...

Tips for displaying a tooltip when hovering over a label in a Material UI slider

I'm currently working on a slider quiz and my goal is to have the tooltip appear when hovering over the label on the slider. Currently, I can only see the tooltip when I hover directly on the thumb at the location of my mouse. Refer to the image belo ...

Activate CSS modules in ReactJS by incorporating a .module extension to the CSS file

Being new to ReactJS, I recently learned about enabling CSS modules and discovered the following: If you append .module to a class name, it will generate a base64 hash string for that class As an example, I created a function-based component called Lay ...

Approaching fashion from a new angle: incorporating perspective into

How can I style elements in the perspective to appear straight(3d) instead of flat due to perspective? I want to achieve a design where the alphabets and flag post image align perfectly. .container { perspective: 100px; perspective-origin: bott ...

Problem aligning ul within div element

I'm having trouble aligning my ul element in the center of a div. I tried using the margin: 0 auto method, but it's not working as expected. I've always wondered if setting the width of the ul within a div is necessary for using margin: 0 au ...

Double rendering of dynamic parameters upon subsequent hard navigation

Initially, I was utilizing the parameters from the URL to retrieve data from a REST API using Axios. During debugging, I noticed an unusual occurrence where the params were being logged twice, with one of the logs returning null. I suspected there was an i ...

Unlocking the API endpoints within nested arrays in a React project

{ [ "quicktype_id": 1, "quicktype": "Laptops", "qucik_details": [ { "type_name": "car", "type_img": "car_img" }, { "type_name": "van", "type_img": ...

I'm having trouble getting a CSS transition to work on a PNG sequence using jQuery in Mozilla

For my latest project, I decided to create an animation using a PNG sprite sequence. The idea was to add a smooth transition effect on hover and have the animation play in reverse when the hover state ends. If you want to take a look at the code yourself, ...

Rendering components before ComponentDidMount runs and Axios handles state updates

When I try to pass the gifs state to my GifList component in the render method, I encounter an issue. It seems that when trying to access the array within the component through props, it is returning as undefined. After investigating further, I noticed t ...

Guide to setting up the Next Image component in Storybook

While attempting to follow this tutorial, I encountered an issue where the CatCard component using the NextJs Image component could not be loaded. import Image from 'next/image'; import styles from './CatCard.module.css'; export interf ...

The message from create-react-app states, "To use Create React App, you must have Node version 14 or higher installed." However, when attempting to update Node, an error message pops up saying, "npm does not work with

I encountered an issue while attempting to develop a react app, and received the following message: $ npx create-react-app react-demo npx: installed 67 in 6.045s You are running Node 10.19.0. Create React App requires Node 14 or higher. Please update your ...

What adjustments can be made to alter the height of the borders on the left and right side of the block quote, and link the border to a different div element

Block Quote Example I'm looking to create a blockquote that resembles the image provided. I have successfully implemented the top and bottom gradients, but am facing challenges with the left and right borders as well as rounding the quotations more. ...

Display a universal loading screen on all React.js pages when making calls from various locations

As I was working on adding a global loading screen for data fetched from an API, I came across this helpful answer and decided to implement something similar. LoadingProvider.js import { createContext, useContext, useState } from "react"; const ...

The component 'Form' cannot be utilized in JSX because its return type, 'ReactNode', is not a valid JSX element

I'm facing an issue with my Next.js application written in TypeScript after updating react-bootstrap. After the update, I am encountering the following error when attempting to use the Form component from react-bootstrap: react-bootstrap: ^2.10.3 @typ ...