Skip to content

Fix/interaction feedback rework #490

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions src/components/Button/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ BasicButton.args = {
color: colors.primaryDark,
disabled: false,
feedbackType: FeedbackTypes.ripple,
// interactionFeedbackProps: {
// transitionProps: {
// from: {
// opacity: 0.5,
// r: 0,
// },
// enter: {
// opacity: 0,
// r: 100,
// },
// },
// },
isLoading: false,
elevation: 1,
isProcessing: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ export const Splash: Story<SplashProps> = ({
clamp,
},
};
const interpolationFunctions = {
r: (r: any) => r.to((val: string) => `${Math.abs(parseFloat(val)).toFixed(1)}`),
opacity: (opacity: any) => opacity.to((val: number) => val.toFixed(2)),
};
// const interpolationFunctions = {
// r: (r: any) => r.to((val: string) => `${Math.abs(parseFloat(val)).toFixed(1)}`),
// opacity: (opacity: any) => opacity.to((val: number) => val.toFixed(2)),
// };
return (
<InteractionFeedback
color={color}
interpolationFunctions={interpolationFunctions}
// interpolationFunctions={interpolationFunctions}
transitionProps={transitionProps}
>
<InteractionInnerContainer onClick={action('button-click')}>
Expand Down
83 changes: 55 additions & 28 deletions src/components/InteractionFeedback/InteractionFeedback.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { animated, useTransition } from '@react-spring/web';
import { animated, useSprings } from '@react-spring/web';
import React, { useCallback, useRef, useState } from 'react';
import styled from 'styled-components';
import useResizeObserver from 'use-resize-observer/polyfilled';
Expand All @@ -21,7 +21,8 @@ const SVGContainer = styled.svg`
`;

type Animation = { cx: string; cy: string; id: string };
type Transition = { r: string } & Animation;
// type Transition = { r: string } & Animation;

export type InteractionFeedbackProps = {
StyledContainer?: StyledSubcomponentType;
StyledSVGContainer?: StyledSubcomponentType;
Expand All @@ -30,23 +31,23 @@ export type InteractionFeedbackProps = {

children?: React.ReactNode;
color?: string;
interpolationFunctions?: Record<string, (val: any) => any>;
// interpolationFunctions?: Record<string, (val: any) => any>;
// TODO add proper type from react-spring
transitionProps?: any;
transitionProps?: Record<string, Record<string, any>>;
containerRef?: React.RefObject<HTMLDivElement>;
SVGContainerRef?: React.RefObject<SVGElement>;
};

const defaultInterpolationFunctions = {
r: (r: any) => r.to((val: string) => `${Math.abs(parseFloat(val)).toFixed(1)}`),
opacity: (opacity: any) => opacity.to((val: number) => val.toFixed(2)),
};
// const defaultInterpolationFunctions = {
// r: (r: any) => r.to((val: string) => `${Math.abs(parseFloat(val)).toFixed(1)}`),
// opacity: (opacity: any) => opacity.to((val: number) => val.toFixed(2)),
// };
const defaultTransitionProps = {
from: {
r: 0,
r: 20,
opacity: 0.5,
},
enter: {
to: {
r: 100,
opacity: 0,
},
Expand All @@ -55,7 +56,7 @@ const defaultTransitionProps = {
tension: 1000,
friction: 20,
round: 1,
clamp: false,
clamp: true,
},
};
const InteractionFeedback = ({
Expand All @@ -68,26 +69,41 @@ const InteractionFeedback = ({
color = colors.primary,

children,
interpolationFunctions = defaultInterpolationFunctions,
// interpolationFunctions = defaultInterpolationFunctions,
transitionProps = { ...defaultTransitionProps },
}: InteractionFeedbackProps): JSX.Element => {
const internalRef = useRef<HTMLDivElement>(null);
const { ref, width = 0, height = 0 } = useResizeObserver<HTMLDivElement>();
const [animations, setAnimations] = useState<Array<Animation>>([]);

const transitions = useTransition(animations, {
keys: (item: Animation) => item.id,
onRest: (item: Transition) => setAnimations(a => a.filter(ani => ani.id === item.id)),
...transitionProps,
});
const fragment = transitions((style, item) => {
const circleProps = Object.entries(style).reduce((acc, [key, val]) => {
return {
...acc,
[key]: interpolationFunctions[key] ? interpolationFunctions[key](val) : val,
};
}, {});
return <animated.circle cx={item.cx} cy={item.cy} fill={color} {...circleProps} />;
const [springs, springApi] = useSprings(
animations.length,
i => ({
onStart: () => {
console.log('started');
},
onChange: () => {
console.log('m');
},
onRest: (result, spring, item) => {
setAnimations(a => a.filter(ani => ani.id === animations[i].id));
console.log(result, spring, item);
},
...transitionProps,
}),
[],
);

const fragment = springs.map((style, ind) => {
return (
<animated.circle
key={animations[ind].id}
cx={animations[ind].cx}
cy={animations[ind].cy}
fill={color}
{...style}
/>
);
});

const mouseDownHandler = useCallback(
Expand All @@ -99,10 +115,21 @@ const InteractionFeedback = ({
const percentX = (100 * (clientX - boundingRect.left)) / boundingRect.width;
const percentY = (100 * (clientY - boundingRect.top)) / boundingRect.height;

setAnimations(a => [...a, { cx: `${percentX}%`, cy: `${percentY}%`, id: randomId(18) }]);
const newAnimation = {
cx: `${percentX}%`,
cy: `${percentY}%`,
id: `${randomId(18)}`,
};
setAnimations(a => [...a, newAnimation]);

springApi.start(i => {
if (i === animations.length) {
return transitionProps;
}
});
}
},
[internalRef],
[transitionProps, springApi, animations],
);

const handleEventWithAnalytics = useAnalytics();
Expand Down Expand Up @@ -138,6 +165,6 @@ const InteractionFeedback = ({
InteractionFeedback.Container = Container;
InteractionFeedback.SVGContainer = SVGContainer;
InteractionFeedback.defaultTransitionProps = defaultTransitionProps;
InteractionFeedback.defaultInterpolationFunctions = defaultInterpolationFunctions;
// InteractionFeedback.defaultInterpolationFunctions = defaultInterpolationFunctions;

export default InteractionFeedback;
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exports[`InteractionFeedback Shows InteractionFeedback with default props 1`] =
cy="NaN%"
fill="#DA4200"
opacity="0.5"
r="0"
r="20"
/>
</svg>
</div>
Expand Down