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

Filtering Quantity Based on Specific Attribute in ReactJs

I want to implement a quantity filter that can be based on color, size, or both. For instance, when I select the red color, it should display the total quantity of red items available. However, if I choose both the color red and size small, it should show ...

Tips for delivering a variable to a React Native Stylesheet

Is there a way to pass a variable to the "shadowColor" property in my stylesheet from an array declared in the code above? I keep encountering a "Can't find name" error. Attempting to use a template literal has not resolved the issue. Any assistance w ...

I'm encountering an issue with my react-dropdown-select component where the item

I am encountering an issue while trying to parse the selected dropdown item into a Prop. The error message I receive is: TypeError: Cannot read property 'name' of undefined I have tried creating a new Prop and parsing it through there, but it di ...

How can I modify the content within a scoped `<style>` tag in Vue?

Is there a way to dynamically change the contents of a <style> block in my Vue component using Vue variables? Many commonly suggested solutions involve inline styles or accessing the .style property with JavaScript. However, I am looking for a metho ...

Issues with padding and margin on iOS devices

On my iPhone (iPhone 6, iOS 8), I noticed that the loader is positioned slightly above the button. However, when I use the Chrome mobile emulator, I can't see this issue. I'm puzzled about how to resolve it. What do you think could be causing thi ...

Enhance Your Cards with Hover Zoom Text

I'm looking to place some text in front of a card hover zoom effect, but currently it appears behind the zoomed image upon hover. <div style="margin-top: 50px;" class="container imgzoom"> <div class="row ...

The function applyMiddleware in React Redux is not working correctly

After thoroughly reviewing my code, I couldn't find any flaws, yet I received this error message: Uncaught TypeError: (0 , _reactRedux.applyMiddleware) is not a function import { applyMiddleware, createStore } from 'react-redux' impor ...

Creating a cutoff with CSS: A step-by-step guide

Is there a way to create something similar using only CSS? https://i.stack.imgur.com/N9c6W.png I have no clue on how to achieve this. Any assistance would be greatly appreciated. ...

Conceal the content within the body and reveal a uniquely crafted div element using Print CSS

I came across numerous resources on using CSS to hide specific parts of a webpage. However, I am not looking to hide multiple divs like this: #flash,#menu,#anuncios { display:none; } Instead, my goal is to hide the entire body of the page and only displ ...

Personalize the way UIkit tooltips appear

I've been working on customizing a uikit tooltip for my checkbox. I managed to change the font and background color, but for some reason, I can't adjust the font size. I even tried adding a custom class without success. Do you think it's pos ...

Design a blurred glass effect background with a circular spotlight focusing on a specific section of an HTML webpage

I've been attempting to achieve a frosted glass effect on a background, with a circular section unblurred. Unfortunately, my current implementation in the sandbox editor is not working as expected - the area of the circle remains blurred. If you&apos ...

Swap out the string variable when it is modified

To generate a string inside the "code" variable that combines the selected image values, the final code should appear similar to: "test1/A=1a/B=1b" or "test2/A=1b/B=1a", etc. If the user modifies icon "A," it should replace the value in the code instead of ...

Define the starting value for Framer Motion's useCycle by taking into account the width of the screen

With the help of Framer Motion's useCycle hook, I am able to toggle the visibility of my sidebar. The desired behavior is to have the sidebar open by default on desktop (window width > 480px) and closed by default on mobile devices. To achieve thi ...

Utilizing a third-party API within the next/api endpoint

I've recently started working with NEXTJS and developed a weather application. I'm integrating the openweather API, but I'm unsure how to use it within the next/api. I attempted creating a file named today.js inside the next/api directory an ...

What is the best way to arrange multiple elements on the second row within a div?

As I attempt to utilize CSS for positioning elements within a div, my challenge lies in the limitation of only being able to use classes/ids due to the fact that the div and its content are generated from a third-party plug-in (datatables). This restricts ...

Having issues with a local image not loading in React?

I am trying to display a local image in React js. After referring to this solution, I have tried the following code - <img src={require('../public/images/icon.png').default} /> The image "icon.png" is stored in my public folder, and I&apos ...

Dynamic scaling and positioning of multiple images in relation to each other using CSS

I am currently working on layering images over a large logo and making sure it remains responsive. I want the overlaid images to maintain their position on the logo even when the browser window size is reduced. Here is what my current browser display look ...

Aggregating all elements in an array to generate a Paypal order

I'm currently working on a React project where I need to integrate the PayPal order function and include every item from an array. Below is my code snippet, but I'm struggling with how to achieve this: export default function Cart(): JSX.Element ...

Enhance the appearance of material-ui checkboxes with custom colors

Is it possible to modify the background color of an active checkbox with material-ui.com-framework? It seems like setting the style property does not impact the svg-color of the checkbox. <Checkbox label="Simple" style={st ...

Attempting to bring in an image file into a React component

My attempt to add an image at the top of my file failed, so I am looking for help with importing it. The code I originally tried did not work. The image does not display using the code below <img src={require('../../blogPostImages/' + post.bl ...