Skip to main content

Sortable Container

<sl-sortable-container> | SlSortableContainer
Since 2.0 hub24

A container for sortable items.

Allow children to be sorted in the vertical orientation only.

Item 1 Item 2 Item 3
<sl-sortable-container>
  <sl-sortable-item>Item 1</sl-sortable-item>
  <sl-sortable-item>Item 2</sl-sortable-item>
  <sl-sortable-item>Item 3</sl-sortable-item>
</sl-sortable-container>

Change event

Emits an sl-change event when the order of the items changes.

Item 1 Item 2 Item 3

<sl-sortable-container id="order-change-example">
  <sl-sortable-item>Item 1</sl-sortable-item>
  <sl-sortable-item>Item 2</sl-sortable-item>
  <sl-sortable-item>Item 3</sl-sortable-item>
</sl-sortable-container>

<p id="order-change-result"></p>

<script>
  const container = document.getElementById('order-change-example');
  container.addEventListener('sl-change', (event) => {
    setTimeout(() => {
      document.getElementById('order-change-result').textContent =
        "New is order is: " +
        Array.from(container .querySelectorAll('sl-sortable-item'))
          .map(item => item.textContent)
          .join(', ');
    }, 100);
  });
</script>

Disabled

Item 1 Item 2 Item 3
<sl-sortable-container disabled>
  <sl-sortable-item>Item 1</sl-sortable-item>
  <sl-sortable-item>Item 2</sl-sortable-item>
  <sl-sortable-item>Item 3</sl-sortable-item>
</sl-sortable-container>

Grid

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
<sl-sortable-container class="grid">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
  <div>Item 4</div>
  <div>Item 5</div>
  <div>Item 6</div>
  <div>Item 7</div>
  <div>Item 8</div>
</sl-sortable-container>

<style>
  .grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, 100px);
    gap: 10px;
    padding: 10px;
  }

  .grid div {
    display: block;
    padding: 10px;
    width: 100px;
    height: 100px;
    border: solid grey 1px;
  }

  .grid div:hover {
      border: solid var(--hds-color-border-primary) 1px;
  }
</style>

Slots

Name Description
(default) The default slot.

Learn more about using slots.

Events

Name React Event Description Event Detail
sl-change When the sort order changes. -

Learn more about events.

Parts

Name Description
base The component’s base wrapper.

Learn more about customizing CSS parts.