From 6ea96858dbe2e3282a7e37f7e12a4f3581adb552 Mon Sep 17 00:00:00 2001 From: Mark Kotarba <80314375+xnameTM@users.noreply.github.com> Date: Wed, 22 May 2024 08:34:31 +0200 Subject: [PATCH] Adding playground to withRepeat subpage (#5917) ## Summary This PR includes a playground to withRepeat subpage. ## Preview withRepeat Playground Zrzut ekranu 2024-04-19 o 14 38 33 ## Test plan Command to change current working dictionary: > cd docs Command to download libraries: > yarn Command to preview feature: > yarn start --- docs/docs/animations/withRepeat.mdx | 4 + .../InteractivePlayground/index.tsx | 22 ++++- .../useRepeatPlayground/Example.tsx | 93 +++++++++++++++++++ .../useRepeatPlayground/index.tsx | 77 +++++++++++++++ docs/src/examples/SpringCarousel.tsx | 1 - docs/src/examples/SpringMassCompare.tsx | 1 - docs/src/examples/TimingEasingCompare.tsx | 1 - docs/src/examples/UseReducedMotion.tsx | 1 - 8 files changed, 194 insertions(+), 6 deletions(-) create mode 100644 docs/src/components/InteractivePlayground/useRepeatPlayground/Example.tsx create mode 100644 docs/src/components/InteractivePlayground/useRepeatPlayground/index.tsx diff --git a/docs/docs/animations/withRepeat.mdx b/docs/docs/animations/withRepeat.mdx index 82aede5a78a..105734e0f36 100644 --- a/docs/docs/animations/withRepeat.mdx +++ b/docs/docs/animations/withRepeat.mdx @@ -71,6 +71,10 @@ A function called on animation complete. In case the animation is cancelled, the A parameter that determines how the animation responds to the device's reduced motion accessibility setting. +import { useRepeatPlayground } from '@site/src/components/InteractivePlayground'; + + + ### Returns `withRepeat` returns an [animation object](/docs/fundamentals/glossary#animation-object) which holds the current state of the animation. It can be either assigned directly to a [shared value](/docs/fundamentals/glossary#shared-value) or can be used as a value for a style object returned from [useAnimatedStyle](docs/core/useAnimatedStyle). diff --git a/docs/src/components/InteractivePlayground/index.tsx b/docs/src/components/InteractivePlayground/index.tsx index 4e0af81af05..2ff18838a66 100644 --- a/docs/src/components/InteractivePlayground/index.tsx +++ b/docs/src/components/InteractivePlayground/index.tsx @@ -10,6 +10,7 @@ import ReducedMotionWarning from '../ReducedMotionWarning'; import useClampPlayground from './useClampPlayground'; import useSpringPlayground from './useSpringPlayground'; import useTimingPlayground from './useTimingPlayground'; +import useRepeatPlayground from './useRepeatPlayground'; import useInterpolateColorPlayground from './useInterpolateColorPlayground'; import useAnimatedSensorPlayground from './useAnimatedSensorPlayground'; import useDecayPlayground from './useDecayPlayground'; @@ -30,6 +31,7 @@ export { useClampPlayground, useSpringPlayground, useTimingPlayground, + useRepeatPlayground, useInterpolateColorPlayground, useAnimatedSensorPlayground, useDecayPlayground, @@ -126,6 +128,7 @@ interface RangeProps { step?: number; value: number; onChange: Dispatch; + disabled?: boolean; label: string; } @@ -151,6 +154,18 @@ const RangeStyling = { }, }; +const DisabledRangeStyling = { + color: 'var(--swm-interactive-slider)', // color of the main path of slider + '& .MuiSlider-thumb': { + backgroundColor: '#ccc', //color of thumb + transform: 'translate(-50%, -40%)', + }, + '& .MuiSlider-rail': { + color: 'var(--swm-interactive-slider-rail)', //color of the rail (remaining area of slider) + opacity: 1, + }, +}; + const TextFieldStyling = { minWidth: 88, '& .MuiInputBase-input': { @@ -169,16 +184,18 @@ export function Range({ max, value, onChange, + disabled, label, step = 1, }: RangeProps) { return ( <>
- + onChange(parseFloat(e.target.value)) } diff --git a/docs/src/components/InteractivePlayground/useRepeatPlayground/Example.tsx b/docs/src/components/InteractivePlayground/useRepeatPlayground/Example.tsx new file mode 100644 index 00000000000..83782aafe2c --- /dev/null +++ b/docs/src/components/InteractivePlayground/useRepeatPlayground/Example.tsx @@ -0,0 +1,93 @@ +import React, { useEffect } from 'react'; +import { StyleSheet, View, Button } from 'react-native'; +import Animated, { + useSharedValue, + useAnimatedStyle, + cancelAnimation, + ReduceMotion, + withTiming, + withRepeat, +} from 'react-native-reanimated'; + +interface Props { + options: { + numberOfReps: number; + reverse: boolean; + reduceMotion: ReduceMotion; + }; +} + +export default function App({ options }: Props) { + const offset = useSharedValue(0); + const [running, setRunning] = React.useState(false); + + const animatedStyles = useAnimatedStyle(() => { + return { + transform: [{ translateX: offset.value }], + }; + }); + + useEffect(() => { + cancelAnimation(offset); + offset.value = 0; + }, [options]); + + const handlePress = () => { + if (running) { + cancelAnimation(offset); + setRunning(false); + return; + } + + setRunning(true); + offset.value = 0; + offset.value = withRepeat( + withTiming(200, { duration: 1000 }), + options.numberOfReps, + options.reverse, + () => setRunning(false), + options.reduceMotion + ); + }; + + return ( + + + + + +