Percentage based control over text filling effect

I'm currently working on developing a text fill effect that will be controlled by a useState. Below is the text I am using:

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

I aim to have it filled from left to right based on a state, with the last letter of the initial text being in a different color, like this example:

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

The code below appears to be partially functional, but the synchronization of the fill and color differentiation is off:

'use client'

import React, { useState } from 'react'

const Intro = () => {
  const [percentage, setPercentage] = useState(0)

  // Function to generate gradient dynamically based on percentage
  const getGradient = (percentage) => {
    return `linear-gradient(to right, white ${percentage}%, transparent ${percentage}%)`
  }

  return (
    <div className="w-full h-[100vh] flex items-center justify-center">
      <div className="flex flex-col">
        <div>
          <span
            className="text-transparent italic text-[148px] text-stroke"
            style={{
              lineHeight: 0.8,
              backgroundImage: getGradient(percentage),
              WebkitBackgroundClip: 'text',
              backgroundClip: 'text',
            }}
          >
            PO
          </span>
          <span
            className="text-transparent italic pr-2 text-[148px] text-stroke"
            style={{
              lineHeight: 0.8,
              backgroundImage: getGradient(percentage),
              WebkitBackgroundClip: 'text',
              backgroundClip: 'text',
            }}
          >
            S
          </span>
        </div>
        <span
          className="text-transparent text-[30px] italic text-stroke-less"
          style={{
            lineHeight: 1,
            letterSpacing: 5.7,
            marginLeft: 6,
            backgroundImage: getGradient(percentage),
            WebkitBackgroundClip: 'text',
            backgroundClip: 'text',
          }}
        >
          PERFORMANCE
        </span>
      </div>
      <input
        type="range"
        min="0"
        max="100"
        value={percentage}
        onChange={(e) => setPercentage(e.target.value)}
        className="mt-4"
      />
    </div>
  )
}

export default Intro

Answer №1

Based on the details provided in your query, it appears that you are looking to synchronize POS with PERFORMANCE for a left-to-right gradient animation. To achieve this effect, you can try the following code snippet:

<div class="flex h-dvh flex-col items-center justify-center bg-black">
  <article class="relative">
    <!-- text stroke -->
    <section class="absolute inset-0 flex flex-col items-center px-10 text-transparent">
      <div class="text-[148px] italic"><span style="-webkit-text-stroke: 1px white">PO</span><span style="-webkit-text-stroke: 1px #3b82f6">S</span></div>
      <span class="text-[40px] italic" style="-webkit-text-stroke: 1px white">PERFORMANCE</span>
    </section>
    <!-- current fill pivot: 50%; 10% gradient effect -->
    <section class="flex flex-col items-center px-10 text-white" style="mask:linear-gradient(to right, black 50%, transparent calc( 50% + 10% ));">
      <div class="text-[148px] italic">PO<span class="text-blue-500">S</span></div>
      <span class="text-[40px] italic">PERFORMANCE</span>
    </section>
  </article>
</div>

To see a demonstration of this code in action, check out the static DEMO on Tailwind Play.

If you wish to create an animated effect by adjusting the percentage dynamically, you can use the following approach:

<div
  className="flex flex-col items-center px-10 text-white"
  style={{
    mask: `linear-gradient(to right, black ${percentage}%, transparent calc( ${percentage}% + 10% ))`,
  }}
>
  <!-- ... -->
</div>

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

Television Mount for Precise Video Placement

Seeking assistance for a unique positioning challenge I am facing. I have a Video that needs to be placed on the home page with a TV-like image "frame" around it, and they should scale together. https://i.sstatic.net/2pLhi.png What I've attempted i ...

Applying classes dynamically to a custom form component in Angular to tailor its appearance

I am currently working on a custom form component where I need to dynamically apply classes to it. import { Component, forwardRef, Injector } from '@angular/core'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms&apos ...

What is the best way to show an SVG icon in React without having to make an HTTP request for the file?

A special requirement for a react application is to display icons in certain parts of the application while offline. The use of inline svg is particularly fitting for this purpose. import React from 'react'; // Utilizing inline svg to showcase i ...

Firebase data not rendering correctly on React Bootstrap table

I am facing an issue while trying to display real-time data from my Firebase database in a React Bootstrap table. The problem is that the live data doesn't appear immediately after refreshing the page. Below is the code for my React component: impor ...

What is the best way to modify foundation's small, medium, and large breakpoint values?

I am currently customizing a Drupal theme child that incorporates the Foundation framework from its parent theme. The predefined breakpoints in the Foundation classes and attributes do not perfectly align with the design of my website. Can anyone provide ...

Using React to implement two MediaPipe models: A guide

I am facing an issue while trying to extract landmarks from two mediapipe models, FaceMesh and FaceDetection. Despite seeing console outputs for both console.log(results), I am only able to visualize landmarks from the model that is called second - which i ...

What is the best way to show the response messages I send in node.js on my frontend using ReactJS?

I'm in the process of creating a simple login/signup application using the MERN stack, which I am new to. As I work on the backend with Node.js and Express, I've included several JSON messages in the code like: return res.status(400).json({messa ...

JSFiddle Functioning Properly, But Documents Are Not Loading

My JSFiddle is functioning properly, but the files on my computer aren't. It seems like there might be an issue with how they are linking up or something that I may have overlooked. I've checked the console for errors, but nothing is popping up. ...

Strategies for Preventing Repetitive Coding of a Function

Apologies in advance for the title being a bit vague, I struggled to find the right words. Essentially, I have 10 buttons each with unique IDs. When these buttons are clicked, I want them to toggle the class of a textarea element. Is there a way to do thi ...

Tips for expanding an md-nav-item in Angular Material

Need help on stretching the md-nav-item element illustration 1 ------------------------------------------------ | ITEM1 | ITEM 2 | ------------------------------------------------ illustration 2 ------------------------------------------------ | ...

flexbox wrapping text styling

Can text be wrapped in flex within a fixed width container? For example: img username added you as a friend Instead of: user added img name you as a friend I am able to achieve this if the image and 'a' were separat ...

Struggling with creating adaptive cards in Bootstrap 4

After building a new website using Bootstrap 4, I encountered an issue with responsiveness. The layout consists of a fluid container with a Grid Row containing 8 columns, each filled with image cards. However, the cards do not display properly on different ...

Elements with absolute positioning are preventing drag events from executing

Struggling to create a slider and encountering an issue. The problem lies in absolute items blocking slider drag events. I need a solution that allows dragging the underlying image through absolute positioned items. Any ideas on how to achieve this? MANY T ...

The box shadow does not envelop the entire block uniformly

I have been struggling to evenly cover the shadows around all the blocks using CSS. Unfortunately, after working on it for 2 days, I still haven't been able to find a solution. The middle line seems to be misaligned and thicker compared to the others. ...

Tips for changing a specific color in a Material UI theme override for a React project

I am looking to customize the text color of all tabs in Material UI for React. While I can adjust some aspects using code like this: const theme = createMuiTheme({ overrides: { MuiTab: { root: { color: '#000000', back ...

Static placement reacting promptly

I am currently experiencing an issue where the content keeps scrolling to the right with the page when resized. I would like it to remain in the same position when resized. To help illustrate the problem, I have created a Codepen. It seems that the div con ...

Error: TypeScript React Google Maps: The object 'google' is not recognized

After successfully loading the Google Maps JavaScript API in the public/index.html, I can log window.google.maps without any issues in the dev tools console. https://i.sstatic.net/XqeG5.png However, TypeScript seems unaware of its presence, resulting in ...

Challenge with TypeScript typing (slightly connected to (P)React)

[Update: I've revised my initial query] Let's say I aim to specify UI components in the exact manner outlined below (the following lines are non-negotiable - any solution that alters these lines is not what I'm seeking): // Please refrain ...

To generate an npm package, you must convert several ES6 files into multiple ES5 files

I have developed various React components. Within my project folder, I have a collection of ES6 files including: components/A.js components/Button.js My goal is to import these components into my future projects in the following way: import A from &ap ...

Troubleshooting IE compatibility issues with jQuery .hide() method

I am encountering a curious issue with hiding span elements using CSS (display: none;). Upon page load, I expect the first span element to be displayed, which it does in all browsers except IE7. This anomaly has left me perplexed as there is no unusual cod ...