Search code examples
reactjsmaterial-uireact-bootstrap

How to hide label text in material-ui on a mobile viewport?


I'm working on fixing some responsive design issues with a sidebar component in React, and one problem with it is that the label text on the tabs gets wicked scrunched up on smaller screens. I want to hide this text but I can't seem to find a good way to do it.

I've tried replacing the label text with a div that is hidden via bootstrap on small/x-small screens, but that doesn't work

<Tab
  icon={<Icon className="material-icons geometry">category</Icon>}
  label={<div className=".hidden-xs .hidden-sm">GEOMETRY</div>}
  value='a' />

Ideally this text should go away on smaller screens but it just acts as though I hadn't put anything there at all. In fact, any bootstrap classes I try to add to that div seem to have no effect. How might I go about fixing this?


Solution

  • If you are using Material UI, you can use Hidden tag to hide label based on mobile viewport. When you use tag 'Hidden' with prop xsDown, the label will be hidden at or below xs breakpoint.

    <Tab 
       icon={<CategoryIcon ></CategoryIcon>}
       label={<Hidden xsDown>GEOMETRY</Hidden>}>
    </Tab>
    

    https://codesandbox.io/s/wispy-bird-vftel?fontsize=14