【问题标题】:React Material UI ListItems Vertical AlignReact Material UI ListItems 垂直对齐
【发布时间】:2019-09-03 18:37:13
【问题描述】:

我正在尝试以垂直对齐模式在 Material UI mini Drawer 上填充 ListItems(ListItemIcon 和 ListItemText)。一些我的 ListItemText 未正确对齐 ListItemIcons 的方式。

这是我的风格:

  link: {
    textDecoration: "none",
    "&:hover, &:focus": {
      backgroundColor: theme.palette.background.yellow
    }
  },
  linkActive: {
    backgroundColor: theme.palette.background.light
  },
  linkIcon: {
    marginRight: theme.spacing(1),
    marginBottom: theme.spacing(4),
    color: theme.palette.text.secondary + "99",
    transition: theme.transitions.create("color"),
    width: 24,
    display: "flex",
    justifyContent: "center"
  },
  linkText: {
    marginTop: theme.spacing(1),
    color: theme.palette.text.secondary + "CC",
    transition: theme.transitions.create(["opacity", "color"]),
    fontSize: 13,
    display: "flex",
    justifyContent: "flex-end"
  }

组件

import React from "react";
import {
  ListItem,
  ListItemIcon,
  ListItemText,
} from "@material-ui/core";
import { Link } from "react-router-dom";
import classnames from "classnames";

// styles
import useStyles from "./styles";

export default function SidebarLink({
  link,
  icon,
  label,
  location,
}) {
  var classes = useStyles();
  var isLinkActive =
    link &&
    (location.pathname === link || location.pathname.indexOf(link) !== -1);

  return (
    <ListItem
      button
      component={link && Link}
      to={link}
      className={classes.link}
    >
      <ListItemIcon
        className={classnames(classes.linkIcon, {
          [classes.linkIconActive]: isLinkActive
        })}
      >
        {icon}
      </ListItemIcon>

      <ListItemText
        classes={{
          primary: classnames(classes.linkText, {
            [classes.linkIconActive]: isLinkActive
          })
        }}
        primary={label}
      ></ListItemText>
    </ListItem>
  );
}

如何在垂直模式下正确对齐 ListItems?我尝试将 textAlign 和 alignItems 添加到 linkText css 类的中心,但它们不起作用。

感谢任何帮助

谢谢,

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    react-router-dom 链接在底层使用 html 标签。
    子项将默认显示在列中。
    您只需要正确设置链接样式:

      link: {
        display: 'flex',
        flexDirection: 'row',
        textDecoration: "none",
        "&:hover, &:focus": {
          backgroundColor: theme.palette.background.yellow
        }
      },
    

    【讨论】:

    • 感谢您的代码。但图标和文本仍然没有正确对齐到中心。
    • 将 flexDirection: 'row' 更改为 column 已解决问题
    猜你喜欢
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2023-04-07
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多