Leveraging the sibling combinator for altering the class of a sibling SVG element with the assistance of Material

I have created an SVG picture that I am trying to animate. Behind the elements I want to animate on mouse hover, I have added transparent rectangles. The hover effect works well on these elements as the cursor changes to pointer.

However, when I attempt to animate other elements above these rectangles on mouse hover using the CSS sibling combinator ~, the element does not animate:

const useStyles = makeStyles({
  btcContainer: {
    fill: "none",
    pointerEvents: "all",
    "&:hover": {
      cursor: "pointer",
      "& ~ $btc": {
        transform: "translateY(-20px)"
      }
    }
  },
  caseContainer: {
    fill: "none",
    pointerEvents: "all",
    "&:hover": {
      cursor: "pointer",
      "& ~ $case": {
        transform: "translateY(-20px)"
      }
    }
  },
  coffeeContainer: {
    fill: "none",
    pointerEvents: "all",
    "&:hover": {
      cursor: "pointer",
      "& ~ $coffee": {
        transform: "translateY(-20px)"
      }
    }
  },
  glassesContainer: {
    fill: "none",
    pointerEvents: "all",
    "&:hover": {
      cursor: "pointer",
      "& ~ $glasses": {
        transform: "translateY(-20px)"
      }
    }
  },
  pcContainer: {
    fill: "none",
    pointerEvents: "all",
    "&:hover": {
      cursor: "pointer",
      "& ~ $pc": {
        transform: "translateY(-20px)"
      }
    }
  },
  glasses: {}
});

This is my style setup using material-ui. So, for instance, when hovering over the glassesContainer, I want the glasses class to move with a translation of -20px.

The JSX code for these classes looks like this:

<g>
 // SVG Elements here
</g>          

I'm having trouble with getting this to work. I've tried removing the ~ but it did not solve the issue.

You can view the codesandbox for this problem here.

Any help would be greatly appreciated!

Answer №1

In order to properly style your Glasses path, you should reverse the order of the glassesContainer rect and the Glasses path.

Referencing https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_combinator (emphasis added):

The general sibling combinator (~) links two selectors together and selects the second element only if it comes after the first element (not necessarily immediately), both being children of the same parent element.

In order for the selector "& ~ $glasses" to target your Glasses path correctly, it must follow the glassesContainer rect sibling (indicated by & in this CSS rule) within the HTML structure.

Check out this functional version of your sandbox that demonstrates correct styling for glasses: https://codesandbox.io/s/jss-sibling-combinator-rn2c2.

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

The MIME type text/html was ignored along with the AddType directive

Encountering an issue where attempting to load a CSS file from a folder results in the console error: "Resource interpreted as Stylesheet but transferred with MIME type text/html". Attempts to resolve this by adding an .htaccess file (in the root/same fold ...

Troubleshooting: ng-disabled not function properly in Angular.js

My goal is to only allow the button to be enabled if there is text in the textfield, but for some reason I am unable to make ng-disabled work: <form novalidate> <button type="submit" ng-click="add('+')" ng-disabled="bittext.$invalid ...

The server returned a response that was void of any content

I have encountered an issue while trying to retrieve data from my server. The request works perfectly fine when tested with Postman, returning the expected data. However, upon implementing the request in my application, I receive an empty object with prope ...

Using jQuery to Toggle the Height of Multiple Sections with a Single Function

I've got three different sections, each with varying heights and a simple structure like this: <section> <h2>My heading</h2> </section> What I want is for these sections to display at first, but then shrink dow ...

Set up an array data by extracting values from an array prop within a Vue component

Within my Vue component, I am dealing with an array prop called selectedSuppliers that consists of objects. My goal is to set up a data property named suppliers and initialize it with the values from selectedSuppliers. However, I do not want any modificati ...

The ajax method for loading an xml file results in displaying the undefined message

When attempting to fetch content from an xml file using ajax, the page initially displays "undefined". However, upon refreshing the page, the content loads successfully. Take a look at my code snippet below: $.ajax({ type: "GET", url: "xm ...

Laravel implementation of Bootstrap Datepicker

Incorporating Laravel bootstrap and a date picker, I have encountered an issue where the todayHighlight feature is not functioning correctly. Additionally, the container aspect is also not working as intended. <link rel="stylesheet" href="https://sta ...

The functionalities of Google Maps are experiencing issues within AngularJS when utilizing the $route feature

Having Issues with Google Maps in AngularJS when using <ng-view></ng-view> Routing Configuration app.config(function($routeProvider, $locationProvider) { $locationProvider.html5Mode(true); $routeProvider .when('/', { t ...

Display routes in React using the react-router package

Can I use a console command at runtime to display all routes in my application? Despite utilizing react-router, not all routes seem to be functioning properly. In Rails, you can retrieve a list of routes during runtime. ...

Having trouble retrieving values from my EXPRESS / NodeJs response error message: [String: 'Error: Request resulted in an error code: 404'

I have been attempting to retrieve values from my express response error. When I use console.log on the error like so... app.use(function(err, req, res, next){ console.log(err) }); The content displayed in the console is as follows: [String: 'E ...

Customize.Print - establish default print preferences

In my current project, I am exploring ways to add a print PDF feature. After discovering print.js, it seems like a possible solution for this task. My main concern is setting default print options to ensure the correct scale (i.e., 100%). Does print.js h ...

Turning off strict mode in the bundling process of React with webpack can be achieved by following

I'm facing an issue with my application. It works perfectly in all browsers except for IE, where it throws the following error: 0x800a0416 - JavaScript runtime error: Multiple definitions of a property not allowed in strict mode In my webpack.config ...

How to delete the final element in a stack trace string using Javascript

Currently, I am working on a Javascript logger that includes the stack trace in error messages. The implementation looks like this: function logMessage(logMessage) { let stackTrace = new Error().stack; logMessage.stackTrace = stackTrace; ... } Whi ...

Ways to set a default value in AngularJS when there is no value to compare in an array

Hello, I'm a newcomer to AngularJS and I have the following code snippet in HTML: // Here, I have another ng-repeat loop where I compare home.home_info_id with avg.home_inof_id <div ng-repeat='home in homeDetailInfo'> <div ng-r ...

Comparison of button text alignment: stacking vs inline placement

I currently have a button on my webpage labeled "Learn More." Unfortunately, the text on the button is not lining up as it should. Instead of flowing in a line, the text seems to be stacking on top of each other. The HTML code for the button is included ...

Triggering a click event on various instances of a similar element type using the onclick function

Hey there, I'm a newcomer to Javascript. I've been practicing my Javascript skills and trying to replicate something similar to what was achieved in this example: Change Button color onClick My goal is to have this functionality work for multip ...

Issue: An incorrect hook call occurred. How can data from one hook be awaited before being instantiated into another?

Hey there, I'm currently working on injecting a header into my fetcher before using swr to fetch the data. The challenge is that I need to await for a custom hook to respond with the data before injecting it into the custom fetcher. I've noticed ...

What is the process for connecting a personalized bootstrap framework with an index.html document?

I have recently dived into the world of bootstrap and I just customized my bootstrap file on http://getbootstrap.com/docs/3.3/customize/ After compiling and downloading, I found it challenging to link it with my index.html file. The source of the bootstra ...

Troubleshooting issue: Failure in proper functionality of Jquery's slideDown

My form is divided into 3 sections, with the latter two initially hidden using CSS. When users click the next button in the first section, the second section is supposed to slide down into view. However, I'm experiencing an issue where the animation s ...

Should I increase the number of followers, or opt for an aggregated method to monitor them?

My system loads products using an infinite scroll, displaying 12 at a time. Sometimes, I may want to sort these products based on the number of followers they have. Here's how I keep track of the followers for each product: The follows are stored i ...