To set up a React application using Bootstrap 5.3 and the dnd-kit library for drag-and-drop functionality, we'll create a layout with two rows (for desktop and tablet) and one row (for mobile), each containing two columns. Each column will hold a card, and we'll make these cards draggable.
Here's a step-by-step guide on how to achieve this:
Step 1: Create a React App
If you haven't already created a React application, you can use Create React App:
npx create-react-app my-dnd-app
cd my-dnd-app
Step 2: Install Dependencies
You need to install Bootstrap and dnd-kit/core:
npm install bootstrap dnd-kit
Step 3: Import Bootstrap CSS
In your src/index.js (or src/index.tsx if using TypeScript) file, import the Bootstrap CSS:
import 'bootstrap/dist/css/bootstrap.min.css';
Step 4: Create the Draggable Layout
Create a new component, e.g., DraggableCards.js, where you will implement the DnD functionality.
// src/DraggableCards.js
import React, { useState } from 'react';
import { DndContext, useDraggable, useDroppable } from '@dnd-kit/core';
import { Container, Row, Col, Card } from 'react-bootstrap';
const initialCards = [
{ id: 'card-1', content: 'Card 1' },
{ id: 'card-2', content: 'Card 2' },
{ id: 'card-3', content: 'Card 3' },
{ id: 'card-4', content: 'Card 4' },
];
const DraggableCard = ({ card }) => {
const { attributes, listeners, setNodeRef, transform } = useDraggable({
id: card.id,
});
return (
<Card ref={setNodeRef} {...listeners} {...attributes} style={{ margin: '10px', transform: CSS.Transform.toString(transform) }}>
<Card.Body>{card.content}</Card.Body>
</Card>
);
};
const DraggableCards = () => {
const [cards, setCards] = useState(initialCards);
const handleDragEnd = (event) => {
// Handle the reordering logic here
console.log(event.active.id, event.over.id);
};
return (
<DndContext onDragEnd={handleDragEnd}>
<Container>
<Row className="desktop-tablet">
<Col>
<DraggableCard card={cards[0]} />
</Col>
<Col>
<DraggableCard card={cards[1]} />
</Col>
</Row>
<Row className="desktop-tablet">
<Col>
<DraggableCard card={cards[2]} />
</Col>
<Col>
<DraggableCard card={cards[3]} />
</Col>
</Row>
<Row className="mobile">
<Col>
<DraggableCard card={cards[0]} />
</Col>
<Col>
<DraggableCard card={cards[1]} />
</Col>
<Col>
<DraggableCard card={cards[2]} />
</Col>
<Col>
<DraggableCard card={cards[3]} />
</Col>
</Row>
</Container>
</DndContext>
);
};
export default DraggableCards;
Step 5: Update App Component
Replace the content of src/App.js with the following code to include the DraggableCards component:
// src/App.js
import React from 'react';
import DraggableCards from './DraggableCards';
const App = () => {
return (
<div>
<h1 className="text-center">Draggable Cards</h1>
<DraggableCards />
</div>
);
};
export default App;
Step 6: Add Some Basic Styles
You can add your custom styles in an appropriate CSS file. For example, create src/App.css and include some styles:
.desktop-tablet {
display: none; /* Hide by default */
}
.mobile {
display: flex; /* Show mobile layout */
}
@media (min-width: 768px) {
.desktop-tablet {
display: flex; /* Show desktop/tablet layout */
}
.mobile {
display: none; /* Hide mobile layout */
}
}
Final Step: Run Your Application
Run your application to see the result:
npm start
You should now have a React application with draggable cards arranged in two rows for desktop/tablet and one row for mobile.
Extending Functionality
-
Drag and Drop Logic: The handleDragEnd function is where you should implement the logic for updating the order of your cards once they have been dragged and dropped. You can use the event.active (the item that was dragged) and event.over (the item that was dropped over) to rearrange your card positions in the state.
-
Accessibility and Usability: Make sure to enhance the usability and accessibility of your drag-and-drop cards as needed. This could involve providing visual feedback when dragging cards or ensuring keyboard accessibility.
-
Styling and Customization: Feel free to customize the styles of the cards and layout as per your requirements.
This is a basic implementation to help you get started with drag-and-drop functionality using dnd-kit, React, and Bootstrap.