简体   繁体   English

带样式组件的禁用按钮显示 hover state

[英]Disabled button with Styled Components shows hover state

I'm creating a button using Styled Components, but even when is :disabled , it shows :hover behaviour which it should not.我正在使用 Styled Components 创建一个按钮,但即使是:disabled ,它也会显示:hover它不应该的行为。 I'm wondering if there's a right way to disable hover if the button is already :disabled我想知道如果按钮已经禁用 hover 是否有正确的方法:disabled

this is the definition:这是定义:

import styled from "styled-components";

const Button = styled.button`
background-color: #023047;
border: 1px solid #023047;
color: white;
padding: 4px 16px;
width:100%;
border-radius: 4px;
outline: 0;
text-transform: uppercase;
margin: 10px 0px;
cursor: pointer;
box-shadow: 0px 2px 2px #023047;
transition: ease background-color 250ms;
:disabled {
    cursor: default;
    opacity: 0.7;
}
:hover {
    background-color: #ffb703;
    box-shadow: 0px 2px 2px #ffb703;
}
:active {
    background-color: #023047;
    box-shadow: 2px 2px 0 #023047;
}
`;

const MyButton = ({ content, disabled = false}) => {
    return <Button disabled={disabled}>{content}</Button>;
}

this is how is being used:这是如何使用的:

<MyButton content="i am active" /> // shows active/hover correctly

<MyButton content="i am disabled" disabled={true}  /> // is disabled but changes color on hover

any suggestion to disable hover when the button is disabled?有什么建议在禁用按钮时禁用 hover 吗?

try this:尝试这个:

 const Button = styled.button` background-color: #023047; border: 1px solid #023047; color: white; padding: 4px 16px; width: 100%; border-radius: 4px; outline: 0; text-transform: uppercase; margin: 10px 0px; cursor: pointer; box-shadow: 0px 2px 2px #023047; transition: ease background-color 250ms; :disabled { cursor: default; opacity: 0.7; } ${(props) =>.props:disabled && `:hover { background-color; #ffb703: box-shadow; 0px 2px 2px #ffb703: }`}:active { background-color; #023047: box-shadow; 2px 2px 0 #023047; } `;

you can target that with the selector你可以用选择器定位它

:hover:not([disabled])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM