2022-04-18 14:02:16 +02:00
|
|
|
import React, { useCallback, useContext, useRef, useState } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2023-02-15 19:07:33 +01:00
|
|
|
import classnames from 'classnames';
|
2022-04-18 14:02:16 +02:00
|
|
|
import { I18nContext } from '../../../contexts/i18n';
|
2023-02-15 19:07:33 +01:00
|
|
|
import { Button } from '../../component-library';
|
|
|
|
import Box from '../../ui/box';
|
2022-04-18 14:02:16 +02:00
|
|
|
import {
|
2023-02-02 21:15:26 +01:00
|
|
|
AlignItems,
|
2022-04-18 14:02:16 +02:00
|
|
|
DISPLAY,
|
2023-02-02 21:15:26 +01:00
|
|
|
JustifyContent,
|
2022-04-18 14:02:16 +02:00
|
|
|
} from '../../../helpers/constants/design-system';
|
|
|
|
|
|
|
|
const radius = 14;
|
|
|
|
const strokeWidth = 2;
|
|
|
|
const radiusWithStroke = radius - strokeWidth / 2;
|
|
|
|
|
|
|
|
export default function HoldToRevealButton({ buttonText, onLongPressed }) {
|
|
|
|
const t = useContext(I18nContext);
|
|
|
|
const isLongPressing = useRef(false);
|
|
|
|
const [isUnlocking, setIsUnlocking] = useState(false);
|
|
|
|
const [hasTriggeredUnlock, setHasTriggeredUnlock] = useState(false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevent animation events from propogating up
|
|
|
|
*
|
|
|
|
* @param e - Native animation event - React.AnimationEvent<HTMLDivElement>
|
|
|
|
*/
|
|
|
|
const preventPropogation = (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event for mouse click down
|
|
|
|
*/
|
|
|
|
const onMouseDown = () => {
|
|
|
|
isLongPressing.current = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event for mouse click up
|
|
|
|
*/
|
|
|
|
const onMouseUp = () => {
|
|
|
|
isLongPressing.current = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 1. Progress cirle completed. Begin next animation phase (Shrink halo and show unlocked padlock)
|
|
|
|
*/
|
|
|
|
const onProgressComplete = () => {
|
|
|
|
isLongPressing.current && setIsUnlocking(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 2. Trigger onLongPressed callback. Begin next animation phase (Shrink unlocked padlock and fade in original content)
|
|
|
|
*
|
|
|
|
* @param e - Native animation event - React.AnimationEvent<HTMLDivElement>
|
|
|
|
*/
|
2022-07-26 20:10:51 +02:00
|
|
|
const triggerOnLongPressed = useCallback(
|
|
|
|
(e) => {
|
|
|
|
onLongPressed();
|
|
|
|
setHasTriggeredUnlock(true);
|
|
|
|
preventPropogation(e);
|
|
|
|
},
|
|
|
|
[onLongPressed],
|
|
|
|
);
|
2022-04-18 14:02:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 3. Reset animation states
|
|
|
|
*/
|
|
|
|
const resetAnimationStates = () => {
|
|
|
|
setIsUnlocking(false);
|
|
|
|
setHasTriggeredUnlock(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderPreCompleteContent = useCallback(() => {
|
|
|
|
return (
|
|
|
|
<Box
|
2023-02-15 19:07:33 +01:00
|
|
|
className={classnames('hold-to-reveal-button__absolute-fill', {
|
|
|
|
'hold-to-reveal-button__absolute-fill': isUnlocking,
|
|
|
|
'hold-to-reveal-button__main-icon-show': hasTriggeredUnlock,
|
|
|
|
})}
|
2022-04-18 14:02:16 +02:00
|
|
|
>
|
|
|
|
<Box className="hold-to-reveal-button__absolute-fill">
|
|
|
|
<svg className="hold-to-reveal-button__circle-svg">
|
|
|
|
<circle
|
|
|
|
className="hold-to-reveal-button__circle-background"
|
|
|
|
cx={radius}
|
|
|
|
cy={radius}
|
|
|
|
r={radiusWithStroke}
|
|
|
|
/>
|
|
|
|
</svg>
|
|
|
|
</Box>
|
|
|
|
<Box className="hold-to-reveal-button__absolute-fill">
|
|
|
|
<svg className="hold-to-reveal-button__circle-svg">
|
|
|
|
<circle
|
|
|
|
onTransitionEnd={onProgressComplete}
|
|
|
|
className="hold-to-reveal-button__circle-foreground"
|
|
|
|
cx={radius}
|
|
|
|
cy={radius}
|
|
|
|
r={radiusWithStroke}
|
|
|
|
/>
|
|
|
|
</svg>
|
|
|
|
</Box>
|
|
|
|
<Box
|
|
|
|
display={DISPLAY.FLEX}
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.center}
|
|
|
|
justifyContent={JustifyContent.center}
|
2022-04-18 14:02:16 +02:00
|
|
|
className="hold-to-reveal-button__lock-icon-container"
|
|
|
|
>
|
|
|
|
<img
|
|
|
|
src="images/lock-icon.svg"
|
|
|
|
alt={t('padlock')}
|
|
|
|
className="hold-to-reveal-button__lock-icon"
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}, [isUnlocking, hasTriggeredUnlock, t]);
|
|
|
|
|
|
|
|
const renderPostCompleteContent = useCallback(() => {
|
|
|
|
return isUnlocking ? (
|
|
|
|
<div
|
2023-02-15 19:07:33 +01:00
|
|
|
className={classnames('hold-to-reveal-button__absolute-fill', {
|
|
|
|
'hold-to-reveal-button__unlock-icon-hide': hasTriggeredUnlock,
|
|
|
|
})}
|
2022-04-18 14:02:16 +02:00
|
|
|
onAnimationEnd={resetAnimationStates}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
onAnimationEnd={preventPropogation}
|
|
|
|
className="hold-to-reveal-button__absolute-fill hold-to-reveal-button__circle-static-outer-container"
|
|
|
|
>
|
|
|
|
<svg className="hold-to-reveal-button__circle-svg">
|
|
|
|
<circle
|
|
|
|
className="hold-to-reveal-button__circle-static-outer"
|
|
|
|
cx={14}
|
|
|
|
cy={14}
|
|
|
|
r={14}
|
|
|
|
/>
|
|
|
|
</svg>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
onAnimationEnd={preventPropogation}
|
|
|
|
className="hold-to-reveal-button__absolute-fill hold-to-reveal-button__circle-static-inner-container"
|
|
|
|
>
|
|
|
|
<svg className="hold-to-reveal-button__circle-svg">
|
|
|
|
<circle
|
|
|
|
className="hold-to-reveal-button__circle-static-inner"
|
|
|
|
cx={14}
|
|
|
|
cy={14}
|
|
|
|
r={12}
|
|
|
|
/>
|
|
|
|
</svg>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className="hold-to-reveal-button__unlock-icon-container"
|
|
|
|
onAnimationEnd={triggerOnLongPressed}
|
|
|
|
>
|
|
|
|
<img
|
|
|
|
src="images/unlock-icon.svg"
|
|
|
|
alt={t('padlock')}
|
|
|
|
className="hold-to-reveal-button__unlock-icon"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : null;
|
2022-05-16 20:38:04 +02:00
|
|
|
}, [isUnlocking, hasTriggeredUnlock, triggerOnLongPressed, t]);
|
2022-04-18 14:02:16 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
onMouseDown={onMouseDown}
|
|
|
|
onMouseUp={onMouseUp}
|
|
|
|
className="hold-to-reveal-button__button-hold"
|
|
|
|
>
|
2023-02-15 19:07:33 +01:00
|
|
|
<Box className="hold-to-reveal-button__icon-container">
|
|
|
|
{renderPreCompleteContent()}
|
|
|
|
{renderPostCompleteContent()}
|
|
|
|
</Box>
|
2022-04-18 14:02:16 +02:00
|
|
|
{buttonText}
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
HoldToRevealButton.propTypes = {
|
|
|
|
/**
|
|
|
|
* Text to be displayed on the button
|
|
|
|
*/
|
|
|
|
buttonText: PropTypes.string.isRequired,
|
|
|
|
/**
|
|
|
|
* Function to be called after the animation is finished
|
|
|
|
*/
|
|
|
|
onLongPressed: PropTypes.func.isRequired,
|
|
|
|
};
|