-
-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop removing values from
will-change
once they finish animating (#…
…2811) * Stop removing will-change when animations finish * Updating filesize * Fixing tests
- Loading branch information
1 parent
cec5d8f
commit c186b5a
Showing
8 changed files
with
27 additions
and
76 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
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
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
40 changes: 5 additions & 35 deletions
40
packages/framer-motion/src/value/use-will-change/WillChangeMotionValue.ts
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 |
---|---|---|
@@ -1,51 +1,21 @@ | ||
import { MotionValue } from ".." | ||
import { WillChange } from "./types" | ||
import { getWillChangeName } from "./get-will-change-name" | ||
import { removeItem } from "../../utils/array" | ||
import { addUniqueItem } from "../../utils/array" | ||
|
||
export class WillChangeMotionValue extends MotionValue implements WillChange { | ||
private output: string[] = [] | ||
private counts = new Map<string, number>() | ||
private values: string[] = [] | ||
|
||
add(name: string) { | ||
const styleName = getWillChangeName(name) | ||
|
||
if (!styleName) return | ||
|
||
/** | ||
* Update counter. Each value has an indepdent counter | ||
* as multiple sources could be requesting the same value | ||
* gets added to will-change. | ||
*/ | ||
const prevCount = this.counts.get(styleName) || 0 | ||
this.counts.set(styleName, prevCount + 1) | ||
|
||
if (prevCount === 0) { | ||
this.output.push(styleName) | ||
if (styleName) { | ||
addUniqueItem(this.values, styleName) | ||
this.update() | ||
} | ||
|
||
/** | ||
* Prevents the remove function from being called multiple times. | ||
*/ | ||
let hasRemoved = false | ||
|
||
return () => { | ||
if (hasRemoved) return | ||
|
||
hasRemoved = true | ||
|
||
const newCount = this.counts.get(styleName)! - 1 | ||
this.counts.set(styleName, newCount) | ||
|
||
if (newCount === 0) { | ||
removeItem(this.output, styleName) | ||
this.update() | ||
} | ||
} | ||
} | ||
|
||
private update() { | ||
this.set(this.output.length ? this.output.join(", ") : "auto") | ||
this.set(this.values.length ? this.values.join(", ") : "auto") | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type { MotionValue } from ".." | ||
|
||
export interface WillChange extends MotionValue { | ||
add(name: string): undefined | VoidFunction | ||
add(name: string): void | ||
} |