1

In the React app I'm building, I would like a button to be hidden until certain conditions are met. Then I'd to animate it in from underneath another element. At the moment I am always rendering the button and adding a class of "hidden" when I'd like it hidden. The SASS looking something like this:

button {
  display: inline-block;
  width: 100%;
  height: 63px;
  transition: height 250ms ease-in-out;
  font-size: 24px;

  &.hidden {
    height: 0;
  }
}

But when the button hides, the element gets smaller, but the text is still visible. Similar to this: https://jsfiddle.net/dtu56e1j/

What am I doing wrong? Or is there a better way to get a button to animate in?

Votemike
  • 762
  • 1
  • 10
  • 28
  • Couple caveats here; `height` won't animate on IE, and it only pertains to the parent box the text sits in and not the font inside. You might consider instead [transform: scale()](https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scale) but be leary of overuse of `transform`. – Chris W. Apr 30 '19 at 18:24
  • I had a go with `scaleY()`, but that didn't give me the desired effect either. Thanks for the IE warning. – Votemike Apr 30 '19 at 18:26
  • Got a visual example of what you want? – Chris W. Apr 30 '19 at 18:26
  • Similar to https://davidwalsh.name/demo/css-slide.php , except the pink area would be a button. And the white area wouldn't remain, that would also disappear. – Votemike Apr 30 '19 at 18:28
  • I want to hide the "Button to Hide" https://jsfiddle.net/dtu56e1j/ – Votemike Apr 30 '19 at 19:36
  • On your fiddle example just change `height: 0` to `display: none` if you want it removed; or use the `visibility` property to toggle visible/hidden in the same manner if you just want it to "hide" – Chris W. Apr 30 '19 at 20:17
  • But would that animate nicely? – Votemike Apr 30 '19 at 20:47

2 Answers2

2

IMO the other answers provide working, but complicated solutions to your problem. Simply put, you're missing a single CSS property - overflow: hidden.

I created this StackBlitz to illustrate the point.

However, the only modification necessary to the original code is this:

button {
  overflow: hidden;
  [...]
}

Fiddle to better match use-case: https://jsfiddle.net/smn6xgv2/

Because the button element has some internal padding, setting height: 0 doesn't completely remove the element from the display. To address that issue, we wrap the button inside a div and then animate the height of the div.

Additionally, the div should be left with the default display: block. In the original example, the display: inline-block causes the browser to reserve a minimum height of line-height. More info in this SO question

Vlad274
  • 6,514
  • 2
  • 32
  • 44
0

With a little work you could use translateY to get this done nicely

const el = document.querySelector("#testButton");

setInterval(() => {
  el.classList.toggle("hidden");
}, 2000);
button {
  display: inline-block;
  background-color: darkblue;
  color: white;
  width: 100%;
  height: 63px;
  transition: transform 250ms ease-in-out;
  font-size: 24px;
}

button.hidden {
  transform: translateY(63px); /* or -63px to invert */
}

/* Make a wrapper class so that your button disappears on transform */
.wrapper {
  height: 63px;
  overflow: hidden;
}
<div class="wrapper">
  <button id="testButton">My Button!</button>
</div>
Robbie Milejczak
  • 5,664
  • 3
  • 32
  • 65
  • Sneaky. I'll see if I can get this to work if I can't find an actual hiding solution. – Votemike Apr 30 '19 at 19:31
  • I think this will be the most performant way you can achieve this effect with just CSS alone, though you could also look into using something like `react-transition-group` and conditional rendering https://github.com/reactjs/react-transition-group/tree/v1-stable best of luck I hope you find something that suits your needs – Robbie Milejczak Apr 30 '19 at 19:34
  • I was looking at `react-transition-group` but it seems a lot to do for a simple animation – Votemike Apr 30 '19 at 19:37