-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Label): celebration animation (#2032)
- Loading branch information
Showing
6 changed files
with
286 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
packages/core/src/components/Label/LabelCelebrationAnimation.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
.celebration { | ||
// Fallback perimeter, changes according to the path length | ||
--container-perimeter: 840; | ||
position: relative; | ||
|
||
.svg { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
height: 100%; | ||
width: 100%; | ||
|
||
.stroke { | ||
fill: none; | ||
stroke-width: 1; | ||
stroke-linecap: round; | ||
stroke-linejoin: round; | ||
animation: stroke-rotate cubic-bezier(0.33, 0, 0.67, 1) forwards; | ||
stroke-dasharray: var(--container-perimeter); | ||
stroke-dashoffset: var(--container-perimeter); | ||
|
||
&.base { | ||
stroke: var(--color-egg_yolk); | ||
animation: fade 200ms linear forwards; | ||
animation-delay: 80ms; | ||
stroke-dasharray: 0; | ||
stroke-dashoffset: 0; | ||
opacity: 0; | ||
} | ||
|
||
&.first { | ||
stroke: var(--color-done-green); | ||
animation-delay: 80ms; | ||
animation-duration: 320ms; | ||
} | ||
|
||
&.second { | ||
stroke: var(--color-stuck-red); | ||
animation-delay: 200ms; | ||
animation-duration: 320ms; | ||
} | ||
|
||
&.third { | ||
stroke: var(--primary-color); | ||
animation-delay: 360ms; | ||
animation-duration: 320ms; | ||
} | ||
} | ||
} | ||
|
||
[data-celebration-text] { | ||
background-size: 300% 100%; | ||
background-repeat: no-repeat; | ||
background-clip: text; | ||
-webkit-background-clip: text; | ||
color: transparent; | ||
animation: gradient-text-fill 680ms linear forwards; | ||
background-image: linear-gradient( | ||
to right, | ||
var(--primary-color) 30%, | ||
var(--color-stuck-red) 40%, | ||
var(--color-done-green) 60%, | ||
var(--color-egg_yolk) 85%, | ||
transparent 90% | ||
); | ||
} | ||
} | ||
|
||
@keyframes fade { | ||
from { | ||
opacity: 0; | ||
} | ||
to { | ||
opacity: 1; | ||
} | ||
} | ||
|
||
@keyframes stroke-rotate { | ||
to { | ||
stroke-dashoffset: 0; | ||
} | ||
} | ||
|
||
@keyframes gradient-text-fill { | ||
from { | ||
background-position: 150% 0; | ||
} | ||
to { | ||
background-position: 0% 0; | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
packages/core/src/components/Label/LabelCelebrationAnimation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React, { cloneElement, forwardRef, useCallback, useRef, useState } from "react"; | ||
import cx from "classnames"; | ||
import useResizeObserver from "../../hooks/useResizeObserver"; | ||
import styles from "./LabelCelebrationAnimation.module.scss"; | ||
|
||
const DEFAULT_BORDER_RADIUS = 4; | ||
const DEFAULT_STROKE_WIDTH = 1; | ||
|
||
export interface LabelCelebrationAnimationProps { | ||
children: React.ReactElement; | ||
onAnimationEnd: () => void; | ||
} | ||
|
||
function LabelCelebrationAnimation({ children, onAnimationEnd }: LabelCelebrationAnimationProps) { | ||
const wrapperRef = useRef<HTMLDivElement>(); | ||
const childRef = useRef<HTMLDivElement>(); | ||
|
||
const [path, setPath] = useState<string>(); | ||
|
||
const resizeObserverCallback = useCallback( | ||
({ borderBoxSize }: { borderBoxSize: { blockSize: number; inlineSize: number } }) => { | ||
const { blockSize: height, inlineSize: width } = borderBoxSize || {}; | ||
|
||
if (wrapperRef.current) { | ||
const d = getPath({ width, height }); | ||
setPath(d); | ||
|
||
const perimeter = getPerimeter({ width, height }); | ||
wrapperRef.current.style.setProperty("--container-perimeter", String(perimeter)); | ||
} | ||
}, | ||
[] | ||
); | ||
|
||
useResizeObserver({ | ||
ref: wrapperRef, | ||
callback: resizeObserverCallback, | ||
debounceTime: 0 | ||
}); | ||
|
||
const ChildComponentWithRef = forwardRef((_props, ref) => | ||
cloneElement(children, { | ||
ref | ||
}) | ||
); | ||
|
||
return ( | ||
<div className={styles.celebration} ref={wrapperRef}> | ||
<svg className={styles.svg}> | ||
<path className={cx(styles.stroke, styles.base)} d={path} /> | ||
<path className={cx(styles.stroke, styles.first)} d={path} /> | ||
<path className={cx(styles.stroke, styles.second)} d={path} /> | ||
<path className={cx(styles.stroke, styles.third)} d={path} onAnimationEnd={onAnimationEnd} /> | ||
</svg> | ||
<ChildComponentWithRef ref={childRef} /> | ||
</div> | ||
); | ||
} | ||
|
||
export default LabelCelebrationAnimation; | ||
|
||
function getPath({ | ||
width, | ||
height, | ||
borderRadius = DEFAULT_BORDER_RADIUS, | ||
strokeWidth = DEFAULT_STROKE_WIDTH | ||
}: { | ||
width: number; | ||
height: number; | ||
borderRadius?: number; | ||
strokeWidth?: number; | ||
}) { | ||
const offset = strokeWidth / 2; | ||
|
||
return `M ${width - strokeWidth / 2}, ${borderRadius} V ${ | ||
height - borderRadius | ||
} A ${borderRadius} ${borderRadius} 0 0 1 ${width - borderRadius} ${height - strokeWidth / 2} H ${ | ||
borderRadius + offset | ||
} A ${borderRadius} ${borderRadius} 0 0 1 ${strokeWidth / 2} ${height - borderRadius} V ${ | ||
borderRadius + offset | ||
} A ${borderRadius} ${borderRadius} 0 0 1 ${borderRadius} ${strokeWidth / 2} L ${width - borderRadius}, ${ | ||
strokeWidth / 2 | ||
} A ${borderRadius} ${borderRadius} 0 0 1 ${width - strokeWidth / 2} ${borderRadius} Z`; | ||
} | ||
|
||
function getPerimeter({ | ||
width, | ||
height, | ||
borderRadius = DEFAULT_BORDER_RADIUS | ||
}: { | ||
width: number; | ||
height: number; | ||
borderRadius?: number; | ||
}) { | ||
const straightWidth = width - 2 * borderRadius; | ||
const straightHeight = height - 2 * borderRadius; | ||
const cornerCircumference = 2 * Math.PI * borderRadius; | ||
return cornerCircumference + 2 * straightWidth + 2 * straightHeight; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters