Skip to content
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

Make example-app use expo 52 and clean worklet functions #43

Merged
merged 10 commits into from
Nov 26, 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
3 changes: 0 additions & 3 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,3 @@ jobs:

- name: Test linting
run: yarn test:lint

- name: Run unit tests
run: yarn test:jest
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.expo
.yarn
Binary file added .yarn/install-state.gz
Binary file not shown.
934 changes: 934 additions & 0 deletions .yarn/releases/yarn-4.5.3.cjs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.5.3.cjs
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,15 @@ Default value: `800`.

> `SwipeAxisDependentProp<(payload: PanGestureHandlerEventPayload) => WithSpringConfig>`

A function that returns a reanimated [SpringConfig](https://docs.swmansion.com/react-native-reanimated/docs/animations/withSpring/) that will be used in the final animation once a swipe is validated.
A workletized function that returns a reanimated [SpringConfig](https://docs.swmansion.com/react-native-reanimated/docs/animations/withSpring/) that will be used in the final animation once a swipe is validated.

**WARNING**: If this function isn't a worklet, the app will crash.

This prop can also accept an object whose keys are swipe axis ("x" and "y") and whose values are functions that return a reanimated [SpringConfig](https://docs.swmansion.com/react-native-reanimated/docs/animations/withSpring/).

`payload` is a gesture-handler payload that you can use to customize the config.

Default value: `() => ({ duration: 300 })`.
Default value: `() => { "worklet"; return ({ duration: 300 }) }`.

#### ▶️ imperativeSwipeAnimationConfig

Expand Down
3 changes: 2 additions & 1 deletion example-app/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"web": {
"favicon": "./assets/favicon.png"
},
"plugins": ["expo-font"]
"plugins": ["expo-font"],
"newArchEnabled": true
}
}
28 changes: 14 additions & 14 deletions example-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
"@emotion/react": "^11.11.3",
"@react-navigation/native": "^6.1.14",
"@react-navigation/native-stack": "^6.9.22",
"expo": "~51.0.24",
"expo-font": "~12.0.9",
"expo-haptics": "~13.0.1",
"expo-image": "~1.12.13",
"expo-linear-gradient": "~13.0.2",
"expo-splash-screen": "~0.27.5",
"expo-status-bar": "~1.12.1",
"react": "18.2.0",
"react-native": "0.74.3",
"react-native-gesture-handler": "~2.16.1",
"react-native-reanimated": "~3.10.1",
"react-native-safe-area-context": "4.10.5",
"react-native-screens": "3.31.1",
"react-native-svg": "15.2.0",
"expo": "^52.0.11",
"expo-font": "~13.0.1",
"expo-haptics": "~14.0.0",
"expo-image": "~2.0.2",
"expo-linear-gradient": "~14.0.1",
"expo-splash-screen": "~0.29.13",
"expo-status-bar": "~2.0.0",
"react": "18.3.1",
"react-native": "0.76.3",
"react-native-gesture-handler": "~2.20.2",
"react-native-reanimated": "~3.16.1",
"react-native-safe-area-context": "4.12.0",
"react-native-screens": "4.1.0",
"react-native-svg": "15.8.0",
"react-native-swipeable-card-stack": "*"
},
"devDependencies": {
Expand Down
17 changes: 7 additions & 10 deletions example-app/src/pokemon/PokemonSwipePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import styled, { css } from '@emotion/native'
import { StatusBar } from 'expo-status-bar'
import { useRef, useState } from 'react'
import {
SafeAreaView,
useSafeAreaFrame,
useSafeAreaInsets,
} from 'react-native-safe-area-context'
Expand All @@ -23,7 +22,7 @@ import { PokemonSwipePageBackground } from './PokemonSwipePageBackground'
import { pokemon } from './pokemon'

export const PokemonSwipePage = () => {
const { top } = useSafeAreaInsets()
const { top, bottom } = useSafeAreaInsets()
const { width, height } = useSafeAreaFrame()
const ref = useRef<SwipeableCardStackRef>(null)
const [score, setScore] = useState(0)
Expand Down Expand Up @@ -64,7 +63,7 @@ export const PokemonSwipePage = () => {
<GoToHomeButtonContainer style={css({ top: top + 16 })}>
<GoToHomeButton />
</GoToHomeButtonContainer>
<UnswipeButtonContainer>
<UnswipeButtonContainer style={{ bottom }}>
<RoundButton
Icon={UndoIcon}
onPress={() => {
Expand All @@ -74,7 +73,7 @@ export const PokemonSwipePage = () => {
size={56}
/>
</UnswipeButtonContainer>
<PokemonControlsContainer>
<PokemonControlsContainer style={{ bottom }}>
<PokemonControls
onPress={(element) => {
const mapping: Record<PokemonElement, SwipeDirection> = {
Expand All @@ -100,16 +99,14 @@ const GoToHomeButtonContainer = styled.View({
left: 16,
})

const PokemonControlsContainer = styled(SafeAreaView)({
const UnswipeButtonContainer = styled.View({
position: 'absolute',
bottom: 0,
right: 0,
left: 0,
padding: 16,
})

const UnswipeButtonContainer = styled(SafeAreaView)({
const PokemonControlsContainer = styled.View({
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
padding: 16,
})
36 changes: 19 additions & 17 deletions example-app/src/pokemon/PokemonSwipePageBackground.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled from '@emotion/native'
import { SafeAreaView } from 'react-native-safe-area-context'
import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context'
import { getBrandedText } from '../shared/fonts/getBrandedText'
import { PokemonElementView } from './PokemonElementView'

Expand All @@ -11,24 +11,26 @@ type Props = {
export const PokemonSwipePageBackground = ({
score,
numberOfSwipes,
}: Props) => (
<Container>
<ScoreContainer>
<ScoreLabel>{`Score: ${score}/${numberOfSwipes}`}</ScoreLabel>
</ScoreContainer>
<PokemonElementView element="grass" />
<MiddleRow>
<PokemonElementView element="fire" />
<PokemonElementView element="water" />
</MiddleRow>
<PokemonElementView element="flying" />
</Container>
)
}: Props) => {
const { top, bottom } = useSafeAreaInsets()

return (
<Container style={{ top, bottom }}>
<ScoreContainer>
<ScoreLabel>{`Score: ${score}/${numberOfSwipes}`}</ScoreLabel>
</ScoreContainer>
<PokemonElementView element="grass" />
<MiddleRow>
<PokemonElementView element="fire" />
<PokemonElementView element="water" />
</MiddleRow>
<PokemonElementView element="flying" />
</Container>
)
}

const Container = styled(SafeAreaView)({
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
justifyContent: 'space-between',
Expand All @@ -43,7 +45,7 @@ const MiddleRow = styled.View({
alignItems: 'center',
})

const ScoreContainer = styled(SafeAreaView)({
const ScoreContainer = styled.View({
position: 'absolute',
top: 0,
right: 0,
Expand Down
10 changes: 5 additions & 5 deletions library/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-swipeable-card-stack",
"description": "Implement a swipeable card stack, similar to Tinder, with ease.",
"version": "1.8.0",
"version": "1.9.0",
"main": "dist/index.js",
"repository": "https://github.com/antoine-cottineau/react-native-swipeable-card-stack",
"author": "Antoine Cottineau",
Expand All @@ -26,10 +26,10 @@
"babel-preset-expo": "~11.0.0",
"jest": "^29.3.1",
"jest-expo": "~51.0.3",
"react": "18.2.0",
"react-native": "0.74.3",
"react-native-gesture-handler": "~2.16.1",
"react-native-reanimated": "~3.10.1",
"react": "18.3.1",
"react-native": "0.76.3",
"react-native-gesture-handler": "~2.20.2",
"react-native-reanimated": "~3.16.1",
"react-test-renderer": "^18.2.0",
"typescript": "~5.3.3"
},
Expand Down
22 changes: 11 additions & 11 deletions library/src/domain/SwipeAxisDependentProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ export const extractPropValue = <T>(
prop: SwipeAxisDependentProp<T>,
axis: SwipeAxis,
) => {
if (isRecordOfSwipeAxis(prop)) {
'worklet'
// I'm forced to inline the check as extracting it in a function crashes the app
if (
(<T>(value: unknown): value is Record<SwipeAxis, T> => {
if (typeof value !== 'object' || value === null) {
return false
}

return allAxis.every((axis) => axis in value)
})(prop)
) {
return prop[axis]
}
return prop
}

const isRecordOfSwipeAxis = <T>(
value: unknown,
): value is Record<SwipeAxis, T> => {
if (typeof value !== 'object' || value === null) {
return false
}

return allAxis.every((axis) => axis in value)
}
1 change: 1 addition & 0 deletions library/src/domain/getSwipeDirection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const getSwipeDirection = ({
xEndedSwipePosition,
yEndedSwipePosition,
}: Params): SwipeDirection => {
'worklet'
const horizontalRatio = Math.abs(xTranslation / xEndedSwipePosition)
const verticalRatio = Math.abs(yTranslation / yEndedSwipePosition)

Expand Down
1 change: 1 addition & 0 deletions library/src/domain/isSwipeLocked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const isSwipeLocked = ({
axis,
lockedDirections,
}: Params): boolean => {
'worklet'
const isXSwipeLocked =
(lockedDirections.includes('left') && translation < 0) ||
(lockedDirections.includes('right') && translation > 0)
Expand Down
6 changes: 4 additions & 2 deletions library/src/view/SwipeableCardStackOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ export type SwipeableCardStackOptions = {
validateSwipeVelocityThreshold: SwipeAxisDependentProp<number>

/**
* A function that returns a reanimated [SpringConfig](https://docs.swmansion.com/react-native-reanimated/docs/animations/withSpring/) that will be used in the final animation once a swipe is validated.
* A workletized function that returns a reanimated [SpringConfig](https://docs.swmansion.com/react-native-reanimated/docs/animations/withSpring/) that will be used in the final animation once a swipe is validated.
*
* **WARNING**: If this function isn't a worklet, the app will crash.
*
* This prop can also accept an object whose keys are swipe axis ("x" and "y") and whose values are functions that return a reanimated [SpringConfig](https://docs.swmansion.com/react-native-reanimated/docs/animations/withSpring/).
*
* `payload` is a gesture-handler payload that you can use to customize the config.
*
* Default value: `() => ({ duration: 300 })`.
* Default value: `() => { "worklet"; return ({ duration: 300 }) }`.
*/
validatedSwipeAnimationConfig: SwipeAxisDependentProp<
(payload: PanGestureHandlerEventPayload) => WithSpringConfig
Expand Down
13 changes: 8 additions & 5 deletions library/src/view/SwipeableCardWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ export const SwipeableCardWrapper = forwardRef(function SwipeableCardWrapper(

const panGesture = Gesture.Pan()
.onStart(({ translationX }) => {
'worklet'
runOnJS(onCardSwipeStatusUpdated)({
direction: translationX > 0 ? 'right' : 'left',
phase: 'started',
})
})
.onUpdate(({ translationX, translationY }) => {
'worklet'
if (
!isSwipeLocked({
translation: translationX,
Expand All @@ -146,6 +148,7 @@ export const SwipeableCardWrapper = forwardRef(function SwipeableCardWrapper(
}
})
.onEnd((payload) => {
'worklet'
const { translationX, translationY, velocityX, velocityY } = payload
const direction = getSwipeDirection({
xTranslation: translationX,
Expand Down Expand Up @@ -200,13 +203,13 @@ export const SwipeableCardWrapper = forwardRef(function SwipeableCardWrapper(

runOnJS(onCardSwipeStatusUpdated)({ direction, phase: 'stopped' })

const targetAnimationPosition = withTiming(
0,
extractPropValue(options.stoppedSwipeAnimationConfig, axis),
const animationConfig = extractPropValue(
options.stoppedSwipeAnimationConfig,
axis,
)

xAnimationPosition.value = targetAnimationPosition
yAnimationPosition.value = targetAnimationPosition
xAnimationPosition.value = withTiming(0, animationConfig)
yAnimationPosition.value = withTiming(0, animationConfig)
})
.withTestId(`swipeable-card-wrapper-${index}-gesture`)

Expand Down
5 changes: 4 additions & 1 deletion library/src/view/useDefaultOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export const useDefaultOptions = (): SwipeableCardStackOptions => {
y: 0.25 * height,
},
validateSwipeVelocityThreshold: 800,
validatedSwipeAnimationConfig: () => ({ duration: 300 }),
validatedSwipeAnimationConfig: () => {
'worklet'
return { duration: 300 }
},
imperativeSwipeAnimationConfig: {
duration: 300,
easing: Easing.inOut(Easing.quad),
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"eslint-plugin-react": "^7.34.0",
"eslint-plugin-react-hooks": "^4.6.0",
"prettier": "^3.2.5"
}
},
"packageManager": "yarn@4.5.3"
}
Loading