styled-components icon indicating copy to clipboard operation
styled-components copied to clipboard

Feature request: React Native style multiple props

Open pettomartino opened this issue 3 years ago • 2 comments

What will it allow you to do that you can't do today?

Add styles to multiple props or target a different prop (other than style)

Currently in react-native there is no way to target children element, so the standard approach is to create multiple props say you have a input field

<MyFancyInputField containerStyle={} labelStyle={} style={} />

or <ScrollView contentContainerStyle={} />

Option 1

const MyScrollView = styles.ScrollView.withConfig({target: 'contentContainerStyle'})`
  background-color: red;
`

Option 2 (allows multiple props)

const MyFancyInputField = style(InputField)({
  containerStyle: {
    backgroundColor: 'red'
  },
  labelStyle: {
    color: 'blue'
  }
})

Option 3 - Ideal in my option

const contentContainerStyle = css`
  background-color: red;
`
const MyFancyInputField = styles.ScrollView.attrs(() => ({
  contentContainerStyle: toStyleSheet(contentContainerStyle)
}))``

pettomartino avatar Feb 15 '22 16:02 pettomartino

+1

devalnor avatar Jan 26 '23 13:01 devalnor

i will create a PR for this

import {StyleSheet} from 'react-native';

import transform from 'css-to-react-native';
import {RuleSet} from 'styled-components';

const toStyleSheet = (style: RuleSet<object>) => {
   const rules = style
       .toString()
       .replace(/,/g, '')
       .split(';')
       .reduce((acc, curr) => {
           const [key, value] = curr.split(':');
           if (key && value) {
               const temp = [key.trim(), value.trim()];
               acc.push(temp);
           }
           return acc;
       }, []);

   const transformed = transform(rules);

   return StyleSheet.create({style: transformed}).style;
};

this will make this work

const contentContainerStyle = css`
  background-color: red;
`
const MyFancyInputField = styles.ScrollView.attrs(() => ({
  contentContainerStyle: toStyleSheet(contentContainerStyle)
}))``

krudos avatar Aug 09 '23 13:08 krudos