Skip to content

Commit cd6c616

Browse files
committed
removed idx from the snippet type and swtiched to title and description for unique key
1 parent fbf4efd commit cd6c616

File tree

3 files changed

+7
-13
lines changed

3 files changed

+7
-13
lines changed

src/components/SnippetList.tsx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ const SnippetList = () => {
3636
<>
3737
<motion.ul role="list" className="snippets">
3838
<AnimatePresence mode="popLayout">
39-
{fetchedSnippets.map((currentSnippet, idx) => {
39+
{fetchedSnippets.map((snippet, idx) => {
40+
const uniqueId = `${snippet.title}-${snippet.description}`;
4041
return (
4142
<motion.li
42-
key={currentSnippet.title + idx}
43-
layoutId={currentSnippet.title + (idx + 1).toString()} // unique id for layout animation
43+
key={uniqueId}
44+
layoutId={uniqueId}
4445
initial={{ opacity: 0, y: 20 }}
4546
animate={{
4647
opacity: 1,
@@ -66,20 +67,14 @@ const SnippetList = () => {
6667
<motion.button
6768
className="snippet | flow"
6869
data-flow-space="sm"
69-
onClick={() =>
70-
handleOpenModal({
71-
...currentSnippet,
72-
// added idx for the layout animation to work correctly
73-
idx: idx + 1,
74-
})
75-
}
70+
onClick={() => handleOpenModal(snippet)}
7671
whileHover={{ scale: 1.01 }}
7772
whileTap={{ scale: 0.98 }}
7873
>
7974
<div className="snippet__preview">
8075
<img src={language.icon} alt={language.lang} />
8176
</div>
82-
<h3 className="snippet__title">{currentSnippet.title}</h3>
77+
<h3 className="snippet__title">{snippet.title}</h3>
8378
</motion.button>
8479
</motion.li>
8580
);

src/components/SnippetModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const SnippetModal: React.FC<Props> = ({
4949
key="modal-content"
5050
className="modal | flow"
5151
data-flow-space="lg"
52-
layoutId={snippet.title + snippet.idx?.toString()} // unique id for layout animation
52+
layoutId={`${snippet.title}-${snippet.description}`}
5353
transition={{ type: "spring", duration: shouldReduceMotion ? 0 : 0.5 }}
5454
>
5555
<div className="modal__header">

src/types/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export type SnippetType = {
1414
code: string;
1515
tags: string[];
1616
author: string;
17-
idx?: number;
1817
};
1918

2019
export type AppState = {

0 commit comments

Comments
 (0)