UNPKG

react-native-arabic-autoheight-text

Version:

Smart Arabic/RTL text renderer for React Native using WebView: auto height, proper justification, line clamp, and skeleton loading.

201 lines (141 loc) โ€ข 6.21 kB
# ๐Ÿ‡ธ๐Ÿ‡ฆ react-native-arabic-autoheight-text **Beautiful Arabic & RTL text inside WebView โ€” Auto Height + Proper Typography** [![npm version](https://img.shields.io/npm/v/react-native-arabic-autoheight-text.svg)](https://www.npmjs.com/package/react-native-arabic-autoheight-text) [![license](https://img.shields.io/npm/l/react-native-arabic-autoheight-text.svg)](./LICENSE) [![platforms](https://img.shields.io/badge/platforms-ios%20%7C%20android-lightgrey.svg)]() [![TypeScript Friendly](https://img.shields.io/badge/TypeScript-ready-blue.svg)]() A tiny, drop-in React Native component for **Arabic / RTL text rendering** with: * ๐Ÿš€ Auto height inside WebView (no scrolling) * ๐Ÿ•Œ Perfect RTL direction + proper justification * โœ‚๏ธ Line clamping using CSS (`-webkit-line-clamp`) * ๐ŸŽจ Converts `TextStyle` โ†’ WebView CSS * ๐Ÿฆด Built-in / custom Skeleton loader * ๐ŸŒ™ Adaptive skeleton for dark mode * ๐Ÿ“ Supports fixed or dynamic layout with accuracy Designed for: **News, corporate content, About screens, product descriptions, long Arabic articlesโ€ฆ** --- ## ๐ŸŽฅ Demo ![react-native-arabic-autoheight-text Demo](https://github.com/ahmedhegazydev/react-native-arabic-autoheight-text/blob/master/src/assets/demo.gif) --- ## ๐Ÿ“ฆ Installation ```bash yarn add react-native-arabic-autoheight-text react-native-webview react-native-skeleton-placeholder # OR npm install react-native-arabic-autoheight-text react-native-webview react-native-skeleton-placeholder ``` iOS ```bash cd ios && pod install ``` --- ## ๐Ÿงฉ Usage ### Basic Example ```tsx <RtlTextView text="ู‡ุฐุง ุงู„ู†ุต ุณูŠุชู… ุถุจุท ุงุฑุชูุงุนู‡ ุชู„ู‚ุงุฆูŠุงู‹ ุญุณุจ ุงู„ู…ุญุชูˆู‰" textStyle={{ fontSize: 18, color: '#333' }} /> ``` --- ### Line Clamping ```tsx <RtlTextView text="ู†ุต ุนุฑุจูŠ ุทูˆูŠู„ ุณูŠุชู… ู‚ุตู‡ ุจุนุฏ ุซู„ุงุซุฉ ุฃุณุทุฑ ูู‚ุท..." numberOfLines={3} textStyle={{ fontSize: 16 }} /> ``` --- ### Custom Skeleton ```tsx <RtlTextView text="ุฌุงุฑู ุชุญู…ูŠู„ ุงู„ู…ุญุชูˆู‰..." textStyle={{ fontSize: 16 }} renderSkeleton={() => ( <View style={{ height: 80, backgroundColor: '#DDD', borderRadius: 8 }} /> )} /> ``` --- ### ๐Ÿ”ฅ Skeleton padding behavior (Recommended) > Skeleton auto-matches **horizontal text padding** > โ†’ perfect alignment during loading ```tsx <RtlTextView text={longArabicText} textStyle={{ fontSize: 16, marginHorizontal: 20 }} /> ``` ๐Ÿ’ก Skeleton width = `full width โ€” horizontal text margins` ### Full-width Skeleton (no margin) ```tsx <RtlTextView text={longArabicText} textStyle={{ marginHorizontal: 20 }} matchTextHorizontalPadding={false} /> ``` --- ### WebView custom props ```tsx <RtlTextView text="Arabic Text" textStyle={{ fontSize: 16 }} webviewProps={{ showsVerticalScrollIndicator: false }} /> ``` --- ## ๐Ÿ”ง Props | Prop | Type | Default | Description | | ---------------------------- | ------------------------------------------------ | -------------- | ------------------------------------------ | | `text` | `string` | โ€” | Arabic text content | | `textStyle` | `StyleProp<TextStyle> \| StyleProp<TextStyle>[]` | โ€” | Converted to CSS inside WebView | | `numberOfLines` | `number` | `undefined` | Max visible lines using CSS clamp | | `isDark` | `boolean` | `false` | Changes skeleton theme | | `fontFamily` | `string` | from textStyle | Override web font | | `renderSkeleton` | `() => React.ReactNode` | default | Custom skeleton UI | | `skeletonHeight` | `number` | `100` | Height during loading | | `skeletonProps` | `Record<string, any>` | `{}` | Props forwarded to Skeleton | | `webviewProps` | `WebViewProps` | `{}` | Forwarded to WebView | | `containerStyle` | `StyleProp<ViewStyle>` | `{}` | Outer wrapper styling | | `matchTextHorizontalPadding` | `boolean` | `true` | Copy textโ€™s horizontal padding to skeleton | --- ## ๐ŸŽจ Styling Details Extracted CSS from last-defined `textStyle` value: * `color`, `backgroundColor` * `fontSize`, `fontWeight`, `lineHeight`, `fontFamily` * `textAlign`, RTL alignment enhancements * Margins: `marginStart`, `marginEnd`, `marginHorizontal`, `marginVertical` * WebView auto background transparency while loading > ๐Ÿ’ก `marginStart` + `marginEnd` recommended for RTL UI --- ## ๐Ÿง  Why WebView? React Native `<Text>` still suffers with: โŒ Justified Arabic glyph shaping โŒ Broken connections in RTL โŒ Poor multi-line rendering โŒ No CSS multi-line clamp This component fixes all of the above โœ”๏ธ --- ## ๐Ÿ›  Troubleshooting | Issue | Fix | | ------------------------------ | ------------------------------------------- | | Skeleton too skinny | remove `matchTextHorizontalPadding={false}` | | Wrong first height | Font loader will update automatically | | Background shows while loading | WebView background auto-hidden | --- ## ๐Ÿ—บ Roadmap ๐Ÿ”น Mixed LTR/RTL detection ๐Ÿ”น โ€œRead moreโ€ collapsed text ๐Ÿ”น Full custom CSS styles ๐Ÿ”น Accessibility enhancements PRs welcome โœจ --- ## ๐Ÿ‘ค Author Built with โค๏ธ by **Ahmed Hegazy** ๐Ÿ“ง [ahmedmhegazy.eg@gmail.com](mailto:ahmedmhegazy.eg@gmail.com) ๐Ÿ™ GitHub: @ahmedhegazydev --- ## ๐Ÿ“„ License MIT โ€” Free for commercial & open-source use