Sun Jan 26 2025
Live DemoPage Lock is a React library I built to solve a common problem in collaborative web applications: preventing multiple users from editing the same content simultaneously. It provides elegant, real-time page ownership management with built-in UI components and hooks.
In collaborative applications, when multiple users try to edit the same record or page simultaneously, you get:
Traditional solutions are either too complex or don't provide good UX.
Page Lock provides a simple, elegant solution with:
๐ Real-time ownership tracking - Know who's editing what, when
๐ Automatic ownership transfer - Seamless handoffs between users
๐จ Beautiful UI components - Ready-to-use badges and modals
๐ฑ Mobile-friendly design - Works great on all devices
๐ฏ TypeScript support - Full type safety out of the box
๐ Framework agnostic - Works with Next.js, Create React App, etc.
ID: 1
ID: 2
ID: 3
ID: 1
ID: 2
ID: 3
// Lock entire pages
const { lockPage, unlockPage } = usePageOwnership({
pageId: 'user-profile-123'
});
// Lock specific components
<OwnershipModal pageId="document-editor-456" />
// Just show ownership status
<OwnerBadge pageId="settings-panel-789" />
// Use localStorage for simple cases
const adapter = createLocalStorageAdapter({
prefix: 'my-app'
});
// Use your API for production
const adapter = createApiStorageAdapter({
getAllOwners: () => fetch('/api/owners').then(r => r.json()),
lockPage: (pageId, userId, userName) =>
fetch('/api/lock', { method: 'POST', body: JSON.stringify({pageId, userId, userName}) }),
unlockPage: (pageId, userId) =>
fetch('/api/unlock', { method: 'POST', body: JSON.stringify({pageId, userId}) }),
takePageOwnership: (pageId, userId, userName) =>
fetch('/api/take-ownership', { method: 'POST', body: JSON.stringify({pageId, userId, userName}) })
});
The library polls for ownership changes at configurable intervals (default 3 seconds), ensuring all users see locks and releases immediately. No websockets required!
Component | Purpose | Use Case |
---|---|---|
OwnershipProvider | Context provider for ownership state | Wrap your app or specific sections |
usePageOwnership | Hook for programmatic control | Custom implementations |
OwnerBadge | Simple ownership indicator | Show who owns what |
OwnershipModal | Full ownership management UI | Complete lock/unlock flow |
interface PageOwner {
user_id: string;
user_name: string;
page_id: string;
timestamp: number;
}
interface OwnershipConfig {
userAdapter: UserAdapter;
ownershipAdapter: OwnershipAdapter;
options?: {
pollingInterval?: number;
};
}
Prevent sales reps from simultaneously editing the same customer record, avoiding data loss and confusion.
Lock document sections while users are editing, similar to Google Docs but for React applications.
Ensure only one admin can modify system settings at a time, preventing conflicting changes.
Lock multi-step forms to prevent multiple submissions or conflicting edits during long workflows.
npm install @joecarot/page-lock
# or
yarn add @joecarot/page-lock
import {
OwnershipProvider,
createLocalStorageAdapter,
OwnerBadge
} from '@joecarot/page-lock';
// Setup adapters
const userAdapter = {
getCurrentUser: () => Promise.resolve({
id: 'user-123',
email: 'user@example.com',
name: 'John Doe'
})
};
const ownershipAdapter = createLocalStorageAdapter({
prefix: 'my-app'
});
const config = {
userAdapter,
ownershipAdapter,
options: {
pollingInterval: 3000
}
};
// Use in your app
function App() {
return (
<OwnershipProvider config={config}>
<div>
<h1>User Profile</h1>
<OwnerBadge pageId="user-profile-123" />
{/* Your form content */}
</div>
</OwnershipProvider>
);
}
# Run tests
npm test
# Coverage report
npm run coverage
# Run example
cd example && npm start
The library includes extensive unit tests covering:
The library is designed to be lightweight:
๐ WebSocket Support - Real-time updates without polling
๐ญ Role-based Permissions - Different lock types for different user roles
๐ More Adapters - Redis, PostgreSQL, MongoDB adapters
๐ Analytics - Usage tracking and ownership metrics
๐จ Theme Customization - More styling options
The project is open source and welcomes contributions! Check out the GitHub repository for:
Building this library taught me about:
Creating intuitive APIs that are both simple for basic use cases and flexible for complex scenarios.
Handling real-time state synchronization across multiple components and user sessions.
Packaging, versioning, and distributing a TypeScript library with proper type definitions.
Balancing functionality with simplicity - making complex ownership logic feel natural.
Page Lock solves a real problem that many collaborative applications face. By providing simple, elegant ownership management, it helps teams build better collaborative experiences without the complexity of traditional solutions.
Try it out in your next project - I'd love to hear how it works for you!
๐ฆ npm package | ๐ GitHub repository | ๐ฏ Live example