gpt4 book ai didi

reactjs - 样式组件按钮中的两种颜色

转载 作者:行者123 更新时间:2023-12-05 04:37:06 25 4
gpt4 key购买 nike

我这里有一个关于将颜色放在按钮上的问题。我的问题是如何分离颜色?

Codesandbox CLICK HERE

const Button = styled.button`
display: flex;
justify-content: center;
align-items: center;
height: 58px;
width: 30%;
cursor: pointer;
font-size: 20px;
color: #fff;
background: ${({ backgroundColor = null }) =>
backgroundColor ? "pink" : "grey"};
border-radius: 20px;
border: 3px solid
${({ borderColor = null }) => (borderColor ? "#FF0000" : "black")};
`;

最佳答案

对于此解决方案,您不需要创建两个状态。在订阅取消订阅之间切换只需要一个状态。我添加了额外的 css 属性,使它看起来更像上面的图片。

Edit dazziling-code

索引.js

import React from "react";
import styled from "styled-components";

const Button = styled.button`
padding: 0;
overflow: hidden;
display: flex;
cursor: pointer;
font-size: 20px;
color: #fff;
border-radius: 20px;

/* new lines */
border: none;
outline: none;
box-sizing: border-box;
box-shadow: inset 0 0 0 2px black; /* Why is there a white button enclosing a button */
& div {
display: flex;
align-items: center;
height: 58px; /* fix for chrome browser */
}
& .number {
background-color: black;
width: 30%;
padding: 0 10px;

/* new lines */
border-radius: 20px 0 0 20px;
border-top: 3px solid ${({ subscribe }) => (subscribe ? "#FF0000" : "grey")};
border-bottom: 3px solid
${({ subscribe }) => (subscribe ? "#FF0000" : "grey")};
border-left: 3px solid
${({ subscribe }) => (subscribe ? "#FF0000" : "grey")};
}
& .text {
padding-left: 10px;
background: ${({ subscribe }) => (subscribe ? "hotpink" : "grey")};

/* new lines */
border-top: 3px solid ${({ subscribe }) => (subscribe ? "#FF0000" : "grey")};
border-bottom: 3px solid
${({ subscribe }) => (subscribe ? "#FF0000" : "grey")};
color: ${({ subscribe }) => (subscribe ? "white" : "black")};
}
& .icon {
padding: 0 10px;
width: 32px;
background: ${({ subscribe }) => (subscribe ? "hotpink" : "grey")};

/* new lines */
border-radius: 0 20px 20px 0;
border-top: 3px solid ${({ subscribe }) => (subscribe ? "#FF0000" : "grey")};
border-bottom: 3px solid
${({ subscribe }) => (subscribe ? "#FF0000" : "grey")};
border-right: 3px solid
${({ subscribe }) => (subscribe ? "#FF0000" : "grey")};
color: ${({ subscribe }) => (subscribe ? "white" : "black")};
& .bell {
width: 2rem;
fill: ${({ subscribe }) => (subscribe ? "white" : "black")};
}
}
`;

const Table = () => {
const [isSubscribe, setIsSubscribe] = React.useState(false);

const onSetColor = () => {
setIsSubscribe((prevState) => !prevState);
};

console.log(isSubscribe);

return (
<div style={{ backgroundColor: "black", padding: "1rem" }}>
<Button type="button" onClick={onSetColor} subscribe={isSubscribe}>
<div className="number">100</div>
<div className="text">Subscribe</div>

<div className="icon">
{isSubscribe && (
<svg className="bell" viewBox="0 0 24 24">
<path d="M21,19V20H3V19L5,17V11C5,7.9 7.03,5.17 10,4.29C10,4.19 10,4.1 10,4A2,2 0 0,1 12,2A2,2 0 0,1 14,4C14,4.1 14,4.19 14,4.29C16.97,5.17 19,7.9 19,11V17L21,19M14,21A2,2 0 0,1 12,23A2,2 0 0,1 10,21M19.75,3.19L18.33,4.61C20.04,6.3 21,8.6 21,11H23C23,8.07 21.84,5.25 19.75,3.19M1,11H3C3,8.6 3.96,6.3 5.67,4.61L4.25,3.19C2.16,5.25 1,8.07 1,11Z" />
</svg>
)}
{!isSubscribe && (
<svg className="bell" viewBox="0 0 24 24">
<path d="M21,19V20H3V19L5,17V11C5,7.9 7.03,5.17 10,4.29C10,4.19 10,4.1 10,4A2,2 0 0,1 12,2A2,2 0 0,1 14,4C14,4.1 14,4.19 14,4.29C16.97,5.17 19,7.9 19,11V17L21,19M14,21A2,2 0 0,1 12,23A2,2 0 0,1 10,21" />
</svg>
)}
</div>
</Button>
</div>
);
};

export default Table;

关于reactjs - 样式组件按钮中的两种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70753005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com