You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lightweight library to block user interactions in browsers.
7
7
8
-
## ⚠️ Breaking Changes in v0.3.0
9
-
10
-
Version 0.3.0 introduces significant API changes from v0.2.x:
11
-
12
-
-**Factory function instead of singleton**: `blokr()` returns an instance instead of being a singleton object
13
-
-**Options-based API**: `lock({ timeout, scope })` instead of separate `setTimeout()` method
14
-
-**No reference counting**: Multiple `lock()` calls return `false` instead of incrementing a counter
15
-
-**No `setTimeout()` method**: Use `lock({ timeout })` option instead
16
-
-**No `unlock(abort)` parameter**: `unlock()` always releases the lock immediately
17
-
18
-
**Migration guide:** See [Migration from v0.2.x](#migration-from-v02x) below.
19
-
20
-
**Note:** This library is under active development. Future versions may introduce additional breaking changes. Please refer to the changelog before upgrading.
21
-
22
8
## Features
23
9
24
10
-**Factory-based API**: Support for both global and element-specific locks
25
-
-**Scope filtering**: Control which events to block (`inside`, `outside`, `self`)
26
11
-**No overlay elements**: Blocks interactions without adding elements to the DOM
27
-
-**All interaction types**: Blocks mouse, keyboard, touch, and wheel events
12
+
-**Scope filtering**: Control which events to block (`inside`, `outside`, `self`)
28
13
-**Per-lock timeout**: Optional automatic unlock after specified time
29
-
-**No dependencies**: Zero external dependencies
30
14
-**TypeScript**: Full type support included
15
+
-**React Hook**: Built-in `useBlokr()` hook for React components
31
16
32
17
## Why Blokr?
33
18
34
-
### Problems with CSS-based Solutions
19
+
### Comparison with Alternative Solutions
20
+
21
+
Blokr provides a unique approach to blocking user interactions. Here's how it compares with other techniques:
22
+
23
+
#### The `inert` Attribute
24
+
25
+
The HTML5 `inert` attribute marks an element as "inert," preventing user interactions including keyboard navigation.
26
+
27
+
#### CSS `pointer-events: none`
28
+
29
+
CSS `pointer-events: none` disables mouse and touch events on elements, but cannot block keyboard events or prevent tab navigation.
30
+
31
+
#### The `<dialog>` Element
32
+
33
+
The HTML5 `<dialog>` element creates a modal dialog but adds a DOM element and provides limited scope flexibility for non-modal use cases.
-**React Hook support**: New `useBlokr()` hook for React applications (React 18+ required)
43
56
44
-
### How Blokr Solves These Problems
57
+
##⚠️ Breaking Changes in v0.4.0
45
58
46
-
-✅ **Blocks all interaction types**: Mouse, keyboard, touch, and wheel events
47
-
-✅ **Optional timeout protection**: Automatically unlock after specified time
48
-
- ✅ **No DOM changes**: Works via event listeners only
49
-
- ✅ **Flexible scoping**: Block events inside, outside, or only on specific elements
50
-
- ✅ **No z-index conflicts**: No overlay elements needed
51
-
- ✅ **TypeScript support**: Full type definitions included
59
+
-**UMD format removed**: CDN usage now requires ES modules only (`blokr/dist/index.js`)
60
+
-**No breaking changes to core API**: All v0.3.0 JavaScript APIs remain unchanged
61
+
62
+
For changes from v0.2.x, see the [Migration from v0.2.x](#migration-from-v02x) section below.
63
+
64
+
**Note:** This library is under active development. Future versions may introduce additional breaking changes. Please refer to the changelog before upgrading.
52
65
53
66
## Installation
54
67
55
68
```bash
56
69
npm install blokr
57
70
```
58
71
59
-
## Usage
72
+
### React Hook Support
73
+
74
+
The `useBlokr()` React Hook is included in the same package. React 18.0+ or React 19.0+ is required to use the hook:
75
+
76
+
```bash
77
+
npm install blokr react
78
+
```
79
+
80
+
The `react` package is an optional peer dependency. If you don't use React, you can ignore this requirement.
-`'inside'`: Block events inside target element (default)
170
-
-`'outside'`: Block events outside target element
171
-
-`'self'`: Block events on target element itself only
178
+
-`'inside'`: Block events inside the target element (default)
179
+
-`'outside'`: Block events outside the target element
180
+
-`'self'`: Block events on the target element only
172
181
173
182
**Returns:**`true` if lock was applied, `false` if already locked
174
183
@@ -320,6 +329,107 @@ async function loadData() {
320
329
}
321
330
```
322
331
332
+
## React Hook
333
+
334
+
The `useBlokr()` hook provides a React-friendly way to manage user interaction blocking. It works seamlessly with the factory-based API and manages refs automatically.
Returns an object containing a ref and three control functions for managing user interaction blocking.
390
+
391
+
**Type Parameters:**
392
+
-`T` (optional): The DOM element type. Default: `Element`
393
+
394
+
**Parameters:**
395
+
-`allowGlobal` (optional): If `true`, enables global lock mode that blocks interactions across the entire page instead of a specific element. When using global lock, the `target` ref is not needed. Default: `false`
396
+
397
+
**Returns:** An object with:
398
+
-`target`: A React ref to assign to the target element (`RefObject<T | null>`)
399
+
-`lock`: Function to lock user interactions on the element (`(options?: Options) => boolean`)
400
+
-`unlock`: Function to unlock user interactions (`() => void`)
401
+
-`isLocked`: Function to check if currently locked (`() => boolean`)
402
+
403
+
**Parameters (lock function):**
404
+
-`options.timeout` (optional): Auto-unlock timeout in milliseconds
405
+
-`options.scope` (optional): Event blocking scope (`'inside'`, `'outside'`, or `'self'`)
406
+
407
+
**Returns (lock function):**`true` if lock was applied, `false` if already locked or if the ref is not set (when using element-specific lock)
408
+
409
+
### allowGlobal Parameter
410
+
411
+
The `allowGlobal` parameter enables global lock mode, which blocks user interactions across the entire page instead of scoping to a specific element.
412
+
413
+
**Global Lock (`allowGlobal=true`):**
414
+
```tsx
415
+
// No need to destructure 'target' since we're not using element-specific locking
-**Only blocks genuine user interactions**: Programmatically triggered events (e.g., `element.click()`) are not blocked.
378
-
-**Event listener priority**: Event listeners are registered at the capture phase. May not work correctly when used with event delegation libraries. Loading Blokr before other libraries may resolve this issue.
379
-
-**Target-specific locks accept Elements only**: The `blokr(target)` factory function only accepts DOM `Element` nodes. To block interactions across the entire page, use the global lock: `blokr()` (without a target parameter).
487
+
-**Event listener priority**: Event listeners are registered at the capture phase. May not work correctly when used with event delegation libraries.
0 commit comments