Okay, let's create a SaaS login page using Shopify Polaris.
Polaris is a React component library, so this will be a React component. You'll need to have a React project set up and Polaris installed to run this code.
Key Polaris Components Used:
AppProvider: The root component for any Polaris application, providing context for styling and localization.
Frame: Used for global components like Toast.
Page: Provides consistent page layout, including spacing and responsiveness.
Layout: For structuring content into sections and columns. We'll use Layout.Section to center our login form.
Card: A container for grouping related content, often used for forms.
Text: For headings, body text, and other typographic elements.
FormLayout: Helps arrange form fields with proper spacing.
TextField: For input fields like email and password.
Button: For the login action.
Link: For "Forgot Password?" and "Sign Up" links.
Image: For displaying your SaaS logo.
Toast: For showing transient messages (e.g., login success/failure).
1. Setup (if you haven't already)
First, make sure you have a React project and Polaris installed:
# Create a new React app (if you don't have one)
npx create-react-app my-saas-login
# Navigate into your project
cd my-saas-login
# Install Polaris
npm install @shopify/polaris @shopify/app-bridge
# or
yarn add @shopify/polaris @shopify/app-bridge
2. LoginPage.js Component
Create a file named LoginPage.js (or similar) in your src directory.
// src/LoginPage.js
import React, { useState, useCallback } from 'react';
import {
AppProvider,
Page,
Layout,
Card,
Text,
FormLayout,
TextField,
Button,
Link,
Image,
Frame,
Toast,
} from '@shopify/polaris';
import en from '@shopify/polaris/locales/en.json';
// --- Placeholder Logo ---
// Replace this with your actual SaaS logo URL.
// For demonstration, using a generic placeholder.
const SAAS_LOGO_URL = 'https://cdn.shopify.com/s/files/1/0770/4574/files/logo.svg?v=1686820542'; // A generic Shopify-like logo
function LoginPage() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [showToast, setShowToast] = useState(false);
const [toastContent, setToastContent] = useState('');
const [toastError, setToastError] = useState(false);
const handleEmailChange = useCallback((value) => setEmail(value), []);
const handlePasswordChange = useCallback((value) => setPassword(value), []);
const toggleToastActive = useCallback(
() => setShowToast((showToast) => !showToast),
[],
);
const handleSubmit = useCallback(async (event) => {
event.preventDefault();
// --- Mock Login Logic ---
// In a real application, you would send this data to your backend
// for authentication (e.g., using fetch or axios).
console.log('Attempting to log in with:', { email, password });
// Simulate API call
try {
// Replace with your actual API call
const response = await new Promise((resolve, reject) => {
setTimeout(() => {
if (email === 'user@example.com' && password === 'password123') {
resolve({ success: true, message: 'Login successful!' });
} else {
reject({ success: false, message: 'Invalid email or password.' });
}
}, 1500); // Simulate network delay
});
if (response.success) {
setToastContent(response.message);
setToastError(false);
setShowToast(true);
// Redirect or perform other actions on successful login
console.log('Login successful! You can now redirect the user.');
}
} catch (error) {
setToastContent(error.message || 'An unexpected error occurred.');
setToastError(true);
setShowToast(true);
console.error('Login failed:', error.message);
}
}, [email, password]);
const toastMarkup = showToast ? (
<Toast content={toastContent} error={toastError} onDismiss={toggleToastActive} />
) : null;
return (
// AppProvider is necessary for Polaris components to work
<AppProvider i18n={en}>
{/* Frame is required for Toast and other global components */}
<Frame>
<Page narrowWidth> {/* narrowWidth helps center content on wide screens */}
<Layout>
<Layout.Section oneThird> {/* Uses one-third width for centering */}
<div
style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
paddingTop: '20px',
paddingBottom: '20px',
}}
>
{/* SaaS Logo */}
<Image
source={SAAS_LOGO_URL}
alt="Your SaaS Logo"
width={70} // Adjust size as needed
height={70}
style={{ marginBottom: '16px' }}
/>
<Text variant="headingXl" as="h1" alignment="center">
Welcome Back!
</Text>
<Text variant="bodyMd" as="p" alignment="center" color="subdued">
Log in to your account
</Text>
</div>
<Card sectioned>
<form onSubmit={handleSubmit}>
<FormLayout>
<TextField
label="Email"
type="email"
value={email}
onChange={handleEmailChange}
autoComplete="email"
placeholder="you@example.com"
required
/>
<TextField
label="Password"
type="password"
value={password}
onChange={handlePasswordChange}
autoComplete="current-password"
placeholder="********"
required
/>
<Button submit primary fullWidth>
Log in
</Button>
</FormLayout>
</form>
</Card>
<div
style={{
marginTop: '20px',
textAlign: 'center',
}}
>
<Link url="#" onClick={() => console.log('Forgot password clicked!')}>
Forgot your password?
</Link>
<Text variant="bodyMd" as="p" color="subdued" style={{ marginTop: '10px' }}>
Don't have an account?{' '}
<Link url="#" onClick={() => console.log('Sign up clicked!')}>
Sign up
</Link>
</Text>
</div>
</Layout.Section>
</Layout>
</Page>
{toastMarkup}
</Frame>
</AppProvider>
);
}
export default LoginPage;
3. Integrate into your App.js
Now, you can use this LoginPage component in your main App.js file:
// src/App.js
import React from 'react';
import './App.css'; // Your global CSS if any
import LoginPage from './LoginPage';
// Import Polaris styles (essential!)
import '@shopify/polaris/build/esm/styles.css';
function App() {
return (
<div className="App">
<LoginPage />
</div>
);
}
export default App;
Explanation:
- Imports: We import all necessary React hooks (
useState, useCallback) and Polaris components. en from locales is for internationalization.
- State Management:
email and password store the input field values.
showToast, toastContent, toastError manage the visibility and content of the toast messages.
SAAS_LOGO_URL: Crucially, change this to your actual SaaS logo URL.
handleSubmit:
- This function is called when the form is submitted.
- It prevents the default form submission (
event.preventDefault()).
- It contains mock login logic. In a real application, you would replace the
setTimeout and hardcoded credentials with an actual fetch or axios call to your backend API.
- On success or failure, it updates the toast state to show a message to the user.
- Polaris Layout (
AppProvider, Frame, Page, Layout, Layout.Section):
AppProvider wraps everything.
Frame is a container for components like Toast that render outside the normal document flow.
Page provides standard page padding and sets up responsiveness. narrowWidth helps keep content centered and readable on very wide screens.
Layout and Layout.Section oneThird are used to create a column that takes up one-third of the available width, which responsively centers the login form on larger screens and expands on smaller ones.
- Branding (
Image, Text):
- An
Image component displays your logo. Adjust width and height.
Text components are used for the main heading and subheading, providing clear typography.
- Login Form (
Card, FormLayout, TextField, Button):
- The
Card component visually groups the login fields.
This setup provides a clean, responsive, and functional SaaS login page adhering to Shopify's Polaris design system.