Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<Fade> in material-ui just disables the visibility of the component . How to get the fade effect and actually hide the component ?

I am using component of material-ui from material-ui.

<Fade in={!randomizeFlag}>
              <Grid>
                <FormControlLabel control={<Switch onChange={this.handleStartValueFlag} ></Switch>} label="Start Value"></FormControlLabel>
                <TextField  type="number" label="Starting Value" value={startValue} onChange={this.handleStartValueChange} />
              </Grid>
</Fade>

I want to completely hide the element Grid when the component fades out but it only disables the visibility of the component and takes up the same space( looks empty) in the DOM .How do i make the element hide after fading it using <Fade>

like image 762
Natesh bhat Avatar asked Oct 04 '18 01:10

Natesh bhat


People also ask

What is fade in material UI?

<Fade> in material-ui just disables the visibility of the component .

How do I hide components in material UI?

Material UI Hidden component is used to change the visibility of any other component. We can use this component to hide a component even on different breakpoints. For example, if you have one sidebar that you want to hide on small screen devices, you can use this component with breakpoints.

What is fade in effect?

The Fade In/Fade Out behavior lets you dissolve into and out of any object by ramping the opacity of the object from 0 percent to 100 percent at the start, and then back to 0 percent at the end. You can eliminate the fade-in or fade-out effect by setting the duration of the Fade In Time or Fade Out Time to 0 frames.


2 Answers

enclose your custom components in divs or some html element and it should work just fine

<Fade in={!randomizeFlag}>
     <div>
                 <Grid>
                    <FormControlLabel control={<Switch onChange= 
                     {this.handleStartValueFlag} ></Switch>} label="Start Value"> 
                     </FormControlLabel>
                    <TextField  type="number" label="Starting Value" value= 
                     {startValue} 
                    onChange={this.handleStartValueChange} />
                 </Grid>
     </div>
</Fade>
like image 182
Rogelio Avatar answered Oct 07 '22 09:10

Rogelio


Hiding element completely will introduce complexity since now you have to also unhide the element when Fade begins. That may interfere with fade effect.

With that said, you have few options:

  • Use CSS attribute selectors to apply styles:

    div[opacity=0] {
      display: none;
    }
    
    div[opacity=1] {
      display: block;
    }
    
  • Use react-transition directly (since that is what Fade uses): https://reactcommunity.org/react-transition-group/transition

    import Transition from 'react-transition-group/Transition';
    
    const duration = 300;
    
    const defaultStyle = {
        transition: `opacity ${duration}ms ease-in-out`,
        opacity: 0,
    }
    
    const transitionStyles = {
        entering: { opacity: 0, display: 'none' },
        entered:  { opacity: 1 , display: 'block'},
        exited:  { opacity: 0, display: 'none'},
    };
    
    const Fade = ({ in: inProp }) => (
        <Transition in={inProp} timeout={duration}>
            {(state) => (
            <div style={{
                ...defaultStyle,
                ...transitionStyles[state]
            }}>
                I'm a fade Transition!
            </div>
            )}
        </Transition>
    );
    
  • Use Fade and pass handlers to Transition, like onExited and set desired states in there. Fade will simply pass extra props to root Transition element so this may work. The only caveat is that you'd be triggering a setState or similar post render phase which can get tricky.

like image 37
Mrchief Avatar answered Oct 07 '22 09:10

Mrchief