Incorporate text inside the border of a Material UI chip

https://i.stack.imgur.com/VATrD.png

I'm looking to replicate this design using a pre-built Chip component from MaterialUI

While the solution might involve Some Text using html and css, it still may not achieve an identical result. I've come across examples where people achieved similar results with MaterialUI by using a TextField with a Chip as an adornment, but it's not quite the same.

Answer №1

If you're interested in utilizing the <Chip> component, one approach is to leverage &::before for styling within the Chip.

For example:

const Chip = styled(MuiChip)(({ chipTitle }) => ({
  "&::before": {
    content: `"${chipTitle}"`,
    position: "absolute",
    top: "-8px",
    left: "16px",
    backgroundColor: "#fff",
    padding: "0 8px"
  }
}));

To use it:

<Chip
  label="Salary Proposal - 500k$"
  variant="outlined"
  onDelete={handleDelete}
  chipTitle="Hello"
/>

Explore this CodeSandbox. Personal styling may vary slightly, so adjust as needed.

For Typescript users:
Begin by extending the default ChipProps type.
eg:

import {styled, Chip as MuiChip, ChipProps as MuiChipProps} from '@mui/material'

interface ChipProps extends MuiChipProps {
    chipTitle: string
}

Then update the styled() function like so:

const Chip = styled(MuiChip)<ChipProps>(({ chipTitle }) => ({
  "&::before": {
    content: `"${chipTitle}"`,
    position: "absolute",
    top: "-8px",
    left: "16px",
    backgroundColor: "#fff",
    padding: "0 8px"
  }
}));

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

Transferring data between two functions within a React.js application

I have two functions called getLocation() and getLocationName(). The getLocation() function performs an XMLHttpRequest, and I want to pass the response to the getLocationName() function to display it in a list. getLocationName = (location) => { var ...

Looking to integrate WhatsApp into your next.js website for optimal responsiveness?

I am trying to incorporate WhatsApp into my next.js responsive website. I have already added an icon which successfully opens the WhatsApp web version when clicked in desktop view. However, I want to make sure that when a user accesses the website on mobi ...

My attempt at deploying my personal React App project on Vercel was unsuccessful

// encountering error during deployment on Vercel npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: @testing-library/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e99b8c888a9da9d8da ...

React Bootstrap always displays tooltips

I have integrated react-bootstrap into my project. I am currently attempting to keep a tooltip always displayed, but I am facing some challenges in achieving this. Below are the approaches I have tried so far: <Card style={{width: '10rem'}} ...

Jquery cascading menu

I'm currently experiencing difficulties in creating dropdown menus using jquery and css. Below is my HTML code: <nav class="topNav"> <ul> <li> <a href="#menu" class="menu-toggle"><img src ...

Struggling to incorporate logout feature with node and passport js

Currently delving into the world of node js, I am in the process of creating a boilerplate utilizing passport js, react, and redux. The issue at hand involves trouble implementing log out functionality as my attempts to log out have been unsuccessful. Anyo ...

Ways to position an element at the center using Flexbox技

Having some difficulty centering an element in my code using flexbox. It seems to work for everything else, but not here. Any suggestions or ideas? .answer { text-shadow: 0 0 2px gr; display: flex; justify-content: center; border-radius: 5px; ...

Mastering Instagram Automation: Techniques for Navigating the Lightbox with Selenium

I am in the process of developing a Python script that assists users in Instagram engagement groups by efficiently liking everyone's photo during each round. I am facing an issue where Selenium is unable to click on the first photo when I reach a user ...

What is the best way to align a button to the right in Material-UI?

I've been experimenting with Material UI, but I'm having trouble aligning buttons to the right :( import * as React from "react"; import SvgIcon from "@mui/material/SvgIcon"; import Button from "@mui/material/Button" ...

Extracting Text from a Span Element Using XPath in Selenium

Here is the HTML code snippet: <div class="a-row a-spacing-small a-size-small"> <div class="a-row"> <a class="a-link-normal a-declarative g-visible-js reviewStarsPopoverLink" href="#" data-action="a-popover" data-a-popover="{"closeButton":" ...

Building CSS columns

Similar Inquiry: Creating dual columns in HTML/CSS layout I'm exploring ways to achieve a two-column layout within a wrapping div element. Currently, I have the following CSS code snippet which centers a div inside the browser: .wrapper {width: ...

How can I clear the div styling once the onDismiss handler has been triggered

Seeking assistance with resetting a div on a Modal after it has been closed. The issue I am facing with my pop-up is that the div retains the previous styling of display: none instead of reverting to display: flex. I have searched for a solution without su ...

Introducing Next JS version 14 with enhanced capabilities for utilizing client API headers

import { useClient } from "../../utils/client"; import { paths } from "../../types/generated-types"; const client = useClient(); export const GET = client.GET; export const PUT = client.PUT; export const POST = client.POST; export con ...

Implementing basic authentication with React and Node.js

I'm currently utilizing the fetch API to send requests from a ReactJS frontend to a Node.js backend with Basic Authorization using the following code... React fetch(baseUrl, { method: 'get', headers: { Accept: 'application/json ...

Compatibility of image maps with browsers and the usage of imagemapster

Currently, I am utilizing ImageMapster to make adjustments to an image map while hovering. However, I am facing challenges with both the image map itself and the ImageMapster plugin. The issues I am encountering are: 1) Despite specifying a height and wid ...

Mastering the art of utilizing drag and drop features for both columns and rows in a React Table within ReactJS

I have managed to create a React Table with columns and rows, but now I'm looking to incorporate drag and drop functionality for both. Does anyone know how I can achieve this? Feel free to check out my CodeSandbox Sample here - https://codesandbox.io ...

Adding text to a Video Js fullscreen window on an iPad

Looking to enhance user experience on iPad while using a video js player, I want to make an image pop up upon completion of the video. Currently, I have it set up to work without full screen mode, but iPad users prefer watching videos in full screen. Is th ...

Configuring a React application within AWS Beanstalk with the help of environment variables

Is there a way to customize React application environment variables based on the Beanstalk environment it is deployed to? The objective is to compile React code with environment-specific variables only for that particular environment. The goal is to avoid ...

Content sliding to the left due to modal prompt

I've searched through various questions and answers but haven't been able to resolve this issue. There's a modal on my page that is causing the content to shift slightly to the left. I've created a sample fiddle, although it doesn&apo ...

Error encountered in React: Unable to read property 'preventDefault' of an undefined value

Within my React application, I have an onClick event that is triggered: onClick={this.selectedComponent} The function being called by the onClick event is as follows: selectedComponent = (e) => { e.preventDefault(); this.setState({ ...