Is your Chakra UI canvas in React displaying blank with zero width and height?

Here is the link to the codesandbox

And here is the code snippet:

import "./styles.css";
import {
  Flex,
  Tab,
  TabList,
  TabPanel,
  TabPanels,
  Tabs
} from "@chakra-ui/react";
import { SketchField, Tools } from "react-sketch";

export default function App() {
  return (
    <Flex height="100vh" width="100vw">
      <Tabs width="100%" display="flex" flexDirection="column" defaultIndex={0}>
        <TabList>
          {["Play", "Draw"].map((val) => (
            <Tab key={Math.random()}>{val}</Tab>
          ))}
        </TabList>
        <TabPanels flex="1 1 auto">
          <TabPanel padding={0} height="100%">
            <h1>Hello world </h1>
          </TabPanel>
          <TabPanel padding={0} height="100%">
            <Flex height="100%" width="100%">
              <SketchField
                width="100%"
                height="100%"
                widthCorrection={0}
                tool={Tools.Pencil}
                lineColor="#3182CE"
                lineWidth={3}
              />
            </Flex>
          </TabPanel>
        </TabPanels>
      </Tabs>
    </Flex>
  );
}

Currently facing an issue where the canvas element in the second tab does not render properly. It seems that its width and height are both zero. I have tried using different canvas components like react-canvas-draw, but the same issue persists. This could be a problem with how Chakra UI tabs interact with React rendering.

Any suggestions on fixing this? Ideally, I want the canvas component to expand to take up the "remaining height and width." I've used '100%' for width and height as I don't know the exact dimensions available for the canvas component.

Note: Interestingly, changing the default index of the tabs to 1 (making the second tab initially active) allows the canvas component to work correctly. The reason behind this behavior is still unclear.

Answer №1

The purpose of wrapping the element with <Flex> at line 26 is not clear to me, but if you remove that it should work.

Update: Apologies, I misunderstood the sandbox refresh mechanism and mistakenly thought the solution above would work.

It seems like the html canvas is not keeping pace with the chakra UI tabs rendering. Consider adding the isLazy prop to the Chakra <Tabs> component.

Reference:

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 best way to sort items in an array in React based on their length being below a specific number?

I am attempting to utilize the filter() method in order to filter out items from an array if the length of the array is below a certain amount, within the context of ReactJS. Unfortunately, I have not yet succeeded in achieving this. Sample Code: class T ...

Adjust the size of an image instead of formatting text to fit around it when the browser window is resized

Is there a way to make an image scale down instead of having text wrap around it when the browser window is resized to be more narrow? Currently, the text wraps under the image before the image scales due to its max-width property. The desired outcome is ...

how can JavaScript be used to retrieve an object based on a condition from an array of objects and an ArrayList

My JavaScript challenge involves working with an array of objects called arrobj and a list called prgList. The goal is to extract the names from arrobj based on the programs listed in prgList. If the program exists in arrobj, it should be displayed accor ...

Tips for maintaining the immutability of an array containing objects in JavaScript

In my program, I am working on creating a new array based on two arrays named "ideaList" and "endorsements", which are declared globally. To ensure the immutability of ideaList and endorsements since they are used in other parts of the codebase, I have att ...

Providing Arguments to a Named Function Using Dependency Injection

I am currently working on an Angular app where I am passing a named function into a controller. The issue arises when I try to inject a provider into that controller for use, as I receive a TypeError: object is not a function in the console. My question i ...

Display all keys and values in a dynamically populated object on my screen - React

I have a dynamic object with nested objects, and I want to display every key and value. Even if there are objects within the main object, I need to show their keys and values as well. Here is an example of the object: info:{ address:{ city: {__o ...

The functionality to generate forms on the click of a button appears to be malfunctioning

I am currently attempting to dynamically create Bootstrap Panels using an `onclick` function. However, I want each Panel to generate multiple forms when the button is clicked. In my current code, the first Panel successfully creates forms when the button i ...

What is the best way to declare multiple types that require specific props?

Trying to implement the following type: type DataTypes = | Link | Event | People | Article | Department | PageSearch | OfficeSearch | CatalogSearch | DocumentSearch | KnowledgeSearch; When implemented this way, it functions correctly: ...

What is the best way to incorporate a translate, rotate, and scale tool into my threejs environment?

Recently, I experimented with the THREE.js editor tool. When you click on an object, a set of tools for translation, rotation, and scaling appear in the scene. These tools allow you to modify the object's position, rotation, and size. I attempted to u ...

Utilizing jQuery to incorporate a radio input function in a POST request

When the Index.php page button is pressed, data is sent to the Resultx.php script page, which then responds with an asynchronous call back on the same Index.php page. index.php <script> $(document).ready(function() { $("#input_form").subm ...

Serve static files using ExpressJS when a post request is made

My Express server is set up to serve static files for my website and it's working fine: var express = require('express'); var app = express(); var path = require('path'); var p = path.join(__dirname, '../web/public'); app ...

The MDL layout spacer is pushing the content to the following line

Here's an interesting approach to Material Design Lite. In this example from the MDL site, notice how the 'mdl-layout-spacer' class positions elements to the right of the containing div, giving a clean layout: Check it out <!-- Event ca ...

Personalize headers in v-data-table while preserving default sorting capabilities

I'm looking to enhance my v-data-table by making the table headers "tab-able". To achieve this, I decided to create a slot and include tabindex on the columns. However, I encountered an issue where the sorting functionality stopped working. Does an ...

Data from the list item is lost when switching to another component in React

I recently started working with React and I'm in the process of developing a basic app using React and Firebase. Within my parent class Homepage.js, I have included another component called HomePageRecipe.js like this: <HomePageRecipe changeToRec ...

Tips for extracting data from a table's options:

Can someone help me with this HTML table issue? In columns col6 and col7, I have select elements that store different options. What I'm trying to do is retrieve all the data from the table based on the options selected in columns col6 and col7. Howev ...

"Efficient Querying with MYSQL XDEVAPI Through Multiple OR (II) Requests

Currently, I am utilizing the XDEVAPI and attempting to incorporate the or(||) statement in JavaScript. However, it appears to malfunction after 1 or/(||) statements have been included. I need to fetch data from the database based on 5 distinct statuses: . ...

ReactJS: Retrieving JSON file from the local server instead of the project folder

Here is the directory structure of my project: https://i.sstatic.net/JJcLx.png In my index.js file, I have the following code for loading the website.json file: index.js componentDidMount() { $.ajax({ url: "../website.json", type: "GE ...

Challenges with AJAX in Web Browsing

I'm encountering some strange issues with AJAX functionality not working consistently across different web browsers. Is there a specific requirement or workaround to ensure smooth operation across all browsers? The initial problem I am facing is that ...

CSS3 :target and display:none inconsistency problem in Internet Explorer and Firefox

Creating a one-page site with navigation tabs that toggle between hidden divs using :target CSS3 selector is my current project. To ensure the first div is visible upon page load, I implemented this line of code: document.location.href = "#div1"; The HTM ...

What is the best way to incorporate the "child_process" node module into an Angular application?

Trying to execute a shell script in my Angular application has been quite the challenge. I came across the "child process" node library that might be able to help me with this task. However, every attempt to import the library led me to the error message: ...