Skip to content

Commit

Permalink
Make @lit-labs/motion pass internal linters (#3860)
Browse files Browse the repository at this point in the history
* Reference css values with quotes for rename safety

* Mark promises in async functions when we're deliberately not awaiting them

Please review this change carefully to be sure that we shouldn't actually be awaiting in these two spots.

* Fix lint.

* Add empty changeset, as this is a no-op change

* Add missing @license comments
  • Loading branch information
rictic committed Apr 28, 2023
1 parent b362585 commit c134604
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .changeset/old-kangaroos-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
5 changes: 5 additions & 0 deletions packages/labs/motion/src/animate-controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import {ReactiveControllerHost} from 'lit';
import {Animate, Options} from './animate.js';

Expand Down
48 changes: 31 additions & 17 deletions packages/labs/motion/src/animate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import {LitElement, ReactiveControllerHost} from 'lit';
import {nothing, AttributePart} from 'lit/html.js';
import {directive, PartInfo, PartType} from 'lit/directive.js';
Expand Down Expand Up @@ -330,12 +335,12 @@ export class Animate extends AsyncDirective {
: frames;
// adjust z so always on top...
z++;
frames!.forEach((f) => (f.zIndex = z));
frames!.forEach((f) => (f['zIndex'] = z));
} else if (this.options.in) {
frames = [...this.options.in, {}];
}
}
this.animate(frames, animationOptions);
noAwait(this.animate(frames, animationOptions));
}

resetStyles() {
Expand Down Expand Up @@ -383,8 +388,9 @@ export class Animate extends AsyncDirective {
// or animation but setting left/top should be rare, especially via
// animation.
const left =
(this._fromValues!.left as number) - (shifted.left as number);
const top = (this._fromValues!.top as number) - (shifted.top as number);
(this._fromValues!['left'] as number) - (shifted['left'] as number);
const top =
(this._fromValues!['top'] as number) - (shifted['top'] as number);
const isStatic = getComputedStyle(this.element).position === 'static';
if (isStatic && (left !== 0 || top !== 0)) {
this.element.style.position = 'relative';
Expand Down Expand Up @@ -478,21 +484,21 @@ export class Animate extends AsyncDirective {
if (ancestorProps !== undefined) {
// gather scaling data for ancestors
ancestorProps.forEach((a) => {
if (a.width) {
dScaleX = dScaleX / (a.width as number);
if (a['width']) {
dScaleX = dScaleX / (a['width'] as number);
}
if (a.height) {
dScaleY = dScaleY / (a.height as number);
if (a['height']) {
dScaleY = dScaleY / (a['height'] as number);
}
});
// Move position by ancestor scaling amount.
if (from.left !== undefined && to.left !== undefined) {
from.left = dScaleX * (from.left as number);
to.left = dScaleX * (to.left as number);
if (from['left'] !== undefined && to['left'] !== undefined) {
from['left'] = dScaleX * (from['left'] as number);
to['left'] = dScaleX * (to['left'] as number);
}
if (from.top !== undefined && to.top !== undefined) {
from.top = dScaleY * (from.top as number);
to.top = dScaleY * (to.top as number);
if (from['top'] !== undefined && to['top'] !== undefined) {
from['top'] = dScaleY * (from['top'] as number);
to['top'] = dScaleY * (to['top'] as number);
}
}
return {from, to};
Expand All @@ -515,15 +521,17 @@ export class Animate extends AsyncDirective {
if (op.transform !== undefined) {
props[p] = op.value!;
hasFrames = true;
fromFrame.transform = `${fromFrame.transform ?? ''} ${op.transform}`;
fromFrame['transform'] = `${fromFrame['transform'] ?? ''} ${
op['transform']
}`;
}
} else if (f !== t && f !== undefined && t !== undefined) {
hasFrames = true;
fromFrame[p] = f;
toFrame[p] = t;
}
}
fromFrame.transformOrigin = toFrame.transformOrigin = center
fromFrame['transformOrigin'] = toFrame['transformOrigin'] = center
? 'center center'
: 'top left';
this.animatingProperties = props;
Expand All @@ -547,7 +555,7 @@ export class Animate extends AsyncDirective {
didAnimate = true;
this.webAnimation = this.element.animate(frames, options);
const controller = this.getController();
controller?.add(this);
noAwait(controller?.add(this));
try {
await this.webAnimation.finished;
} catch (e) {
Expand All @@ -573,6 +581,12 @@ export class Animate extends AsyncDirective {
}
}

/**
* Used in an async function to mark a promise that we're deliberately not
* awaiting.
*/
function noAwait(_p: null | undefined | Promise<unknown>) {}

/**
* The `animate` directive animates a node's layout between renders.
* It will perform a "tweening" animation between the two states based on
Expand Down
5 changes: 5 additions & 0 deletions packages/labs/motion/src/position.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import {LitElement} from 'lit';
import {nothing, AttributePart} from 'lit/html.js';
import {directive, PartInfo, PartType} from 'lit/directive.js';
Expand Down

0 comments on commit c134604

Please sign in to comment.