-
-
Notifications
You must be signed in to change notification settings - Fork 835
Expand file tree
/
Copy pathfocus-controller.ts
More file actions
71 lines (61 loc) · 1.57 KB
/
focus-controller.ts
File metadata and controls
71 lines (61 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* FocusController - demonstrates focus management controller via composition
*
* This controller:
* 1. Manages focus state (isFocused, hasFocus)
* 2. Tracks focus/blur events
* 3. Provides methods to handle focus lifecycle
*/
import { forceUpdate } from '@stencil/core';
import type { ReactiveControllerHost, ReactiveController } from '@stencil/core';
export class FocusController implements ReactiveController {
private host: ReactiveControllerHost;
private isFocused: boolean = false;
private focusCount: number = 0;
private blurCount: number = 0;
constructor(host: ReactiveControllerHost) {
this.host = host;
host.addController(this);
}
// Lifecycle methods
hostDidLoad() {
// Setup focus tracking on component load
this.setupFocusTracking();
}
hostDisconnected() {
// Cleanup focus tracking
this.cleanupFocusTracking();
}
private setupFocusTracking() {
// Default implementation - can be extended
}
private cleanupFocusTracking() {
// Default implementation - can be extended
}
// Handle focus event
handleFocus() {
this.isFocused = true;
this.focusCount++;
forceUpdate(this.host);
}
// Handle blur event
handleBlur() {
this.isFocused = false;
this.blurCount++;
forceUpdate(this.host);
}
// Get focus state
getFocusState() {
return {
isFocused: this.isFocused,
focusCount: this.focusCount,
blurCount: this.blurCount,
};
}
// Reset focus tracking
resetFocusTracking() {
this.focusCount = 0;
this.blurCount = 0;
forceUpdate(this.host);
}
}