From 44e238a3bbc46804536f5470f2e7426f576f9354 Mon Sep 17 00:00:00 2001 From: Matt Perry Date: Tue, 14 Feb 2023 17:35:11 +0100 Subject: [PATCH] Removing more optional chains --- packages/framer-motion/package.json | 6 +-- .../drag/VisualElementDragControls.ts | 2 +- .../src/motion/features/viewport/index.ts | 2 +- .../src/projection/geometry/delta-calc.ts | 14 ++++++- .../src/projection/geometry/delta-remove.ts | 16 +++++++- .../projection/node/create-projection-node.ts | 39 ++++++++++++------- 6 files changed, 55 insertions(+), 24 deletions(-) diff --git a/packages/framer-motion/package.json b/packages/framer-motion/package.json index dfdf61eadb..366435a645 100644 --- a/packages/framer-motion/package.json +++ b/packages/framer-motion/package.json @@ -72,7 +72,7 @@ "bundlesize": [ { "path": "./dist/size-rollup-motion.js", - "maxSize": "29.89 kB" + "maxSize": "29.82 kB" }, { "path": "./dist/size-rollup-m.js", @@ -84,7 +84,7 @@ }, { "path": "./dist/size-rollup-dom-max.js", - "maxSize": "25.65 kB" + "maxSize": "25.56 kB" }, { "path": "./dist/size-webpack-m.js", @@ -96,7 +96,7 @@ }, { "path": "./dist/size-webpack-dom-max.js", - "maxSize": "30.5 kB" + "maxSize": "30.45 kB" } ], "gitHead": "ee87da357b48f83024e97e581157a5d6f620ce30" diff --git a/packages/framer-motion/src/gestures/drag/VisualElementDragControls.ts b/packages/framer-motion/src/gestures/drag/VisualElementDragControls.ts index fcef4ada26..3b6676796c 100644 --- a/packages/framer-motion/src/gestures/drag/VisualElementDragControls.ts +++ b/packages/framer-motion/src/gestures/drag/VisualElementDragControls.ts @@ -437,7 +437,7 @@ export class VisualElementDragControls { ? externalMotionValue : this.visualElement.getValue( axis, - (props.initial && props.initial[axis]) || 0 + (props.initial ? props.initial[axis] : undefined) || 0 ) } diff --git a/packages/framer-motion/src/motion/features/viewport/index.ts b/packages/framer-motion/src/motion/features/viewport/index.ts index 463b73f85e..a296c02a89 100644 --- a/packages/framer-motion/src/motion/features/viewport/index.ts +++ b/packages/framer-motion/src/motion/features/viewport/index.ts @@ -20,7 +20,7 @@ export class InViewFeature extends Feature { const { root, margin: rootMargin, amount = "some", once } = viewport const options = { - root: root?.current, + root: root ? root.current : undefined, rootMargin, threshold: typeof amount === "number" ? amount : thresholdNames[amount], diff --git a/packages/framer-motion/src/projection/geometry/delta-calc.ts b/packages/framer-motion/src/projection/geometry/delta-calc.ts index 43c84225c4..6560607dcd 100644 --- a/packages/framer-motion/src/projection/geometry/delta-calc.ts +++ b/packages/framer-motion/src/projection/geometry/delta-calc.ts @@ -33,8 +33,18 @@ export function calcBoxDelta( target: Box, origin?: ResolvedValues ): void { - calcAxisDelta(delta.x, source.x, target.x, origin?.originX as number) - calcAxisDelta(delta.y, source.y, target.y, origin?.originY as number) + calcAxisDelta( + delta.x, + source.x, + target.x, + origin ? (origin.originX as number) : undefined + ) + calcAxisDelta( + delta.y, + source.y, + target.y, + origin ? (origin.originY as number) : undefined + ) } export function calcRelativeAxis(target: Axis, relative: Axis, parent: Axis) { diff --git a/packages/framer-motion/src/projection/geometry/delta-remove.ts b/packages/framer-motion/src/projection/geometry/delta-remove.ts index c442af2765..4f12c223a8 100644 --- a/packages/framer-motion/src/projection/geometry/delta-remove.ts +++ b/packages/framer-motion/src/projection/geometry/delta-remove.ts @@ -106,6 +106,18 @@ export function removeBoxTransforms( originBox?: Box, sourceBox?: Box ): void { - removeAxisTransforms(box.x, transforms, xKeys, originBox?.x, sourceBox?.x) - removeAxisTransforms(box.y, transforms, yKeys, originBox?.y, sourceBox?.y) + removeAxisTransforms( + box.x, + transforms, + xKeys, + originBox ? originBox.x : undefined, + sourceBox ? sourceBox.x : undefined + ) + removeAxisTransforms( + box.y, + transforms, + yKeys, + originBox ? originBox.y : undefined, + sourceBox ? sourceBox.y : undefined + ) } diff --git a/packages/framer-motion/src/projection/node/create-projection-node.ts b/packages/framer-motion/src/projection/node/create-projection-node.ts index 9c83ba3bd6..295467a97f 100644 --- a/packages/framer-motion/src/projection/node/create-projection-node.ts +++ b/packages/framer-motion/src/projection/node/create-projection-node.ts @@ -425,8 +425,8 @@ export function createProjectionNode({ // TODO: Check here if an animation exists const layoutTransition = - this.options.transition ?? - visualElement.getDefaultTransition() ?? + this.options.transition || + visualElement.getDefaultTransition() || defaultLayoutTransition const { @@ -579,8 +579,9 @@ export function createProjectionNode({ if (layoutId === undefined && !layout) return const transformTemplate = this.getTransformTemplate() - this.prevTransformTemplateValue = - transformTemplate && transformTemplate(this.latestValues, "") + this.prevTransformTemplateValue = transformTemplate + ? transformTemplate(this.latestValues, "") + : undefined this.updateSnapshot() shouldNotifyListeners && this.notifyListeners("willUpdate") @@ -1212,7 +1213,10 @@ export function createProjectionNode({ scheduleRender(notifyAll = true) { this.options.scheduleRender && this.options.scheduleRender() - notifyAll && this.getStack()?.scheduleRender() + if (notifyAll) { + const stack = this.getStack() + stack && stack.scheduleRender() + } if (this.resumingFrom && !this.resumingFrom.instance) { this.resumingFrom = undefined } @@ -1232,7 +1236,9 @@ export function createProjectionNode({ hasOnlyRelativeTargetChanged: boolean = false ) { const snapshot = this.snapshot - const snapshotLatestValues = snapshot?.latestValues || {} + const snapshotLatestValues = snapshot + ? snapshot.latestValues + : undefined || {} const mixedValues = { ...this.latestValues } const targetDelta = createDelta() @@ -1268,7 +1274,8 @@ export function createProjectionNode({ this.relativeTarget && this.relativeTargetOrigin && this.layout && - this.relativeParent?.layout + this.relativeParent && + this.relativeParent.layout ) { calcRelativePosition( relativeLayout, @@ -1308,7 +1315,7 @@ export function createProjectionNode({ startAnimation(options: AnimationOptions) { this.notifyListeners("animationStart") - this.currentAnimation?.stop() + this.currentAnimation && this.currentAnimation.stop() if (this.resumingFrom) { this.resumingFrom.currentAnimation?.stop() } @@ -1328,10 +1335,10 @@ export function createProjectionNode({ ...(options as any), onUpdate: (latest: number) => { this.mixTargetDelta(latest) - options.onUpdate?.(latest) + options.onUpdate && options.onUpdate(latest) }, onComplete: () => { - options.onComplete?.() + options.onComplete && options.onComplete() this.completeAnimation() }, }) @@ -1350,7 +1357,8 @@ export function createProjectionNode({ this.resumingFrom.preserveOpacity = undefined } - this.getStack()?.exitAnimationComplete() + const stack = this.getStack() + stack && stack.exitAnimationComplete() this.resumingFrom = this.currentAnimation = this.animationValues = @@ -1361,7 +1369,7 @@ export function createProjectionNode({ finishAnimation() { if (this.currentAnimation) { - this.mixTargetDelta?.(animationTarget) + this.mixTargetDelta && this.mixTargetDelta(animationTarget) this.currentAnimation.stop() } @@ -1533,7 +1541,7 @@ export function createProjectionNode({ // Force a render of this element to apply the transform with all rotations // set to 0. - visualElement?.render() + visualElement.render() // Put back all the values we reset for (const key in resetValues) { @@ -1803,7 +1811,8 @@ function notifyLayoutUpdate(node: IProjectionNode) { hasRelativeTargetChanged, }) } else if (node.isLead()) { - node.options.onExitComplete?.() + const { onExitComplete } = node.options + onExitComplete && onExitComplete() } /** @@ -1841,7 +1850,7 @@ function clearMeasurements(node: IProjectionNode) { function resetTransformStyle(node: IProjectionNode) { const { visualElement } = node.options - if (visualElement?.getProps().onBeforeLayoutMeasure) { + if (visualElement && visualElement.getProps().onBeforeLayoutMeasure) { visualElement.notify("BeforeLayoutMeasure") }