0

How do I add a className to a div, based on either event.

Here's my code below.

I've tried:

className={isActive ? "active" : "", isClicked ? "clicked" : ""}

but this only works if the event is clicked.

this works when the event is hovered

className={isActive ? "active" : ""}

so does this when it's clicked

className={isClicked ? "clicked" : ""}

Just not together.

What am I doing wrong?

Here's my full code:

const [isActive, setIsActive] = useState(false);
const [isClicked, setIsClicked] = useState(false);

<MenuButton
  onMouseEnter={() => setIsActive(!isActive)}
  onMouseLeave={() => setIsActive(!isActive)}
  onClick={() => setIsClicked(!isClicked)}
  className={`${isActive ? "active" : ""} ${isClicked ? "clicked" : ""}`}
>

const MenuButton = styled.div`
  background: #3498db;
  &.clicked {
    display: none;
  }
  &.active {
    transform: scale(1.2, 1.2) translate3d(0, 0, 0);
  }
`;

UPDATE: I have now included my events, states and how I am using them in my css

2
  • Where are your event handlers? Can you share them? Commented Mar 2, 2021 at 13:52
  • Sure, I'll update my question. Commented Mar 2, 2021 at 15:28

2 Answers 2

1

ok, Please use component classnames (https://www.npmjs.com/package/classnames)

example 1:

<Button 
  className={classNames(styles.button, 'button-secondary')} 
  onClick={handleReturn}
>
  back
</Button>

example 2:

        <div 
            className={classnames(
                styles.ShadowIconButtonWrapper,
                {
                    [styles.ShadowIconButtonWrapperOpen]: open,
                    [styles.ShadowIconButtonWrapperClose]: !open
                }
            )}
        >
            <ShadowIconButton 
                renderIcon={renderIcon}
                elevation={elevation}
                onClick={handleDrawerButtonClick}
            />
        </div>

decision:

className={classnames(
                    {
                        ['active']: isActive,
                        ['clicked']: isClicked 
                    }
)}
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure I want to add extra packages just for something so simple.
0

You can use string literals.

className={`${isActive ? "active" : ""} ${isClicked ? "clicked" : ""}`}

or separate the logic:

const isActiveClass = isActive ? 'active' : '';
const isClickedClass = isClicked ? 'clicked' : '';

className={`${isActiveClass} ${isClickedClass}`}

or you can use a library to achieve a similar effect.

https://www.npmjs.com/package/clsx

2 Comments

This example does work but when using prettier in vs code, the format is ugly.
You can extract the expression to variable. For example, const isActiveClass = isActive ? 'active' : ''; const isClickedClass = isClicked ? 'clicked' : ''; className={${isActiveClass} ${isClickedClass}}

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.