Insert a dash between two spans, but ensure that it is only done when the left span consists of a single line

Apologies for the lackluster title, struggling to find a more suitable description for this particular issue.

Within a container, there are 2 spans that contain varying content, making it difficult to rely solely on media queries. The length of text in the left span can range from 20 to 100 characters, for example.

My objective is to have a hyphen placed between the left and right span elements only when the text in the left span occupies a single line. If the text breaks onto a new line, the hyphen should be removed.

Component

import React from "react";
import styled from "styled-components";

const Container = styled.div`
  display: flex;
  height: 44px;
  align-items: center;
  justify-content: center;
  cursor: pointer;
`;

const Wrapper = styled.div`
  display: flex;
  flex-direction: row;
  align-items: center;
  cursor: pointer;
  overflow: hidden;
  padding: 0 10px;
`;

const Text = styled.p`
  font-weight: normal;
  color: #ffffff;
  font-size: 14px;
  display: flex;
`;

const Left = styled.span`
  white-space: pre-line;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-right: 5px;
  background: green;
  &::after {
    content: " - ";
  }
`;

const Right = styled.span`
  background: blue;
`;

const Content = ({ rightText, leftText }) => {
  return (
    <Container>
      <Wrapper>
        <Text>
          <Left>{leftText}</Left>
          <Right>{rightText}</Right>
        </Text>
      </Wrapper>
    </Container>
  );
};

export default Content;

In the first instance shown here, a hyphen is present to the right of the text, which isn't desired for that specific case but is required for the other two instances.

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

Is there a method using CSS or JS to dynamically add the hyphen only when the left text fits on a single line?

https://codesandbox.io/s/cranky-chaplygin-gdjgjk?fontsize=14&hidenavigation=1&theme=dark

Answer №1

One possible approach is to compare the height of the paragraph with its font size.

If the paragraph's clientHeight is greater than the computed fontSize plus padding, it indicates that the paragraph is now multiline. In this case, remove the pseudo-class.

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

parent node of React event target

Can we access the parent node of the event target in the virtual DOM? Within my simple React component, I have a function that is activated by an onClick event. Is it possible to retrieve the properties of the parent virtual DOM node within this function? ...

How can hover effects be incorporated within an inline style?

I utilized the Button component from ant design and adjusted its color using style. Now, I am looking to add a hover color effect to this Button. How can I achieve this? export default class NavBar extends React.Component{ render(){ return( ...

Can the ng-keypress listen for arrow key presses?

I'm looking to implement a functionality similar to the konami code "up, up, down, down, a, b, a, b, enter" -> triggering an action. Is it feasible to detect arrow key presses using ng-keypress in AngularJS? It doesn't seem to be working as e ...

Utilize vibrant spectrum hues for the SVG design in Fabricjs

Applying spectrum color to all objects in SVG by storing unique colors in an array and displaying them in a spectrum. The goal is to change object color using the spectrum color picker. SVG used in this program: Tiger SVG View Working Demo here: JS Fidd ...

Issue: Unsightly blank space appearing at the top of my website. Struggling to determine the cause

I am experiencing an issue on my blog. In Firefox, there is a noticeable gap at the top of the site that is not present in Chrome, and I can't seem to fix it. Even Firebug didn't provide any solutions. It's strange: when I adjust the margi ...

What is the reason for three.js utilizing for loops instead of while loops?

Could this be a Repeat Question? Discussion on faster loops: while vs. for In the world of three.js, I have noticed a common code pattern that exists in various programming languages: for ( var i = 0, l = something.length; i < l; i++ ) { perfo ...

In TypeScript, what do we call a property that is accessed dynamically?

I have a reusable function that can be used on various properties of a custom type. Here's an example: interface MyType { prop1: string; prop2: number; } type MyTypeKey = keyof MyType; const testValue = ( obj: MyType, property: MyTypeKey, v ...

The combination of Fabric.js, Darkroom.js, and devicePixelRatio offset creates a powerful tool

Having reached out on the Darkroom.js GitHub page with little activity, I'm turning to this platform for assistance. Despite being a great plugin overall, I've run into some issues while testing it on a Retina screen. Everything works fine on a ...

Verify whether a div element is styled in a specific manner

Upon initial load of my website, if the page is maximized or in fullscreen mode, the comBrand div will have specific CSS properties applied. However, during a resize event, I use the .css() function to adjust the properties of this element so it doesn&apos ...

Updating ListItemText styles in MUI when active with react-router

I am currently using ListItem from Material-UI. I am attempting to increase its font weight when it is active, but for some reason the font-weight property does not seem to have any effect. Below you will find the code snippet: <List component=&a ...

Nodejs and express authentication feature enables users to securely access their accounts by verifying their identity before they

I am currently working on a straightforward registration page that needs to save user input (name, email, password) into a database. My tools for this task are express and node. What I am experimenting with is consolidating all the database operations (suc ...

When two elements overlap, adjust their CSS positions to ensure responsiveness

Is there a way to adjust the position of an element when it comes into contact with another? Check out this example http://jsfiddle.net/mZYR8/1/ In the example, if .title-wrap (yellow) touches .right-wrap (orange), I want the orange element to slide unde ...

Error when importing: 'react-transition-group' module does not have a default export which was attempted to be imported as 'ReactCSSTransitionGroup'

I'm having issues with setting up ReactCSSTransitionGroup. I have followed the following steps: Ran npm install react-transition-group Added 'import ReactCSSTransitionGroup from 'react-transition-group';' at the top of my JS file. ...

Bootstrap column spacing explained

This code was created following the official bootstrap tutorial -> See Screenshot Implemented using the code below -> <div class="card-deck mb-3 text-center"> <div class="card mb-4 shadow-sm"> <div class="c ...

Unexpected behavior in IE8 with setAttribute following Object extension

In my current project, I've implemented a custom micro-library similar to JQuery, but tailored specifically to my needs. window.$ = function(selector, context, undefined) { var getSelectorTest = function(selector, context, undefined) { ...

What is the best way to center a div vertically on the page?

I've utilized Bootstrap 3 to create this row: <div class="cart"> <div class="row border"> <div class="col-md-8 desc pad itemsheight"> <div class="col-md-12"> <!-- react-text: 141 -->Docos <!-- /react ...

Updating a dynamic div with success using Jquery .ajax

I need assistance with updating dynamic divs on a page that contains multiple forms generated from a database. Each form has a unique div at the bottom with dynamic IDs like tempdiv_1, tempdiv_2, and so on. My goal is to use jQuery to update these divs ba ...

When the user clicks on a specific element, ensure that it is the main focus and generate an overlay

One of my challenges is implementing a custom element that captures user input upon clicking, focusing on it and overlaying other elements. I want the overlay to disappear if the user clicks outside the div. I attempted to achieve this using the iron-over ...

Ajax unable to update automatically

I'm currently in the process of setting up a registration box for new account creation. I'm attempting to load an HTML form through AJAX and pass data to a PHP file. My goal is to have the div containing the form reload every time the "register" ...

Aligning a paragraph element within a header

I'm attempting to center the <p> element within the header, but it's consistently misaligned. I attempted placing it in a grid and consulted ChafGpt, but no solution worked. I've also tried enclosing it in a container and applying &qu ...