Twikit

Get Started Components Avatar Badge Button Carousel Checkbox Color Picker Item Context Menu Context Menu Item Icon Image Picker Item Input Input Unscoped Modal Nav Nav Group Nav Item Picker Progress Radio Radio Group Range Select Sortable Sortable Item Spinner Step Steps Tab Tab Group Textarea Toggle Tooltip CSS Framework Design Tokens Theming

Sortable

The mandatory container for sortable items.

<p class="tw-margin-be-s tw-font-weight-medium">🙊 Sort the animals</p>

<tw-sortable>
  <tw-sortable-item>Dog 🐕</tw-sortable-item>
  <tw-sortable-item>Bird 🦜</tw-sortable-item>
  <tw-sortable-item>Fish 🐟</tw-sortable-item>
  <tw-sortable-item>Mouse 🐭</tw-sortable-item>
</tw-sortable>

<script>
  const sortable = document
    .getElementsByTagName("tw-sortable")[0];
  const result = document
    .getElementsByTagName("p")[0];
  sortable.addEventListener("sorted", (e) => 
    result.innerText =
      `🙈 You moved an animal from ${e.detail.from} to ${e.detail.to}`);
</script>

Properties

Property Attribute Description Type Default
disabled disabled boolean false
nested nested Enable nested/tree mode boolean false
maxDepth max-depth Maximum depth for nested mode (default: Infinity) number Infinity
rootGutter root-gutter Width in px of the left gutter zone for forcing root-level drops. When dragging in nested mode, if cursor is within this distance from left edge, the item will always drop at root level, even if hovering over nested items. This provides an easy way to "unnest" items by dragging them to the left. Default: 32px number 32

Events

Name Description
nestable-error Fired in **nested mode** when a drop is not allowed. Detail: `{ code: "max-depth" | "self-drop"; message: string; maxDepth?: number }` - `"max-depth"`: the move would exceed the allowed depth. - `"self-drop"`: trying to drop an item into itself or its own children.
sorted Dispatched when the order of the items has changed. The event detail contains the old and new index of the dragged item in the form of `{ from: 0, to: 5 }`.
restructured Fired in **nested mode** when an item is moved in the tree. Detail: `PathChange` - `from`: array with the old path of the moved item (e.g. `[1, 2]`). - `to`: array with the new path of the moved item after the drop. - `drop`: info about where the drop happened: - `type`: `"before" | "inside" | "after" | "root"` - `targetPath`: path of the target item, or `null` if dropped at root. - `dest`: normalized destination: - `parentPath`: path of the parent container where the item is inserted. - `index`: insert position inside that parent. - `parents`: convenience flags: - `fromIsRoot`: true if the item was at root before. - `toIsRoot`: true if the item is at root after.