-
Notifications
You must be signed in to change notification settings - Fork 17
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 #17 from AlexSciFier/category-links-sorting
Category links sorting
- Loading branch information
Showing
15 changed files
with
470 additions
and
158 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,21 @@ | ||
import React, { useState } from "react"; | ||
|
||
export default function LazyIcon({ id, title }) { | ||
const [isLoading, setIsLoading] = useState(true); | ||
return ( | ||
<> | ||
<img | ||
onLoad={() => setIsLoading(false)} | ||
className={`w-8 h-8 bg-contain bg-no-repeat bg-center transition-opacity ${isLoading ? "opacity-0" : "opacity-100"}`} | ||
height={32} | ||
width={32} | ||
loading="lazy" | ||
alt={`icon for ${title}`} | ||
src={`${process.env.NODE_ENV !== "production" ? "http://localhost:3333" : ""}/api/bookmarks/${id}/icon`} | ||
></img> | ||
<div | ||
className={`w-8 h-8 animate-pulse rounded bg-gray-200 absolute top-0 ${isLoading ? "" : "hidden"}`} | ||
></div> | ||
</> | ||
); | ||
} |
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,31 @@ | ||
import React from "react"; | ||
import { useSortable } from "@dnd-kit/sortable"; | ||
import { CSS } from "@dnd-kit/utilities"; | ||
|
||
export default function SortableItem({ item, isActive = false, children }) { | ||
const { | ||
attributes, | ||
listeners, | ||
setNodeRef, | ||
isDragging, | ||
transform, | ||
transition, | ||
} = useSortable({ id: item.id }); | ||
const style = { | ||
transform: CSS.Translate.toString(transform), | ||
transition, | ||
}; | ||
return ( | ||
<div | ||
{...listeners} | ||
ref={setNodeRef} | ||
style={style} | ||
{...attributes} | ||
className={`flex items-center gap-3 border dark:border-neutral-600 p-1 rounded bg-white dark:bg-gray-900 hover:bg-neutral-200 dark:hover:bg-gray-800 dark:text-white transition-shadow ${ | ||
isActive ? "shadow-xl z-50 cursor-grabbing" : "cursor-grab" | ||
} ${isDragging ? "opacity-40" : ""}`} | ||
> | ||
{children} | ||
</div> | ||
); | ||
} |
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,98 @@ | ||
import { | ||
DndContext, | ||
DragOverlay, | ||
MouseSensor, | ||
TouchSensor, | ||
useSensor, | ||
useSensors, | ||
} from "@dnd-kit/core"; | ||
import { | ||
arrayMove, | ||
SortableContext, | ||
verticalListSortingStrategy, | ||
} from "@dnd-kit/sortable"; | ||
import { | ||
restrictToVerticalAxis, | ||
restrictToParentElement, | ||
} from "@dnd-kit/modifiers"; | ||
import React, { useMemo, useState } from "react"; | ||
import { useEffect } from "react"; | ||
import SortableItem from "./SortableItem"; | ||
import LazyIcon from "../LazyIcon"; | ||
|
||
export default function SortableList({ items, onDragEnd }) { | ||
const [itemsState, setItemsState] = useState(items); | ||
const [active, setActive] = useState(null); | ||
const activeItem = useMemo( | ||
() => itemsState.find((item) => item.id === active?.id), | ||
[active, itemsState] | ||
); | ||
|
||
useEffect(() => { | ||
setItemsState(items); | ||
}, [items]); | ||
|
||
function handleDragEnd(e) { | ||
const { active, over } = e; | ||
|
||
if (over && active.id !== over?.id) { | ||
const activeIndex = itemsState.findIndex(({ id }) => id === active.id); | ||
const overIndex = itemsState.findIndex(({ id }) => id === over.id); | ||
let movedArray = arrayMove(itemsState, activeIndex, overIndex); | ||
setItemsState(movedArray); | ||
let sortedArray = movedArray.map((item, idx) => { | ||
return { ...item, position: ++idx }; | ||
}); | ||
|
||
let idPositionPairArray = sortedArray.map((item) => { | ||
return { id: item.id, position: item.position }; | ||
}); | ||
onDragEnd(idPositionPairArray); | ||
} | ||
} | ||
const sensors = useSensors( | ||
useSensor(MouseSensor), | ||
useSensor(TouchSensor, { | ||
activationConstraint: { | ||
delay: 250, | ||
tolerance: 5, | ||
}, | ||
}) | ||
); | ||
return ( | ||
<DndContext | ||
sensors={sensors} | ||
modifiers={[restrictToVerticalAxis, restrictToParentElement]} | ||
onDragEnd={handleDragEnd} | ||
onDragStart={({ active }) => { | ||
setActive(active); | ||
}} | ||
> | ||
<SortableContext | ||
items={itemsState} | ||
strategy={verticalListSortingStrategy} | ||
> | ||
<div className="flex flex-col gap-3"> | ||
{itemsState.map((item) => ( | ||
<SortableItem key={item.id} item={item}> | ||
<div className="relative flex-none"> | ||
<LazyIcon id={item.id} /> | ||
</div> | ||
<div className="truncate flex-1">{item.title}</div> | ||
</SortableItem> | ||
))} | ||
</div> | ||
</SortableContext> | ||
<DragOverlay> | ||
{activeItem ? ( | ||
<SortableItem item={activeItem} isActive={true}> | ||
<div className="relative flex-none"> | ||
<LazyIcon id={activeItem.id} /> | ||
</div> | ||
<div className="truncate flex-1">{activeItem.title}</div> | ||
</SortableItem> | ||
) : null} | ||
</DragOverlay> | ||
</DndContext> | ||
); | ||
} |
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
Oops, something went wrong.