How can I style a wrapper View without targeting the children? - react-native

Sorry I know this probably is a stupid question but I'm new to React Native and I can't seem to find a solution to this looking it up. Thank you in advance for your help.
I'm trying to give the HeadingWrapper a box-shadow but it seems to give the shadow to the inner contents. Here is my code that produces this result:
import React, { Component } from 'react';
import styled from 'styled-components';
import H1 from "../common/headings";
import LeftArrow from "../assets/LeftArrow.png";
const MainWrapper = styled.View`
box-shadow: 5px 5px 5px #000000;
`;
const HeadingWrapper = styled.View`
padding: 60px 10px 0 10px;
height: 60px;
flex: 1;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
`;
const ScrollArea = styled.ScrollView`
`;
const Img = styled.Image`
`;
class LandingPage extends Component {
render() {
return (
<MainWrapper style={styles.mainWrapper}>
<HeadingWrapper>
<Img style={styles.leftArrow} source={LeftArrow}/>
<H1>Chats</H1>
</HeadingWrapper>
<ScrollArea>
</ScrollArea>
</MainWrapper>
);
}
}
headings.js:
import styled from 'styled-components';
import React from "react";
const InnerH1 = styled.Text`
font-size: 34px;
font-weight: bold;
`;
const H1Container = styled.View`
flex: 1;
`;
const H1 = (props) => {
return (
<H1Container>
<InnerH1>{props.children}</InnerH1>
</H1Container>
)
};
export default H1;

Currently, the background color of HeadingWrapper is transparent as no background color is currently specified.
To create the 'box-shadow', all you need to do is give HeadingWrapper a background color of which is the same as your background.
Per the screenshot provided, the background color is white. Thus, you can use white as the background color for HeadingWrapper.
For inline style, you can use the below code:
<HeadingWrapper style={{ backgroundColor: 'white' }}>
<Img style={styles.leftArrow} source={LeftArrow}/>
<H1>Chats</H1>
</HeadingWrapper>

Related

How can I use Pressable inside Animated in React native?

I applied Animated.View to the Card component and ScrollView to the CardMain component. And pressable is applied to the child component, called ModeContainer. In ModeContainer, changemodefunc is executed when onPress is triggered.
When I run in emulator and press ModeContainer(Pressable), onPress runs fine, changemodefunc works fine. However, problem is if I connect it to a real device and run it, it will not run even if I press ModeContainer(Pressable). How do we solve this problem?
i'm using window and Emulator
this is my code
import {
Animated,
View,
ScrollView,
} from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
const Container = styled(LinearGradient)`
flex: 1;
justify-content: center;
align-items: center;
`;
const SecondContainer = styled.View`
flex: 9.1;
`;
const Card = styled(Animated.createAnimatedComponent(View))`
flex: 1;
`;
const CardWapper = styled(LinearGradient)`
flex: 1;
`;
const CardMain = styled(ScrollView)`
flex: 9;
`;
const ModeWrapper = styled.View`
flex:1;
`;
const ModeContainer = styled.Pressable`
`;
const IconCon = styled.Pressable`
width: 50px;
`;
<Container
>
<SecondContainer
>
<Card
style={{
transform: [...POSITION.getTranslateTransform()],
}}
>
<CardWapper>
<CardMain>
<ModeWrapper>
(
<ModeContainer
onPress={() => changemodefunc('black')}>
<IconCon
>
</IconCon>
</ModeContainer>
)
</ModeWrapper>
</CardMain>
</CardWapper>
</Card>
</SecondContainer>
</Container>

How can I use my spritesheet of icons in react native?

How can I properly use sprite sheets in react-native? Or do people typically load icons separately and not use sprite sheets? Mind you this is not sprites for animation, just pure icons.
For example here is my sheet...
And here is the css for one icon...
.Sprite {
background-image: url(./assets/images/spritesheet.png);
background-repeat: no-repeat;
display: inline-block;
}
.IconMenu30px {
width: 30px;
height: 30px;
background-position: -53px -5px;
}
And I tried to translate this into React Native like this...
<Image source={require("../assets/images/spritesheet.png")} style={styles.menuIcon} />
const styles = StyleSheet.create({
menuIcon: {
width: 30,
height: 30,
},
})
But apparently there is no background position attribute in React Native.
Did you try
<ImageBackground />
This might help with your issue

Using react-native and styled-components how can I change a child props?

I need to change a color of a <Text/> that's inside a TouchableHighlight styled-component. Already tried:
const Button = styled.TouchableHighlight`
width: 100%;
justify-content: center;
background-color: #337ab7;
Text {
color: orange;
}
`;
Didn't worked... also this Text it's from react-native, not an styled-component component.
create separate style component for Text
// styled component
const StyledText = styled.Text`
color: orange;
`;
and use StyledText instead of Text in your code

What is the "default" value of a react-native component's flex property?

I have a simple Button Component in my react native app, made with just styled-components.
This is what it looks like:
const ButtonContainer = styled.TouchableOpacity`
border-radius: 12px;
margin: 6px;
flex-basis: ${props => props.flexBasis || "20%"};
background: ${props => props.backgroundColor || "orange"};
justify-content: center;
align-items: center;
flex: ${props => props.flex || 0};
`;
So when I pass in a flex prop like this <ButtonContainer flex="1" /> I want it to fill the available space. It works as expected.
But when I don't pass in the flex prop, I want it to behave like I never set it. (Don't take up the whole available space).
This does not work. It's still taking up all the available space.
When I get rid of the line flex: ${props => props.flex || 0}; all together it's working, but I want to set it to flex: 1 sometimes (reusable component)
So what is the default flex setting in a react-native component?
I already tried undefined, null and -1 but no effect at all.
setting flex: none worked for me,
so -
const Component = styled.View`
flex: ${props => props.flex || 'none'};
`;

Change the color of a TextInput placeholder with Styled Components in React Native

How do you set the color of TextInput its placeholder with Styled Components in React Native?
I tried the following without any luck:
1.
const Input = styled.TextInput`
border: 1px solid green;
display: block;
margin: 0 0 1em;
::placeholder {
color: green;
}
`
2.
const Input = styled.TextInput`
border: 1px solid green;
display: block;
margin: 0 0 1em;
&::placeholder {
color: green;
}
`
The best way to make this:
export const Input = styled.TextInput.attrs({
placeholderTextColor: "red"
})`
background-color: "#000";
`;
you can try:
export const NewTodo = styled.input`
padding: 16px 16px 16px 60px;
font-weight: 300;
font-size: 15px;
::placeholder,
::-webkit-input-placeholder {
color: red;
}
:-ms-input-placeholder {
color: red;
}
`;
How do I style an input placeholder with Styled Components?
Adding on to #Fernando Pascoal Gomes's answer, if you wish to access the theme object, consider passing a function to .attrs() that returns an object that the component inherits for its props.
For your case, TextInput accepts a placeholderTextColor prop, so it might look like this:
const Input = styled.TextInput.attrs((props) => ({
placeholderTextColor: props.theme.palette.placeholderColor,
}))`
background-color: #fff;
color: #000;
...
`
You cannot style the placeholder color with styled-components directly, but you can pass the placeholderTextColor property to your styled Textinput.
Example:
const Input = styled.TextInput`
border: 1px solid green;
display: block;
margin: 0 0 1em;
`
and then inside your render function:
<Input placeholder="hello" placeholderTextColor="green" />
Output:
Working example:
https://snack.expo.io/rybp-nKaE
To change the textinputs placeholder's text color user prperty
"placeholderTextColor"
eg
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
onChangeText={(text) => this.setState({text})}
placeholder = 'Enter text'
placeholderTextColor = 'red'
value={this.state.text}
/>