2

I am using csstype in a react component library. I want to add a hover effect, which is supported through simple pseudos. Unfortunately I cannot find any example of this being used and I can't get it to work.

I have included some code below that expressed the idea of what I want to do, but I have no idea really

import CSS from "csstype";

export const DropdownContainer: CSS.Properties = {
  display: "grid",
  gridTemplateColumns: "3fr 15fr 30fr",
};

export const DropdownText: CSS.Properties = {
  placeSelf: 'start',
}

export const DropdownTextHov: { [P in CSS.SimplePseudos]?: CSS.Properties }  = {
  ':hover': {
    color: 'blue',
  },
}

<snipped code>

    return (
      <div style={DropdownContainer}>
        <p style={DropdownText}></p>
        <p style={{DropdownText} + {DropdownTextHov}}>this is text</p>
      </div>
    );

I have tried a number of alternatives. I have added the hover to DropdownText, but that doesn't work.

4
  • Alternatively, you can use a CSS module in your project and improve your CSS to SCSS. For example; import styles from './styles.css' and usage; <div className={styles.example}></div> Commented Jan 23, 2023 at 20:00
  • I tried styled-components, but there is a bug in the way it works with rollup which breaks the component when it is imported into an application. I can take a look at SCSS if I can't work out how to use basic css-in-js Commented Jan 24, 2023 at 14:07
  • Did you find how to solve it? Commented Oct 14, 2023 at 19:46
  • No, never did, sorry Commented Jan 26, 2024 at 13:10

1 Answer 1

0

I think the answer is probably here: How can I write 'a:hover' in inline CSS? -

Explanation: The React style parameter sets the inline style for the element. It doesn't behave the same as a css style sheet.

In this case, you are trying to add inline styling for :hover which isn't supported using inline css. To set :hover you will either need a separate css sheet or add a <style></style> block either in your component or elsewhere in the html document.

Sign up to request clarification or add additional context in comments.

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
Please write answer, then in addition you can add the url/references

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.