Adding multiple text as label and sublabel in a React MUI Tooltip can be achieved by utilizing the appropriate

I'm struggling to incorporate a MaterialUI Tooltip into my React application with two separate text lines. The first line should serve as the main title, while the second line functions as a sublabel.

In this screenshot provided, you can see where I am facing some difficulties:

https://i.sstatic.net/vHVUA.png

The objective is to have all the content contained within one card structured as follows:

The label should display: Self-assessment opened: [n]

Sub-label: Click to drill down

Attached is an example from Figma illustrating how the card should visually appear:

https://i.sstatic.net/AyZkv.png

As someone who is new to MUI, I am unsure about how to approach this. Below is the code snippet that I have managed to put together thus far:

import { Box, Popper, Tooltip } from '@mui/material';
import { styled } from '@mui/material/styles';
import { useTheme } from '@mui/styles';
import { useIntl } from 'react-intl';

// Utilize styles from src/analytics/components/TooltipCard/TooltipCard.js for consistent styling
const TooltipCardPopper = styled(Popper)(({ theme }) => ({
  '& > div': {
    ...theme.typography.caption,
    backgroundColor: 'white',
    boxShadow: theme.shadows[7],
    borderRadius: '5px',
    paddingLeft: theme.spacing(1.2),
    paddingRight: theme.spacing(1.2),
    paddingTop: theme.spacing(0.5),
    paddingBottom: theme.spacing(0.5),
    borderWidth: 1,
    borderStyle: 'solid',
    borderColor: theme.palette.grey[300],
  },
}));

const calculateTotals = ({ data }) =>
  data?.reduce(function (accumulator, item) {
    return accumulator + item.total;
  }, 0);

const CenteredMetricToolTip = ({ position, data }) => {
  const theme = useTheme();
  const intl = useIntl();
  const show = !!position;

  const total = calculateTotals({ data });

  return (
    <Tooltip
      open={show}
      disableFocusListener
      disableHoverListener
      disableTouchListener
      disableInteractive
      title={intl.formatMessage({ defaultMessage: 'Click to drill down' })}
      PopperComponent={TooltipCardPopper}
      TransitionProps={{ timeout: 0 }} // timeout more than 0 => transition => causes re-positioning and blinking
    >
      <Box
        sx={{
          position: 'absolute',
          display: show ? 'block' : 'hide',
          left: `${position?.x ?? 0}px`,
          top: `${position?.y ?? 0}px`,
        }}
      >
        {intl.formatMessage(
          { defaultMessage: ' Self-assessment opened: {total}' },
          { total },
        )}
      </Box>
    </Tooltip>
  );
};

export default CenteredMetricToolTip;

Answer №1

Make a modification to the title attribute by changing the value from

 title={intl.formatMessage({ defaultMessage: 'Click to drill down' })}
to

 title={
       <div>
         <div>{intl.formatMessage({ defaultMessage:"Self-assessment opened: [n]" })}<div>
         <div>{intl.formatMessage({ defaultMessage:"Click to drill down"})}</div>
       <div> 
 }

for the Tooltip Component

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

Creating dynamic components in Vue.js using VueJS and jQuery synergistically

New to Vue.js and in the process of building a Vue component inspired by this custom select menu. I want to include an ionicon with each list item. Typically, I can add the icon in Vue.js using: <component class="icon" :is="name-of-icon& ...

What is causing this to function properly on Firefox but not on Chrome or IE?

After clicking the process_2banner button on my html page, a piece of code is executed. Surprisingly, this code performs as expected in Firefox, but not in Chrome or Internet Explorer. In those browsers, although the ajax code is triggered, the div spinner ...

What is the process for configuring the injector in my application?

https://code.angularjs.org/1.0.0rc9/angular-1.0.0rc9.js I am facing an issue with the above link as it defines an external js file that I am not familiar with the injector to angular-1.0.0rc9.js. Due to this, my app is not running in the browser. Here is ...

"Exploring the Differences between JavaScript, AJAX, and Servlet for String

I am attempting to compare a string that is received from a servlet. The servlet page returns the following: out.println("pass"); In JavaScript: function Check() { if (ajax.responseText === "pass") { document.getElementById("pass").innerHTML = "This is ...

Invoking AJAX function post readystatechange

Currently, I am in the process of making an Ajax call to a server and attempting to invoke another function once the response is ready (readystatechanged). As of now, there isn't any serverside code implemented. Surprisingly, Chrome and Firefox encoun ...

Ways to structure this updateone query for mongoose formatting

UPDATE: After making adjustments to the query using arrayFilters recommended by someone here, the query is returning success. However, the values in the database are not being updated. I am attempting to update specific fields within a MongoDB collection ...

Having trouble with PHP not receiving data when using a $.ajax POST request?

I'm facing an issue where my code seems to be correctly passing a JavaScript variable to PHP. On the JavaScript side, everything works fine - I receive success messages and see the data in the network tab. However, on the PHP side, I am unable to retr ...

Executing a function upon clicking a Card in a MERN Stack application

I am facing an issue where I want to trigger a function when the user clicks on a Card component in my project, which is built using react-bootstrap. Despite trying to add an onclick function, it does not seem to work as expected. clientCard.js import Rea ...

Formik state is mysteriously reverting field values to their default state

I've encountered an issue with my form and song state while trying to add a new field called "appleMusicId". Unfortunately, every time I add this field, it seems to reset the values of timeDescription and sceneDescription. I've spent hours tryin ...

Incorporate various Vue.js components into the main parent component

I am currently utilizing Vue.js to create a user interface for my HTML5 game. I have a scenario where I want to define UI containers that essentially group other UI components and position them on the screen. Here's an example of what I'd like to ...

Adjust the position of vertices when the mouse hovers over them

I have a PlaneGeometry and I am trying to adjust the z position of the vertex being hovered over, but I am uncertain about how to retrieve it. //THREE.WebGLRenderer 69 // Creating plane var geometryPlane = new THREE.PlaneGeometry( 100, 100, 20, 10 ); ...

Encountering a 'Not Found' error while deploying my React Node application with an AWS-hosted database on Heroku

const express = require("express"); const app = express(); const bodyParser = require("body-parser"); const port = process.env.PORT || 3002; const cors = require("cors"); const path=require("path"); const routing = ...

Utilizing React for incorporating HTML5 canvas gradients

I'm currently working on an app that allows users to generate a background gradient with two distinct colors using React. The issue I'm facing is that while the first color appears correctly, the second color ends up looking more like a solid col ...

What is the best way to transmit extra data when tunneling a TLS connection?

I have developed a basic HTTP proxy that utilizes the HTTP CONNECT method for HTTP tunneling. const http = require('http'); const https = require('https'); const pem = require('pem'); const net = require('net'); con ...

Tips for effectively crafting a component capable of managing both a value and an observable for that specific value

I'm actually curious about two things. When is it appropriate to pass an observable of an object into a component versus resolving it with the | async method? If I want to create a versatile reusable component that can handle both scenarios - accept ...

The issue with the NextJS Layout component is that it is failing to display the page content, with an error message indicating that objects cannot be used

I am embarking on my first project using Next JS and after watching several tutorial videos, I find the Layout component to be a perfect fit for my project! However, I am encountering an issue where the page content does not display. The Menu and Footer a ...

Error in React: Unable to access the 'id' property of an undefined object

I have a product.js file where I am trying to retrieve data by id using the URL http://localhost:3000/product/2 (/product/:id). However, I'm encountering a TypeError: Cannot read property 'id' of undefined in React. I simply want to retrieve ...

What causes the failure of hot reloading in the ReactJS Visual Studio 2019 template?

I have been experimenting with ReactJS on my personal and work computers. On my personal computer, I am using Visual Studio 2019, NodeJS 10.16.2, NPM 6.9.0, and webpack 3.11.0. Initially, hot reloading was functioning correctly when I started my project. ...

Resource loading unsuccessful: server returned a 401 status code for React webpage

( ) I'm currently working on a website dedicated to searching for GitHub users as part of my React course. However, I seem to be facing an issue with the website not fetching the response properly, possibly due to an Axios error. When searching for ...

Issue encountered when AngularJS struggles to evaluate regular expression within ng-pattern

Currently, I am implementing the ng-pattern argument in an input text field to restrict input to only numeric values: <input type="text" ng-model="numericField" ng-pattern="/^[0-9]*$/" /> However, there seems to be an unusual behavior in the regex ...