简体   繁体   English

如何使用 Tabs API 在 Material-UI 中水平对齐选项卡标签和选项卡图标

[英]How to Align tab-label and tab-icon horizontally in material-UI using Tabs API

I am trying to overwrite the Material UI CSS and align the the phone icon and the phone text in the same line and closer to each other.我正在尝试覆盖 Material UI CSS 并将电话图标和电话文本对齐在同一行中并且彼此靠近。 I researched and found Tabs API .我研究并找到了Tabs API

Then I debugged and found the wrapper property was creating a problem.然后我调试并发现包装器属性产生了问题。 I tried fixing by setting display to block but the phone icon and phone text are still not aligning in same line.我尝试通过将显示设置为阻止来修复,但电话图标和电话文本仍未在同一行对齐。

I've provided code and sandbox below.我在下面提供了代码和沙箱。 All of my code is in tab-demo.js我所有的代码都在 tab-demo.js

https://codesandbox.io/s/7p4ryw691 https://codesandbox.io/s/7p4ryw691

const styles = theme => ({
  root: {
    // flexGrow: 1,
    width: "100%",
    // flex: 0,
    textTransform: "capitalize"
    // backgroundColor: theme.palette.background.paper
    //  backgroundColor: 'red'
  },
  sportsAdvancedSearch: {
    // backgroundColor: 'green'
    color: "red",
    fontSize: 16,
    fontWeight: "bold"
  },
  sportsTotalNumber: {
    fontWeight: "bold"
  },
  sportsExportContainer: {
    paddingTop: 8,
    paddingBottom: 8
  },

  indicator: {
    backgroundColor: "red"
  },
  tabsIndicator: {
    backgroundColor: "red",
    textTransform: "capitalize"
  },
  tabRoot: {
    textTransform: "initial",
    width: "100%",
    display: "block",

    "&:hover": {
      color: "red",
      opacity: 1,
      textTransform: "initial"
    },
    "&$tabSelected": {
      color: "red",
      fontWeight: theme.typography.fontWeightMedium,
      textTransform: "capitalize"
    },
    "&:focus": {
      color: "red",
      textTransform: "capitalize"
    }
  },
  tabSelected: {},
  sportsHeading: {
    fontSize: 32,
    fontWeight: "bold",
    padding: 16
  },
  sportsTabHeader: {
    //  border: "1px solid red",
    backgroundColor: "#f5f5f5"
  },
  alignment: {
    display: "block"
    //  color: 'red'
  }
});

  <Tabs
            value={value}
            onChange={this.handleChange}
            scrollable
            scrollButtons="on"
            classes={{ indicator: classes.tabsIndicator }}
          >
            <Tab
              label="phone"
              icon={<PhoneIcon style={{ display: "block" }} />}
              classes={{
                root: classes.tabRoot,
                selected: classes.tabSelected,
                wrapper: classes.alignment
              }}
            />
            <Tab
              favorites={favorites}
              label="Favorites"
              icon={<FavoriteIcon style={{ display: "block" }} />}
              classes={{
                root: classes.tabRoot,
                selected: classes.tabSelected,
                wrapper: classes.alignment
              }}
            />
          </Tabs>

This successfully aligns tab-text and tab-icon horizontally without disturbing the Tabs/TabPanel functionality.这成功地水平对齐选项卡文本和选项卡图标,而不会干扰选项卡/TabPanel 功能。

The "Put Everything inside label" Way “把所有东西都放在标签里”的方式

<Tab label= {<div><HomeIcon style = { {verticalAlign : 'middle'} } /> Home </div>}/>

Source 资源

CSS Way CSS方式

Just add this to the CSS attached to your tabs component.只需将其添加到附加到选项卡组件的 CSS 中。

.MuiTab-wrapper {
  flex-direction: row !important;
}

Don't forget to add '!important', as we are overriding a predefined class .MuiTab-wrapper provided by mat-UI, so may not work after a reload without '!important'.不要忘记添加 '!important',因为我们正在覆盖 mat-UI 提供的预定义类.MuiTab-wrapper ,因此在没有 '!important' 的情况下重新加载后可能无法工作。

As a side note,作为旁注,

If you enclose Icons and tabs within a div and add some CSS to align both, it works however, you lose the Tabs/TabPanel functionality that material-UI gives out of the box.如果您将图标和选项卡包含在 div 中并添加一些 CSS 以对齐两者,那么它可以工作,但是您将失去 Material-UI 开箱即用的 Tabs/TabPanel 功能。

If you are not bothered about the functionality, you can try this.如果您不介意该功能,可以试试这个。

<div style={{display:'flex',alignItems:'center'}}>
       <HomeIcon/>
       <Tab label="Home" />
</div>

Hope this helps!希望这可以帮助!

Change the line 331 to:将第 331 行更改为:

icon={<PhoneIcon style={{ display: "inline-block", marginBottom:"-10px" }} />}

It's because the svg has a display of block, and it's pushing the text underneath.这是因为 svg 有一个块的显示,它把文本推到下面。 You can play with margins there and position it wherever you like.您可以在此处使用边距并将其放置在您喜欢的任何位置。

<Tab label={<><div>Some other label<Icon/></div></>}/>

尝试内联块

display: inline-block;

Change :改变 :

alignment: {
    display: "block"
    //  color: 'red'
  }

to :至 :

alignment: {
    display: "flex",
    flexDirection: "row"
    //  color: 'red'
  }

Just tried.刚试过。 It works.有用。

Flex layout handles it without much pain across all browsers!! Flex 布局在所有浏览器上都可以轻松处理它!

EDIT:编辑:

Updated Fiddle with width of tabs : https://codesandbox.io/s/82ynv5qwy9更新了标签宽度的小提琴https ://codesandbox.io/s/82ynv5qwy9

Changes:变化:

1. 1.

classes={{
              indicator: classes.tabsIndicator,
              scrollButtons: { display: "flex", flex: 1 }
            }}

2. 2.

  tabRoot: {
    textTransform: "initial",
    width: "stretch",
    display: "flex",
    flex: 1,

If anyone is searching for the solution to this, the easiest/cleanest way for MUI v5+ is to use the iconPosition attribute of Tab.如果有人正在寻找解决方案,MUI v5+ 最简单/最干净的方法是使用 Tab 的 iconPosition 属性。 See https://mui.com/material-ui/react-tabs/#icon-positionhttps://mui.com/material-ui/react-tabs/#icon-position

Sample code from the link posted here for convenience:为方便起见,此处发布的链接中的示例代码:

<Tabs
  value={value}
  onChange={handleChange}
  aria-label="icon position tabs example"
>
  <Tab icon={<PhoneIcon />} label="top" />
  <Tab icon={<PhoneMissedIcon />} iconPosition="start" label="start" />
  <Tab icon={<FavoriteIcon />} iconPosition="end" label="end" />
  <Tab icon={<PersonPinIcon />} iconPosition="bottom" label="bottom" />
</Tabs>

Both "start" and "end" options puts the icon on the same line as the text in the label. “开始”和“结束”选项都将图标与标签中的文本放在同一行。

This is the change that made it possible: https://github.com/mui/material-ui/pull/28764这是使之成为可能的更改: https ://github.com/mui/material-ui/pull/28764

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM