Skip to content
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

TypeScript conversion of animation package #12702

Merged
merged 42 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2703f8d
WIP
Nov 11, 2022
cd8582e
Merge branch 'main' into code/12207-ts-anim
Nov 11, 2022
304e92e
WIP: Partial refactor and conversion to TypeScript
Nov 16, 2022
30a0dfa
Forgot to simplify the animation creation in this file
Nov 16, 2022
c00e7d7
Merge branch 'main' into code/12207-ts-anim
Nov 17, 2022
8c72503
Merge branch 'main' into code/12207-ts-anim
Nov 17, 2022
714caae
Converted tests to TS and added to tsconfig
Nov 17, 2022
6c767c6
Fixed test for `sanitizeTimings`
Nov 17, 2022
c244c8f
Updated story editor to use new animation state enum
Nov 17, 2022
7ee5959
Fixed a bunch of references to anim enum
Nov 17, 2022
19dbb9b
Merge branch 'code/12207-ts-anim' of github.com:GoogleForCreators/web…
Nov 17, 2022
5b1ac88
Fixed constant
Nov 18, 2022
faad110
Merge branch 'main' into code/12207-ts-anim
Nov 18, 2022
5ac2e96
Excluded stories from TS
Nov 18, 2022
1ee9108
Excluded stories from TS
Nov 18, 2022
008ed8b
Fixed some bugs found in test failures
Nov 21, 2022
46d3e99
Fixed last ts errors
Nov 21, 2022
3a882a7
Merge branch 'main' into code/12207-ts-anim
Nov 21, 2022
f521fe2
Restructured animation parts
Nov 21, 2022
035b83c
Merge branch 'main' into code/12207-ts-anim
Nov 24, 2022
863c9a9
Fixed a missing export
Nov 24, 2022
e74d57b
Adjusted comment to clarify
Nov 24, 2022
c9bc3be
Fixed bug, that I feel must have been there before too?
Nov 24, 2022
ee1159c
Fixed bug due to misspelled field name
Nov 24, 2022
c8c6323
Fixed pulse keyframe generator test
Nov 24, 2022
9183145
Fixed types and test for `getGlobalSpace`
Nov 24, 2022
40a9134
Trying to fix errors in d.ts. files
Nov 24, 2022
4e93aeb
Fixed test with bad animation ids
Nov 24, 2022
611d2fb
(Re)fixed test with bad animation ids
Nov 24, 2022
494ab5b
Fix config for text-sets package
swissspidy Nov 25, 2022
2ea8b2e
Update lock file
swissspidy Nov 25, 2022
7ce8445
Fix types for amp components
swissspidy Nov 25, 2022
848917a
Update tsconfig
swissspidy Nov 25, 2022
2498304
Try to adjust web-animations-js types
swissspidy Nov 25, 2022
6014dc6
Merge branch 'main' into code/12207-ts-anim
Nov 28, 2022
c6c3e45
Removed irrelevant polyfill
Nov 28, 2022
72c630b
Moved element offset functions once more
Nov 28, 2022
7c04623
Added clarifying comment
Nov 28, 2022
0e56a96
Fixed some types
Nov 28, 2022
99a9703
Fix case
Nov 28, 2022
1e4419c
Typo
Nov 28, 2022
f864b46
Fixed import after reorg
Nov 29, 2022
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
1 change: 1 addition & 0 deletions .storybook/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = {
'../packages/wp-story-editor/src/**/stories/*.@(js|mdx)',
'../packages/activation-notice/src/**/stories/*.@(js|mdx)',
'../packages/design-system/src/**/stories/*.@(js|mdx)',
'../packages/animation/src/**/stories/*.@(js|mdx)',
],
addons: [
'@storybook/addon-a11y/register',
Expand Down
42 changes: 31 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions packages/animation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,30 @@
},
"customExports": {
".": {
"default": "./src/index.js"
"default": "./src/index.ts"
}
},
"main": "dist/index.js",
"module": "dist-module/index.js",
"source": "src/index.js",
"types": "dist-types/index.d.ts",
"source": "src/index.ts",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@googleforcreators/elements": "*",
"@googleforcreators/i18n": "*",
"@googleforcreators/media": "*",
"@googleforcreators/react": "*",
"@googleforcreators/units": "*",
"flagged": "^2.0.6",
"prop-types": "^15.8.1",
"styled-components": "^5.3.6",
"uuid": "^9.0.0",
"web-animations-js": "^2.3.2"
"uuid": "^9.0.0"
},
"devDependencies": {
"@testing-library/react": "^12.1.5",
"@testing-library/react-hooks": "^8.0.1"
"@testing-library/react-hooks": "^8.0.1",
"@types/styled-components": "^5.1.26",
"@types/web-animations-js": "^2.2.12"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ function AMPAnimations() {
actions: { getAnimationParts },
} = useStoryAnimationContext();

return animationTargets.reduce(
(animators, target) => [
...animators,
...getAnimationParts(target).map(({ id, AMPAnimation }) => (
<AMPAnimation key={id} prefixId={providerId} />
)),
],
[]
return (
<>
{animationTargets
.map((target) => [
getAnimationParts(target).map(({ id, AMPAnimation }) => (
<AMPAnimation key={id} prefixId={providerId} />
)),
])
.flat(2)}
</>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
* External dependencies
*/
import { useMemo } from '@googleforcreators/react';
import PropTypes from 'prop-types';
import type { FunctionComponent, PropsWithChildren } from 'react';

/**
* Internal dependencies
*/
import type { AnimationPart } from '../parts';
import type { WrapperProps } from './types';
import useStoryAnimationContext from './useStoryAnimationContext';

const fullSizeAbsoluteStyles = {
Expand All @@ -32,15 +34,26 @@ const fullSizeAbsoluteStyles = {
position: 'absolute',
top: 0,
left: 0,
};
} as const;

function ComposableWrapper({ animationParts, children }) {
type ComposableWrapperProps = PropsWithChildren<{
animationParts: AnimationPart[];
}>;

type Composed = PropsWithChildren<unknown>;

function ComposableWrapper({
animationParts,
children,
}: ComposableWrapperProps) {
const root: FunctionComponent = (props: Composed) =>
props.children as React.ReactElement;
const ComposedWrapper = useMemo(
() =>
animationParts.reduce(
(Composable, animationPart) => {
animationParts.reduce<FunctionComponent>(
(Composable: FunctionComponent, animationPart: AnimationPart) => {
const { AMPTarget } = animationPart;
const Composed = function (props) {
const Composed = (props: Composed) => {
return (
<Composable>
<AMPTarget style={fullSizeAbsoluteStyles}>
Expand All @@ -49,23 +62,16 @@ function ComposableWrapper({ animationParts, children }) {
</Composable>
);
};
Composed.propTypes = { children: PropTypes.node };
return Composed;
},
(props) => props.children
root
),
[animationParts]
);

return <ComposedWrapper>{children}</ComposedWrapper>;
}

ComposableWrapper.propTypes = {
animationParts: PropTypes.arrayOf(PropTypes.object),
children: PropTypes.node.isRequired,
};

function AMPWrapper({ target, children }) {
function AMPWrapper({ target, children }: WrapperProps) {
const {
actions: { getAnimationParts },
} = useStoryAnimationContext();
Expand All @@ -77,9 +83,4 @@ function AMPWrapper({ target, children }) {
);
}

AMPWrapper.propTypes = {
target: PropTypes.string,
children: PropTypes.node,
};

export default AMPWrapper;
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,36 @@
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { useCallback, useRef, useEffect } from '@googleforcreators/react';

/**
* Internal dependencies
*/
import createKeyframeEffect from '../utils/createKeyframeEffect';
import FullSizeAbsolute from './fullSizeAbsolute';
import { WAAPIAnimationProps } from './types';
import type { WAAPIAnimationWrapperProps, WrapperProps } from './types';
import useStoryAnimationContext from './useStoryAnimationContext';

function WAAPIAnimationWrapper({
children,
hoistAnimation,
keyframes,
timings,
targetLeafElement,
}) {
const target = useRef(null);
targetLeafElement = false,
}: WAAPIAnimationWrapperProps) {
const target = useRef<HTMLDivElement | null>(null);

useEffect(() => {
if (!keyframes) {
return () => {};
return () => undefined;
}

const targetEl = targetLeafElement
? target.current?.querySelector('[data-leaf-element="true"]')
: target.current;

if (!targetEl) {
return () => {};
return () => undefined;
}
const effect = createKeyframeEffect(targetEl, keyframes, timings);
return hoistAnimation(new Animation(effect, document.timeline));
Expand All @@ -55,21 +54,20 @@ function WAAPIAnimationWrapper({
return <FullSizeAbsolute ref={target}>{children}</FullSizeAbsolute>;
}

WAAPIAnimationWrapper.propTypes = WAAPIAnimationProps;

function WAAPIWrapper({ children, target }) {
const { hoistWAAPIAnimation, AnimationParts } = useStoryAnimationContext(
function WAAPIWrapper({ children, target }: WrapperProps) {
const { hoistWAAPIAnimation, animationParts } = useStoryAnimationContext(
({ actions }) => ({
hoistWAAPIAnimation: actions.hoistWAAPIAnimation,
AnimationParts: actions.getAnimationParts(target),
animationParts: actions.getAnimationParts(target),
})
);
const WAAPIAnimationParts = AnimationParts?.map(
const WAAPIAnimationParts = animationParts?.map(
(anim) => anim.WAAPIAnimation
);

const hoistAnimation = useCallback(
(animation) => hoistWAAPIAnimation({ animation, elementId: target }),
(animation: Animation) =>
hoistWAAPIAnimation({ animation, elementId: target }),
[target, hoistWAAPIAnimation]
);

Expand Down Expand Up @@ -101,9 +99,4 @@ function WAAPIWrapper({ children, target }) {
);
}

WAAPIWrapper.propTypes = {
children: PropTypes.node,
target: PropTypes.string,
};

export default WAAPIWrapper;
Loading