Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="root">
</div>

              
            
!

CSS

              
                .selago {background-color: #F0F1FE}
.logan {background-color: #A3AACC}
.comet {background-color: #596080}
.charade {background-color: #242633}

.pampas {background-color: #F1EBE8}
.moon_mist {background-color: #E2E0D2}
.soft_amber {background-color: #D4C2B7}
.corvette {background-color: #FBD5A8}

.banana_mania {background-color: #FCE7BB}
.skeptic {background-color: #C3E4D9}
.hawkes_blue {background-color: #D2E4FB}
.french_lilac {background-color: #E9CBEC}

.vanilla_ice {background-color: #F5E0E9}
.cinderella {background-color: #FEE1DF}
.tan_hide {background-color: #FBA76A}
.ronchi {background-color: #F0C44C}

.dragWrapper{
  position: absolute;
  top:-100px;
}

svg {
  width: 100px;
  height: 100px;
}
              
            
!

JS

              
                const root = ReactDOM.createRoot(document.getElementById('root'));

const Trash = () => {
  return (
    <svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 315 315" enableBackground="new 0 0 315 315">
      <g>
        <path
          d="m256.774,23.942h-64.836v-6.465c0-9.636-7.744-17.477-17.263-17.477h-34.348c-9.521,0-17.266,7.841-17.266,17.478v6.465h-64.835c-9.619,0-17.445,7.76-17.445,17.297v11.429c0,7.168 4.42,13.33 10.698,15.951 1.989,39.623 13.5,231.193 14.018,239.801 0.222,3.696 3.284,6.58 6.987,6.58h170.033c3.703,0 6.766-2.884 6.987-6.58 0.518-8.607 12.028-200.178 14.018-239.801 6.278-2.621 10.698-8.783 10.698-15.951v-11.43c5.68434e-14-9.537-7.826-17.297-17.446-17.297zm-119.713-6.464c0-1.918 1.465-3.478 3.266-3.478h34.348c1.8,0 3.264,1.56 3.264,3.478v6.465h-40.877v-6.465zm-82.282,23.761c0-1.818 1.546-3.297 3.445-3.297h198.549c1.899,0 3.445,1.478 3.445,3.297v11.429c0,1.819-1.546,3.299-3.445,3.299h-198.548c-1.899,0-3.445-1.479-3.445-3.299v-11.429zm181.143,259.761h-156.848c-2.055-34.247-11.479-191.674-13.51-231.033h183.867c-2.031,39.359-11.454,196.786-13.509,231.033z"
        />
        <path
          d="m157.5,95.125c-3.866,0-7,3.134-7,7v176.109c0,3.866 3.134,7 7,7 3.866,0 7-3.134 7-7v-176.109c0-3.866-3.134-7-7-7z"
        />
        <path
          d="m110.2,102.04c-0.202-3.86-3.507-6.837-7.355-6.625-3.86,0.201-6.827,3.494-6.625,7.355l9.182,175.829c0.195,3.736 3.285,6.635 6.984,6.635 0.123,0 0.247-0.003 0.371-0.01 3.86-0.201 6.827-3.494 6.625-7.355l-9.182-175.829z"
        />
        <path
          d="m212.155,95.415c-3.899-0.223-7.153,2.764-7.355,6.625l-9.184,175.829c-0.202,3.861 2.765,7.154 6.625,7.355 0.125,0.007 0.248,0.01 0.371,0.01 3.698,0 6.789-2.898 6.984-6.635l9.184-175.829c0.202-3.861-2.764-7.154-6.625-7.355z"
        />
      </g>
    </svg>
  );
};
const colors = [
  'selago',
  'logan',
  'comet',
  'charade',

  'pampas',
  'moon_mist',
  'soft_amber',
  'corvette',

  'banana_mania',
  'skeptic',
  'hawkes_blue',
  'french_lilac',

  'vanilla_ice',
  'cinderella',
  'tan_hide',
  'ronchi',
];
const DEFAULT_STEP = 10000;
const Separator = styled.div`
  margin: 2px 0;
  width: 150px;
  height: ${({ active }) => (active ? '2px' : '0px')};
  background-color: ${({ active }) => (active ? '#0000FF7E' : '')};
`;
const Tag = styled.div`
  width: fit-content;
  padding: 4px 6px;
  border-radius: 10px;
`;

const Main = styled.div`
  display: flex;
`;

const Row = styled.div`
  width: 200px;
  box-sizing: border-box;
  border-radius: 10px;
  transition-duration: .4s;
  transition-property: height, padding, opacity;
  overflow: hidden;
  cursor: pointer;
  padding: ${({ isDragItem }) => (isDragItem ? '0px' : '5px')};
  opacity: ${({ isDragItem }) => (isDragItem ? 0 : 1)};
  height: ${({ isDragItem }) => (isDragItem ? '0px' : '34px')};
`;

const TrashWrapper = styled.div`
  margin-top: 120px;
  width: 100px;
  height: 100px;
  box-shadow: ${({ isDragHover }) => (isDragHover ? ' 0 0 7px 7px rgba(0 170 246/ 30%);' : '')};
  border-radius: 10px;
  background-color: ${({ isDragHover }) => (isDragHover ? '#2727FF08' : '')};
`;

const EndHandler = styled.div`
  width: 200px;
  height: 20px;
`;

const initData = Array.from({ length: 10 }, (_, i) => ({
  id: (Math.random() + 1).toString(36).substring(7),
  color: colors[i],
  position: i * DEFAULT_STEP + DEFAULT_STEP,
}));


const Item = ({ id, color, onDragStartHandler, isDragItem, index, onDragEndHandler }) => {
  return (
    <Row
      draggable
      isDragItem={isDragItem}
      onDragEnd={onDragEndHandler}
      onDragStart={(e) => onDragStartHandler(e, index)}
    >
      <Tag id={id} className={color}>{id}</Tag>
    </Row>
  );
};

const App = () => {
  const [list, setList] = React.useState(initData);
  const [draggableIndex, setDraggableIndex] = React.useState(-1);
  const [dragOverIndex, setDragOverIndex] = React.useState(-1);
  const dropDepthRef = React.useRef(0);

  const onDragLeaveHandler = () => {
    dropDepthRef.current -= 1;
    if (dropDepthRef.current > 0) return;
    setDragOverIndex(-1);
  };

  const onDragOverHandler = (e) => {
    e.preventDefault();
  };

  const onDragEndHandler = () => {
    setDraggableIndex(-1);
    setDragOverIndex(-1);
    dropDepthRef.current = 0;
    const div = document.getElementById('dragg');
    if (div) document.body.removeChild(div);
  };

  const onDragEnterHandler = (hoverIndex) => (e) => {
    dropDepthRef.current += 1;
    setDragOverIndex(hoverIndex);
  };
  const onDragStartHandler = (e, index) => {
    setTimeout(() => setDraggableIndex(index), 0);

    const divDraggable = document.getElementById(list[index].id);
    const newDivDraggable = divDraggable.cloneNode(true);
    const div = document.createElement('div');
    div.className = 'dragWrapper';
    div.id = 'dragg';
    div.appendChild(newDivDraggable);
    document.body.appendChild(div);
    const rect = divDraggable.getBoundingClientRect();
    e.dataTransfer.setDragImage(div, e.clientX - rect.left, e.clientY - rect.top);
  };

  const onDropHandler = (dropIndex) => (e) => {
    const topItemPosition = list[dropIndex - 1]?.position ?? 0;
    const downItemPosition = list[dropIndex]?.position ?? topItemPosition + DEFAULT_STEP;
    const newPosition = (topItemPosition + downItemPosition) / 2;

   if (e.dataTransfer.items.length) {
      e.preventDefault();
      e.stopPropagation();
      setDraggableIndex(-1);
      setDragOverIndex(-1);
      if (e.dataTransfer.types.filter(i => i === 'Files').length) {
        const fileName = e.dataTransfer.items[0].getAsFile().name;
        const newItem = { id: fileName, color: colors[list.length - 1], position: newPosition };
        const newList = [...list, newItem];
        setList(newList.sort((a, b) => a.position - b.position));
      } else if (e.dataTransfer.types.filter(i => i === 'text/plain').length) {
        const newItem = { id: e.dataTransfer.getData('text/plain'), color: colors[list.length - 1], position: newPosition };
        const newList = [...list, newItem];
        setList(newList.sort((a, b) => a.position - b.position));
      }
      return;
    }
    
    
    const currentItem = list[draggableIndex];
    const newList = list.filter(item => item.id !== currentItem.id);
    const updatedItem = { ...currentItem, position: newPosition };

    newList.push(updatedItem);
    setList(newList.sort((a, b) => a.position - b.position));
  };

  const onDropToTrashHandler = (e) => {
 
     if (e.dataTransfer.items.length) {
      e.preventDefault();
      e.stopPropagation();
      setDraggableIndex(-1);
      setDragOverIndex(-1);
      return;
    }
    const currentItem = list[draggableIndex];
    setDraggableIndex(-1);
    setDragOverIndex(-1);
    const div = document.getElementById('dragg');
    if (div) document.body.removeChild(div);
    setList(list.filter(item => item.id !== currentItem.id));
  };

  return (
    <Main>
      <div>
        <h1>Hello, DnD!</h1>
        {list.map((item, index) => (
          <div
            key={item.id}
            onDragOver={onDragOverHandler}
            onDragEnter={onDragEnterHandler(index)}
            onDragLeave={onDragLeaveHandler}
            onDrop={onDropHandler(index)}
          >
            <Separator active={index === dragOverIndex} />
            <Item
              setDraggableItem={setDraggableIndex}
              isDragItem={draggableIndex === index}
              index={index}
              onDragEndHandler={onDragEndHandler}
              onDragStartHandler={onDragStartHandler}
              draggableItem
              {...item}
            />
          </div>
        ))}
        <EndHandler
          onDragEnter={onDragEnterHandler(list.length)}
          onDragLeave={onDragLeaveHandler}
          onDragOver={onDragOverHandler}
          onDrop={onDropHandler(list.length)}
        >
          <Separator active={list.length === dragOverIndex} />
        </EndHandler>
      </div>
      <TrashWrapper
        onDragEnter={onDragEnterHandler('trash')}
        onDragLeave={onDragLeaveHandler}
        onDragOver={onDragOverHandler}
        onDrop={onDropToTrashHandler}
        isDragHover={dragOverIndex === 'trash'}
      >
        <Trash />
      </TrashWrapper>
    </Main>
  );
};
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
              
            
!
999px

Console