Spaces:
Running
π Building a React App with shadcn/ui Styling & Sorting Tables
π Table of Contents
- Project Overview
- Prerequisites
- Step-by-Step Build Process
- File Structure & Purpose
- Key Implementation Details
- Customization Guide
- Deployment to Hugging Face
- Troubleshooting
π― Project Overview
This guide shows you how to build a MongoDB Data Viewer - a React application with:
- β¨ Modern shadcn/ui styling (custom CSS implementation)
- π Interactive data tables with sorting functionality
- π Real-time data refresh capabilities
- π± Responsive design for all devices
- π¨ Professional UI/UX without external CSS frameworks
Why This Approach?
- No Tailwind CSS dependencies - avoids compatibility issues
- Custom CSS variables - easy theming and customization
- Modular components - maintainable and scalable
- Clean architecture - follows React best practices
π§ Prerequisites
Required Software:
- Node.js (version 14+ recommended)
- npm (comes with Node.js)
- Git (for version control)
- Code editor (VS Code recommended)
Knowledge Requirements:
- Basic React concepts (components, hooks, state)
- Basic JavaScript (ES6+, async/await)
- Basic CSS understanding
- Basic Git commands
ποΈ Step-by-Step Build Process
Phase 1: Project Setup
Step 1: Create React App
npx create-react-app mongodb-data-viewer
cd mongodb-data-viewer
Step 2: Clean Up Default Files
Remove unnecessary files:
src/logo.svg
src/reportWebVitals.js
src/setupTests.js
public/logo*.png
public/manifest.json
public/robots.txt
Step 3: Install Dependencies
npm install @radix-ui/react-slot
npm install -D tailwindcss postcss autoprefixer
Phase 2: CSS Architecture
Step 4: Create Custom CSS System
Create src/index.css
with:
- CSS variables for theming
- Utility classes (similar to Tailwind)
- Component-specific styles
- Responsive design rules
Step 5: Set Up PostCSS (Optional)
npx tailwindcss init -p
Note: We'll use custom CSS instead of Tailwind
Phase 3: Component Architecture
Step 6: Create Utility Functions
Create src/lib/utils.js
:
import { clsx } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs) {
return twMerge(clsx(inputs))
}
Step 7: Build UI Components
Create component files:
src/components/ui/button.jsx
src/components/ui/table.jsx
Step 8: Create Main Data Component
Create src/components/DataTable.jsx
with:
- State management for data
- Sorting functionality
- Loading states
- Error handling
Phase 4: App Integration
Step 9: Update Main App
Modify src/App.js
to:
- Import custom components
- Use custom CSS classes
- Create responsive layout
Step 10: Configure Entry Point
Update src/index.js
to:
- Remove unused imports
- Import custom CSS
- Render main App component
π File Structure & Purpose
mongodb-data-viewer/
βββ .git/ # Git repository
βββ .gitignore # Git ignore rules
βββ package.json # Dependencies & scripts
βββ package-lock.json # Locked dependency versions
βββ README.md # Project documentation
βββ BUILD_GUIDE.md # This guide
βββ public/ # Static assets
β βββ index.html # Main HTML template
β βββ favicon.ico # Browser icon
βββ src/ # Source code
βββ index.js # App entry point
βββ index.css # Custom CSS system
βββ App.js # Main app component
βββ App.css # Basic React styling (required for Streamlit)
βββ App.test.js # Test file
βββ components/ # Reusable components
βββ DataTable.jsx # Main data display component
βββ ui/ # UI component library
βββ button.jsx # Button component
βββ table.jsx # Table components
File Purpose Breakdown:
Root Level Files:
package.json
: Defines dependencies, scripts, and project metadata.gitignore
: Specifies which files Git should ignoreREADME.md
: Project documentation and Hugging Face configuration
Public Directory:
index.html
: HTML template that React renders intofavicon.ico
: Browser tab icon (required for professional appearance)
Source Directory:
index.js
: JavaScript entry point that bootstraps the React appindex.css
: Custom CSS system with shadcn/ui design tokensApp.js
: Main application component that orchestrates the UIApp.css
: Basic React styling (required for Streamlit compatibility)App.test.js
: Test file for the main App component
Components Directory:
DataTable.jsx
: Core business logic for displaying and sorting dataui/button.jsx
: Reusable button component with consistent stylingui/table.jsx
: Table components for structured data display
π Key Implementation Details
1. Custom CSS System
:root {
--background: #ffffff;
--foreground: #020817;
--primary: #0f172a;
--primary-foreground: #f8fafc;
/* ... more variables */
}
Why Custom CSS?
- No framework dependencies - avoids compatibility issues
- Full control - customize every aspect of styling
- Performance - no unused CSS classes
- Maintainability - clear, organized structure
2. Component State Management
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [sortConfig, setSortConfig] = useState({ key: null, direction: 'asc' });
State Structure:
data
: Array of objects to display in tableloading
: Boolean for loading stateserror
: String for error messagessortConfig
: Object tracking sort column and direction
3. Sorting Algorithm
const sortData = (key) => {
let direction = 'asc';
if (sortConfig.key === key) {
if (sortConfig.direction === 'asc') {
direction = 'desc';
} else if (sortConfig.direction === 'desc') {
setSortConfig({ key: null, direction: 'none' });
return;
}
}
setSortConfig({ key, direction });
};
Three-State Sorting:
- Ascending (β): First click, sorts AβZ
- Descending (β): Second click, sorts ZβA
- None (βοΈ): Third click, returns to original order
4. Responsive Design
.container {
width: 100%;
margin-left: auto;
margin-right: auto;
padding-left: 2rem;
padding-right: 2rem;
}
@media (min-width: 1400px) {
.container {
max-width: 1400px;
}
}
Responsive Features:
- Mobile-first approach
- Flexible containers that adapt to screen size
- Consistent spacing across devices
- Touch-friendly interactions
π¨ Customization Guide
Changing Colors
Modify CSS variables in src/index.css
:
:root {
--primary: #your-color-here;
--background: #your-bg-color;
/* ... */
}
Adding New Columns
- Update mock data in
DataTable.jsx
- Add table header with sorting capability
- Add table cell for new data
- Update sorting logic if needed
Modifying Table Styling
Edit table-specific CSS in src/index.css
:
.table th {
/* Custom header styles */
}
.table td {
/* Custom cell styles */
}
Adding New Features
- Search: Add input field and filter logic
- Pagination: Implement page-based data loading
- Export: Add CSV/PDF download functionality
- Charts: Integrate data visualization libraries
π Deployment to Hugging Face
1. Configure README.md
---
title: MongoDB Data Viewer
emoji: π
colorFrom: blue
colorTo: indigo
sdk: static
pinned: false
app_build_command: npm run build
app_file: build/index.html
---
2. Build the App
npm run build
3. Push to Git Repository
git add .
git commit -m "Add MongoDB data viewer with sorting"
git push origin main
4. Hugging Face Integration
- Create new Space on Hugging Face
- Connect Git repository
- Automatic deployment will begin
- Monitor build logs for any issues
π§ Troubleshooting
Common Issues & Solutions:
Build Errors
- Problem: Missing dependencies
- Solution: Run
npm install
and checkpackage.json
Styling Issues
- Problem: CSS not loading
- Solution: Verify
index.css
import inindex.js
Component Errors
- Problem: Import/export mismatches
- Solution: Check file paths and component names
Deployment Issues
- Problem: Hugging Face build fails
- Solution: Verify
app_file
points tobuild/index.html
Performance Optimization:
- Lazy loading for large datasets
- Memoization for expensive calculations
- Debouncing for search inputs
- Virtual scrolling for very long tables
π Next Steps & Enhancements
Immediate Improvements:
- Add search functionality across all columns
- Implement pagination for large datasets
- Add data export (CSV, JSON)
- Create dark mode toggle
Advanced Features:
- Real-time updates with WebSocket
- Advanced filtering with multiple criteria
- Data visualization with charts
- User authentication and permissions
Backend Integration:
- MongoDB connection with Node.js/Express
- RESTful API endpoints
- Database optimization with indexing
- Caching layer for performance
π― Summary
This guide has walked you through building a professional-grade React application with:
β
Clean architecture following React best practices
β
Custom CSS system without external dependencies
β
Interactive data tables with sorting functionality
β
Responsive design for all devices
β
Professional styling that matches shadcn/ui design system
β
Easy deployment to Hugging Face Spaces
The result is a maintainable, scalable, and beautiful application that you can easily customize and extend for your specific needs.
Happy coding! π