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

未关闭
#1,433 14 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
22/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
javascript, react-native, tailwindcss

调研方向

Start with the linked reproduction, specifically src/app/components/EditScreenInfo.tsx, where ltr: and rtl: classes are applied to a Text. Then find where NativeWind resolves direction variants and compare them with I18nManager.isRTL on native. Run the reproduction on a development build, not Expo Go, and check which class wins. Done means ltr:/rtl: apply only when the active direction matches, and the reporter's tw workaround is no longer needed.

由索引模型根据 Issue 内容生成。

描述

deferred (post-5.0) needs investigation needs reproduction

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>

主要语言
JavaScript
星标
8.1k
派生
409
平均合并
13 小时 16 分钟
30 天内合并 PR
3

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

nativewind/nativewind 的其他 Issue

查看 nativewind/nativewind 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。