我想在一组选项卡下面添加一条线,并突出显示选定的选项卡。我使用的是react和styled组件,当用户选择活动列表项时,列表项下面应该会有一个条突出显示。
这就是它目前的样子

这就是它应该是什么样子

这是我到目前为止所拥有的代码
import { List, ListItems, Div } from './messageTab.elements';
function MessageTab() {
return (
<Tabs>
<div label="Recent">
Recent component
</div>
<div label="Unread">
Unread component
</div>
<div label="Groups">
Groups component
</div>
</Tabs>
)
}
function Tabs(props) {
const [activeTab, setActiveTab] = useState(props.children[0].props.label)
const switchTabs = (tab) => {
setActiveTab(tab);
}
console.log(activeTab);
return (
<Div>
<List>
{props.children.map((child) => {
const { label } = child.props;
return (
<Tab
activeTab={activeTab}
key={label}
label={label}
onClick={switchTabs}
/>
);
})}
</List>
</Div>
);
}
function Tab(props) {
const { label, onClick, activeTab } = props;
console.log(activeTab);
console.log(label);
return (
<ListItems onClick={() => onClick(label)}>{label}</ListItems>
);
}以下是我的样式化组件
import styled from 'styled-components';
const ListItems = styled.li`
margin-right : 6%;
color: #9095A4;
`;
const List = styled.ul`
display : flex;
list-style-type : none;
float : right;
flex-direction: row;
`;
const Div = styled.div`
display : flex;
margin-left : 6%;
`;
export { List, ListItems, Div };发布于 2021-06-30 15:54:04
您可以使用::after伪选择器来创建和设置“选项卡”的样式。
const ListItems = styled.li`
margin-right: 6%;
color: #9095a4;
${(props) =>
props.activeTab &&
css`
color: #0095ff;
::after {
content: "";
position: relative;
height: 5px;
margin-top: 0.5rem;
border-radius: 5px 5px 0 0;
background-color: #0095ff;
display: block;
}
`}
`;在列表中添加底部边框并设置宽度。
const List = styled.ul`
display: flex;
list-style-type: none;
float: right;
flex-direction: row;
border-bottom: 1px solid lightgray;
width: 100%;
`;

https://stackoverflow.com/questions/68190075
复制相似问题