react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[Idea: Style] display:none and visibility:hidden

Open ide opened this issue 11 years ago • 23 comments

I've been using onLayout a bit and have been thinking about these two CSS concepts in RN. Here's what each would do and how they fit into React:

display:none: the React component is rendered in the component hierarchy so it can retain state, but no shadow views are created for it. No layout calculations are performed nor are UIViews created. Asking for its measurements gives a zero rect, just like the DOM.

visibility:hidden: shadow views are created for the React component and layout is calculated, but no UIViews are created, which is how this differs from opacity:0.

I haven't measured any performance problems so these aren't hi-pri for me, but could be good to anticipate as more view optimizations are written. Display:none (or maybe visibility:hidden if layout is needed) in particular seems like a great fit for ListView cells since off-screen cells could retain component state and not incur any native overhead.

ide avatar May 18 '15 02:05 ide

@ide what's the use case for needing visibility:hidden that display:none or opacity:0 wouldn't cover? I guess visibility:hidden wouldn't block touch events?

Purely curious. I've found myself wanting display:none several times. I also like that it increases parity with the web.

lelandrichardson avatar May 19 '15 01:05 lelandrichardson

display:none would be very useful. In some case I had to use transfom:translate() to move the element out of screen to hide it.

magicismight avatar May 19 '15 01:05 magicismight

@lelandrichardson: opacity:0 would still create UIViews and acts like uiView.alpha = 0 or uiView.hidden = YES. We may want to override the hit testing so that fully transparent views still receive touches though this is not the case currently.

Meanwhile visibility:hidden would not create UIViews and would not receive touch events.

ide avatar May 19 '15 01:05 ide

See also https://github.com/facebook/react-native/issues/638

matth3wga0 avatar May 19 '15 02:05 matth3wga0

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

ghost avatar Aug 05 '15 18:08 ghost

Still think this is good. For now have been making do with opacity:0 but in the long run I think we'll want this as a primitive on which optimizations can be written.

ide avatar Aug 05 '15 22:08 ide

It's worth noting here that on Web and Android, fully transparent views still receive touches - iOS is the odd one out.

brentvatne avatar Sep 13 '15 22:09 brentvatne

there's only one way to implement this, just set the container view's height to 0.

JohnnieFucker avatar Sep 24 '15 08:09 JohnnieFucker

I would appreciate this, I've been doing something similar to others in this thread, setting width and height to 0 when I want an element to not be displayed.

ryanmcdermott avatar Oct 12 '15 20:10 ryanmcdermott

Hi guys ! Any update on this since from 0.15.0, views with opacity:0 receive touch ?

ahanriat avatar Nov 24 '15 11:11 ahanriat

@ahanriat Yes, they do, both on Android and iOS.

satya164 avatar Nov 24 '15 11:11 satya164

Use pointerEvents to ignore touches. Unfortunately on Android touches don't go through overlapping absolutely positioned views though.

ide avatar Nov 24 '15 16:11 ide

@satya164 & @ide Thanks ! I now use pointerEvents, and I think it's better this way ;) I assume returning false instead of the component will do the same as a css display: none on the component itself

ahanriat avatar Nov 25 '15 09:11 ahanriat

The React component is still mounted when you use CSS "display: none". If you return null, the component will be unmounted and its state will be destroyed.

ide avatar Nov 25 '15 14:11 ide

Hi there! This issue is being closed because it has been inactive for a while.

But don't worry, it will live on with ProductPains! Check out its new home: https://productpains.com/post/react-native/idea-style-displaynone-and-visibilityhidden

ProductPains helps the community prioritize the most important issues thanks to its voting feature. It is easy to use - just login with GitHub.

Also, if this issue is a bug, please consider sending a PR with a fix. We're a small team and rely on the community for bug fixes of issues that don't affect fb apps.

mkonicek avatar Apr 10 '16 16:04 mkonicek

{height: 0, opacity: 0} worked for me as an alternative to {display: none}

codazoda avatar May 23 '16 18:05 codazoda

There is a bug on android that changing height to 0 doesn't effect real height (i used Animated.Value), so that doesn't work for me. Invisible view still intercept touches.

Moving view from screen (top: Dimensions.get('window').height) helped.

gentlee avatar Jun 03 '16 19:06 gentlee

I need to add css like display:block; Any-one how help me?

uc-hus avatar Dec 28 '16 10:12 uc-hus

You can use my module react-native-display to show/hide components.

sundayhd avatar Jan 28 '17 12:01 sundayhd

https://github.com/facebook/react-native/commit/4d69f4b2d1cf4f2e8265fe5758f28086f1b67500

gre avatar Mar 11 '17 09:03 gre