我在很多 MUI 代码中看到,他们在 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);