Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@ function Example({ horizontal }: ExampleProps) {
const dimension = horizontal ? 'width' : 'height';

const renderItem = useCallback<SortableGridRenderItem<string>>(
({ item }) => (
<Animated.View
layout={LinearTransition}
style={[
styles.card,
{ [dimension]: collapsed ? sizes.lg : sizes.xxxl }
]}>
<Animated.Text layout={LinearTransition} style={styles.text}>
{item}
</Animated.Text>
</Animated.View>
),
({ item }) => {
const layoutTransition = LinearTransition.delay(collapsed ? 0 : 50);

return (
<Animated.View
layout={layoutTransition}
style={[
styles.card,
{ [dimension]: collapsed ? sizes.lg : sizes.xxxl }
]}>
<Animated.Text layout={layoutTransition} style={styles.text}>
{item}
</Animated.Text>
</Animated.View>
);
},
[collapsed, dimension]
);

Expand Down Expand Up @@ -97,11 +101,9 @@ function Example({ horizontal }: ExampleProps) {
scrollableRef={scrollableRef}
autoAdjustOffsetDuringDrag
onActiveItemDropped={() => {
console.log('onActiveItemDropped');
setCollapsed(false);
}}
onDragStart={() => {
console.log('onDragStart');
setCollapsed(true);
}}
/>
Expand Down
13 changes: 13 additions & 0 deletions packages/react-native-sortables/src/components/SortableFlex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DEFAULT_SORTABLE_FLEX_PROPS } from '../constants';
import type { RequiredBy } from '../helperTypes';
import type { PropsWithDefaults } from '../hooks';
import { useDragEndHandler, usePropsWithDefaults } from '../hooks';
import { useStableCallbackValues } from '../integrations/reanimated';
import {
FLEX_STRATEGIES,
FlexProvider,
Expand All @@ -27,13 +28,24 @@ import { SortableContainer } from './shared';
function SortableFlex(props: SortableFlexProps) {
const {
children,
onActiveItemDropped,
onDragEnd: _onDragEnd,
onDragMove,
onDragStart,
onOrderChange,
strategy,
...rest
} = usePropsWithDefaults(props, DEFAULT_SORTABLE_FLEX_PROPS);

const items = processChildren(children);

const callbacks = useStableCallbackValues({
onActiveItemDropped,
onDragMove,
onDragStart,
onOrderChange
});

const onDragEnd = useDragEndHandler(_onDragEnd, {
order: params =>
function <I>(data: Array<I>) {
Expand All @@ -45,6 +57,7 @@ function SortableFlex(props: SortableFlexProps) {
<ItemsProvider items={items}>
<SortableFlexInner
{...rest}
{...callbacks}
key={useStrategyKey(strategy)}
strategy={strategy}
onDragEnd={onDragEnd}
Expand Down
17 changes: 16 additions & 1 deletion packages/react-native-sortables/src/components/SortableGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { runOnUI, useAnimatedStyle } from 'react-native-reanimated';
import { DEFAULT_SORTABLE_GRID_PROPS, IS_WEB } from '../constants';
import type { PropsWithDefaults } from '../hooks';
import { useDragEndHandler, usePropsWithDefaults } from '../hooks';
import { useAnimatableValue } from '../integrations/reanimated';
import {
useAnimatableValue,
useStableCallbackValues
} from '../integrations/reanimated';
import {
GRID_STRATEGIES,
GridProvider,
Expand All @@ -26,7 +29,11 @@ function SortableGrid<I>(props: SortableGridProps<I>) {
columns,
data,
keyExtractor = defaultKeyExtractor,
onActiveItemDropped,
onDragEnd: _onDragEnd,
onDragMove,
onDragStart,
onOrderChange,
renderItem,
rowHeight,
rows,
Expand All @@ -52,6 +59,13 @@ function SortableGrid<I>(props: SortableGridProps<I>) {
[data, keyExtractor]
);

const callbacks = useStableCallbackValues({
onActiveItemDropped,
onDragMove,
onDragStart,
onOrderChange
});

const onDragEnd = useDragEndHandler(_onDragEnd, {
data: params => orderItems(data, items, params, true)
});
Expand All @@ -60,6 +74,7 @@ function SortableGrid<I>(props: SortableGridProps<I>) {
<ItemsProvider items={items} renderItem={renderItem}>
<SortableGridInner
{...rest}
{...callbacks}
groups={groups}
isVertical={isVertical}
key={useStrategyKey(strategy)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { default as useAnimatableValue } from './useAnimatableValue';
export { default as useAnimatedDebounce } from './useAnimatedDebounce';
export { default as useMutableValue } from './useMutableValue';
export { default as useStableCallbackValue } from './useStableCallbackValue';
export { default as useStableCallbackValues } from './useStableCallbackValues';
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { AnyFunction } from '../../../helperTypes';
import useStableCallbackValue from './useStableCallbackValue';

export default function useStableCallbackValues<
T extends Record<string, AnyFunction>
>(callbacks: T) {
return Object.fromEntries(
Object.entries(callbacks).map(([key, callback]) => [
key,
// eslint-disable-next-line react-hooks/rules-of-hooks
useStableCallbackValue(callback)
])
) as { [K in keyof T]: T[K] };
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ const { AutoScrollProvider, useAutoScrollContext } = createProvider(
const scrollBy = useCallback(
(distance: number, animated: boolean) => {
'worklet';
if (distance === 0) {
return;
}

const offset = currentScrollOffset.value + distance;
scrollTo(
scrollableRef,
Expand Down
Loading
Loading