Skip to content

Support RTL in ResponsiveGrid #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,13 @@ A lower value results in more frequent updates, offering smoother visual updates
<td><code>false</code></td>
<td> Defines the base unit height for items within the grid. This value serves as a foundational measure to determine the actual height of each grid item. The item's final height is calculated by multiplying this base unit height (<code>itemUnitHeight</code>) by the item's heightRatio, allowing for proportional scaling of items based on their content or design requirements. While <code>widthRatio</code> affects the item's width in relation to the column width, <code>itemUnitHeight</code> and <code>heightRatio</code> together define the item's vertical dimension, enabling dynamic grid layouts that adapt seamlessly to varying content sizes.</td>
</tr>
<tr>
<td><code>direction</code></td>
<td><code>string</code></td>
<td><code>ltr</code></td>
<td><code>false</code></td>
<td> Determines the direction for arranging grid items in the layout: left-to-right (ltr) or right-to-left (rtl). </td>
</tr>
<tr>
<td><code>virtualization</code></td>
<td><code>Boolean</code></td>
Expand Down
26 changes: 16 additions & 10 deletions src/responsive-grid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({
keyExtractor = (_, index) => String(index), // default to item index if no keyExtractor is provided
HeaderComponent = null,
FooterComponent = null,
direction = 'ltr',
}) => {
const [visibleItems, setVisibleItems] = useState<TileItem[]>([]);

Expand Down Expand Up @@ -120,6 +121,20 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({
onEndReachedCalled.current = false;
}, [gridItems, containerSize, virtualization]);

const getItemPositionStyle = (item: TileItem) => {
const baseStyle = {
position: 'absolute' as const,
top: item.top,
width: item.width,
height: item.height,
};

return {
...baseStyle,
...(direction === 'rtl' ? { right: item.left } : { left: item.left }),
};
};

return (
<View
style={[{ flexGrow: 1 }, style]}
Expand Down Expand Up @@ -154,16 +169,7 @@ export const ResponsiveGrid: React.FC<ResponsiveGridProps> = ({
{renderedItems.map((item, index) => (
<View
key={keyExtractor(item, index)}
style={[
{
position: 'absolute',
top: item.top,
left: item.left,
width: item.width,
height: item.height,
},
itemContainerStyle,
]}
style={[getItemPositionStyle(item), itemContainerStyle]}
>
{renderItem({ item, index })}
</View>
Expand Down
6 changes: 6 additions & 0 deletions src/responsive-grid/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export interface ResponsiveGridProps {
| React.ReactElement<any, string | React.JSXElementConstructor<any>>
| null
| undefined;

/**
* Determines the direction for arranging grid items in the layout: left-to-right (ltr) or right-to-left (rtl).
* @default ltr
*/
direction?: 'rtl' | 'ltr';
}

export interface TileItem {
Expand Down
Loading