我看到很多 Material UI 代码都在 React 样式组件中使用伪选择器。我想自己尝试一下,但无法成功。我不知道我做错了什么,也不知道这是否可行。
我正在尝试制作一些 CSS,以使该元素与固定标题偏移。
import React from 'react'; import { createStyles, WithStyles, withStyles, Typography } from '@material-ui/core'; import { TypographyProps } from '@material-ui/core/Typography'; import GithubSlugger from 'github-slugger'; import Link from './link'; const styles = () => createStyles({ h: { '&::before': { content: 'some content', display: 'block', height: 60, marginTop: -60 } } }); interface Props extends WithStyles<typeof styles>, TypographyProps { children: string; } const AutolinkHeader = ({ classes, children, variant }: Props) => { // I have to call new slugger here otherwise on a re-render it will append a 1 const slug = new GithubSlugger().slug(children); return ( <Link to={`#${slug}`}> <Typography classes={{ root: classes.h }} id={slug} variant={variant} children={children} /> </Link> ); }; export default withStyles(styles)(AutolinkHeader);
我发现内容属性需要像这样用双引号引起来
const styles = () => createStyles({ h: { '&::before': { content: '"some content"', display: 'block', height: 60, marginTop: -60 } } });
然后一切都按预期进行