A custom Gutemberg block with drag and drop sorting
-
Hello everyone,
Here’s a question for both WP and React developers. I want to develop a custom Gutemberg block using the official create-block library. For UX reasons, I would like this block to have a drag-and-drop sorting system, like the gif bellow. But I haven’t found a solution. There’s this library https://github.com/lucprincen/gutenberg-sortable, but it’s obsolete and won’t compile.
Any ideas? Here’s a simplified code that you can improve.
import { registerBlockType } from "@wordpress/blocks"; import "./style.scss"; import metadata from "./block.json"; import { useBlockProps } from "@wordpress/block-editor"; registerBlockType(metadata.name, { attributes: { items: { type: "array", default: ["carrot", "cabbage", "zucchini"], }, }, edit: (props) => { const blockProps = useBlockProps(); const { attributes: { items }, setAttributes, } = props; const sortItems = (sortedItems) => { setAttributes({ items: sortedItems }); }; return ( <div {...blockProps}> {items.map((item) => ( <div className="item">{item}</div> ))} </div> ); }, save: (props) => { const blockProps = useBlockProps.save(); const { attributes: { items }, } = props; return ( <div {...blockProps}> {items.map((item) => ( <div className="item">{item}</div> ))} </div> ); }, });
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘A custom Gutemberg block with drag and drop sorting’ is closed to new replies.