-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [bento][amp-sidebar] Update animations to obey APIs mid-animation (#…
…32584) * mostly working * Update animations to obey apis mid animation * Clean up typos and style * Update animation time * Refactor to custom hook * Review comments * Use useValueRef for side and add useValueRef to utils * Update Direction constant to AnimationState * Simplify animation states * Move value ref location * Removed test code * Review comments * Add a comment explaning side * Tests for animations
- Loading branch information
Showing
7 changed files
with
410 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/** | ||
* Copyright 2021 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {Side} from './sidebar-config'; | ||
import {assertDoesNotContainDisplay, setStyles} from '../../../src/style'; | ||
import {useLayoutEffect, useRef} from '../../../src/preact'; | ||
import {useValueRef} from '../../../src/preact/component'; | ||
|
||
const ANIMATION_DURATION = 350; | ||
const ANIMATION_EASE_IN = 'cubic-bezier(0,0,.21,1)'; | ||
|
||
const ANIMATION_KEYFRAMES_FADE_IN = [{'opacity': '0'}, {'opacity': '1'}]; | ||
const ANIMATION_KEYFRAMES_SLIDE_IN_LEFT = [ | ||
{'transform': 'translateX(-100%)'}, | ||
{'transform': 'translateX(0)'}, | ||
]; | ||
const ANIMATION_KEYFRAMES_SLIDE_IN_RIGHT = [ | ||
{'transform': 'translateX(100%)'}, | ||
{'transform': 'translateX(0)'}, | ||
]; | ||
|
||
const ANIMATION_STYLES_SIDEBAR_LEFT_INIT = {'transform': 'translateX(-100%)'}; | ||
const ANIMATION_STYLES_SIDEBAR_RIGHT_INIT = {'transform': 'translateX(100%)'}; | ||
const ANIMATION_STYLES_BACKDROP_INIT = {'opacity': '0'}; | ||
const ANIMATION_STYLES_SIDEBAR_FINAL = {'transform': ''}; | ||
const ANIMATION_STYLES_BACKDROP_FINAL = {'opacity': ''}; | ||
|
||
/** | ||
* @param {!Element} element | ||
* @param {!Object<string, *>} styles | ||
*/ | ||
function safelySetStyles(element, styles) { | ||
setStyles(element, assertDoesNotContainDisplay(styles)); | ||
} | ||
|
||
/** | ||
* @param {boolean} opened | ||
* @param {{current: function|undefined}} onAfterClose | ||
* @param {string} side | ||
* @param {{current: Element|null}} sidebarRef | ||
* @param {{current: Element|null}} backdropRef | ||
* @param {function} setMounted | ||
*/ | ||
export function useSidebarAnimation( | ||
opened, | ||
onAfterClose, | ||
side, | ||
sidebarRef, | ||
backdropRef, | ||
setMounted | ||
) { | ||
const onAfterCloseRef = useValueRef(onAfterClose); | ||
const sidebarAnimationRef = useRef(null); | ||
const backdropAnimationRef = useRef(null); | ||
const currentlyAnimatingRef = useRef(false); | ||
useLayoutEffect(() => { | ||
const sidebarElement = sidebarRef.current; | ||
const backdropElement = backdropRef.current; | ||
// The component might start in a state where `side` is not known | ||
// This effect must be restarted when the `side` becomes known | ||
if (!sidebarElement || !backdropElement || !side) { | ||
return; | ||
} | ||
|
||
const postVisibleAnim = () => { | ||
safelySetStyles(sidebarElement, ANIMATION_STYLES_SIDEBAR_FINAL); | ||
safelySetStyles(backdropElement, ANIMATION_STYLES_BACKDROP_FINAL); | ||
sidebarAnimationRef.current = null; | ||
backdropAnimationRef.current = null; | ||
currentlyAnimatingRef.current = false; | ||
}; | ||
const postInvisibleAnim = () => { | ||
if (onAfterCloseRef.current) { | ||
onAfterCloseRef.current(); | ||
} | ||
sidebarAnimationRef.current = null; | ||
backdropAnimationRef.current = null; | ||
currentlyAnimatingRef.current = false; | ||
setMounted(false); | ||
}; | ||
|
||
// reverse animation if currently animating | ||
if (currentlyAnimatingRef.current) { | ||
const sidebarAnimation = sidebarAnimationRef.current; | ||
if (sidebarAnimation) { | ||
sidebarAnimation.reverse(); | ||
sidebarAnimation.onfinish = opened | ||
? postVisibleAnim | ||
: postInvisibleAnim; | ||
} | ||
const backdropAnimation = backdropAnimationRef.current; | ||
if (backdropAnimation) { | ||
backdropAnimation.reverse(); | ||
} | ||
return; | ||
} | ||
|
||
// begin animation if fully opened or closed | ||
if (opened) { | ||
// make visible animation | ||
if (!sidebarElement.animate || !backdropElement.animate) { | ||
postVisibleAnim(); | ||
return; | ||
} | ||
safelySetStyles( | ||
sidebarElement, | ||
side === Side.LEFT | ||
? ANIMATION_STYLES_SIDEBAR_LEFT_INIT | ||
: ANIMATION_STYLES_SIDEBAR_RIGHT_INIT | ||
); | ||
safelySetStyles(backdropElement, ANIMATION_STYLES_BACKDROP_INIT); | ||
const sidebarAnimation = sidebarElement.animate( | ||
side === Side.LEFT | ||
? ANIMATION_KEYFRAMES_SLIDE_IN_LEFT | ||
: ANIMATION_KEYFRAMES_SLIDE_IN_RIGHT, | ||
{ | ||
duration: ANIMATION_DURATION, | ||
fill: 'both', | ||
easing: ANIMATION_EASE_IN, | ||
} | ||
); | ||
sidebarAnimation.onfinish = postVisibleAnim; | ||
const backdropAnimation = backdropElement.animate( | ||
ANIMATION_KEYFRAMES_FADE_IN, | ||
{ | ||
duration: ANIMATION_DURATION, | ||
fill: 'both', | ||
easing: ANIMATION_EASE_IN, | ||
} | ||
); | ||
sidebarAnimationRef.current = sidebarAnimation; | ||
backdropAnimationRef.current = backdropAnimation; | ||
currentlyAnimatingRef.current = true; | ||
} else { | ||
// make invisible animation | ||
if (!sidebarElement.animate || !backdropElement.animate) { | ||
postInvisibleAnim(); | ||
return; | ||
} | ||
const sidebarAnimation = sidebarElement.animate( | ||
side === Side.LEFT | ||
? ANIMATION_KEYFRAMES_SLIDE_IN_LEFT | ||
: ANIMATION_KEYFRAMES_SLIDE_IN_RIGHT, | ||
{ | ||
duration: ANIMATION_DURATION, | ||
direction: 'reverse', | ||
fill: 'both', | ||
easing: ANIMATION_EASE_IN, | ||
} | ||
); | ||
sidebarAnimation.onfinish = postInvisibleAnim; | ||
const backdropAnimation = backdropElement.animate( | ||
ANIMATION_KEYFRAMES_FADE_IN, | ||
{ | ||
duration: ANIMATION_DURATION, | ||
direction: 'reverse', | ||
fill: 'both', | ||
easing: ANIMATION_EASE_IN, | ||
} | ||
); | ||
sidebarAnimationRef.current = sidebarAnimation; | ||
backdropAnimationRef.current = backdropAnimation; | ||
currentlyAnimatingRef.current = true; | ||
} | ||
}, [opened, onAfterCloseRef, side, sidebarRef, backdropRef, setMounted]); | ||
} |
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,21 @@ | ||
/** | ||
* Copyright 2021 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** @protected @enum {string} */ | ||
export const Side = { | ||
LEFT: 'left', | ||
RIGHT: 'right', | ||
}; |
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
Oops, something went wrong.