Is it achievable to create an inline block+text layout in React Native?

In my React Native project, I am aiming to replicate a layout that I successfully implemented in HTML+CSS on the web:

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

The challenging part is getting the gray breadcrumb element to align horizontally with the description. Here is the code snippet I have for the breadcrumb and description section:

 <Text style={styles.description}>
    <Text style={styles.quantity}>{quantity}</Text>
    {description}
 </Text>

styles.ts:

 description: {
    fontSize: 14,
    fontFamily: 'light',
    flexDirection: 'row',
    flexWrap: 'wrap',
  },
  quantity: {
    fontSize: 13,
    color: Color.white,
    backgroundColor: Color.placeholder,
    borderRadius: 16,
    paddingHorizontal: 400,
  },

However, this implementation leads to the following outcome:

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

Unfortunately, the borderRadius and padding properties seem to be disregarded for the quantity element.

I also experimented with using View elements instead of Text, but the description text consistently wrapped onto a new line when it exceeded the container width (displaying typical block behavior).

Is there any clever workaround or method to achieve this desired layout in React Native? Your insights would be greatly appreciated. Thank you.

Answer №1

Enclose the inner text within a container with a row flex direction, and proceed to wrap the text component in another container (demo):

import * as React from 'react';
import { View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import { Text } from 'react-native-paper';
const Color = {
  white: 'white',
  placeholder: 'blue',
};
const quantity = 50;
const description =
  'There was once a tale so long that it took ages to recount.';

const InnerTextComponent = ({ style, children, ...props }) => {
  style = StyleSheet.flatten(style)
  const textStyle = {
    ...style,
    // // remove styles that are not compatible with android/ios

    paddingHorizontal: null,
    marginHorizontal: null,
    borderRadius: null
  }
  return (
    <View style={[{alignItems:'center',justifyContent:'center'},style]}>
      <Text  {...props} style={textStyle}>{children}</Text>
    </View>
  );
};

export default function App() {
  return (
    <View style={styles.container}>
      <View style={{ flexDirection: 'row' }}>
        <Text style={styles.description}>
          {/* The InnerTextComponent no longer inherits parent text styles*/}
          <InnerTextComponent style={[styles.description,styles.quantity]}>{quantity}</InnerTextComponent>
          {description}
        </Text>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  description: {
    fontSize: 14,
    fontFamily: 'light',
    flexDirection: 'row',
    flexWrap: 'wrap',
  },
  quantity: {
    fontSize: 13,
    color: Color.white,
    backgroundColor: Color.placeholder,
    borderRadius: 16,
    paddingHorizontal: 5,
    marginHorizontal: 3,
  },
});

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

Populate the div with an image that stretches to the full height

I'm facing an issue with my website's front page design, where a div is divided into two sections using the Twitter Bootstrap grid system. When the second grid (span9) is taller than the first (span3), the image perfectly fills up the span9 area ...

Is there a way to disable auto rotation on a website when accessed from a mobile phone?

My current solution involves the following code: @media (max-height: 480px) and (min-width: 480px) and (max-width: 600px) { html{ -webkit-transform: rotate(-90deg); -moz-transform: rotate(-90deg); -ms-transform: rotate(- ...

Trouble with buttons in table cells

There is a problem with a button placed in a table cell that spans two rows and does not fill the second row. How can I ensure that this button fills both rows? I have attempted to adjust the height in the button's style as well as in the table cell i ...

Continuing the Grid

I'm working on a solution where the grid should be filled with empty squares when it's empty. And as we add items to the grid, the number of empty squares should decrease accordingly, not the other way around. <span class="box"> ...

Enhancing Your Website with WordPress Customizer API: Introducing Kirki's Repeater Image Feature

Allow me to clarify this situation as thoroughly as I can. To begin with, I am utilizing a toolkit known as Kirki to enhance the WordPress Customizer API Within my theme, there is a section that features a 4-column layout of services, each accompanied by ...

Unable to connect to subview in Interface Builder

Just a quick question: I am using IB and have created a subview within a ViewController. Inside this subview, there is a label that I want to link to my custom subview class. However, for some reason, IB is not allowing me to do so. Can anyone help me unde ...

How can I change the color of the fa-star symbol from its default yellow to white?

When I set color:white; or use the classes icon-light or icon-white, all font-awesome-icons turn white except for fa-star which stubbornly remains yellow. I've tried various methods to override the CSS, including using !important in inline-styling on ...

HTML Scripts: Enhancing Web Functionality

I have another question regarding executing a script (docs()) upon clicking on text. I attempted to use the following code: <p><span class="skill">Documentation can be found here.<script>docs()</script></span></p> Unfo ...

Tips on adjusting the label on a datetime-local button

While using mobile browsers, I noticed that the datetime local feature includes three buttons. One of these buttons is called the Set button, but I would like to change its name to Ok. Is there a way to do this? I tried looking for solutions but couldn&apo ...

PrimeNG ToolTip fails to appear

I am experiencing issues with getting tooltips to display properly on my webpage. When I hover over the tooltip trigger, the screen seems to expand with a vertical scrollbar appearing, but no tooltip is shown. It appears as though the tooltip might be tryi ...

Tips on eliminating the excess whitespace remaining at the bottom of the webpage when using JavaScript parallax scrolling

I'm currently working on a project that involves using parallax effects for images and divs. However, I've noticed that there is some space left at the end of the parallax effect for unknown reasons. Could someone please assist me in removing thi ...

What is causing an empty box to appear due to padding? Is there a way to conceal it

There seems to be an issue with adding padding to the results id div box. The padding is causing a blank yellow box to appear at the bottom before any results are submitted. I've tried to hide this box without success by adding a displayResult() funct ...

Tips on positioning an element within a bootstrap carousel using z-index or alternative methods

To achieve the desired output, I have been attempting to position an image both inside and outside of a background element. However, despite my efforts using z-index and positioning properties, I have not been successful in accomplishing this task. Below ...

Incorporate dynamic classes into an HTML list using PHP

I'm struggling to assign consecutive classes to each list item in a list with the 'nav' class. My goal is to give each list item a class of 'nthChild-x', where x corresponds to its position in the list. I'm pretty new to PHP, ...

In order for Filedrop to function properly, the function must contain the alert statement

I have implemented the code below - $(function () { **alert("html5 upload");** var dropbox = $('#dropbox'), message = $('.message', dropbox); dropbox.filedrop({ // The name of the $_FILES entry: paramname: 'files&apos ...

Bring in the SCSS using a relative path with Angular CLI

When trying to import a *.css file into a .scss using the @import rule, I encountered some issues. The .css file contains relative path references like url('path') and is part of an imported library located in the node_modules directory. My .com ...

Is there a way to modify the form's style to show "none" without submitting the form?

Clicking the Close button will submit the form to Lcar.php. The goal is to hide the CSS and remain on the current page after clicking the button. The issue of the form being submitted to Lcar.php when the Close button is clicked arises despite the fact t ...

Disable the borders on HTML input fields

I am looking to implement a search feature on my website. It seems that in Firefox and Internet Explorer, the search function appears fine without any unexpected borders. Firefox IE However, when viewing the search function in Chrome and Safari, there ...

Applying CSS link dynamically in NextJS based on the window width

Is it feasible to adjust NextJS link stylesheets depending on media queries in the context of using css modules? Here is an example: CSS module file: @media(max-width: 800px) { .myClass{ /* ...mobile styles */ } } @media(min-width: 800px) { .myC ...

Ways to hide menu click when hovering over it

As a beginner, I have created a menu that shows options on hover and click. However, I am encountering an issue where clicking on one menu option and then hovering over another menu creates two lists of options. Is there a way to make the clicked menu opti ...