nativewind icon indicating copy to clipboard operation
nativewind copied to clipboard

`rtl` and `ltr` doesn't work on nativewind4 on native platforms

Open ws-rush opened this issue 9 months ago • 13 comments

Describe the bug when use ltr: or rtl: in nativewind it should not apply calss unless direction as in prefix, but nativewind run classes without care about them, it apply the last class always as prefix direction is not exist at all

Reproduction https://github.com/ws-rush/reactive-expo

  • above project contain example in src/app/components/EditScreenInfo.tsx: <Text className="ltr:text-green-500 rtl:text-red-500">{description}</Text>, i tried it also as <Text className="text-green-500 rtl:text-red-500">{description}</Text> and other cominations
  • the code change direction in src/app/globals/i18n.ts:
  async set(value: string) {
    // load and activate locale
    const { messages, direction } = this.localesData[value];
    i18n.load(value, messages);
    i18n.activate(value);

    // reload app
    if (direction === 'rtl') {
      await I18nManager.allowRTL(true);
      await I18nManager.forceRTL(true);
    } else {
      await I18nManager.allowRTL(false);
      await I18nManager.forceRTL(false);
    }

    if ((await locale.loadLocale()) !== value) {
      // set app default lang done from app settings
      await AsyncStorage.setItem('locale', value);

      if (Platform.OS === 'ios' || Platform.OS === 'android') reloadAsync();
      else if (Platform.OS === 'web') window.location.reload();
    }
  },
  • finally the app should run in dvelopment build not expo go to get direction changed correctly (get I18nManager.isRTL correct value)

Expected behavior it should apply classes with direction prefix only if application run in this direction

Additional context actually original issue is https://github.com/nativewind/nativewind/issues/959 from nativewind 4.0, but problems still exist. I solved this proble currently by add this to my project

import { I18nManager, Platform } from "react-native";

export function tw(strings: TemplateStringsArray, ...values: any[]) {
    // Combine the string literals and the values into a single string
    let combinedString = strings.reduce((result, string, i) => {
      return result + string + (values[i] || '');
    }, '');
  
    // Split the combined string into an array of classes
    let classes = combinedString.split(' ');
  
    // Filter out classes that start with "rtl:"
    let filteredClasses = classes.filter(cls => {
      if (Platform.OS !== 'web') return !cls.startsWith(I18nManager.isRTL ? 'ltr:' : 'rtl:')
      else return true
    });
  
    // Join the filtered classes back into a single string
    return filteredClasses.join(' ');
}

then apply it as <Text className={twltr:text-green-500 rtl:text-red-500}>{solved ${description}}</Text>

ws-rush avatar Mar 10 '25 23:03 ws-rush

@danstepanov this is still happening in newer version using v4.1.21

my icon gets rotate 180, in LTR as well

<Pressable onPress={handleBackPress}>
    <View className="rtl:rotate-180">
         <assets.icons.back height={24} width={24} />
    </View>
</Pressable>

hamzaCarswitch avatar Mar 25 '25 07:03 hamzaCarswitch

@ws-rush i also added your code snippet but it does not fix the issue,

one more thing do we always need to reload the app after switching to LTR or RTL

hamzaCarswitch avatar Apr 17 '25 10:04 hamzaCarswitch

Hello Nativewind team, any update regarding this issue. we are still having this in 4.1. @danstepanov

mazenkhalil avatar Apr 22 '25 08:04 mazenkhalil

i'll bring it up during the state of nativewind this week

danstepanov avatar Apr 22 '25 13:04 danstepanov

@ws-rush i also added your code snippet but it does not fix the issue,

one more thing do we always need to reload the app after switching to LTR or RTL

It work for me but not in expo go

ws-rush avatar Apr 24 '25 15:04 ws-rush

@danstepanov Hi, is there any update on this?

hamzaCarswitch avatar May 05 '25 07:05 hamzaCarswitch

will look into this tomorrow (Friday), just got the website together

danstepanov avatar May 09 '25 07:05 danstepanov

Hi, @danstepanov, it is big issue for rtl users, I wish we can help to fix it, is there a way to help. I am not expert but I wish to help

ameen770 avatar May 10 '25 08:05 ameen770

I'll be taking a look at this and other issues on Monday as the new website launch was delayed to today.

danstepanov avatar May 10 '25 08:05 danstepanov

@danstepanov any update on this?

xshapira avatar Jul 20 '25 18:07 xshapira

@danstepanov any update on this? Starting a new app and trying to decide if I should work around this or if there will be a fix released in the near future.

altnp avatar Jul 22 '25 04:07 altnp

i'll discuss this with Mark on Wednesday's State of Nativewind

danstepanov avatar Jul 22 '25 05:07 danstepanov

@danstepanov were you able to discuss this?

altnp avatar Jul 28 '25 05:07 altnp