Skip to content

feat: Customizable dimensions animation type - layout and worklet #355

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 2 commits into from
Apr 17, 2025
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 @@ -141,9 +141,9 @@ export default function DataChangeExample() {

<Sortable.Flex
columnGap={spacing.sm}
dimensionsAnimationType='worklet'
rowGap={spacing.xs}
scrollableRef={scrollableRef}
animateHeight
hapticsEnabled
onDragEnd={({ order }) => setData(order(data))}>
{data.map(item => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ export default function DataChangeExample() {
columnGap={spacing.sm}
columns={COLUMNS}
data={data}
dimensionsAnimationType='worklet'
renderItem={renderItem}
rowGap={spacing.xs}
scrollableRef={scrollableRef}
animateHeight
hapticsEnabled
onDragEnd={({ data: newData }) => setData(newData)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default function DragHandleExample() {
columnGap={10}
columns={columns}
data={DATA}
dimensionsAnimationType='layout'
dragActivationDelay={0}
overDrag={overDrag}
renderItem={renderItem}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export default function OrderingStrategyExample() {
columnGap={spacing.xs}
columns={COLUMNS}
data={DATA}
dimensionsAnimationType='worklet'
renderItem={renderItem}
rowGap={spacing.xs}
strategy='insert'
animateHeight
/>
</Section>

Expand All @@ -39,10 +39,10 @@ export default function OrderingStrategyExample() {
columnGap={spacing.xs}
columns={COLUMNS}
data={DATA}
dimensionsAnimationType='worklet'
renderItem={renderItem}
rowGap={spacing.xs}
strategy='swap'
animateHeight
/>
</Section>
</Stagger>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ export default function StaggerAnimationExample() {
columnGap={10}
columns={3}
data={data}
dimensionsAnimationType='worklet'
overflow='visible'
renderItem={renderItem}
rowGap={10}
animateHeight
onDragEnd={handleDragEnd}
/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ function SortableFlex(props: SortableFlexProps) {
},
sharedProps: {
DropIndicatorComponent,
animateHeight,
animateWidth,
dimensionsAnimationType,
dropIndicatorStyle,
itemEntering,
itemExiting,
Expand Down Expand Up @@ -77,9 +76,8 @@ function SortableFlex(props: SortableFlexProps) {
useAdditionalValues={useFlexLayoutContext}
/>
<SortableFlexInner
animateHeight={animateHeight}
animateWidth={animateWidth}
childrenArray={childrenArray}
dimensionsAnimationType={dimensionsAnimationType}
DropIndicatorComponent={DropIndicatorComponent}
dropIndicatorStyle={dropIndicatorStyle}
itemEntering={itemEntering}
Expand Down Expand Up @@ -107,8 +105,7 @@ type SortableFlexInnerProps = {
Required<
Pick<
SortableFlexProps,
| 'animateHeight'
| 'animateWidth'
| 'dimensionsAnimationType'
| 'itemEntering'
| 'itemExiting'
| 'itemsLayout'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ function SortableGrid<I>(props: SortableGridProps<I>) {
},
sharedProps: {
DropIndicatorComponent,
animateHeight,
animateWidth,
dimensionsAnimationType,
dropIndicatorStyle,
itemEntering,
itemExiting,
Expand Down Expand Up @@ -109,10 +108,9 @@ function SortableGrid<I>(props: SortableGridProps<I>) {
useAdditionalValues={useGridLayoutContext}
/>
<SortableGridInner
animateHeight={animateHeight}
animateWidth={animateWidth}
columnGap={columnGapValue}
data={data}
dimensionsAnimationType={dimensionsAnimationType}
DropIndicatorComponent={DropIndicatorComponent}
dropIndicatorStyle={dropIndicatorStyle}
groups={groups}
Expand Down Expand Up @@ -142,9 +140,8 @@ type SortableGridInnerProps<I> = {
Required<
Pick<
SortableGridProps<I>,
| 'animateHeight'
| 'animateWidth'
| 'data'
| 'dimensionsAnimationType'
| 'itemEntering'
| 'itemExiting'
| 'itemsLayout'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type ViewStyle
} from 'react-native';
import Animated, {
LinearTransition,
useAnimatedStyle,
withTiming
} from 'react-native-reanimated';
Expand All @@ -16,30 +17,29 @@ import {
useCommonValuesContext,
useMeasurementsContext
} from '../../providers';
import {
AbsoluteLayoutState,
type DropIndicatorSettings,
type Overflow
import type {
DimensionsAnimation,
DropIndicatorSettings,
Overflow
} from '../../types';
import { AbsoluteLayoutState } from '../../types';
import AnimatedOnLayoutView from './AnimatedOnLayoutView';
import DropIndicator from './DropIndicator';

const SCREEN_DIMENSIONS = Dimensions.get('screen');

type AnimatedHeightContainerProps = PropsWithChildren<
{
animateHeight: boolean;
animateWidth: boolean;
dimensionsAnimationType: DimensionsAnimation;
overflow: Overflow;
style?: StyleProp<ViewStyle>;
} & DropIndicatorSettings
>;

export default function SortableContainer({
DropIndicatorComponent,
animateHeight,
animateWidth,
children,
dimensionsAnimationType,
dropIndicatorStyle,
overflow,
showDropIndicator,
Expand All @@ -58,6 +58,9 @@ export default function SortableContainer({
const { handleHelperContainerMeasurement, measurementsContainerRef } =
useMeasurementsContext();

const animateWorklet = dimensionsAnimationType === 'worklet';
const animateLayout = dimensionsAnimationType === 'layout';

const outerContainerStyle = useAnimatedStyle(() => {
if (absoluteLayoutState.value !== AbsoluteLayoutState.COMPLETE) {
return EMPTY_OBJECT;
Expand All @@ -73,18 +76,18 @@ export default function SortableContainer({
return {
height: maybeAnimate(
ctrl.height ? containerHeight.value : null,
animateHeight
animateWorklet
),
overflow:
activeItemKey.value !== null || !activeItemDropped.value
? 'visible'
: overflow,
width: maybeAnimate(
ctrl.width ? containerWidth.value : null,
animateWidth
animateWorklet
)
};
}, [animateHeight, animateWidth]);
}, [dimensionsAnimationType]);

const innerContainerStyle = useAnimatedStyle(() => {
if (absoluteLayoutState.value !== AbsoluteLayoutState.COMPLETE) {
Expand Down Expand Up @@ -119,6 +122,7 @@ export default function SortableContainer({

return (
<Animated.View
layout={animateLayout ? LinearTransition : undefined}
// @ts-expect-error - contain is a correct CSS prop on web
style={[outerContainerStyle, IS_WEB && { contain: 'layout' }]}>
{showDropIndicator && (
Expand Down
3 changes: 1 addition & 2 deletions packages/react-native-sortables/src/constants/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ export const DEFAULT_SHARED_PROPS = {
activeItemOpacity: 1,
activeItemScale: 1.1,
activeItemShadowOpacity: 0.2,
animateHeight: false,
animateWidth: false,
autoScrollActivationOffset: 75,
autoScrollDirection: 'vertical',
autoScrollEnabled: true,
autoScrollSpeed: 1,
customHandle: false,
debug: false,
dimensionsAnimationType: 'none',
dragActivationDelay: 200,
dragActivationFailOffset: 5,
dropAnimationDuration: 300,
Expand Down
8 changes: 4 additions & 4 deletions packages/react-native-sortables/src/types/props/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,14 @@ export type SortableCallbacks = {

export type Overflow = 'hidden' | 'visible';

export type DimensionsAnimation = 'layout' | 'none' | 'worklet';

export type SharedProps = Simplify<
Omit<SortableCallbacks, 'onDragEnd'> &
Partial<
{
/** Whether to animate container height changes when items are moved */
animateHeight: boolean;
/** Whether to animate container width changes when items are moved */
animateWidth: boolean;
/** Whether and how to animate container dimensions changes */
dimensionsAnimationType: DimensionsAnimation;
/** Enable haptic feedback when sorting items */
hapticsEnabled: boolean;
/** Controls whether sorting functionality is enabled */
Expand Down
Loading