Skip to content

enhancement: Smoothed modal animation + reduced motion #174

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

Merged
merged 9 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
39 changes: 34 additions & 5 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"snippets:consolidate": "node ./utils/consolidateSnippets.js"
},
"dependencies": {
"framer-motion": "^11.15.0",
"motion": "^11.15.0",
"prismjs": "^1.29.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
85 changes: 50 additions & 35 deletions src/components/SnippetList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { motion, AnimatePresence } from "framer-motion";
import { motion, AnimatePresence, useReducedMotion } from "motion/react";
import { useState } from "react";

import { useAppContext } from "@contexts/AppContext";
Expand All @@ -13,6 +13,8 @@ const SnippetList = () => {
const { fetchedSnippets } = useSnippets();
const [isModalOpen, setIsModalOpen] = useState(false);

const shouldReduceMotion = useReducedMotion();

if (!fetchedSnippets)
return (
<div>
Expand All @@ -34,41 +36,54 @@ const SnippetList = () => {
<>
<motion.ul role="list" className="snippets">
<AnimatePresence mode="popLayout">
{fetchedSnippets.map((snippet, idx) => (
<motion.li
key={idx}
initial={{ opacity: 0, y: 20 }}
animate={{
opacity: 1,
y: 0,
transition: {
delay: idx * 0.05,
duration: 0.2,
},
}}
exit={{
opacity: 0,
y: -20,
transition: {
delay: (fetchedSnippets.length - 1 - idx) * 0.01,
duration: 0.09,
},
}}
>
<motion.button
className="snippet | flow"
data-flow-space="sm"
onClick={() => handleOpenModal(snippet)}
whileHover={{ scale: 1.01 }}
whileTap={{ scale: 0.98 }}
{fetchedSnippets.map((currentSnippet, idx) => {
return (
<motion.li
key={currentSnippet.title + idx}
layoutId={currentSnippet.title + (idx + 1).toString()} // unique id for layout animation
initial={{ opacity: 0, y: 20 }}
animate={{
opacity: 1,
y: 0,
transition: {
delay: shouldReduceMotion ? 0 : 0.09 + idx * 0.05,
duration: shouldReduceMotion ? 0 : 0.2,
},
}}
exit={{
opacity: 0,
y: -20,
transition: {
delay: idx * 0.01,
duration: shouldReduceMotion ? 0 : 0.09,
},
}}
transition={{
type: "spring",
duration: shouldReduceMotion ? 0 : 0.5,
}}
>
<div className="snippet__preview">
<img src={language.icon} alt={language.lang} />
</div>
<h3 className="snippet__title">{snippet.title}</h3>
</motion.button>
</motion.li>
))}
<motion.button
className="snippet | flow"
data-flow-space="sm"
onClick={() =>
handleOpenModal({
...currentSnippet,
// added idx for the layout animation to work correctly
idx: idx + 1,
})
}
whileHover={{ scale: 1.01 }}
whileTap={{ scale: 0.98 }}
>
<div className="snippet__preview">
<img src={language.icon} alt={language.lang} />
</div>
<h3 className="snippet__title">{currentSnippet.title}</h3>
</motion.button>
</motion.li>
);
})}
</AnimatePresence>
</motion.ul>

Expand Down
12 changes: 6 additions & 6 deletions src/components/SnippetModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { motion } from "framer-motion";
import { motion, useReducedMotion } from "motion/react";
import React from "react";
import ReactDOM from "react-dom";

Expand All @@ -23,6 +23,8 @@ const SnippetModal: React.FC<Props> = ({
}) => {
const modalRoot = document.getElementById("modal-root");

const shouldReduceMotion = useReducedMotion();

useEscapeKey(handleCloseModal);

if (!modalRoot) {
Expand All @@ -41,16 +43,14 @@ const SnippetModal: React.FC<Props> = ({
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.2 }}
transition={{ duration: shouldReduceMotion ? 0 : 0.2 }}
>
<motion.div
key="modal-content"
className="modal | flow"
data-flow-space="lg"
initial={{ scale: 0.8, opacity: 0, y: 20 }}
animate={{ scale: 1, opacity: 1, y: 0 }}
exit={{ scale: 0.8, opacity: 0, y: 20 }}
transition={{ type: "spring", duration: 0.5 }}
layoutId={snippet.title + snippet.idx?.toString()} // unique id for layout animation
transition={{ type: "spring", duration: shouldReduceMotion ? 0 : 0.5 }}
>
<div className="modal__header">
<h2 className="section-title">{snippet.title}</h2>
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type SnippetType = {
code: string;
tags: string[];
author: string;
idx?: number;
};

export type AppState = {
Expand Down
Loading