Shadcn is a fantastic UI component library built on top of Tailwind CSS. Here's a simple and beautiful login page example using Shadcn. I'll guide you through the process, and by the end, you'll have a sleek login page up and running.
Prerequisites:
Make sure you have Node.js (14 or higher) and npm installed on your machine.
Familiarize yourself with basic HTML, CSS, and JavaScript (not mandatory but helpful).
Step 1: Set up a new project
Create a new folder for your project and navigate into it:
bash Copy code
mkdir shadcn-login-page
cd shadcn-login-page
Initialize a new npm project:
Step 2: Install required dependencies
Install Tailwind CSS, Shadcn, and other necessary dependencies:
bash Copy code
npm install tailwindcss shadcn @shadcn/ui @heroicons/react react react-dom
Here's what each package does:
tailwindcss: The utility-first CSS framework.
shadcn: The UI component library we're using.
@shadcn/ui: Shadcn's pre-built UI components.
@heroicons/react: Optional, but we'll use some icons from Heroicons.
react and react-dom: We'll use React to render our login page (you can use vanilla JavaScript too, but React makes it easier).
Step 3: Configure Tailwind CSS
Create a new file tailwind.config.js with the following content:
javascript Copy code
module.exports = {
mode: 'jit',
purge: ['./index.html', './src/**/*.{js,jsx,ts,tsx}'],
theme: {
extend: {},
},
variants: {},
plugins: [],
};
This configures Tailwind to use Just-In-Time (JIT) mode, which is faster and more efficient.
Step 4: Create the HTML file
Create a new file index.html in the root of your project:
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Login Page</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div id="root"></div>
<script src="index.js"></script>
</body>
</html>
Step 5: Create the CSS file (compiled by Tailwind)
Create a new file styles.css (yes, we'll write almost no CSS here):
css Copy code
@tailwind base;
@tailwind components;
@tailwind utilities;
This imports Tailwind's base styles, components, and utility classes.
Step 6: Build the Login Page with Shadcn and React
Create a new folder src and inside it, create index.js:
bash Copy code
mkdir src
touch src/index.js
Now, open src/index.js and paste this code:
jsx Copy code
import React from 'react';
import ReactDOM from 'react-dom';
import { Button, Input, Label, Card } from '@shadcn/ui';
import { LockClosedIcon, MailIcon } from '@heroicons/react/solid';
const LoginPage = () => {
const handleSubmit = (e) => {
e.preventDefault();
// Add your login logic here (API calls, auth, etc.)
console.log('Login form submitted!');
};
return (
<div className="h-screen flex justify-center items-center bg-gray-100">
<Card className="w-full max-w-md p-8 space-y-4 bg-white rounded-lg shadow-md">
<h2 className="text-2xl font-bold text-center text-gray-700">Welcome Back!</h2>
<form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="email" className="text-sm font-medium text-gray-600">
Email Address
</Label>
<div className="relative">
<MailIcon className="absolute top-3 left-3 w-5 h-5 text-gray-400" />
<Input
id="email"
type="email"
placeholder="your@email.com"
className="pl-10"
required
/>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="password" className="text-sm font-medium text-gray-600">
Password
</Label>
<div className="relative">
<LockClosedIcon className="absolute top-3 left-3 w-5 h-5 text-gray-400" />
<Input
id="password"
type="password"
placeholder="••••••••"
className="pl-10"
required
/>
</div>
</div>
<Button type="submit" className="w-full">
Log In
</Button>
</form>
<p className="text-sm text-center text-gray-500">
Don't have an account?{' '}
<a href="#" className="font-medium text-indigo-600 hover:text-indigo-500">
Sign up
</a>
</p>
</Card>
</div>
);
};
ReactDOM.render(
<React.StrictMode>
<LoginPage />
</React.StrictMode>,
document.getElementById('root')
);
Here's what we're doing:
Importing necessary components from Shadcn (Button, Input, Label, Card) and Heroicons (LockClosedIcon, MailIcon).
Creating a LoginPage React component with a simple form.
Using Tailwind utility classes for styling (e.g., space-y-4, text-gray-600, pl-10).
Adding basic form handling with handleSubmit.
Step 7: Compile Tailwind CSS
Add a build script to your package.json:
json Copy code
"scripts": {
"build:css": "tailwindcss -i styles.css -o dist/styles.css",
"start": "npm run build:css && parcel index.html"
},
This script compiles your styles.css (with Tailwind imports) into dist/styles.css.
Step 8: Install Parcel (optional but recommended)
To make development easier, let's install Parcel, a fast, zero-config web bundler:
bash Copy code
npm install parcel-bundler
Step 9: Run your Login Page!
Finally, run:
This will compile your Tailwind CSS and launch your login page in the default browser. If everything went smoothly, you should see a beautiful, simple login page:
Voilà!
You now have a stunning login page built with Shadcn, Tailwind CSS, and React. Feel free to customize it as you like!
Tips & Variations:
To add dark mode support, follow Tailwind's official guide .
Experiment with different Shadcn components, like Alert, Avatar, or Dropdown.
Replace the Heroicons with your preferred icon set or SVGs.
Integrate a real login system using your favorite backend framework (e.g., Node.js, Django, Ruby on Rails).
Happy coding! If you encounter issues or have questions, don't hesitate to ask.