Sortable

Ready Added in 2.0.0

Create sortable grids and lists to rearrange the order of its elements.

Drag and drop an element to a new location within the sortable grid, while the other items adjust to fit. This is great, if you want to sort items like gallery or menu items, for example.


Usage

To apply this component, add the gls-sortable attribute to a container and create child elements.

<div gls-sortable>
    <div></div>
    <div></div>
</div>
    • Item 1
    • Item 2
    • Item 3
    • Item 4
    • Item 5
    • Item 6
    • Item 7
    • Item 8
  • <ul class="gls-grid-small gls-child-width-1-2 gls-child-width-1-4@s" gls-sortable="handle: .gls-card" gls-grid>
        <li>
            <div class="gls-card gls-card-default gls-card-body gls-text-center">Item 1</div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body gls-text-center">Item 2</div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body gls-text-center">Item 3</div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body gls-text-center">Item 4</div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body gls-text-center">Item 5</div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body gls-text-center">Item 6</div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body gls-text-center">Item 7</div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body gls-text-center">Item 8</div>
        </li>
    </ul>
    

Handle

By default, the entire sortable element can be used for drag and drop sorting. To create a handle which will be used instead, just add the handle: SELECTOR option to the attribute and add the handle class to the element that you want to use as a handle.

<ul gls-sortable="handle: .gls-sortable-handle">
    <li>
        <div class="gls-sortable-handle"></div>
        ...
    </li>
</ul>
    • Item 1
    • Item 2
    • Item 3
    • Item 4
    • Item 5
    • Item 6
    • Item 7
    • Item 8
  • <ul class="gls-grid-small gls-child-width-1-2 gls-child-width-1-4@s" gls-sortable="handle: .gls-sortable-handle" gls-grid>
        <li>
            <div class="gls-card gls-card-default gls-card-body">
                <span class="gls-sortable-handle gls-margin-small-right gls-text-center" gls-icon="icon: table"></span>Item 1
            </div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body">
                <span class="gls-sortable-handle gls-margin-small-right gls-text-center" gls-icon="icon: table"></span>Item 2
            </div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body">
                <span class="gls-sortable-handle gls-margin-small-right gls-text-center" gls-icon="icon: table"></span>Item 3
            </div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body">
                <span class="gls-sortable-handle gls-margin-small-right gls-text-center" gls-icon="icon: table"></span>Item 4
            </div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body">
                <span class="gls-sortable-handle gls-margin-small-right gls-text-center" gls-icon="icon: table"></span>Item 5
            </div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body">
                <span class="gls-sortable-handle gls-margin-small-right gls-text-center" gls-icon="icon: table"></span>Item 6
            </div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body">
                <span class="gls-sortable-handle gls-margin-small-right gls-text-center" gls-icon="icon: table"></span>Item 7
            </div>
        </li>
        <li>
            <div class="gls-card gls-card-default gls-card-body">
                <span class="gls-sortable-handle gls-margin-small-right gls-text-center" gls-icon="icon: table"></span>Item 8
            </div>
        </li>
    </ul>
    

Group

To be able to sort items from one list to another, you can group them by adding the group: GROUP-NAME option to the gls-sortable attribute on each list.

<div gls-sortable="group: my-group">
    <div></div>
</div>

<div gls-sortable="group: my-group">
    <div></div>
</div>
  • Group 1

    Item 1
    Item 2
    Item 3
    Item 4

    Group 2

    Item 1
    Item 2
    Item 3
    Item 4

    Empty Group

  • <div class="gls-child-width-1-3@s" gls-grid>
        <div>
            <h4>Group 1</h4>
            <div gls-sortable="group: sortable-group">
                <div class="gls-margin">
                    <div class="gls-card gls-card-default gls-card-body gls-card-small">Item 1</div>
                </div>
                <div class="gls-margin">
                    <div class="gls-card gls-card-default gls-card-body gls-card-small">Item 2</div>
                </div>
                <div class="gls-margin">
                    <div class="gls-card gls-card-default gls-card-body gls-card-small">Item 3</div>
                </div>
                <div class="gls-margin">
                    <div class="gls-card gls-card-default gls-card-body gls-card-small">Item 4</div>
                </div>
            </div>
        </div>
        <div>
            <h4>Group 2</h4>
            <div gls-sortable="group: sortable-group">
                <div class="gls-margin">
                    <div class="gls-card gls-card-default gls-card-body gls-card-small">Item 1</div>
                </div>
                <div class="gls-margin">
                    <div class="gls-card gls-card-default gls-card-body gls-card-small">Item 2</div>
                </div>
                <div class="gls-margin">
                    <div class="gls-card gls-card-default gls-card-body gls-card-small">Item 3</div>
                </div>
                <div class="gls-margin">
                    <div class="gls-card gls-card-default gls-card-body gls-card-small">Item 4</div>
                </div>
            </div>
        </div>
        <div>
            <h4>Empty Group</h4>
            <div gls-sortable="group: sortable-group">
        </div>
    </div>
    

Custom class

You can also apply one or more custom classes to items when they are being dragged. To do so, add the cls-custom: MY-CLASS option to the attribute.

<ul gls-sortable="cls-custom: my-class">...</ul>

Note In this example, we are using a nav from the Nav component. When dragging an item it will get a box-shadow and background.

  • <ul class="gls-nav gls-nav-default gls-width-medium" gls-sortable="cls-custom: gls-box-shadow-small gls-flex gls-flex-middle gls-background">
        <li class="gls-active"><a href="#">Active</a></li>
        <li><a href="#">Item</a></li>
        <li><a href="#">Item</a></li>
        <li><a href="#">Item</a></li>
        <li><a href="#">Item</a></li>
    </ul>
    

Component options

Any of these options can be applied to the component attribute. Separate multiple options with a semicolon. Learn more

Option Value Default Description
group String '' The group
animation String, Boolean ‘slide’ Animation mode: slide, false.
duration Number 250 Animation duration in milliseconds.
threshold Number 5 Mouse move threshold before dragging starts.
cls-item String gls-sortable-item The item class.
cls-placeholder String gls-sortable-placeholder The placeholder class.
cls-drag String gls-sortable-drag The ghost class.
cls-drag-state String gls-drag The body’s dragging class.
cls-base String gls-sortable The list’s class.
cls-no-drag String gls-sortable-nodrag Disable dragging on elements with this class.
cls-empty String gls-sortable-empty The empty list class.
cls-custom String '' The ghost’s custom class.
handle String false The handle selector.

JavaScript

Learn more about JavaScript components.

Initialization

Gloss.sortable(element, options);

Events

The following events will be triggered on elements with this component attached:

Name Description
start Fires after dragging starts.
stop Fires after dragging stops.
moved Fires after an element has been moved.
added Fires after an element has been added.
removed Fires after an element has been removed.