Skip to content

Commit 4d69f4b

Browse files
Emil Sjolanderfacebook-github-bot
authored andcommitted
Add display:none support to react native
Summary: This diff adds display:none support to React Native. This enables hiding components which still calling their render method and keeping them within the state of your application. This enables preserving state in a component even though the component is not visible. Previously this was often implemented by rendering a component off screen as a work around. See below playground for usage. ``` class Playground extends React.Component { render() { return ( <View style={{width: '100%', height: '100%', flexDirection: 'row', backgroundColor: 'white'}}> <View style={{width: 100, height: 100, display: 'none', backgroundColor: 'red'}}/> <View style={{width: 100, height: 100, backgroundColor: 'blue'}}/> </View> ); } } ``` Reviewed By: astreet Differential Revision: D4611771 fbshipit-source-id: 0dbe0494d989df42994ab9ad5125d47f3233cc5a
1 parent 31f848a commit 4d69f4b

File tree

9 files changed

+33
-0
lines changed

9 files changed

+33
-0
lines changed

Libraries/StyleSheet/LayoutPropTypes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ var ReactPropTypes = require('React').PropTypes;
2727
* algorithm and affect the positioning and sizing of views.
2828
*/
2929
var LayoutPropTypes = {
30+
/** `display` sets the display type of this component.
31+
*
32+
* It works similarly to `display` in CSS, but only support 'flex' and 'none'.
33+
* 'flex' is the default.
34+
*/
35+
display: ReactPropTypes.string,
36+
3037
/** `width` sets the width of this component.
3138
*
3239
* It works similarly to `width` in CSS, but in React Native you

React/Base/RCTConvert.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ typedef id NSPropertyList;
112112

113113
typedef BOOL css_backface_visibility_t;
114114
+ (YGOverflow)YGOverflow:(id)json;
115+
+ (YGDisplay)YGDisplay:(id)json;
115116
+ (css_backface_visibility_t)css_backface_visibility_t:(id)json;
116117
+ (YGFlexDirection)YGFlexDirection:(id)json;
117118
+ (YGJustify)YGJustify:(id)json;

React/Base/RCTConvert.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,11 @@ + (NSPropertyList)NSPropertyList:(id)json
645645
@"scroll": @(YGOverflowScroll),
646646
}), YGOverflowVisible, intValue)
647647

648+
RCT_ENUM_CONVERTER(YGDisplay, (@{
649+
@"flex": @(YGDisplayFlex),
650+
@"none": @(YGDisplayNone),
651+
}), YGDisplayFlex, intValue)
652+
648653
RCT_ENUM_CONVERTER(YGFlexDirection, (@{
649654
@"row": @(YGFlexDirectionRow),
650655
@"row-reverse": @(YGFlexDirectionRowReverse),

React/Views/RCTShadowView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ typedef void (^RCTApplierBlock)(NSDictionary<NSNumber *, UIView *> *viewRegistry
133133
@property (nonatomic, assign) YGAlign alignContent;
134134
@property (nonatomic, assign) YGPositionType position;
135135
@property (nonatomic, assign) YGWrap flexWrap;
136+
@property (nonatomic, assign) YGDisplay display;
136137

137138
@property (nonatomic, assign) float flexGrow;
138139
@property (nonatomic, assign) float flexShrink;

React/Views/RCTShadowView.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ - (type)getProp \
704704
RCT_STYLE_PROPERTY(Position, position, PositionType, YGPositionType)
705705
RCT_STYLE_PROPERTY(FlexWrap, flexWrap, FlexWrap, YGWrap)
706706
RCT_STYLE_PROPERTY(Overflow, overflow, Overflow, YGOverflow)
707+
RCT_STYLE_PROPERTY(Display, display, Display, YGDisplay)
707708
RCT_STYLE_PROPERTY(Direction, direction, Direction, YGDirection)
708709
RCT_STYLE_PROPERTY(AspectRatio, aspectRatio, AspectRatio, float)
709710

React/Views/RCTViewManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ - (RCTViewManagerUIBlock)uiBlockToAmendWithShadowViewRegistry:(__unused NSDictio
310310
RCT_EXPORT_SHADOW_PROPERTY(aspectRatio, float)
311311

312312
RCT_EXPORT_SHADOW_PROPERTY(overflow, YGOverflow)
313+
RCT_EXPORT_SHADOW_PROPERTY(display, YGDisplay)
313314

314315
RCT_EXPORT_SHADOW_PROPERTY(onLayout, RCTDirectEventBlock)
315316

ReactAndroid/src/main/java/com/facebook/react/uimanager/LayoutShadowNode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import com.facebook.yoga.YogaAlign;
1313
import com.facebook.yoga.YogaConstants;
14+
import com.facebook.yoga.YogaDisplay;
1415
import com.facebook.yoga.YogaFlexDirection;
1516
import com.facebook.yoga.YogaJustify;
1617
import com.facebook.yoga.YogaOverflow;
@@ -311,6 +312,15 @@ public void setOverflow(@Nullable String overflow) {
311312
overflow.toUpperCase(Locale.US).replace("-", "_")));
312313
}
313314

315+
@ReactProp(name = ViewProps.DISPLAY)
316+
public void setDisplay(@Nullable String display) {
317+
if (isVirtual()) {
318+
return;
319+
}
320+
setDisplay(display == null ? YogaDisplay.FLEX : YogaDisplay.valueOf(
321+
display.toUpperCase(Locale.US).replace("-", "_")));
322+
}
323+
314324
@ReactPropGroup(names = {
315325
ViewProps.MARGIN,
316326
ViewProps.MARGIN_VERTICAL,

ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.ArrayList;
1616

1717
import com.facebook.yoga.YogaAlign;
18+
import com.facebook.yoga.YogaDisplay;
1819
import com.facebook.yoga.YogaEdge;
1920
import com.facebook.yoga.YogaConstants;
2021
import com.facebook.yoga.YogaDirection;
@@ -662,6 +663,10 @@ public void setOverflow(YogaOverflow overflow) {
662663
mYogaNode.setOverflow(overflow);
663664
}
664665

666+
public void setDisplay(YogaDisplay display) {
667+
mYogaNode.setDisplay(display);
668+
}
669+
665670
public void setMargin(int spacingType, float margin) {
666671
mYogaNode.setMargin(YogaEdge.fromInt(spacingType), margin);
667672
}

ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewProps.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class ViewProps {
2727
public static final String ALIGN_SELF = "alignSelf";
2828
public static final String ALIGN_CONTENT = "alignContent";
2929
public static final String OVERFLOW = "overflow";
30+
public static final String DISPLAY = "display";
3031
public static final String BOTTOM = "bottom";
3132
public static final String COLLAPSABLE = "collapsable";
3233
public static final String FLEX = "flex";
@@ -124,6 +125,7 @@ public class ViewProps {
124125
JUSTIFY_CONTENT,
125126
OVERFLOW,
126127
ALIGN_CONTENT,
128+
DISPLAY,
127129

128130
/* position */
129131
POSITION,

0 commit comments

Comments
 (0)