Display: Inline Equivalent in React Native - css

It seems as though I am having problems with creating a display: inline styling equivalent with flexbox. So far I have achieved the following (where the red and blue lines are governed by the border function to help with styling):
With this code:
var React = require('react-native');
var {
View,
ScrollView,
Image,
StyleSheet,
Text,
TouchableHighlight,
} = React;
//additional libraries
var Parse = require('parse/react-native'); //parse for data storage
Icon = require('react-native-vector-icons/Ionicons'); //vector icons
//dimensions
var Dimensions = require('Dimensions');
var window = Dimensions.get('window');
//dynamic variable components
var ImageButton = require('../common/imageButton');
//var KeywordBox = require('./onboarding/keyword-box');
module.exports = React.createClass({
render: function() {
return (
<View style={[styles.container]}>
<Image
style={styles.bg}
source={require('./img/login_bg1_3x.png')}>
<View style={[styles.header, this.border('red')]}>
<View style={[styles.headerWrapper]} >
<Image
resizeMode={'contain'}
style={[styles.onboardMsg]}
source={require('./img/onboard_msg.png')} >
</Image>
</View>
</View>
<View style={[styles.footer, this.border('blue')]}>
<ScrollView
horizontal={false}
style={styles.footerWrapperNC}
contentContainerStyle={[styles.footerWrapper]}>
{this.renderKeywordBoxes()}
</ScrollView>
</View>
</Image>
</View>
);
},
renderKeywordBoxes: function() {
//renders array of keywords in keyword.js
//and maps them onto custom component keywordbox to show in the onboarding
//component
var Keywords = ['LGBQT', '#BlackLivesMatter', 'Arts', 'Hip-Hop', 'History',
'Politics', 'Comedy', 'Fashion', 'Entrepreneurship', 'Technology', 'Business',
'International', 'Health', 'Trending', 'Music', 'Sports', 'Entertianment'];
return Keywords.map(function(keyword, i) {
return <TouchableHighlight
style={styles.keywordBox}
key={i}
underlayColor={'rgb(176,224,230, 0.6)'} >
<Text style={styles.keywordText} >{keyword}</Text>
</TouchableHighlight>
});
},
//function that helps with laying out flexbox itmes
//takes a color argument to construct border, this is an additional
//style because we dont want to mess up our real styling
border: function(color) {
return {
borderColor: color,
borderWidth: 4,
}
},
});
styles = StyleSheet.create({
header: {
flex: 2,
},
headerWrapper: {
flex: 1,
flexDirection: 'column',
alignItems: 'center',
justifyContent:'space-around',
marginTop: window.height/35,
},
onboardMsg: {
width: (window.width/1.3),
height: (452/1287)*((window.width/1.3)),
},
footer: {
flex: 7,
marginTop: window.height/35,
},
//container style wrapper for scrollview
footerWrapper: {
flexWrap: 'wrap',
alignItems: 'flex-start',
},
//non-container style wrapper for scrollview
footerWrapperNC: {
flexDirection:'row',
},
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
bg: {
flex: 1,
width: window.width,
height: window.height,
},
actionButtonIcon: {
fontSize: 20,
height: 22,
color: 'white',
},
keywordText: {
fontFamily: 'Bebas Neue',
fontSize: 18,
padding: 6,
fontWeight: 'bold',
color: 'white',
letterSpacing: 1.5,
textAlign: 'center'
},
keywordBox: {
backgroundColor: 'transparent',
margin: 3,
borderColor: 'rgb(176,224,230, 0.6)',
borderWidth: 1,
},
});
But I would like to achieve this:
any ideas?
EDIT** ANSWER:
Needed to change the styling to the following:
//container style wrapper for scrollview
footerWrapper: {
flexWrap: 'wrap',
alignItems: 'flex-start',
flexDirection:'row',
},
//non-container style wrapper for scrollview
footerWrapperNC: {
flexDirection:'column',
},
So use of flexDirection in column and row for scrollView works children stay inline

Needed to change the styling to the following:
//container style wrapper for scrollview
footerWrapper: {
flexWrap: 'wrap',
alignItems: 'flex-start',
flexDirection:'row',
},
//non-container style wrapper for scrollview
footerWrapperNC: {
flexDirection:'column',
},

This works for me:
import {View, Text} from 'react-native';
<View style={styles.container}>
<Text>Hello</Text>
</View>
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignSelf: 'flex-start'
}
});

Related

Align items to the left and right in React Native

How do you align items to the left and the right in React Native?
Whatever combination of flexbox properties I try, the two sets of text are stuck next to each other like this:
How do you make it so "Sign In" is on the right side of the screen and Repositories on the left?
Here is my code:
import { View, StyleSheet, Text, ScrollView } from 'react-native';
import Constants from 'expo-constants';
import { Link } from "react-router-native";
const styles = StyleSheet.create({
container: {
paddingTop: Constants.statusBarHeight,
paddingLeft: 10,
paddingBottom: 20,
backgroundColor: 'grey',
},
text: {
fontSize: 20,
color: 'white',
fontWeight: 'bold'
},
linkText: {
color: 'white',
},
nesteddivleft: {
flex: 1,
display: "flex",
justifyContent: "flex-start",
alignItems: "center",
},
nesteddivright: {
flex: 1,
display: "flex",
justifyContent: "flex-end",
alignItems: "center",
},
scrollbar: {
display: 'flex',
justifyContent: 'space-between'
}
});
const AppBar = () => {
return <><View style={styles.container}>
<ScrollView style="scrollview" horizontal>
<View style={styles.nesteddivleft}><Link to="/"><Text style={styles.text}>Repositories</Text></Link></View>
<View style={styles.nesteddivright}><Link to="/signin"><Text style={styles.linkText}>Sign In</Text></Link></View>
</ScrollView>
</View>
</>
};
export default AppBar;
App:
import Main from './src/components/Main'
import { StatusBar } from 'expo-status-bar';
import { NativeRouter } from 'react-router-native';
export default function App() {
return (
<>
<NativeRouter>
<Main/>
</NativeRouter>
<StatusBar style="auto" />
</>
);
}
As you can see, I have tried putting justify-content: space-between on the parent div and that does nothing.
I also tried this solution and it has done nothing: Aligning elements left, center and right in flexbox
import { View, StyleSheet, Text, ScrollView } from 'react-native';
import Constants from 'expo-constants';
import { Link } from "react-router-native";
const styles = StyleSheet.create({
container: {
paddingTop: Constants.statusBarHeight,
paddingLeft: 10,
paddingBottom: 20,
backgroundColor: 'grey',
display: "flex",
justifyContent: "space-between",
minWidth: '100%',
flexDirection: "row"
},
text: {
fontSize: 20,
color: 'white',
fontWeight: 'bold'
},
linkText: {
color: 'white',
},
nesteddivleft: {
},
nesteddivright: {
},
scrollbar: {
}
});
const AppBar = () => {
return <><View style={styles.container}>
<View style={styles.nesteddivleft}><Link to="/"><Text style={styles.text}>Repositories</Text></Link></View>
<View style={styles.nesteddivright}><Link to="/signin"><Text style={styles.linkText}>Sign In</Text></Link></View>
</View>
</>
};
export default AppBar;
Steps to solve issue:
Take out the scrollview component.
set Display to flex, justify Content to space-between, minWidth to 100% AND flex Direction to Row in parent component.
Not optimal as I need the Scrollview component in there as well, I will need to find a solution that allows me to have that component as a child.

I have mentioned flex value to 0.5 but it is not working

I have written following code but my flex under 'titleContainer' is not working.
export const Focus = () => {
return (
<View style={styles.container}>
<View style={styles.titleContainer}>
<Text style={styles.title}>What would you like to focus on?</Text>
<TextInput />
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#43840',
},
titleContainer: {
flex: 0.5,
padding: 16,
justifyContent: 'center',
backgroundColor: '#559974',
},
title: {
color: 'white',
fontWeight: 'bold',
fontSize: 15,
},
});
Please see the screen shot. The green background should cover half of the screen.
Its resolved now!
Actually this component was getting displayed conditionally. There was an issue with that condition. I have fixed that and everything worked.
Flex in RN only take interger value
In case you want titleContainer take 50% size of container by using flex, This is how you can achieve that.
You need another view the take the same flex value with titleContainer, so titleContainer and that dump view will take 50% size of parent view each
export const Focus = () => {
return (
<View style={styles.container}>
<View style={styles.titleContainer}>
<Text style={styles.title}>What would you like to focus on?</Text>
<TextInput />
</View>
<View style={{flex: 1}} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#43840',
},
titleContainer: {
flex: 1,
padding: 16,
justifyContent: 'center',
backgroundColor: '#559974',
},
title: {
color: 'white',
fontWeight: 'bold',
fontSize: 15,
},
});
To use flex property on child tag your parent tag needs to have a display: "flex" property on it.
container: {
display: 'flex',
flexDirection: 'column',
flex: 1,
backgroundColor: '#43840',
},

How to create 3D button with gradient colour - React Native?

In React Native how would you build a button like the below?
I have managed to get this far:
However there is an issue with the border. As you can see there is a gap between the border radius and the linear gradient component. This seems to be an issue caused by using LinearGradient as it does not exist if the Linear Gradient component is replaced with a button component for example.
import React, { useState} from "react";
import {ImageBackground, StyleSheet, View, Text, TouchableOpacity } from "react-native";
import { LinearGradient } from 'expo-linear-gradient';
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "column"
},
marginContainer: {
flex: 1,
flexDirection: "column",
margin:20
},
buttonContainer:{
flex:3,
justifyContent: 'center',
alignContent: "center",
alignItems:"center"
},
modeButton:{
width:300,
height:100,
borderRadius:20,
borderRightWidth: 1,
borderLeftWidth:1,
borderBottomWidth: 14,
borderColor: '#024e51',
elevation:30,
shadowColor: 'rgba(0, 0, 0, 0.4)',
shadowOpacity: 0.8,
elevation: 30,
shadowRadius: 15 ,
shadowOffset : { width: 1, height: 13},
},
pressableArea:{
flexDirection:"row",
justifyContent: 'center',
alignItems: 'center',
width:"100%",
height:"100%"
}
});
function SelectModeScreen() {
return(
<View style={styles.container}>
<View style={styles.marginContainer}>
<View style={[styles.buttonContainer,{ backgroundColor: "darkorange" }]} >
<LinearGradient colors={['#5be9aa', '#09949d']} style={styles.modeButton}>
<TouchableOpacity style={styles.pressableArea} >
</TouchableOpacity>
</ LinearGradient>
</View>
</View>
</View>
)
}```
Here is the Working Example: Expo Snack
import React, { useState } from 'react';
import {
ImageBackground,
StyleSheet,
View,
Text,
TouchableOpacity,
} from 'react-native';
import { LinearGradient } from 'expo-linear-gradient';
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
},
buttonGrad: {
height: 50,
width: 200,
borderRadius: 10,
position: 'absolute',
bottom: 5,
},
buttonParent: {
height: 50,
width: 200,
borderRadius: 10,
backgroundColor: '#024e51',
},
});
function SelectModeScreen() {
return (
<View style={styles.container}>
<TouchableOpacity onPress={() => console.log('btn pressed')}>
<View style={styles.buttonParent}>
<LinearGradient
colors={['#5be9aa', '#09949d']}
style={styles.buttonGrad}></LinearGradient>
</View>
</TouchableOpacity>
</View>
);
}
export default SelectModeScreen;
if you want to add shadow around the button add below style properties to above answer buttonParent style
...
buttonParent: {
...
shadowColor: 'black',
shadowOpacity: 1,
shadowOffset: {width: 2, height: 2},
shadowRadius: 10,
elevation: 8,
}
And by this properties you can make any type of elevated view

How to set different heights on columns with flex-direction: 'row'

I'm trying to set container with two columns with different heights.
I have my container which has flex-direction: 'row' and flexWrap: 'wrap'.
Now I want to display there elements (only two can fit into container width) with different heights. I know that this can't be done with just flexbox, because rows has to be the same height when wrapping. So my question is: can this be done in React Native when Flexbox is default display type?
Code:
Container:
import React from 'react';
import {StyleSheet, View} from 'react-native';
import Reminder from './../components/reminders/Reminder';
const reminders = require('../../public/reminders.json');
const RemindersPage = () => {
return (
<View style={styles.reminders}>
{reminders.map((reminder) => {
return <Reminder key={reminder.id} reminder={reminder} />;
})}
</View>
);
};
const styles = StyleSheet.create({
reminders: {
flex: 1,
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'space-between',
},
});
export default RemindersPage;
Element:
import React from 'react';
import {StyleSheet, Text, View, Image} from 'react-native';
const Reminder = ({reminder}) => {
let source = require('./../../../public/forest.jpg');
return (
<View style={styles.reminder}>
<View style={styles.imageContainer}>
<Image style={styles.image} source={source} />
</View>
<View style={styles.reminders}>
{reminder.alerts.map((alert) => {
return (
<View style={styles.alert} key={alert.time}>
<Text style={styles.time}>{alert.time}</Text>
</View>
);
})}
</View>
</View>
);
};
const styles = StyleSheet.create({
reminder: {
minWidth: '48%',
height: 'auto',
backgroundColor: '#333',
color: 'white',
padding: 5,
marginVertical: 4,
borderStyle: 'solid',
borderWidth: 1,
borderColor: 'white',
alignSelf: 'flex-start',
},
imageContainer: {
alignItems: 'center',
borderStyle: 'solid',
borderBottomWidth: 1,
borderColor: 'white',
paddingBottom: 8,
},
image: {
width: '100%',
height: 150,
resizeMode: 'cover',
},
alerts: {
flexDirection: 'column',
marginTop: 5,
},
alert: {
flexDirection: 'row',
},
time: {
fontWeight: 'bold',
color: 'white',
textShadowColor: 'black',
textShadowOffset: {width: 0, height: 0},
textShadowRadius: 5,
},
});
export default Reminder;
My goal is that the element with red frame goes up as the arrow is pointing.
It should looks like this:
I don't want to set flex-direction to 'column' beacause I want this page to be scrollable.

What styles I have to write for alignment items (react-native)

I have a component with TextInput and another component which is called CompanyAddress. Each company can have many addresses, therefore I have to have "plus" button.
There are container styles
const AppStyles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#ffffff",
alignItems: "center",
justifyContent: "center"
},
inputStyle: {
height: 40,
width: '80%',
borderColor: '#28c9d9',
textAlign: 'center',
borderWidth: 1,
marginBottom: 20,
fontSize: 16
}
});
there is main component
<KeyboardAvoidingView behavior="padding" style={AppStyles.container}>
<TextInput
autoCompleteType="name"
placeholder="organization"
maxLength={30}
style={AppStyles.inputStyle}
onChangeText={(text) => { this.setState({name: text})} }
/>
<CompanyAddress/>
</KeyboardAvoidingView>
and there is CompanyAddress
class CompanyAddress extends React.Component<any, any> {
render() {
return (
<View style={styles.container}>
<TextInput
autoCompleteType="name"
placeholder="address"
maxLength={30}
style={[AppStyles.inputStyle, styles.textInput]}
/>
<AntDesign style={styles.plus} name="plus" size={24} color="black" />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
width: '100%',
flexDirection: 'row'
},
textInput: {
width: '70%',
},
plus: {
width: '30%'
}
});
but my CompanyAddress now looks like that
but I want to change styles for achieve that
you can add justifyContent:center to the container and for the children alignSelf:Center
ex:
<View style={{justifyContent : center,flexDirection:row}}>
<View style={{alignSelf:Center}}><View>
<View style={{alignSelf:Center}}><View>
</View>

Resources