ValentinH / react-easy-sort

A React component to sort items in lists or grids
https://ValentinH.github.io/react-easy-sort
MIT License
160 stars 25 forks source link

:sparkles: Added custom holder for dragged elements #28

Closed mo-paralenz closed 2 years ago

mo-paralenz commented 2 years ago

Added the ability to pass an element reference to SortableList to be used as a temporary container for dragged elements instead of using body.

When using CSS or SASS is very common to define component styles as children of some container, so when the dragged component gets moved to body all the styles get disabled.

// styles.scss
form {
  width: 100px;
  input {
    background: black;
    color: white;
    width: 50%;
  }
}
// MyComponent.(jsx|tsx)
const MyComponent = () => {
  const customHolderRef = useRef()

  return (
    <form>
      <div className="custom-holder" ref={customHolderRef} />
      <SortableList customHolderRef={customHolderRef} {...props}>
        {items.map((item) => (
          <SortableItem key={item}>
            <input type="text" value={item} />
          </SortableItem>
        ))}
      </SortableList>
    </form>
  )
}

This way the holder element for the dragged components can be located inside the same parent component as SortableList and keep all styles intact.

ValentinH commented 2 years ago

First of all, I'm sorry for the delay to review this PR. I've been quite busy recently. 😞

Then, thank you very much for this nice PR! I'm merging it now and will publish a new version.

ValentinH commented 2 years ago

Published as https://github.com/ValentinH/react-easy-sort/releases/tag/v1.5.0