-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
969 additions
and
702 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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { useSortable } from "@dnd-kit/sortable" | ||
import { CSS } from '@dnd-kit/utilities'; | ||
import type { OGElement } from "../../lib/types"; | ||
import { NotVisibleIcon } from "../icons/NotVisibleIcon" | ||
import { TextIcon } from "../icons/TextIcon" | ||
import { VisibleIcon } from "../icons/VisibleIcon" | ||
import { useOg } from "../OgPlayground" | ||
import { BoxIcon } from "../icons/BoxIcon" | ||
import { CircleIcon } from "../icons/CircleIcon" | ||
import { ImageIcon } from "../icons/ImageIcon" | ||
import { MagicWandIcon } from "../icons/MagicWandIcon" | ||
|
||
interface ElementRowProps { | ||
element: OGElement | ||
} | ||
|
||
export function ElementRow({ element }: ElementRowProps) { | ||
const { selectedElement, setSelectedElement, updateElement } = useOg() | ||
const { | ||
attributes, | ||
listeners, | ||
setNodeRef, | ||
transform, | ||
transition, | ||
} = useSortable({ id: element.id }); | ||
|
||
const style = { | ||
transform: CSS.Transform.toString(transform), | ||
transition, | ||
}; | ||
|
||
return ( | ||
<div className="flex justify-between items-center pb-2 cursor-auto" ref={setNodeRef} style={style} {...attributes} {...listeners}> | ||
<button | ||
className={`flex gap-2 select-none text-gray-600 hover:text-gray-900 w-full ${selectedElement === element.id ? '!text-blue-500' : ''} ${!element.visible ? '!text-gray-300' : ''}`} | ||
onClick={() => { setSelectedElement(element.id); }} | ||
type="button" | ||
> | ||
{element.tag === 'p' ? ( | ||
<> | ||
<TextIcon height="1.4em" width="1.4em" /> | ||
{element.content.slice(0, 25)} | ||
</> | ||
) : null} | ||
{element.tag === 'div' && element.backgroundImage ? ( | ||
<> | ||
<ImageIcon height="1.4em" width="1.4em" /> | ||
Image | ||
</> | ||
) : null} | ||
{element.tag === 'div' && !element.backgroundImage && !element.radius ? ( | ||
<> | ||
<BoxIcon height="1.4em" width="1.4em" /> | ||
Box | ||
</> | ||
) : null} | ||
{element.tag === 'div' && !element.backgroundImage && element.radius ? ( | ||
<> | ||
<CircleIcon height="1.4em" width="1.4em" /> | ||
Rounded box | ||
</> | ||
) : null} | ||
{element.tag === 'span' ? ( | ||
<> | ||
<MagicWandIcon height="1.4em" width="1.4em" /> | ||
Dynamic text | ||
</> | ||
) : null} | ||
</button> | ||
<button | ||
className="text-gray-600 hover:text-gray-900" | ||
onClick={() => { | ||
updateElement({ | ||
...element, | ||
visible: !element.visible, | ||
}); | ||
}} | ||
type="button" | ||
> | ||
{element.visible ? <VisibleIcon /> : <NotVisibleIcon />} | ||
</button> | ||
</div> | ||
) | ||
} |
53 changes: 53 additions & 0 deletions
53
apps/dashboard/src/components/LeftPanel/ElementsSection.tsx
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,53 @@ | ||
import type { DragEndEvent } from "@dnd-kit/core"; | ||
import { DndContext, KeyboardSensor, PointerSensor, closestCenter, useSensor, useSensors } from "@dnd-kit/core"; | ||
import { SortableContext, arrayMove, sortableKeyboardCoordinates, verticalListSortingStrategy } from "@dnd-kit/sortable"; | ||
import { restrictToVerticalAxis } from "@dnd-kit/modifiers"; | ||
import { useOg } from "../OgPlayground"; | ||
import { ElementRow } from "./ElementRow"; | ||
|
||
export function ElementsSection() { | ||
const { elements, setElements } = useOg() | ||
const sensors = useSensors( | ||
useSensor(PointerSensor, { | ||
activationConstraint: { | ||
distance: 5, | ||
} | ||
}), | ||
useSensor(KeyboardSensor, { | ||
coordinateGetter: sortableKeyboardCoordinates, | ||
}) | ||
); | ||
|
||
function handleDragEnd(event: DragEndEvent) { | ||
const { active, over } = event; | ||
|
||
if (!over) { | ||
return; | ||
} | ||
|
||
if (active.id !== over.id) { | ||
const oldIndex = elements.findIndex(element => element.id === active.id) | ||
const newIndex = elements.findIndex(element => element.id === over.id) | ||
|
||
const newElements = arrayMove(elements, oldIndex, newIndex); | ||
setElements(newElements); | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<p className="text-xs text-gray-600">Elements</p> | ||
<div className="flex flex-col-reverse w-full"> | ||
<DndContext collisionDetection={closestCenter} modifiers={[restrictToVerticalAxis]} onDragEnd={handleDragEnd} onDragOver={handleDragEnd} sensors={sensors}> | ||
<SortableContext items={elements} strategy={verticalListSortingStrategy}> | ||
{elements.map(element => ( | ||
<ElementRow element={element} key={element.id} /> | ||
))} | ||
</SortableContext> | ||
</DndContext> | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
|
Oops, something went wrong.