-
-
Notifications
You must be signed in to change notification settings - Fork 901
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2660 from framer/fix/animate-block-none
Interpolation between visible and hidden
- Loading branch information
Showing
8 changed files
with
228 additions
and
7 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,32 @@ | ||
import { useState } from "react" | ||
import { MotionConfig, motion } from "framer-motion" | ||
|
||
/** | ||
* An example of the tween transition type | ||
*/ | ||
|
||
const style = { | ||
width: 100, | ||
height: 100, | ||
background: "white", | ||
} | ||
|
||
export const App = () => { | ||
const [state, setState] = useState(true) | ||
|
||
return ( | ||
<MotionConfig transition={{ duration: 1 }}> | ||
<motion.div | ||
initial={{ display: "block" }} | ||
animate={{ | ||
display: state ? "block" : "none", | ||
visibility: state ? "visible" : "hidden", | ||
opacity: state ? 1 : 0.2, | ||
}} | ||
onUpdate={(latest: any) => console.log(latest)} | ||
style={style} | ||
/> | ||
<button onClick={() => setState(!state)}>Toggle</button> | ||
</MotionConfig> | ||
) | ||
} |
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
18 changes: 18 additions & 0 deletions
18
packages/framer-motion/src/utils/mix/__tests__/mix-visibility.test.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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { mixVisibility } from "../visibility" | ||
|
||
describe("mixVisibility", () => { | ||
test("mixes binary visibility", () => { | ||
expect(mixVisibility("visible", "hidden")(0)).toBe("visible") | ||
expect(mixVisibility("visible", "hidden")(0.5)).toBe("visible") | ||
expect(mixVisibility("visible", "hidden")(1)).toBe("hidden") | ||
expect(mixVisibility("hidden", "visible")(0)).toBe("hidden") | ||
expect(mixVisibility("hidden", "visible")(0.5)).toBe("visible") | ||
expect(mixVisibility("hidden", "visible")(1)).toBe("visible") | ||
expect(mixVisibility("block", "none")(0)).toBe("block") | ||
expect(mixVisibility("block", "none")(0.5)).toBe("block") | ||
expect(mixVisibility("block", "none")(1)).toBe("none") | ||
expect(mixVisibility("none", "block")(0)).toBe("none") | ||
expect(mixVisibility("none", "block")(0.5)).toBe("block") | ||
expect(mixVisibility("none", "block")(1)).toBe("block") | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const invisibleValues = new Set(["none", "hidden"]) | ||
|
||
/** | ||
* Returns a function that, when provided a progress value between 0 and 1, | ||
* will return the "none" or "hidden" string only when the progress is that of | ||
* the origin or target. | ||
*/ | ||
export function mixVisibility(origin: string, target: string) { | ||
if (invisibleValues.has(origin)) { | ||
return (p: number) => (p <= 0 ? origin : target) | ||
} else { | ||
return (p: number) => (p >= 1 ? target : origin) | ||
} | ||
} |