### Example Drag Start Animation Styles Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/api-reference/Block.md Illustrates the animation styles applied to a Block component when it is being dragged. ```typescript { zIndex: 3, transform: [{ scale: dragStartAnimatedValue }], shadowColor: '#000000', shadowOpacity: 0.2, shadowRadius: 6, shadowOffset: { width: 1, height: 1 } } ``` -------------------------------- ### Making Draggable Grid Responsive with useWindowDimensions Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/troubleshooting.md This example demonstrates how to make the grid responsive by dynamically setting the number of columns based on the screen width. It uses the `useWindowDimensions` hook to get the current width. ```typescript import { Dimensions, useWindowDimensions } from 'react-native' export function ResponsiveGrid() { const { width } = useWindowDimensions() const numColumns = width > 600 ? 4 : 2 return ( ) } ``` -------------------------------- ### Custom Drag Start Animation Source: https://github.com/shisme/react-native-draggable-grid/blob/master/README.md Implement a custom animation when dragging starts by providing a `dragStartAnimation` prop. This example uses `Animated.timing` to scale the item. ```javascript render() { return ( ); } private onDragStart = () => { this.state.animatedValue.setValue(1); Animated.timing(this.state.animatedValue, { toValue:3, duration:400, }).start(); } ``` -------------------------------- ### Using Draggable Grid with React Native Web Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/troubleshooting.md This example shows the basic setup for using `DraggableGrid` in a web application with `react-native-web`. Ensure `react-native-web` is installed and configured in your project. ```typescript // App.web.tsx import { DraggableGrid } from 'react-native-draggable-grid' export default function App() { return } ``` -------------------------------- ### Install react-native-draggable-grid Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/integration-guide.md Install the library using npm or yarn. Ensure you meet the React and React Native version requirements. ```bash npm install react-native-draggable-grid # or yarn add react-native-draggable-grid ``` -------------------------------- ### Install react-native-draggable-grid Source: https://github.com/shisme/react-native-draggable-grid/blob/master/README.md Install the library using npm. This command adds the package as a dependency to your project. ```bash npm install react-native-draggable-grid --save ``` -------------------------------- ### TypeScript Code Example Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/README.md This snippet demonstrates the use of TypeScript for defining code examples within the documentation. It indicates recommended practices. ```typescript // Shows actual TypeScript code // ✓ Recommended // ✗ Anti-pattern ``` -------------------------------- ### Basic Draggable Grid Usage Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/api-reference/DraggableGrid.md Demonstrates the fundamental setup of the DraggableGrid component with a simple array of items and basic styling. ```typescript import React, { useState } from 'react' import { View, Text, StyleSheet } from 'react-native' import { DraggableGrid } from 'react-native-draggable-grid' interface Item { key: string name: string } export function BasicGrid() { const [items, setItems] = useState([ { key: '1', name: 'Item 1' }, { key: '2', name: 'Item 2' }, { key: '3', name: 'Item 3' }, ]) return ( ( {item.name} )} onDragRelease={(sortedData) => { setItems(sortedData) }} /> ) } const styles = StyleSheet.create({ itemContainer: { flex: 1, backgroundColor: '#e0e0e0', justifyContent: 'center', alignItems: 'center', }, }) ``` -------------------------------- ### Example Usage: findKey Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/api-reference/utilities.md Demonstrates how to use the findKey utility function to locate an item's key based on a property value, including cases where the item is not found. ```typescript const orderMap = { 'item1': { order: 0 }, 'item2': { order: 1 }, 'item3': { order: 2 }, } // Find the key of the item with order 1 const key = findKey(orderMap, item => item.order === 1) console.log(key) // 'item2' // Find the first key (returns first found) const firstKey = findKey(orderMap, () => true) console.log(firstKey) // 'item1' // Returns undefined if not found const notFound = findKey(orderMap, item => item.order === 99) console.log(notFound) // undefined ``` -------------------------------- ### Custom Animated Value Drag Start Animation Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/integration-guide.md Implement complex, dynamic drag start animations using `Animated.Value` and `Animated.timing` for transformations like scaling and rotation. Requires `useNativeDriver: false` for certain properties. ```typescript import { useRef } from 'react' import { Animated, Easing } from 'react-native' export function GridWithCustomAnimation() { const scaleAnim = useRef(new Animated.Value(1)).current const rotateAnim = useRef(new Animated.Value(0)).current const handleDragStart = () => { // Parallel animations Animated.parallel([ Animated.timing(scaleAnim, { toValue: 1.3, duration: 300, easing: Easing.elastic(1), useNativeDriver: false, }), Animated.timing(rotateAnim, { toValue: 1, duration: 300, useNativeDriver: false, }), ]).start() } const handleDragRelease = (newData: Item[]) => { // Reset animations Animated.parallel([ Animated.timing(scaleAnim, { toValue: 1, duration: 200, useNativeDriver: false, }), Animated.timing(rotateAnim, { toValue: 0, duration: 200, useNativeDriver: false, }), ]).start() setItems(newData) } return ( } /> ) } ``` -------------------------------- ### Custom Static Drag Start Animation Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/integration-guide.md Apply custom static styles like opacity and background color to the item during drag start using the `dragStartAnimation` prop. ```typescript } /> ``` -------------------------------- ### Handling Drag Events in Draggable Grid Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/api-reference/DraggableGrid.md Provides examples of various event handlers for tracking item interactions, drag start, active drag, and release events. ```typescript function GridWithEvents() { return ( } onItemPress={(item) => { console.log('Item tapped:', item.key) }} onDragStart={(item) => { console.log('Started dragging:', item.key) }} onDragItemActive={(item) => { console.log('Item activated:', item.key) }} onDragging={(gestureState) => { console.log('Current drag position:', { x: gestureState.moveX, y: gestureState.moveY, }) }} onResetSort={(newData) => { console.log('Item swapped, new order:', newData.map(i => i.key)) }} onDragRelease={(newData) => { console.log('Drag completed, final order:', newData.map(i => i.key)) setItems(newData) }} /> ) } ``` -------------------------------- ### Basic Simple Grid Setup Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/integration-guide.md Demonstrates setting up a basic draggable grid with a specified number of columns and custom item rendering. The onDragRelease callback is used to update the component's state with the new item order. ```typescript import React, { useState } from 'react' import { View, StyleSheet } from 'react-native' import { DraggableGrid } from 'react-native-draggable-grid' interface Item { key: string name: string } export function SimpleGrid() { const [items, setItems] = useState([ { key: '1', name: 'Item 1' }, { key: '2', name: 'Item 2' }, { key: '3', name: 'Item 3' }, { key: '4', name: 'Item 4' }, ]) return ( ( {item.name} )} onDragRelease={(newData) => { setItems(newData) }} /> ) } const styles = StyleSheet.create({ item: { flex: 1, backgroundColor: '#e0e0e0', justifyContent: 'center', alignItems: 'center', borderRadius: 8, }, }) ``` -------------------------------- ### TypeScript Usage Example Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/module-exports.md Demonstrates how to import and use the DraggableGrid component with TypeScript, including defining item types and props. It shows the basic setup for integrating the component into a React Native application. ```typescript import { DraggableGrid, type IDraggableGridProps } from 'react-native-draggable-grid' interface PhotoItem { key: string uri: string } type PhotoGridProps = IDraggableGridProps const MyGrid: React.FC = (props) => { return } ``` -------------------------------- ### RTL Adjustments for Positioning and Styling Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/internals.md Provides examples of how to adjust positioning and styling logic to support Right-to-Left (RTL) layouts using `I18nManager.isRTL`. ```typescript // In drag positioning const x = activeOrigin.x + (I18nManager.isRTL ? x0 : -x0) // In movement tracking const moveX = I18nManager.isRTL ? -moveXOriginal : moveXOriginal // In block styling (web-specific) right: I18nManager.isRTL && Platform.OS === 'web' ? left : undefined left: I18nManager.isRTL && Platform.OS === 'web' ? undefined : left ``` -------------------------------- ### Package Import Paths: Default and Named Imports Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/module-exports.md Demonstrates how to import the DraggableGrid component and its types using default and named import syntax from the installed package. ```typescript // Default import import DraggableGrid from 'react-native-draggable-grid' // Named imports import { DraggableGrid, type IDraggableGridProps } from 'react-native-draggable-grid' // Type imports (TypeScript) import type { IDraggableGridProps } from 'react-native-draggable-grid' ``` -------------------------------- ### Example of getSortData Output Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/internals.md Illustrates the output of the `getSortData` function, showing how items are placed into the resulting array according to their order index. ```typescript items = [ { key: 'a', itemData: {name: 'A'} }, { key: 'b', itemData: {name: 'B'} }, { key: 'c', itemData: {name: 'C'} } ] orderMap = { 'a': { order: 2 }, 'b': { order: 0 }, 'c': { order: 1 } } getSortData() returns: [ {name: 'B'}, // order 0 {name: 'C'}, // order 1 {name: 'A'} // order 2 ] ``` -------------------------------- ### Custom Item Type Extension Example Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/types.md Illustrates how to extend the base IBaseItemType to create a custom data structure for grid items. This example shows adding specific properties like 'title' and 'thumbnail', and demonstrates setting disabled flags. ```typescript interface CustomItem extends IBaseItemType { key: string title: string thumbnail: string metadata?: Record disabledDrag?: boolean disabledReSorted?: boolean } const items: CustomItem[] = [ { key: 'item1', title: 'Photo 1', thumbnail: 'https://example.com/photo1.jpg', }, { key: 'item2', title: 'Header', thumbnail: '', disabledReSorted: true, }, ] ``` -------------------------------- ### Example Base Layout Styles Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/api-reference/Block.md Shows the base layout styles applied to a Block component when rendered by DraggableGrid. ```typescript { justifyContent: 'center', alignItems: 'center', width: blockWidth, height: blockHeight, position: 'absolute', top: currentPosition.getLayout().top, left: currentPosition.getLayout().left, } ``` -------------------------------- ### Tracking Dragged Item with onDragStart Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/troubleshooting.md This example shows how to track the currently dragged item using the `onDragStart` callback. The dragged item's state is updated and then cleared on drag release. ```typescript const [draggedItem, setDraggedItem] = useState(null) { setDraggedItem(item) // Track dragged item }} onDragRelease={(newData) => { setItems(newData) setDraggedItem(null) // Clear }} renderItem={(item) => ( {item.name} )} /> ``` -------------------------------- ### Customizing Drag Start Animation with Animated.spring Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/troubleshooting.md This snippet shows how to customize the drag start animation using the `dragStartAnimation` prop with `Animated.spring`. It requires importing `Animated` and using `useRef` for the animation value. ```typescript const scale = useRef(new Animated.Value(1)).current const handleDragStart = () => { Animated.spring(scale, { toValue: 1.3, useNativeDriver: false, }).start() } ``` -------------------------------- ### Items Array Example Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/internals.md Shows the structure of the 'items' array, where each element contains an item's key, data, and its animated position values. ```typescript items = [ { key: 'item1', itemData: {...}, currentPosition: AnimatedValueXY(...) }, { key: 'item2', itemData: {...}, currentPosition: AnimatedValueXY(...) }, ... ] ``` -------------------------------- ### Accessibility Example for Block Children Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/api-reference/Block.md Provides an example of how to add accessibility props to a child component rendered within a Block. This ensures proper accessibility labels and roles for interactive elements. ```typescript {item.content} ``` -------------------------------- ### Draggable Grid with Custom Item Height and Animations Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/api-reference/DraggableGrid.md Shows how to customize item height and implement drag start animations using Animated API for a more interactive user experience. ```typescript import React, { useState } from 'react' import { Animated, Easing } from 'react-native' import { DraggableGrid } from 'react-native-draggable-grid' export function CustomAnimationGrid() { const [scaleAnim] = useState(new Animated.Value(1)) const handleDragStart = () => { Animated.timing(scaleAnim, { toValue: 1.2, duration: 200, easing: Easing.ease, useNativeDriver: false, }).start() } return ( } dragStartAnimation={{ transform: [{ scale: scaleAnim }], opacity: 0.8, }} onDragStart={handleDragStart} onDragRelease={(sortedData) => { scaleAnim.setValue(1) updateData(sortedData) }} delayLongPress={500} /> ) } ``` -------------------------------- ### DraggableGrid Usage Example Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/types.md Demonstrates how to configure and use the DraggableGrid component with custom data and event handlers. Ensure your data items conform to the IDraggableGridProps generic type. ```typescript const gridProps: IDraggableGridProps = { numColumns: 3, data: myItems, renderItem: (item, order) => , onDragRelease: (sortedData) => updateState(sortedData), } ``` -------------------------------- ### TypeScript Setup for Draggable Grid Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/troubleshooting.md This snippet illustrates how to set up TypeScript for the `react-native-draggable-grid` library, including importing types and defining props for a custom item interface. ```typescript import { DraggableGrid, type IDraggableGridProps } from 'react-native-draggable-grid' interface Item { key: string title: string } type Props = IDraggableGridProps const MyGrid: React.FC = (props) => { return } ``` -------------------------------- ### Default Drag Start Animation Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/api-reference/DraggableGrid.md This is the default animation applied when an item starts being dragged, unless a custom animation is provided via the dragStartAnimation prop. ```typescript { transform: [{ scale: 1.1 }], shadowColor: '#000000', shadowOpacity: 0.2, shadowRadius: 6, shadowOffset: { width: 1, height: 1 } } ``` -------------------------------- ### Example Usage: findIndex Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/api-reference/utilities.md Demonstrates how to use the findIndex utility function to locate an element's index in an array based on a condition, including cases where no match is found. ```typescript const items = [ { key: 'a', name: 'Alpha' }, { key: 'b', name: 'Beta' }, { key: 'c', name: 'Gamma' }, ] // Find index of item with key 'b' const idx = findIndex(items, item => item.key === 'b') console.log(idx) // 1 // Returns -1 if not found const notFound = findIndex(items, item => item.key === 'z') console.log(notFound) // -1 // Find first element matching condition const firstString = findIndex(items, item => item.name.length > 4) console.log(firstString) // 2 (Gamma has 5 characters) ``` -------------------------------- ### Extending IBaseItemType with Explicit Source Import Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/module-exports.md Example of explicitly extending IBaseItemType by importing it directly from the source file, which requires a source import path. ```typescript import type { DraggableGrid } from 'react-native-draggable-grid/src/draggable-grid' interface MyItem extends IBaseItemType { key: string name: string } ``` -------------------------------- ### Simple Object Data Pattern Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/integration-guide.md Example of using simple objects with a 'key' property for grid items. The DraggableGrid component is typed with the 'Photo' interface. ```typescript interface Photo { key: string uri: string width: number height: number } const photos: Photo[] = [ { key: 'photo1', uri: 'https://...', width: 300, height: 400 }, { key: 'photo2', uri: 'https://...', width: 300, height: 400 }, ] data={photos} renderItem={(photo) => ( )}/> ``` -------------------------------- ### Configuring Accessibility for DraggableGrid Items Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/integration-guide.md This example shows how to enhance the accessibility of DraggableGrid items by setting accessibility labels, roles, and hints. This improves usability for users with assistive technologies. ```typescript ( )} onDragRelease={(newData) => setItems(newData)} /> ``` -------------------------------- ### Extending IBaseItemType with Implicit Extension Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/module-exports.md Example of how to define a custom item type that implicitly extends IBaseItemType by including the required properties. ```typescript import { DraggableGrid } from 'react-native-draggable-grid' interface MyItem { key: string name: string // extends IBaseItemType implicitly } ``` -------------------------------- ### Persisting Grid Order with API Call on Drag Release Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/troubleshooting.md This example shows how to persist the grid's order to a backend by making an API call within the `onDragRelease` callback. It includes optimistic updates and error handling for rollback. ```typescript { setItems(newData) // Optimistic update try { await api.updateGridOrder( newData.map((item, order) => ({ key: item.key, order, })) ) } catch (error) { setItems(previousItems) // Rollback showError('Failed to save') } }} {...props} /> ``` -------------------------------- ### Large List Handling with FlatList Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/integration-guide.md Use FlatList for lists exceeding 100 items to enable pagination and windowing. This example demonstrates loading items incrementally. ```typescript export function LargeGrid() { const [items, setItems] = useState([]) const [page, setPage] = useState(1) useEffect(() => { loadMoreItems(page) }, [page]) const visibleItems = items.slice(0, page * 50) return ( ( } onDragRelease={(newData) => setItems(newData)} /> )} onEndReached={() => setPage(p => p + 1)} /> ) } ``` -------------------------------- ### Full Drag Lifecycle Event Handling Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/integration-guide.md Demonstrates handling various events throughout the drag-and-drop lifecycle, from item press to drag start, dragging, reset sort, and final drag release. Useful for providing user feedback like sounds or animations. ```typescript export function GridWithEvents() { return ( } // 1. User taps item (no drag) onItemPress={(item) => { console.log('Tapped:', item.key) // Handle navigation, modal, etc. }} // 2. User holds item for delayLongPress (default 300ms) onDragItemActive={(item) => { console.log('Drag activated:', item.key) // Play feedback sound, haptic feedback }} // 3. Gesture recognizes as drag (starts moving) onDragStart={(item) => { console.log('Drag started:', item.key) // Start custom animations }} // 4. Dragging (continuous) onDragging={(gestureState) => { console.log('Current position:', { x: gestureState.moveX, y: gestureState.moveY, vx: gestureState.vx, vy: gestureState.vy, }) }} // 5a. Item position changes (swap) onResetSort={(newData) => { console.log('Swapped, new order:', newData.map(i => i.key)) // Update UI state if needed }} // 5b. User releases (drag ends) onDragRelease={(newData) => { console.log('Drag complete, final order:', newData.map(i => i.key)) // Persist to backend saveGridOrder(newData) }} /> ) } ``` -------------------------------- ### Handling Item Deletion in Draggable Grid Source: https://github.com/shisme/react-native-draggable-grid/blob/master/_autodocs/troubleshooting.md This example demonstrates how to handle item deletion by filtering the data array. The grid automatically re-renders to reflect the removal of the item. ```typescript const deleteItem = (keyToDelete: string) => { const newItems = items.filter(item => item.key !== keyToDelete) setItems(newItems) // Grid automatically updates } // In renderItem renderItem={(item) => ( {item.name}