Skip to content

Commit a7836ad

Browse files
Merge pull request #48 from linked-planet/dev
Dev
2 parents 603c256 + 53272cb commit a7836ad

File tree

13 files changed

+789
-99
lines changed

13 files changed

+789
-99
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { DragDropContext, Droppable, type DropResult } from "@hello-pangea/dnd"
2+
import { useCallback } from "react"
3+
4+
export type DnDListProps<T> = {
5+
items?: Array<T>
6+
onOrderChanged?: (items: Array<T>) => void
7+
onIndexChanged?: (oldIndex: number, newIndex: number) => void
8+
9+
children: JSX.Element[]
10+
}
11+
12+
export function DnDList<T>({
13+
items,
14+
onOrderChanged,
15+
onIndexChanged,
16+
children,
17+
}: DnDListProps<T>) {
18+
const onDragEnd = useCallback(
19+
(result: DropResult) => {
20+
// dropped outside the list
21+
if (!result.destination) {
22+
return
23+
}
24+
if (onIndexChanged) {
25+
onIndexChanged(result.source.index, result.destination.index)
26+
}
27+
if (items && onOrderChanged) {
28+
const [removed] = items.splice(result.source.index, 1)
29+
const newOrder = items.slice()
30+
newOrder.splice(result.destination.index, 0, removed)
31+
onOrderChanged(newOrder)
32+
}
33+
},
34+
[items, onIndexChanged, onOrderChanged],
35+
)
36+
37+
return (
38+
<div
39+
className={`${
40+
children.length > 0
41+
? "block border-border hover:border-border-bold"
42+
: "opacity-0 border-transparent"
43+
} box-border border`}
44+
>
45+
<DragDropContext onDragEnd={onDragEnd}>
46+
<Droppable droppableId="droppable">
47+
{(provided) => (
48+
<div
49+
{...provided.droppableProps}
50+
ref={provided.innerRef}
51+
className="flex flex-col w-full"
52+
>
53+
{children}
54+
{provided.placeholder}
55+
</div>
56+
)}
57+
</Droppable>
58+
</DragDropContext>
59+
</div>
60+
)
61+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React from "react"
2+
import { twMerge } from "tailwind-merge"
3+
import { Draggable } from "@hello-pangea/dnd"
4+
import { MoreVerticalIcon } from "lucide-react"
5+
6+
export type DragItemProps = {
7+
draggableId: string
8+
index: number
9+
children: React.ReactNode
10+
thin?: boolean
11+
className?: string
12+
style?: React.CSSProperties
13+
}
14+
15+
export function DragItem({
16+
draggableId,
17+
index,
18+
children,
19+
className,
20+
style,
21+
thin = false,
22+
}: DragItemProps) {
23+
const draggableRef = React.useRef(null)
24+
return (
25+
<Draggable draggableId={draggableId} index={index}>
26+
{({ innerRef, draggableProps, dragHandleProps }) => {
27+
// offset bug workaround: https://github.com/atlassian/react-beautiful-dnd/issues/1881
28+
const dragStyle = {
29+
...style,
30+
...draggableProps.style,
31+
top: "auto",
32+
left: "auto",
33+
}
34+
//
35+
return (
36+
<div ref={draggableRef}>
37+
<div
38+
{...draggableProps}
39+
style={dragStyle}
40+
className={twMerge(
41+
"flex h-full w-full bg-surface border-border border rounded-sm",
42+
className,
43+
)}
44+
ref={innerRef}
45+
>
46+
<div
47+
className={`flex-none ${thin ? "p-0" : "p-2"} flex justify-center items-center border-r border-border bg-surface-raised hover:bg-surface-raised-hovered active:bg-surface-raised-pressed`}
48+
{...dragHandleProps}
49+
>
50+
<MoreVerticalIcon area-label="Drag Handle" />
51+
</div>
52+
<div className={`flex-1 ${thin ? "pl-2" : "p-2"}`}>
53+
{children}
54+
</div>
55+
</div>
56+
</div>
57+
)
58+
}}
59+
</Draggable>
60+
)
61+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { DnDList, type DnDListProps as DnDListPropsType } from "./DnDList"
2+
import { DragItem, type DragItemProps as DragItemPropsType } from "./DragItem"
3+
4+
export const DnD = {
5+
List: DnDList,
6+
DragItem,
7+
}
8+
9+
export namespace DnD {
10+
export type DnDListProps<T> = DnDListPropsType<T>
11+
export type DragItemProps = DragItemPropsType
12+
}

library/src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ export * from "./Blanket"
3333
export * from "./Breadcrumbs"
3434
export * from "./SectionMessage"
3535
export * from "./codeblock"
36+
export * from "./dnd"

library/src/localization/LocaleContext.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ const translationsPath = "./translations-compiled"
2929
"./translations-compiled/de.json"
3030
)*/
3131
const messageTranslations: Record<string, Record<string, string>> = {}
32-
export const defaultLanguage = navigator?.language.substring(0, 2) ?? "en"
32+
export const defaultLanguage =
33+
typeof navigator !== "undefined"
34+
? (navigator?.language.substring(0, 2) ?? "en")
35+
: "en"
3336
// this would load all the message translations immediatly, but we simply use dynamic imports
3437
/*;(async function main() {
3538
const loadMessages = async (language: string) => {

0 commit comments

Comments
 (0)