Creating dynamic text bubble to accommodate wrapped text in React using Material-UI (MUI)

I am currently developing a chat application using ReactJS/MUI, and I have encountered an issue with the sizing of the text bubbles. The bubble itself is implemented as a Typography component:

<Typography variant="body1" sx={bubbleStyle}>
    {text}
</Typography>

The styles for the bubble are defined as follows:

const bubbleStyle = {
    bgcolor: fromUser ? theme.palette.primary.main : '#f1f1f1',
    color: fromUser ? 'white' : 'black',
    p: 1,
    borderRadius: '8px',
    maxWidth: '65%',
    wordBreak: 'break-word',
    display: 'inline-block',
    my: 0,
    fontFamily: 'Inter, sans-serif',
    fontWeight: '100',
    fontSize: '14px',
};

Currently, when the text inside the bubble exceeds a single line, the width of the bubble extends to 65%, resulting in unnecessary white space on its right side. An example of this issue can be seen in the following image:

problem bubble

I have already tried several solutions to address this problem, including setting width='fit-content', using hidden <div> or <span> elements, employing a ResizeObserver, creating a nested MessageComponentText component, and dynamically measuring the text with JavaScript for resizing purposes - none of which have provided a satisfactory solution.

Although I have explored similar HTML/CSS-based approaches to resolve this issue, my limited knowledge of these technologies has hindered my ability to adapt those solutions to MUI.

Answer №1

From my understanding, the optimal solution seems to be using the CSS property word-break: break-all. This will help in breaking up long words to fit the content area and eliminate any unnecessary spaces. I've yet to come across a method that retains the word structure while eliminating additional spaces.

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

Leveraging the Power of SASS Variables Across Your Entire NativeScript Application

Currently, I am in the process of developing an app using NativeScript 6.4.1 along with Angular 8. In order to enhance my styling capabilities, I have decided to incorporate SASS due to its advantageous features like variable names. My designer has suppli ...

Detecting clicks outside of a component and updating its state using Typescript in SolidJS

Hi there, I am currently learning the SolidJS framework and encountering an issue. I am trying to change the state of an element using directives, but for some reason it is not working. Can anyone point out what I might be doing wrong? You can find the ful ...

Extract the content from the span element on Whatsapp Web

Currently, I am in the process of learning Python along with Selenium and have encountered a challenge. I am attempting to extract the username from the most recent message in a WhatsApp group conversation. Despite several attempts, I have been unsuccessfu ...

Is there a method to instruct crawlers to overlook specific sections of a document?

I understand that there are various methods to control the access of crawlers/spiders to documents such as robots.txt, meta tags, link attributes, etc. However, in my particular case, I am looking to exclude only a specific portion of a document. This por ...

Navigating through a multistep form in AngularJS using UI Router and arrow keys for seamless movement

Is there a way to navigate to the next or previous form step using arrow keys in AngularJS UI Router? The code provided below is currently allowing navigation with previous and next buttons. .config(function($stateProvider, $urlRouterProvider) { $stat ...

Using d3 to showcase pictures sourced from a csv file

Having recently embarked on a journey to learn javascript, d3, and the polymer project, I am facing a challenge that I hope to get some guidance on. After successfully parsing a csv file containing image information and creating an array specifically for ...

When floated to the right, the element is being pushed onto the next line in Firefox

Within a div, there are four elements. I specifically want the last element to be positioned on the far right, so I applied float: right to it. However, in Firefox, the last element gets pushed to the end of the next line instead. This issue does not occ ...

Having trouble getting background image transitions to work properly in CSS?

I'm encountering an issue with setting the transition CSS property for a hover effect, as it doesn't seem to be working correctly. Here is the relevant HTML: <div class = "menuItemBox" id = "Mughlai"> <p class = ...

The component fails to update even after changes are made to the Redux state

I currently have 4 buttons, each with a different color, and I want to display the last 10 button clicks as colors on 10 squares. The redux state is being used to map and display the square colors, which are also stored in localStorage. When a button is c ...

IntelliJ is indicating a typescript error related to react-bootstrap-table-next

Working with react-bootstrap-table-next (also known as react-bootstrap-table2) has been causing a Typescript error in my IntelliJ environment, specifically on the validator field within my column definition. Despite trying various solutions, such as adding ...

An error has occurred: Unable to access the property "filter" as it is undefined

After deploying my react-app online using npm run build, I encountered an issue where a page on my app displayed an error in Container.js. Despite being unfamiliar with this file and its purpose, I attempted to resolve the issue by reinstalling all node_mo ...

Text will not be moved to a new line

Hello, I am encountering an issue with my messaging system where the messages retrieved from the database are not properly going to a new line in the window. Despite adjusting the width, the messages continue to overlap. Below is the code snippet: This co ...

Viewing HTML web pages using Mozilla Firebox

Printing an HTML table with lots of content has been a challenge for me. Google Chrome didn't work, so I switched to Mozilla Firefox. However, now Firefox is breaking the page inside the table. My question is how can I trigger print preview in Firefox ...

What is the process for implementing filtered HTML attributes in a directive?

I’ve created a custom directive that generates a popup menu by extracting data from HTML content. The purpose is to transform a group of Bootstrap carousel-compatible elements into a structured list. However, each .item element contains an attribute with ...

How do I dynamically pass the color down to my style in Material UI?

I am facing a challenge with passing down a color dynamically in React and Material UI. I have been unable to find a suitable reference in my searches, including StackOverflow and the documentation. Exploration Material-ui hoverColor for MenuItem componen ...

I tried implementing Redux into my code, but it seems to be causing some issues. Can anyone help me troubleshoot and identify what might

Currently, I am in the process of validating input. My goal is to send the validation result of the input to another component using redux. I have set up redux and connected it, but unfortunately, I am receiving an undefined value instead of the expected v ...

Tips for displaying Next.js dynamic paths on a static S3/CloudFront website

Help Needed with Next.js Setup on S3 and CloudFront While setting up Next.js with static website hosting on S3 and CloudFront, I have encountered an issue with dynamic routes. My directory structure is as follows: pages/ index.js about.js [id].js A ...

Is there a way to disable a JavaScript hover effect when the mouse moves away?

I came across this JavaScript code that swaps the background of a parent div when clicking on a link in a child div. However, I noticed that the hover state remains even after moving the mouse out of the link. Can someone help me modify the code so that ...

Error Encountered with Next.js 13.4.1 when using styled-components Button in React Server-Side Rendering

I am currently working on a React project using Next.js version 13.4.1 and styled-components. One problem I'm facing is with a custom Button component that I've created: import React from 'react'; import styled from 'styled-compone ...

Divs Stacked and Centered

I am currently utilizing the card components from Google's Material Design Lite HTML theme, which can be found at . At the moment, my cards are positioned next to each other and aligned to the left side of the page. I am looking to center the cards o ...