React Radium Class Style Overrides - css

I am trying to write clean and DRY code styles with Radium.
This is what my button is so far.
/**
*
* Button
*
*/
import React, { PropTypes } from 'react';
import Radium from 'radium';
const styles = {
base: {
borderRadius: '3px',
backgroundColor: '#23cdf4',
fontSize: '13px',
},
primaryBlue: {
backgroundColor: '#23cdf4',
},
primaryBlueBig: {
borderRadius: '8px',
fontSize: '16px',
padding: '14px 28px',
},
primaryRed: {
backgroundColor: '#F99CAC',
},
};
class Button extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<button
style={[styles.base, styles[this.props.type], this.props.style]}
>
{text}
</button>
);
}
}
Button.propTypes = {
text: PropTypes.any,
type: PropTypes.oneOf(['primaryBlue', 'primaryBlueBig']).isRequired,
style: PropTypes.object,
disabled: PropTypes.bool,
};
export default Radium(Button);
Two things I cannot figure out:
How can I extend the background color used by primaryBlue into primaryBlueBig without repeating myself?
How can I change the background color of both blue buttons if the disabled is true?
This is a slimmed down version of what I am currently working with and am trying to get away from having giant if else blocks in the render function. Thanks! ^.^

You can use modifiers.
This is basically what you’re already doing, but with a little more refactoring.
I would define the styles a little differently:
const styles = {
base: {
borderRadius: '3px',
backgroundColor: '#ffffff',
fontSize: '13px',
},
primaryBlue: {
backgroundColor: '#23cdf4',
},
primaryRed: {
backgroundColor: '#F99CAC',
},
big: {
borderRadius: '8px',
fontSize: '16px',
padding: '14px 28px',
},
disabled: {
primaryBlue: {
backgroundColor: '#eeeeee',
},
primaryRed: {
backgroundColor: '#eff0f1',
}
}
};
Then you can have a big and a disabled prop.
return (
<button
style={[
styles.base,
styles[this.props.type],
this.props.big && styles.big,
this.props.disabled && styles.disabled[this.props.type]
]}>
{text}
</button>
);

Related

Styling the DatePicker from MUI

I'm using a DatePicker component from Mui Lab and I am trying to style the Calendar component by adding some border or background color. I used the PaperProps prop for DatePicker but it's not styling it. Trying to understand why I cant just use an SX prop for it
this is the calendar where i want to add a border to
import PropTypes from 'prop-types';
import { TextField } from '#material-ui/core';
import { alpha } from '#material-ui/core/styles';
import DatePicker from '#material-ui/lab/DatePicker';
import { AppBorderStyle } from '../theme';
export const DateField = (props) => {
const {
error,
fullWidth,
helperText,
label,
onChange,
onBlur,
placeholder,
disabled,
value,
name,
...other
} = props;
return (
<DatePicker
PopperProps={{
sx: {
'& .MuiPaper-root': {
backgroundColor: 'red',
border: '1px solid black',
}
}
}}
onChange={onChange}
renderInput={({ InputProps, ...rest }) => (
<TextField
{...rest}
disabled={disabled}
onBlur={onBlur}
name={name}
error={error}
fullWidth={fullWidth}
helperText={helperText}
label={label}
placeholder={placeholder}
sx={{
'& .MuiFilledInput-root': {
backgroundColor: 'background.paper',
borderRadius: 1,
border: AppBorderStyle,
px: 1.5,
py: 0.75,
transition: (theme) => theme.transitions.create([
'border-color',
]),
'&:hover': {
backgroundColor: 'background.paper'
},
'&.Mui-focused': {
backgroundColor: 'background.paper',
boxShadow: (theme) => `${alpha(theme.palette.primary.main,
0.25)} 0 0 0 0.2rem`
},
'& .MuiFilledInput-input': {
fontSize: 14,
height: 'unset',
lineHeight: 1.6,
p: 0
},
'&.Mui-disabled': {
backgroundColor: 'action.disabledBackground',
boxShadow: 'none',
borderColor: alpha('#D6DBE1', 0.5)
}
}
}}
variant="filled"
InputProps={{
disableUnderline: true,
...InputProps
}}
InputLabelProps={{
shrink: true,
sx: {
color: 'text.primary',
fontSize: 14,
fontWeight: 500,
mb: 0.5,
position: 'relative',
transform: 'none'
}
}}
/>
)}
value={value}
{...other}
/>
);
};
DateField.defaultProps = {
disabled: false,
};
DateField.propTypes = {
disabled: PropTypes.bool,
error: PropTypes.bool,
fullWidth: PropTypes.bool,
helperText: PropTypes.string,
label: PropTypes.string,
name: PropTypes.string,
onChange: PropTypes.func.isRequired,
onBlur: PropTypes.func.isRequired,
placeholder: PropTypes.string,
value: PropTypes.instanceOf(Date)
};
I noticed you're using depecrated version of mui as you can check here. For newer versions of material UI components, we import material components from #mui and not from #material-ui.
I'm not sure if the classes are the same in your version or not. But the issue for your case looks like you are targeting the wrong class to add a border. You need to target MuiPickersPopper-root class to change the border.
PopperProps={{
sx: {'&.MuiPickersPopper-root': {border: '4px solid red'},},
}}
I created a solution of DatePicker with the border here. Cheers!

How to change TextField input's focus border using Material-UI theme

I'm trying to create my own theme with Material-Ui v.5.4.0. And I faced with problem. I can't change TextField focused border color and width. I spend hours for research and didn't find working solution. And I started to think is it even possible to do that in theme? But it's not logical.
My current theme code:
import { createTheme } from '#mui/material/styles'
// Colors
const blue = '#5A5BD4'
const blueDark = '#4f4fd8'
// Parameters
const buttonsBorderRadius = 20
const buttonPadding = '5px 15px'
const inputBorderRadius = 10
const theme = createTheme({
components: {
// Buttons
MuiButton: {
variants: [
// Blue button
{
props: { variant: 'blueButton' },
style: {
backgroundColor: blue,
color: '#ffffff',
borderRadius: buttonsBorderRadius,
textTransform: 'none',
padding: buttonPadding,
'&:hover': {
backgroundColor: blueDark
}
}
},
// Transparent button
{
props: { variant: 'transparentButton' },
style: {
color: blue,
borderRadius: buttonsBorderRadius,
textTransform: 'none',
padding: buttonPadding
}
}
]
},
// Inputs
MuiOutlinedInput: {
styleOverrides: {
root: {
borderRadius: inputBorderRadius,
'& fieldset': {
border: `1px solid ${blue}`
}
},
focus: {
border: `1px solid ${blueDark}`
}
}
}
}
})
export default theme
My input code:
<TextField
size='small'
variant='outlined'
label={t('paslelbimo_data_nuo')}
type='date'
InputLabelProps={{
shrink: true
}}
fullWidth
value={publicationDateFrom}
onChange={(e) => setPublicationDateFrom(e.target.value)}
/>
Since I wasn't able to tell exactly what your desired effect was on focus vs not focused, I decided to just create a generic example, with overly dramatic styling, that may be useful to modify for your needs:
Essentially, I'm just overriding .MuiOutlinedInput-notchedOutline for both the focused an unfocused states:
const theme = createTheme({
components: {
// Inputs
MuiOutlinedInput: {
styleOverrides: {
root: {
...
"& .MuiOutlinedInput-notchedOutline": {
border: `5px solid green`,
},
"&.Mui-focused": {
"& .MuiOutlinedInput-notchedOutline": {
border: `5px dotted red`,
},
}
},
}
}
}
});
Working example CodeSandbox: https://codesandbox.io/s/customstyles-material-demo-forked-uri26?file=/theme.js:84-531

Overriding existing Mui classes using makeStyles

So, this can be a very minute implementation issue on my end I think. I used to keep stylesheets completely separate from my component.js file but lately, I started to see some benefits of writing styles inside the component with styled components, emotions, make styles, etc. I tried it on a dummy project and started to implement it out on some components on an app I am working on right now. The reasons were primarily more manageable, dev-experience, and it worked for me.
But then I found myself in this issue of overriding existing mui styles.
For example, I am using material-ui to make this accordion component. And there is a nested property
.MuiAccordionSummary-root.Mui-expanded {
min-height: 64px;
}
To edit this is my sass file would be very easy but how do I make it work with make styles?
My current styles look like this
const useStyles = makeStyles((theme) => ({
root: {
borderBottom: '0.5px solid',
borderBottomColor: theme.palette.primary.main,
marginBottom: '16px',
maxHeight: '35px',
},
summary: {
backgroundColor: theme.palette.complimentory.main,
borderBottom: '0.5px solid',
borderBottomColor: theme.palette.primary.main,
maxHeight: '35px',
height: '35px',
},
title: {
fontFamily: 'Helvetica',
fontStyle: 'normal',
fontWeight: 'normal',
fontSize: '14px',
lineHeight: '14px',
letterSpacing: '0.266667px',
textTransform: 'uppercase',
},
content: {
marginTop: '15px',
fontSize: '14px',
fontStyle: 'normal',
lineHeight: '16.8px',
letterSpacing: '0.375px',
fontWeight: '400',
fontFamily: 'Helvetica',
textAlign: 'left',
},
}))
I am struggling to target it inside makestyles, I think it must be possible, I just am having a hard time as I am not used to it.
You should use <StyledEngineProvider injectFirst> at the root of you project
import { render } from "react-dom";
import { StyledEngineProvider } from "#mui/material";
import App from "./App";
const rootElement = document.getElementById("root");
render(
<StyledEngineProvider injectFirst>
<App />{" "}
</StyledEngineProvider>,
rootElement
);
import * as React from "react";
import AppBar from "#mui/material/AppBar";
import Box from "#mui/material/Box";
import Toolbar from "#mui/material/Toolbar";
import Typography from "#mui/material/Typography";
import Button from "#mui/material/Button";
import IconButton from "#mui/material/IconButton";
import MenuIcon from "#mui/icons-material/Menu";
import makeStyles from "#mui/styles/makeStyles";
const useStyles = makeStyles((theme) => {
return {
root: {
backgroundColor: "red",
color: "pink"
}
};
});
export default function App() {
const classes = useStyles();
return (
<Box sx={{ flexGrow: 1 }}>
<AppBar position="static" className={classes.root}>
<Toolbar>
<IconButton
size="large"
edge="start"
color="inherit"
aria-label="menu"
sx={{ mr: 2 }}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
</Box>
);
}
Working demo here https://codesandbox.io/s/wizardly-stonebraker-qnh3t
I'll use the AccordionSummary as an example on how to override existing styles. The Accordion Summary has 6 CSS rules that can be overridden: root, expanded, focused, disabled, content and expandIcon. You can see this at the CSS section of the AccordionSummary API page in Material UI's official documents. Each of these correspond to a Material UI class for the respective component e.g. root for the AccordionSummary corresponds to the class .MuiAccordionSummary-root. To override these styles, you do it via overriding styles with classes.
Using your example:
Create the class name and add the styling in makeStyles. I'll create one called summaryExpanded and will include the styling minHeight: '64px'.
const useStyles = makeStyles((theme) => ({
root: {
borderBottom: '0.5px solid',
borderBottomColor: theme.palette.primary.main,
marginBottom: '16px',
maxHeight: '35px',
},
summary: {
backgroundColor: theme.palette.complimentory.main,
borderBottom: '0.5px solid',
borderBottomColor: theme.palette.primary.main,
maxHeight: '35px',
height: '35px',
},
summaryExpanded: {
minHeight: '64px',
},
title: {
fontFamily: 'Helvetica',
fontStyle: 'normal',
fontWeight: 'normal',
fontSize: '14px',
lineHeight: '14px',
letterSpacing: '0.266667px',
textTransform: 'uppercase',
},
content: {
marginTop: '15px',
fontSize: '14px',
fontStyle: 'normal',
lineHeight: '16.8px',
letterSpacing: '0.375px',
fontWeight: '400',
fontFamily: 'Helvetica',
textAlign: 'left',
},
}))
Override the style in the AccordionSummary component by accessing the relevant rule name and assigning your custom class. In this case, it is expanded, as we are looking to override .Mui-expanded in the AccordionSummary component.
const classes = useStyles();
<Accordion>
<AccordionSummary
classes: {{
expanded: classes.summaryExpanded,
}}
>
</AccordionSummary>
<AccordionDetail>
</AccordionDetail>
</Accordion>
You can do something like this - play around override properties.
import { Overrides } from "#material-ui/core/styles/overrides";
const overrides: Overrides = {
MuiExpansionPanelSummary: {
root: {
minHeight: "3.25rem",
"&$expanded": {
minHeight: "3.25rem",
},
},
content: {
margin: "0.25rem",
"&$expanded": {
margin: "0.25rem",
},
},
expandIcon: {
padding: "0.5rem",
},
expanded: {},
},
};

Hover style '&:hover:{ }' doesn't work for the button within react component

I am working with react components these days and had some issue in styling one of my buttons with a hover style. Following is the code snippet I used in my component.
const darkTheme = createMuiTheme({
palette: {
type: 'dark',
secondary:amber
},
typography: {
useNextVariants: true,
}
});
const lightTheme = createMuiTheme({
palette: {
type: 'light',
secondary:amber
},
typography: {
useNextVariants: true,
}
});
Above is the custom themes I am using.
class APIWidget extends Widget {
constructor(props) {
super(props);
this.styles = {
button: {
backgroundColor: amber[500],
'&:hover': {
backgroundColor: amber[700],
},
position: 'absolute',
bottom: 20,
right: 20
},
};
}
render() {
return (
<MuiThemeProvider theme={this.props.muiTheme.name === 'dark' ? darkTheme : lightTheme}>
<Button variant="contained" color="secondary" style={this.styles.button}>
<ArrowBack style={{marginRight:15}}/>
Back
</Button>
</MuiThemeProvider>
);
}
}
global.dashboard.registerWidget('APIWidget', APIWidget);
I am using material ui and i am importing the colors from it. My button background color works while the hover color doesn't work.
Can you please point out whether there is any mistake in my code or suggest any method to get the necessary hover color.
Thanks in advance.
You can't leverage pseudo-classes such as :hover within inline styles. Instead the styles need to be in a CSS class. Below is an example (based on your APIWidget class) using withStyles to generate the CSS class.
import {
createMuiTheme,
MuiThemeProvider,
withStyles
} from "#material-ui/core/styles";
import Button from "#material-ui/core/Button";
import amber from "#material-ui/core/colors/amber";
import blue from "#material-ui/core/colors/blue";
import React from "react";
import ArrowBack from "#material-ui/icons/ArrowBack";
const darkTheme = createMuiTheme({
palette: {
type: "dark",
primary: amber,
secondary: blue
}
});
const styles = (theme) => ({
button: {
backgroundColor: "red",
"&:hover": {
backgroundColor: "blue"
},
position: "absolute",
bottom: 20,
right: 20
}
});
class APIWidget extends React.Component {
render() {
return (
<MuiThemeProvider theme={darkTheme}>
<Button
variant="contained"
color="secondary"
className={this.props.classes.button}
startIcon={<ArrowBack />}
>
Back
</Button>
</MuiThemeProvider>
);
}
}
export default withStyles(styles)(APIWidget);

Can I make dynamic styles in React Native?

Say I have a component with a render like this:
<View style={jewelStyle}></View>
Where jewelStyle =
{
borderRadius: 10,
backgroundColor: '#FFEFCC',
width: 20,
height: 20,
},
How could I make the background colour dynamic and randomly assigned? I've tried
{
borderRadius: 10,
backgroundColor: getRandomColor(),
width: 20,
height: 20,
},
But this makes all instances of View have the same colour, I want each one to be unique.
Any tips?
I usually do something along the lines of:
<View style={this.jewelStyle()} />
...
jewelStyle = function(options) {
return {
borderRadius: 12,
background: randomColor(),
}
}
Every time View is rendered, a new style object will be instantiated with a random color associated with it. Of course, this means that the colors will change every time the component is re-rendered, which is perhaps not what you want. Instead, you could do something like this:
var myColor = randomColor()
<View style={jewelStyle(myColor)} />
...
jewelStyle = function(myColor) {
return {
borderRadius: 10,
background: myColor,
}
}
Yes you can and actually, you should use StyleSheet.create to create your styles.
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View
} from 'react-native';
class Header extends Component {
constructor(props){
super(props);
}
render() {
const { title, style } = this.props;
const { header, text } = defaultStyle;
const combineStyles = StyleSheet.flatten([header, style]);
return (
<View style={ combineStyles }>
<Text style={ text }>
{ title }
</Text>
</View>
);
}
}
const defaultStyle = StyleSheet.create({
header: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
height: 60,
paddingTop: 15,
shadowColor: '#000',
shadowOffset: { width: 0, height: 3 },
shadowOpacity: 0.4,
elevation: 2,
position: 'relative'
},
text: {
color: '#0d4220',
fontSize: 16
}
});
export default Header;
And then:
<Header title="HOME" style={ {backgroundColor: '#10f1f0'} } />
If you still want to take advantage of StyleSheet.create and also have dynamic styles, try this out:
const Circle = ({initial}) => {
const initial = user.pending ? user.email[0] : user.firstName[0];
const colorStyles = {
backgroundColor: randomColor()
};
return (
<View style={[styles.circle, colorStyles]}>
<Text style={styles.text}>{initial.toUpperCase()}</Text>
</View>
);
};
const styles = StyleSheet.create({
circle: {
height: 40,
width: 40,
borderRadius: 30,
overflow: 'hidden'
},
text: {
fontSize: 12,
lineHeight: 40,
color: '#fff',
textAlign: 'center'
}
});
Notice how the style property of the View is set as an array that combines your stylesheet with your dynamic styles.
The easiest is mine:
<TextInput
style={[
styles.default,
this.props.singleSourceOfTruth ?
{ backgroundColor: 'black' }
: { backgroundColor: 'white' }
]}/>
Had some issue syntactically.
This worked for me
<Text style={[styles.textStyle,{color: 'red'}]}> Hello </Text>
const styles = StyleSheet.create({
textStyle :{
textAlign: 'center',
fontFamily: 'Arial',
fontSize: 16
}
});
You'll want something like this:
var RandomBgApp = React.createClass({
render: function() {
var getRandomColor = function() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
};
var rows = [
{ name: 'row 1'},
{ name: 'row 2'},
{ name: 'row 3'}
];
var rowNodes = rows.map(function(row) {
return <Text style={{backgroundColor:getRandomColor()}}>{row.name}</Text>
});
return (
<View>
{rowNodes}
</View>
);
}
});
In this example I take the rows array, containing the data for the rows in the component, and map it into an array of Text components. I use inline styles to call the getRandomColor function every time I create a new Text component.
The issue with your code is that you define the style once and therefore getRandomColor only gets called once - when you define the style.
I know this is extremely late, but for anyone still wondering here's an easy solution.
You could just make an array for the styles :
this.state ={
color: "#fff"
}
style={[
styles.jewelstyle, {
backgroundColor: this.state.BGcolor
}
The second will override any original background color as stated in the stylesheet. Then have a function that changes the color:
generateNewColor(){
var randomColor = '#'+Math.floor(Math.random()*16777215).toString(16);
this.setState({BGcolor: randomColor})
}
This will generate a random hex color. Then just call that function whenever and bam, new background color.
Actually, you can write your StyleSheet.create object as a key with function value, it works properly but it has a type issue in TypeScript:
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const SomeComponent = ({ bgColor }) => (
<View style={styles.wrapper(bgColor)}>
<Text style={styles.text}>3333</Text>
</View>
);
const styles = StyleSheet.create({
wrapper: color => ({
flex: 1,
backgroundColor: color,
}),
text: {
color: 'red',
},
});
import React, { useContext, useMemo } from 'react';
import { Text, StyleSheet, View } from 'react-native';
import colors from '../utils/colors';
import ThemeContext from './../contexts/ThemeContext';
export default (props) => {
const { theme } = useContext(ThemeContext);
// Constructing styles for current theme
const styles = useMemo(() => createStyles(theme), [theme]);
return (
<View style={styles.container}>
<Text style={styles.label}>{label}</Text>
</View>
);
};
const createStyles = (theme: AppTheme) =>
StyleSheet.create({
container: { width: '100%', position: 'relative', backgroundColor: colors[theme].background },
label: {
fontSize: 13,
fontWeight: 'bold',
},
});
colors.ts
export type AppTheme = 'dark' | 'light';
const light: Colors = {
background: '#FFFFFF',
onBackground: '#333333',
gray: '#999999',
grayLight: '#DDDDDD',
red: 'red',
};
const dark: Colors = {
background: '#333333',
onBackground: '#EEEEEE',
gray: '#999999',
grayLight: '#DDDDDD',
red: 'red',
};
const colors = {
dark,
light,
primary: '#2E9767',
secondary: '#F6D130',
};
export default colors;
Using object spread operator "..." worked for me:
<View style={{...jewelStyle, ...{'backgroundColor': getRandomColor()}}}></View>
Yes, you can make dynamic styles. You can pass values from Components.
First create StyleSheetFactory.js
import { StyleSheet } from "react-native";
export default class StyleSheetFactory {
static getSheet(backColor) {
return StyleSheet.create({
jewelStyle: {
borderRadius: 10,
backgroundColor: backColor,
width: 20,
height: 20,
}
})
}
}
then use it in your component following way
import React from "react";
import { View } from "react-native";
import StyleSheetFactory from './StyleSheetFactory'
class Main extends React.Component {
getRandomColor = () => {
var letters = "0123456789ABCDEF";
var color = "#";
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
};
render() {
return (
<View>
<View
style={StyleSheetFactory.getSheet(this.getRandomColor()).jewelStyle}
/>
<View
style={StyleSheetFactory.getSheet(this.getRandomColor()).jewelStyle}
/>
<View
style={StyleSheetFactory.getSheet(this.getRandomColor()).jewelStyle}
/>
</View>
);
}
}
<View
style={[styles.categoryItem,{marginTop: index <= numOfColumns-1 ? 10 : 0 }]}
>
I know there are several answers, but i think the best and most simple is using a state "To change" is the state purpose.
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
style: {
backgroundColor: "white"
}
};
}
onPress = function() {
this.setState({style: {backgroundColor: "red"}});
}
render() {
return (
...
<View style={this.state.style}></View>
...
)
}
}
You can bind state value directly to style object. Here is an example:
class Timer extends Component{
constructor(props){
super(props);
this.state = {timer: 0, color: '#FF0000'};
setInterval(() => {
this.setState({timer: this.state.timer + 1, color: this.state.timer % 2 == 0 ? '#FF0000' : '#0000FF'});
}, 1000);
}
render(){
return (
<View>
<Text>Timer:</Text>
<Text style={{backgroundColor: this.state.color}}>{this.state.timer}</Text>
</View>
);
}
}
If you are using a screen with filters for example, and you want to set the background of the filter regarding if it was selected or not, you can do:
<TouchableOpacity style={this.props.venueFilters.includes('Bar')?styles.filterBtnActive:styles.filterBtn} onPress={()=>this.setFilter('Bar')}>
<Text numberOfLines={1}>
Bar
</Text>
</TouchableOpacity>
On which set filter is:
setVenueFilter(filter){
var filters = this.props.venueFilters;
filters.push(filter);
console.log(filters.includes('Bar'), "Inclui Bar");
this.setState(previousState => {
return { updateFilter: !previousState.updateFilter };
});
this.props.setVenueFilter(filters);
}
PS: the function this.props.setVenueFilter(filters) is a redux action, and this.props.venueFilters is a redux state.
You can do something like this.
In your component:
const getRandomColor = () => {
// you can use your component props here.
}
<View style={[styles.jewelStyle, {backgroundColor: getRandomColor()}]} />
Create your style using stylesheet:
const styles = StyleSheet.create({
jewelStyle: {
backgroundColor: 'red',
},
});
If you are following the functional approach of React-Native, you can use a package called dynamic-styles that tries to solve exactly your problem.
// -- theme.js ------------------------------------------------------
// Initialization of a StyleSheet instance called 'styleSheet'
export const styleSheet = createStyleSheet({
theme: /* optional theme */
});
// -- MyComponent.js -----------------------------------------------
// Create dynamic stylesheet that has access
// to the previously specified theme and parameters
const useStyles = styleSheet.create(({theme, params}) => ({
root: /* Dynamic Styles */,
button: /* Dynamic Styles */,
text: /* Dynamic Styles */,
}));
const MyComponent = (props) => {
// Access dynamic styles using the created 'useStyles()' hook
// and specify the corresponding parameters
const { styles } = useStyles({ color: props.color, fontSize: 10 });
return (
<div className={styles.root}>
{/* */}
</div>
);
}
It basically allows you to create dynamic stylesheets
and link them to functional Components using the React hook pattern.
-> Codesandbox
In case someone needs to apply conditions
selectedMenuUI = function(value) {
if(value==this.state.selectedMenu){
return {
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 20,
paddingVertical: 10,
backgroundColor: 'rgba(255,255,255,0.3)',
borderRadius: 5
}
}
return {
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 20,
paddingVertical: 10
}
}
Here is what worked for me:
render() {
const { styleValue } = this.props;
const dynamicStyleUpdatedFromProps = {
height: styleValue,
width: styleValue,
borderRadius: styleValue,
}
return (
<View style={{ ...styles.staticStyleCreatedFromStyleSheet, ...dynamicStyleUpdatedFromProps }} />
);
}
For some reason, this was the only way that mine would update properly.
you can use styled-components for react native it will provide you dynamic styling just like emotion or styled-components for web.
For something relatively simple, you can use this approach:
StyleSheet.create({
item: props.selectedId === item.id ? {
backgroundColor: 'red',
}: null
});

Resources