Change the right border style for the second and third ToggleButtons in the ToggleButtonGroup

I've been working on this for a few hours now and I can't seem to get it right.

Currently, I'm using Mui v5 and trying to style the ToggleButtons to look like regular MUI buttons.

So far, I was able to achieve this transformation:

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

starting from this initial state:

https://i.stack.imgur.com/9XRT5.png

by implementing the following custom theme:

import { createTheme } from "@mui/material/styles";

let theme = createTheme();

export const CustomTheme = createTheme(theme, {
  components: {
    MuiButton: {
      defaultProps: {
        disableRipple: true,
      },
    },
    MuiToggleButton: {
      defaultProps: {
        disableRipple: true,
      },
      styleOverrides: {
        root: {
          transition: "",
          width: "6.30rem",
          height: "2.3rem",
          textTransform: "capitalize",
          color: theme.palette.primary.main,

          "&:hover": { backgroundColor: "transparent" },
          "&.MuiButtonBase-root": { borderRadius: "1rem" },
          "&.MuiToggleButtonGroup-grouped": {
            borderRadius: "5px !important",
            border: "1px solid !important",
            borderColor: theme.palette.primary.light + " !important",
          },
          "&.Mui-selected, &.Mui-selected:hover": {
            color: "white",
            backgroundColor: theme.palette.primary.main,
            borderColor: theme.palette.primary.main,
            borderColor: theme.palette.primary.main + " !important",
          },
        },
      },
    },
  },
});

I came across this solution on StackOverflow, but unfortunately, I can't recall the exact source. I simply copied and pasted the code, and it worked like a charm.

However, I'm stuck at figuring out how to target this specific class:

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

Queries:

  1. Is there a more elegant way to change the border without relying on "!important" for the MuiToggleButtonGroup-grouped class?
  2. How can I specifically target the class
    MuiToggleButtonGroup-root .MuiToggleButtonGroup-grouped:not(:last-of-type)
    and customize its styling? For instance, if I wanted to specify a different style for the nth child of the button group.

Answer №1

Here is a solution that worked for me:

MuiToggleButtonGroup: {
...theme.components?.MuiToggleButtonGroup,
styleOverrides: {
  grouped: {
    '&:not(:last-of-type)': {
      border: `1px solid ${theme.palette.brand.lightGray}`,
      borderRadius: 4,
      '&:hover': {
        boxShadow: 'none',
        backgroundColor: 'transparent',
        border: `2px solid ${theme.palette.brand.slateBlue}`,
      },
    },
    '&:not(:first-of-type)': {
      margin: 'inherit',
      border: `1px solid ${theme.palette.brand.lightGray}`,
      borderRadius: 4,
      '&:hover': {
        boxShadow: 'none',
        backgroundColor: 'transparent',
        border: `2px solid ${theme.palette.brand.slateBlue}`,
      },
    },
  },
},

Hopefully, this solution helps you with your problem.

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

Comparing form submission with a button click to pass data using Ajax - success in one method but failure in the other

I am facing an issue with two pieces of jquery code in my Flask application. While one works perfectly, the other is not functioning as expected. Both the form and a button trigger the same function through ajax calls. Currently, for troubleshooting purpos ...

What could be causing my CORS fetch request to not send cookies to the server?

Trying to work out a CORS request using the fetch method: fetch('https://foobar.com/auth', { method: 'GET', mode: 'cors', credentials: 'include', }) The server-side code in express for impl ...

Tips for incorporating a background color into a specific section

Code: .headline { font: 150 50px/0.8 'Pacifico', Helvetica, sans-serif; color: #2b2b2b; text-shadow: 3px 3px 0px rgba(0, 0, 0, 0.1), 7px 7px 0px rgba(0, 0, 0, 0.05); text-align: center; } <link href='http://fonts.googleapis.com ...

Is it achievable to animate dynamic height using CSS? Open to JS alternatives as well

I am currently utilizing AngularJS, which enables me to utilize ng-show and ng-hide in order to display or hide elements based on a logical condition. My goal is to animate the size changes of the container when the child objects are shown or hidden, creat ...

`initMap` does not exist as a function

Hey everyone, I need some help: I recently integrated the Google Maps API into my AngularJs project and encountered an error in the console: Uncaught message: "initMap is not a function" name: "InvalidValueError" This occurs when the Google Map API is ...

Guide to creating scroll-based animations for a Div element

I've been brainstorming ways to rotate a div as I scroll. My goal is to recreate the effect shown in this codepen. However, I'm struggling to implement scrolling functionality. What I envision is scrolling down causing the word Data to rotate in ...

Troubleshooting Problem with ListItem Alignment in Material UI v0 involving Custom Avatar Component

Material UI version: v0.20.0 I'm facing an issue with aligning the leftAvatar value using the CustomAvatar component, as shown in the attached screenshot. Any assistance would be appreciated. CustomAvatar: The functionality of this component is cond ...

Error: Invariant Violation occurred with code 29 while using React Apollo and GraphQL

I encountered an error that says "Invariant Violation: 29." Could someone explain what this error means and if I missed something in my code that triggered it? The error occurred when I was trying to import the LocationSearch component into my index.js. im ...

Is it feasible to develop a functional computer interface using threejs?

Is it feasible to integrate a window into threejs that could facilitate the use of standard desktop applications (such as code editors) within the virtual scene? Please note: This is being implemented within a custom application or a node-webkit environme ...

Is there a way to integrate a JQuery plugin into another plugin seamlessly without causing any conflicts or disruptions?

With the code provided, a flip effect is being generated using the JQuery Flip plugin. Within the "verso" property, there is a select menu with the ID #ddlBookletType, which triggers another JQuery plugin that creates dropdown menus. The current HTML stru ...

Tips on accessing a function or variable within a script executed using $.getScript

After running a script with $.getScript, is there a way to access the namespace of that script? I expected to be able to do so since the plugin function is defined in the global scope. index.js $.getScript('plugin.js').then((...result) => co ...

Exploring the integration of React with Spotify's Web Playback SDK

There is an exciting new beta feature on Spotify that allows for full song playback in the browser called the Web Playback SDK. The documentation showcases initializing a player immediately using script tags in the main HTML file, requiring an access token ...

What is the best way to optimize Bootstrap 5 grids for mobile responsiveness?

Within my portfolio, I have a projects section that switches between having the description on the left with an image on the right and vice versa. Despite my efforts to make it responsive, I'm struggling to figure out how to ensure the image appears a ...

How to change the border color of a Bootstrap card when hovered over

I'm struggling to achieve a border color change effect on hover for Bootstrap cards. <b-card no-body class="my-card border"> <button>Click here</button> <b-collapse> <b-card-body> <b-c ...

Effortless link to a Gremlin database using a JavaScript app

I apologize for my lack of technical knowledge, but I am struggling to solve this issue despite studying the documentation. My goal is to establish a connection with a standard Gremlin DB (Cosmos) using gremlin. While it works well from the server, encoun ...

Validation is not being enforced for the input field labeled as 'name' in the form

Can anyone assist me with a form validation issue I'm encountering while working on my project? The validation is functioning correctly for email and institution fields, but it seems to be ignoring the name field. Any suggestions or help would be grea ...

There seems to be a connection issue (ETIMEDOUT) when trying to create a React app using npm

Struggling to set up a react app with npx create-react-app my-app. Unfortunately, an error keeps popping up in the terminal: npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! network request to https://registry.npmjs.org/cr ...

How to Implement Full Height CSS for Child Elements?

How can I set the child containers "left" and "right" to be 100% height of the parent container, which is already at 100% height? Also, what's the best way to position the container "hexCont" horizontally? CSS: <style type="text/css"> html ...

Having trouble getting the webpage to update after entering information?

My current project involves automating a website using selenium python. I've encountered an issue where manually entering J590 into the target textbox requires clicking or pressing tab to refresh the page, resulting in an available option in a dropdow ...

I am experiencing an issue where my Visual Studio Code extension is failing to display code correctly

Hey everyone, I'm a student and a beginner in Visual Studio code. Recently, I was working with Java Script and ran into an issue. Every time I tried to save my code by pressing Ctrl+S, it would automatically indent the code, causing problems in my ter ...