Splitting the string into individual characters using string.split("").map may cause layout issues on small devices

Shoutout to @pratik-wadekar for the amazing help in creating a working text animation. However, I encountered an issue when testing it on various screen sizes and mobile devices - the animated word plants breaks into pieces like PLA on one line and NTS on the next. How can I prevent this from happening and keep the word intact?

I initially attempted to use

\xC2\xA0 – non-breaking space or  
before and after the word, but this did not solve the problem. The CSS word-wrap property allows long words to break, but there isn't an option to make a word unbreakable in the reverse case.

It seems that the CSS word-break: "keep-all property would be ideal, however, even with its application, the word still gets split on smaller screens.

Check out the Codepen link

And here's the code snippet from App.tsx:

import React from "react";
import { styled } from "@mui/material/styles";
import { Typography } from "@mui/material";

const AnimatedTypography = styled(Typography)`
 & {
   position: relative;
   -webkit-box-reflect: below -20px linear-gradient(transparent, rgba(0, 0, 0, 0.2));
   font-size: 60px;
 }

 & span {
   color: #fbbf2c;
   font-family: "Alfa Slab One", sans-serif;
   position: relative;
   display: inline-block;
   text-transform: uppercase;
   animation: waviy 1s infinite;
   animation-delay: calc(0.1s * var(--i));
 }

 @keyframes waviy {
   0%,
   40%,
   100% {
     transform: translateY(0);
   }
   20% {
     transform: translateY(-20px);
   }
 }
`;

interface Styles extends React.CSSProperties {
 "--i": number;
}

function App() {
 const string = "plants";
 return (
   <Typography variant={"h3"} fontWeight={"bold"}>
     All Your
     <AnimatedTypography>
       {string.split("").map((char, idx) => {
         const styles: Styles = {
           "--i": idx + 1
         };
         return (
           <span key={`${char}-${idx}`} style={styles}>
             {char}
           </span>
         );
       })}
     </AnimatedTypography>
     in One Place
   </Typography>
 );
}

export default App;



Answer №1

Each and every character is enclosed within its own <span> tag, preventing it from being treated as a single word. One easy fix would be to convert the parent into a flex container:

<AnimatedText style={{display:"flex"}}>

https://codesandbox.io/s/blissful-sea-4o7498

Answer №2

Aha! I figured it out. In order to make the animation work correctly, I had to create a separate component for it and include <AnimatedTypography> with the component={"span"} attribute along with white-space: nowrap. In addition to defining my animated typography component with

const AnimatedTypography = styled(Typography)
, I also had to specify as typeof Typography to prevent Typescript errors. Check out this link for more information: Dealing with issues related to the component prop

After importing the component and incorporating it within the Typography element in my text, I was able to maintain the layout of my text while ensuring that the animation doesn't break apart into individual characters.

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

Implementing conditional rendering using custom division return functions with onClick attribute on a submit button in React: A step-by-step guide

import React from "react"; class Input extends React.Component { constructor() { super(); this.state = { phone: "", weight: "", height: "", gender: "", smoke: "", lazy: "", bmi: "", pain: "", ...

I'm having trouble getting the Tanstack/react-query library to function properly within my Vercel application

Exploring the packaging and troubleshooting process... "@tanstack/react-query": "^5.14.2", "@tanstack/react-query-devtools": "^5.14.2", "next": "^13.5.6", "react": "18.2.0", & ...

What causes a standard React component with a default render prop to not pass PropTypes validation successfully?

I'm currently working on a React component with a render-prop that has a generic type. To improve usability, I want to set a default value for the render-prop. The code is functioning correctly, but during type-checking, I encountered a warning regard ...

Creating a json object using data from an HTML table

I am dealing with HTML returned from a web service that I cannot control. The HTML structure returned by the web service looks like this: <table id="result"> <tbody> <tr> <td><b>Name</b></td> < ...

Split up Python Flask files into individual components

After researching extensively on stack overflow, I find myself facing a challenging issue that I am unsure how to resolve. The content of my main.py file is as follows: from flask import Flask, jsonify, request from flask_pymongo import PyMongo from bso ...

When hosted, OpenCart encounters a JavaScript error stating that the property "document" cannot be read because it is null

After successfully running opencart on my local machine, I encountered some errors upon uploading it to the hosting/server. The specific error message is as follows: Uncaught TypeError: Cannot read property 'document' of null f.each.contents @ j ...

Error: Unable to locate module '@material-ui/core/AppBar'

UPDATE: After further investigation, I have found that none of the modules installed using npm install --save can be resolved. It appears that the issue is affecting all modules, even those within the node_modules folder. I have spent the past 2 hours tro ...

Customize Color of Bootstrap Tooltips

Is there a way to customize the appearance of BootStrap tooltips by changing the color and aligning the text to the left? I've tried various CSS modifications from different posts, but none seem to work. Any idea on how to alter the CSS of the tooltip ...

How can I utilize Hooks to effectively filter arrays?

Currently, I am working on transitioning from a class-based function to a stateless function. As part of this process, I am refactoring my code by updating each event handler that uses this.SetState to use useState instead (in this case setMovies). Below ...

Tips on sorting through an array using Angular 11 and data

I'm working on a subpage that contains a large amount of detailed data in the form of thousands of records. I want to filter this data based on the "id" from my route, which is also included in the dataset. However, I've run into some difficultie ...

A sleek CSS text link for a stylish video carousel

I am attempting to create a CSS-only text link to video slider within our Umbraco CMS. Due to the limitations of TinyMCE WYSIWYG, I am restricted in the amount of code I can use as it will strip out most of it. So far, I have developed a basic CSS slider ...

Trying to determine the specific key of an object based on its value in TypeScript?

Looking to create a function that can retrieve the key for a given value. type Items<T> = Exclude<{ [P in keyof T]: [P, T[P]]; }[keyof T], undefined>[]; export const getKeyName = <T extends Record<PropertyKey, unknown>, V>( col ...

What would be the best way to rewrite this text utilizing React Hooks?

How would you convert this to use React Hooks? const UsersList = ({ users, fetchUsers }) => { useEffect(() => { fetchUsers(); }, []); const renderUsers = () => { return ( <ul> {users.map(user => ( & ...

Retrieving text from a draggable div using jQuery

I have a draggable div that I can move over another element with the class .outerDiv which contains text content. Is there a way for me to retrieve the text from .outerDiv that overlaps with the draggable div? $(".outerDiv .isStore").draggable({ contain ...

Explaining how to use the describe function in TypeScript for mapping class constructors

I am working on a function that will return a JavaScript Object with the class instances as values and the class names as keys. For example: const init = (constructorMap) => { return Object.keys(constructorMap).reduce((ret, constructorName) => ...

What are the steps for initializing a session in Vue.js with Django upon a successful login?

Upon successful login, I want to redirect to a page indicating success and also include a session. How can this be achieved? I am using HTML with Vue.js for the front end and Django for the back end. Below is my Vue.js script for the login: <script> ...

Why are some of my JSX files missing from the bundled file?

Currently, I am in the process of integrating reactJS into my existing asp.net application. My goal is to keep the node_modules folder separate from my solution. Since I am new to node.js and npm, I may be making unconventional choices here. The objectiv ...

Conditionally displaying an input model in Vue.js using v-if

Just starting to learn vue.js and I'm looking to display table data. My idea is that when the table is in display mode, it should only show the data. However, when I click on the edit button of a table row, I want that specific row to switch to edit m ...

Determine whether a component is linked to an event listener

If a <Form> component is called with a @cancel event listener attached to it, the cancel button that triggers this event should be visible. If no @cancel event is present, the cancel button should not be displayed. Is there a method to verify if a c ...

Guide on serving static HTML files using vanilla JavaScript and incorporating submodules

Is it possible to serve a static html file with elements defined in a javascript file using imports from submodules using vanilla JS? Or do I need bundling tools and/or other frameworks for this task? Here's an example code structure to showcase what ...