react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[TextView] [CSS] text-transform support?

Open grabbou opened this issue 11 years ago • 94 comments

Hey, Given recent updates to text-decoration implementation, I was wondering if this would've been in scope of React Native? I know we can pretty easily do this:

<Text>{'something'.toUpperCase()}</Text>

but having universal apps in mind, basic support for upperCase and lowerCase would be nice.

<Text style={{textTransform: 'uppercase'}}>something</Text>

Depending on how Text is rendered, either basic NSString bindings below might be used or plain JS methods:

  • capitalize - NSString -capitalizedString
  • uppercase - NSString -uppercaseString
  • lowercase - NSString -lowercaseString

If the answer to my question is Yes - I am happy to add in this in a PR.

grabbou avatar Jul 22 '15 09:07 grabbou

Not sure I understand the request - what do you want exactly that you can't do with plain JavaScript?

On Jul 22, 2015, at 11:22 AM, Mike Grabowski [email protected] wrote:

Hey, Given recent updates to text-decoration implementation, I was wondering if this would've been in scope of React Native? I know we can pretty easily do this:

{'something'.toUpperCase()} but having universal apps in mind, basic support for upperCase and lowerCase would be nice. If the answer to my question is Yes - I am happy to add in this in a PR.

— Reply to this email directly or view it on GitHub.

sahrens avatar Jul 22 '15 11:07 sahrens

Well, just wondering what are the pros/cons of implementing textTransform so we don't have to use plain JS (it's mainly for the sake of CSS-like approach as I am happy with plain JS anyway). Not a real issue, feel free to close if no point in implementing.

grabbou avatar Jul 22 '15 11:07 grabbou

Our general philosophy on RN is not to provide APIs unless they both provide clear value and can't be reasonably done otherwise. Since you can easily use JS to do this, I don't think we'll provide specific support for it. On Wed, Jul 22, 2015 at 1:10 PM Mike Grabowski [email protected] wrote:

Well, just wondering what are the pros/cons of implementing textTransform so we don't have to use plain JS (it's mainly for the sake of CSS-like approach as I am happy with plain JS anyway). Not a real issue, feel free to close if no point in implementing.

— Reply to this email directly or view it on GitHub https://github.com/facebook/react-native/issues/2088#issuecomment-123668794 .

sahrens avatar Jul 22 '15 12:07 sahrens

Adding on to @sahrens' comment above, if anyone comes across some use cases where there is a significant benefit to doing this in a style rather than with JS, please post them here so we can consider!

brentvatne avatar Jul 22 '15 17:07 brentvatne

+1 strongly in favor of keeping code in user space.

ide avatar Jul 22 '15 18:07 ide

Just stumbled upon this thread from Google; I'd say that adding textTransform support is necessary to separate your concerns. As others have pointed out, you can do this in JS, however what's the point in having a class full of styles and then having to use JS to force caps.

Say a couple of months down the line you don't wish to use caps any more then you've got to trawl through your app removing all the references to it. Having this as a style would alleviate that problem.

mikefrancis avatar Sep 17 '15 14:09 mikefrancis

@brentvatne Just dropping in to echo @mikefrancis's comments, which are right on the money in my experience.

Here's a quick example from a project I'm working on. This is the same screen with two different design options — labels rendered in title case, or labels rendered in small caps.

texttransform

There's two things that make a textTransform property attractive to me in this case. The first is that I think there's a semantic difference between setting the actual content of <Text> to CITY rather than City. If preserving that semantic state is important, then I think this becomes a matter of style.

The second is to @mikefrancis's points around separation of concerns and maintainability. If it's a matter of style, then it makes sense to manage it with CSS.

Here's a sample of the JSX for both of those views:

Title Case:

<View { ...style('field') }>
  <TextInput
    { ...style('input') }
  />
  <Text { ...style('label') }>Address Line 1</Text>
</View>

<View { ...style('field') }>
  <TextInput
    { ...style('input') }
  />
  <Text { ...style('label') }>Address Line 2</Text>
</View>

<View { ...style('field') }>
  <TextInput
    { ...style('input') }
  />
  <Text { ...style('label') }>City</Text>
</View>

<View { ...style('field') }>
  <TextInput
    { ...style('input') }
  />
  <Text { ...style('label') }>State</Text>
</View>

<View { ...style('field') }>
  <TextInput
    { ...style('input') }
  />
  <Text { ...style('label') }>Zipcode</Text>
</View>

And the same code modified for small caps:

<View { ...style('field') }>
  <TextInput
    { ...style('input') }
  />
  <Text { ...style('label.upper') }>{'Address Line 1'.toUpperCase()}</Text>
</View>

<View { ...style('field') }>
  <TextInput
    { ...style('input') }
  />
  <Text { ...style('label.upper') }>{'Address Line 2'.toUpperCase()}</Text>
</View>

<View { ...style('field') }>
  <TextInput
    { ...style('input') }
  />
  <Text { ...style('label.upper') }>{'City'.toUpperCase()}</Text>
</View>

<View { ...style('field') }>
  <TextInput
    { ...style('input') }
  />
  <Text { ...style('label.upper') }>{'State'.toUpperCase()}</Text>
</View>

<View { ...style('field') }>
  <TextInput
    { ...style('input') }
  />
  <Text { ...style('label.upper') }>{'Zipcode'.toUpperCase()}</Text>
</View>

In the second version, a simple design change requires a lot of updates to the template content itself. Even going the "Find and Replace" route would be pretty tricky.

I also find that in many scenarios, small caps tend to look better with a little bit of letterSpacing applied. In that case, I'd also end up modifying my styles too.

IMO, it's a lot of overhead for a common use case, and something that could go from all this to a single line in a stylesheet with textTransform support.

jonlong avatar Mar 01 '16 21:03 jonlong

Another option is to wrap the Text component with one that adds the capabilities that you want, so then if you have the uppercase prop set on it, it will toUpperCase() everything. Can you create an issue on ProductPains that includes the great description of the problem that you just stated @jonlong?

brentvatne avatar Mar 06 '16 23:03 brentvatne

@brentvatne sure thing — done: https://productpains.com/post/react-native/texttransform-should-be-available-in-stylesheet/

jonlong avatar Mar 07 '16 03:03 jonlong

+1 I really need this on a specific app that uses it a lot. Thanks!

afilp avatar Jun 13 '16 08:06 afilp

I'm in a situation where I can't access the Text tags directly that I want to put uppercase because I'm dynamically generating them - thus, applying conditional JS is much harder than just passing in styles.

dwilt avatar Jul 18 '16 19:07 dwilt

+1 for me as well. I am a designer which likes to develop UI's when I get the chance. Unfortunately, my Javascript skills are not great, yet. Having a more CSS centric approach to building UI's would really help me. Granted I should probably just learn Javascript and be done with it. However, I am sure there are others like me which like to code UI's but have not reached the level where they can use Javascript as an alternative. My 2 cents.

vanpoos avatar Jul 20 '16 05:07 vanpoos

+1 it's clearly a style concern. it was a concern on the web and now on mobile. I wouldn't want to worry about conditionally upper-casing text of a button component every time I use it but rather specify its style exactly like color or font-size, especially when I have to support two different style for the same component, each one with different text-transform style (Android vs iOs)

ethanshar avatar Aug 02 '16 07:08 ethanshar

+1

leeight avatar Oct 09 '16 07:10 leeight

+1

guiherzog avatar Nov 11 '16 05:11 guiherzog

+1

neiker avatar Nov 15 '16 17:11 neiker

+1

stephenmeszaros avatar Nov 16 '16 01:11 stephenmeszaros

+1

mjracca avatar Nov 16 '16 17:11 mjracca

the advantage of css "class" is to not duplicate codes.

I strongly think we need "whole supports" css system for RN.

amazingandyyy avatar Nov 20 '16 21:11 amazingandyyy

+1

mxfp0 avatar Nov 29 '16 11:11 mxfp0