Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/MohamedRejeb/compose-dnd

Compose DND is a library that allows you to easily add drag and drop functionality to your Jetpack Compose or Compose Multiplatform projects.
https://github.com/MohamedRejeb/compose-dnd

android android-library compose compose-library compose-multiplatform compose-multiplatform-library compose-ui dnd drag drag-and-drop drag-drop draggable jetpack-compose kotlin kotlin-android kotlin-multiplatform

Last synced: about 1 month ago
JSON representation

Compose DND is a library that allows you to easily add drag and drop functionality to your Jetpack Compose or Compose Multiplatform projects.

Awesome Lists containing this project

README

        

# Compose Drag And Drop

Compose DND is a library that allows you to easily add drag and drop functionality to your Jetpack Compose or Compose Multiplatform projects.

[![Kotlin](https://img.shields.io/badge/kotlin-1.9.22-blue.svg?logo=kotlin)](http://kotlinlang.org)
[![MohamedRejeb](https://raw.githubusercontent.com/MohamedRejeb/MohamedRejeb/main/badges/mohamedrejeb.svg)](https://github.com/MohamedRejeb)
[![Apache-2.0](https://img.shields.io/badge/License-Apache%202.0-green.svg)](https://opensource.org/licenses/Apache-2.0)
[![BuildPassing](https://shields.io/badge/build-passing-brightgreen)](https://github.com/MohamedRejeb/compose-dnd/actions)
[![Maven Central](https://img.shields.io/maven-central/v/com.mohamedrejeb.dnd/compose-dnd)](https://search.maven.org/search?q=g:%22com.mohamedrejeb.dnd%22%20AND%20a:%compose-dnd%22)

![Compose DND thumbnail](docs/images/thumbnail.png)

## Installation

[![Maven Central](https://img.shields.io/maven-central/v/com.mohamedrejeb.dnd/compose-dnd)](https://search.maven.org/search?q=g:%22com.mohamedrejeb.dnd%22%20AND%20a:%compose-dnd%22)

Add the following dependency to your module `build.gradle.kts` file:

```kotlin
implementation("com.mohamedrejeb.dnd:compose-dnd:0.3.0")
```

## Usage

### Drag and Drop

To implement drag and drop functionality:
- Create a `DragAndDropState` with `rememberDragAndDropState`.

```kotlin
val dragAndDropState = rememberDragAndDropState()
```

- Add `DragAndDropContainer` composable which will wrap the draggable items.

```kotlin
DragAndDropContainer(
state = dragAndDropState,
) {

}
```

- Add `DraggableItem` composable for each draggable item.

```kotlin
DraggableItem(
state = dragAndDropState,
key = task.id, // Unique key for each draggable item
data = task, // Data to be passed to the drop target
) {

}
```

- Add `Modifier.dropTarget` for each drop target.

```kotlin
Modifier.dropTarget(
state = dragAndDropState,
key = task.id, // Unique key for each drop target
onDrop = { state -> // Data passed from the draggable item
// Handle drop
}
)
```

> For more details, check out the [sample](https://github.com/MohamedRejeb/compose-dnd/tree/main/sample/common/src/commonMain/kotlin)

### Reorder List

To implement reorder list functionality:

- Create a `ReorderState` with `rememberReorderState`.

```kotlin
val reorderState = rememberReorderState()
```

- Add `ReorderContainer` composable which will wrap the reorderable items.

```kotlin
ReorderContainer(
state = reorderState,
) {

}
```

- Add `ReorderableItem` composable for each reorderable item.

```kotlin
ReorderableItem(
state = reorderState,
key = task.id, // Unique key for each reorderable item
data = task, // Data to be passed to the drop target
onDrop = { state -> // Data passed from the draggable item
// Handle drop
}
) {

}
```

The `ReorderableItem` composable is at the same time a `DraggableItem` and a `dropTarget`.

> For more details, check out the [sample](https://github.com/MohamedRejeb/compose-dnd/tree/main/sample/common/src/commonMain/kotlin)

### Enable/Disable Drag and Drop

If you want to enable/disable drag and drop functionality, you can use the `enabled` parameter in the `DragAndDropContainer` and `ReorderContainer` composable.

```kotlin
DragAndDropContainer(
state = dragAndDropState,
enabled = false
) {

}
```

```kotlin
ReorderContainer(
state = reorderState,
enabled = false
) {

}
```

> This will disable the drag and drop functionality for all the draggable items.

If you want to disable drag and drop for a specific item, you can use the `enabled` parameter in the `DraggableItem` and `ReorderableItem` composable.

```kotlin
DraggableItem(
state = dragAndDropState,
key = task.id,
data = task,
enabled = false
) {

}
```

```kotlin
ReorderableItem(
state = reorderState,
key = task.id,
data = task,
onDrop = { state ->
// Handle drop
},
enabled = false
) {

}
```

## Contribution
If you've found an error in this sample, please file an issue.

Feel free to help out by sending a pull request :heart:.

[Code of Conduct](https://github.com/MohamedRejeb/compose-dnd/blob/main/CODE_OF_CONDUCT.md)

## Find this library useful? :heart:
Support it by joining __[stargazers](https://github.com/MohamedRejeb/compose-dnd/stargazers)__ for this repository. :star:

Also, __[follow me](https://github.com/MohamedRejeb)__ on GitHub for more libraries! 🤩

You can always

# License
```
Copyright 2023 Mohamed Rejeb

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```