Tips for aligning text with a progress bar

Is there a way to align my currentProgresstext with the progress bar? Currently, when I set progressWidth to 100, the text appears on the left side of the progress bar. Additionally, the progress bar's text disappears when progressWidth is small. How can I ensure that the text follows the progress bar?

Below is the code snippet:

import { Text, View, StyleSheet } from 'react-native';
import React, {useState} from 'react';
import { responsiveWidth } from 'react-native-responsive-dimensions';
import { Feather } from '@expo/vector-icons';

type Props = { containerStyle?: object };

const CartStatusOverlay: React.FC<Props> = (): JSX.Element => {
    const minTotalOrderQuantity = 18;
    const maxTotalOrderQuantity = 20;

    const width = responsiveWidth(90);

    // Max would be 90
    const [progressWidth, setProgressWidth] = useState(10);

    const convertKgToPercentage = (kg: number) => {
        return kg / maxTotalOrderQuantity * 100;
    }

    return (
        <View
            style={[
                styles.overlay,
                { width: width },
            ]} >
            <View style={styles.currentProgress}>
                <View style={[styles.content, {width: `${progressWidth}%`}]}>
                    {/*<Feather name="shopping-cart" size={24} color="white" />*/}
                    <Text style={styles.currentProgressText}>6 Kg</Text>
                </View>
...............
// Rest of the code remains the same.

This is how it currently looks with a progressWidth of 50:

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

This is what I aim to achieve:

https://i.sstatic.net/0k2YdjAC.png

Jsfiddle: https://jsfiddle.net/psahmad/fxpmnh15/

Answer №1

I made a simple adjustment by adding justify-content: flex-end to the .currentProgress class and removing width: 100% from the .content class.

.currentProgress {
  display: flex;
  flex-direction: row;
  height: 60px;
  border-radius: 24px;
  background-color: skyblue;
  align-items: center;
  width: 90%;
  /* progressWidth */
  justify-content: flex-end;
}

.content {
  padding: 0 20px;
}

Answer №2

I was able to solve the issue by making adjustments to the CSS for .content.

Previous CSS:

.content {
  padding: 0 20px;
  width: 100%;
}

Updated CSS:

.content {
  margin-left: auto;
  padding: 0 20px;
}

Check out this solution on jsFiddle!

Answer №3

After experimenting with your fiddle project, I was able to achieve the desired result by making adjustments to the following classes:

        .content {
            padding: 0 20px;
            width: 100%;
            display: flex;
            flex-direction: row;
            justify-content: flex-end
        }

        .currentProgressText {
            width: 100%;
            color: white;
            font-size: 16px;
            font-weight: bold;
            text-align: right;
        }

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

Answer №4

To ensure the proper alignment of the text with the progress bar, you'll need to adjust its position in relation to the width of the progress bar. It is recommended to always use absolute positioning for the text elements. I have already included the code in an expo snack for you to make any necessary modifications.

Expo Snack Link

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 eliminate a vertical line from an HTML table?

I am looking to remove specific vertical lines from an HTML table. There are only 3 vertical lines in total, and I want to remove the first and third lines. Below is my code: <html> <head> <style type="text/css"> .table1{ background: ...

Issues with Bootstrap Navigation Javascript Functionality

I'm having trouble with my JavaScript code that is supposed to open the second layer of my navigation. I can't figure out why it's not working. Am I missing something obvious? It's a bit warm in my office right now. ...

Converting time to hexadecimal format then reversing it as a byte array

I've been struggling since yesterday to figure out the reverse of a formula related to working with the HART (Highway Addressable Remote Transducer) Protocol. According to the specification, a constant-expression must resolve to an unsigned 4-byte or ...

What is the process of converting the timing from my stopwatch to a standard time format?

I am currently working on a stopwatch project where I log the time into an array. However, when I try to use Math.min(array) or Math.max(array), it returns NaN (not a number). The time format for the stopwatch is like 00:00:15.91 which is not recognized as ...

Having trouble importing XML data from an external file with jQuery

Attempting to import external XML with the code below is proving unsuccessful $( document ).load( "data.xml", function( response, status, xhr ) { console.log( xhr.status + " " + xhr.statusText ); }); Both data.xml and js files are in the ...

"Enhanced file manager: Elfinder with multiple buttons to seamlessly update text input fields

Every button is responsible for updating the respective element: <input type="text" id="field" name="image" value="<?php echo @$DuzenleSonuc[0]['image']; ?>" /> I need to ensure that each button updates the correct field: onclick ...

Attempting to retrieve the position of an image within an array upon clicking

function displayGalleryIndex(){ console.log($(this).index()); } $("body").on( "click", "#gallery img", displayGalleryIndex); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section class="grid- ...

Changing the names of object keys in JavaScript by utilizing a conversion object

Let's dive right in: I have a list of countries along with their respective values: { Mainland China: 14375, Japan: 20, Thailand: 19, Singapore: 18, South Korea: 15, Hong Kong: 14, Taiwan: 10, Germany: 8, Malaysia: 8, Macau: 7, France: 6, Vietnam: 6 ...

Applying CSS styles to position the input panel on the left side of the page

On my webpage, I have a layout that consists of a header, a footer, a left panel with inputs, and a right panel with outputs, all designed using bootstrap 4. The left panel is typically shorter than the right panel, and its height may vary in relation to ...

What is the best method for utilizing JSON to import Bureau of Labor Statistics information into Highcharts?

When it comes to integrating JSON files into charts, Highcharts offers some helpful examples. However, most of their samples feature rather straightforward JSON files, like this one: pretty simple JSON files. My goal is to take a JSFiddle example (http:// ...

Tips for incorporating multi-lingual email templates into your node.js application

I integrated node email templates into my project to automatically send emails to users based on certain events. Check out the Node Email Templates GitHub page for more information. For sending emails with Node.js, Nodemailer is a great tool. While I wa ...

Error: The JavaScript SRC cheat is malfunctioning

Having an issue with the code below. The 'dummy1' and 'dummy2' variables are not loading their content as expected on the page. Any suggestions? <html> <head> <title>JavaScript On-line Test</title> <script LA ...

Dividing the text by its position value and adding it to a fresh object

I needed to divide the paragraph into sections based on its entityRanges. Here is what the original paragraph looks like: { type: 'paragraph', depth: 1, text: 'Do you have questions or comments and do you wish to contact ABC? P ...

Using the Jquery .on(change) event on a <select> input will only alter the first row

I need help with a table that allows users to add rows. Within the table, there is a select input that triggers an ajax call to update values in another select field. The issue I'm facing is that when a new row is added, the .on(change) event affect ...

Ways to resolve the pg-promise error: "Promise library must be specified."

After successfully creating a simple API in ExpressJS with pg-promise to communicate with my PostgreSQL database on Windows, I encountered an issue when attempting to run it on Ubuntu 15.04. The following error message appears when starting the server: / ...

Exploring the power of Typescript and Map in Node.js applications

I am feeling a little perplexed about implementing Map in my nodejs project. In order to utilize Map, I need to change the compile target to ES6. However, doing so results in outputted js files that contain ES6 imports which causes issues with node. Is t ...

The Redis ReJSON consistently throws two errors at me: one for a missing key in a non-terminal path, and another for the requirement to create a

Understanding the second error is relatively simple, while the first error poses a bit more of a challenge. I have experimented with various combinations to address this error, but unfortunately, no progress has been made. When I check my console, I recei ...

Implementing dynamic toggling of the 'enableSorting' feature in the ui-grid component

I am facing an issue with enabling disabled sorting in ui-grid in Angular 1. I have attempted to do this in the grid columnsDefs: { ... enableSorting: false }, Furthermore, I have tried to override it when a certain event occurs in my controller: $ ...

Retrieve JSON data within a service and provide it to a component

I am currently facing an issue with loading data from a JSON file into my component using a service. The data is successfully loaded in the service, as confirmed by the console log; however, when trying to access the data in the component, it does not disp ...

Explore and choose JSON data for specific items depending on an array of criteria

I need to extract specific objects from a JSON file by matching them with values stored in an array. To illustrate, I have an array var list = ['test1', 'test2']; and there is a variable containing the following data: var data = [ ...