@@ -7,7 +7,8 @@ export default function SearchInteractions({ searchInput }) {
7
7
let observer = null ;
8
8
let dropdownObserver = null ;
9
9
let dropdownMenu = null ;
10
-
10
+ const debug = false ; // Set to true for debugging logs
11
+
11
12
// Fade content wrapper when focusing on search input
12
13
function handleFocus ( ) {
13
14
contentWrapper . style . opacity = '0.35' ;
@@ -18,53 +19,62 @@ export default function SearchInteractions({ searchInput }) {
18
19
function handleBlur ( event ) {
19
20
// Only process blur if not clicking within dropdown
20
21
const relatedTarget = event . relatedTarget ;
21
- if ( relatedTarget && (
22
- relatedTarget . closest ( '.algolia-autocomplete' ) ||
23
- relatedTarget . closest ( '.ds-dropdown-menu' ) ) ) {
22
+ if (
23
+ relatedTarget &&
24
+ ( relatedTarget . closest ( '.algolia-autocomplete' ) ||
25
+ relatedTarget . closest ( '.ds-dropdown-menu' ) )
26
+ ) {
24
27
return ;
25
28
}
26
-
29
+
27
30
contentWrapper . style . opacity = '1' ;
28
31
contentWrapper . style . transition = 'opacity 200ms' ;
29
-
32
+
30
33
// Hide dropdown if it exists
31
34
if ( dropdownMenu ) {
32
35
dropdownMenu . style . display = 'none' ;
33
36
}
34
37
}
35
-
38
+
36
39
// Add event listeners
37
40
searchInput . addEventListener ( 'focus' , handleFocus ) ;
38
41
searchInput . addEventListener ( 'blur' , handleBlur ) ;
39
-
42
+
40
43
// Use MutationObserver to detect when dropdown is added to the DOM
41
44
observer = new MutationObserver ( ( mutations ) => {
42
45
for ( const mutation of mutations ) {
43
46
if ( mutation . type === 'childList' ) {
44
- const newDropdown = document . querySelector ( '.ds-dropdown-menu:not([data-monitored])' ) ;
47
+ const newDropdown = document . querySelector (
48
+ '.ds-dropdown-menu:not([data-monitored])'
49
+ ) ;
45
50
if ( newDropdown ) {
46
- console . log ( 'DocSearch dropdown detected' ) ;
47
-
48
51
// Save reference to dropdown
49
52
dropdownMenu = newDropdown ;
50
53
newDropdown . setAttribute ( 'data-monitored' , 'true' ) ;
51
-
54
+
52
55
// Monitor dropdown removal/display changes
53
56
dropdownObserver = new MutationObserver ( ( dropdownMutations ) => {
54
57
for ( const dropdownMutation of dropdownMutations ) {
55
- if ( dropdownMutation . type === 'attributes' &&
56
- dropdownMutation . attributeName === 'style' ) {
57
- console . log ( 'Dropdown style changed:' , dropdownMenu . style . display ) ;
58
+ if ( debug ) {
59
+ if (
60
+ dropdownMutation . type === 'attributes' &&
61
+ dropdownMutation . attributeName === 'style'
62
+ ) {
63
+ console . log (
64
+ 'Dropdown style changed:' ,
65
+ dropdownMenu . style . display
66
+ ) ;
67
+ }
58
68
}
59
69
}
60
70
} ) ;
61
-
71
+
62
72
// Observe changes to dropdown attributes (like style)
63
- dropdownObserver . observe ( dropdownMenu , {
73
+ dropdownObserver . observe ( dropdownMenu , {
64
74
attributes : true ,
65
- attributeFilter : [ 'style' ]
75
+ attributeFilter : [ 'style' ] ,
66
76
} ) ;
67
-
77
+
68
78
// Add event listeners to keep dropdown open when interacted with
69
79
dropdownMenu . addEventListener ( 'mousedown' , ( e ) => {
70
80
// Prevent blur on searchInput when clicking in dropdown
@@ -74,24 +84,24 @@ export default function SearchInteractions({ searchInput }) {
74
84
}
75
85
}
76
86
} ) ;
77
-
87
+
78
88
// Start observing the document body for dropdown creation
79
- observer . observe ( document . body , {
80
- childList : true ,
81
- subtree : true
89
+ observer . observe ( document . body , {
90
+ childList : true ,
91
+ subtree : true ,
82
92
} ) ;
83
-
93
+
84
94
// Return cleanup function
85
95
return function cleanup ( ) {
86
96
searchInput . removeEventListener ( 'focus' , handleFocus ) ;
87
97
searchInput . removeEventListener ( 'blur' , handleBlur ) ;
88
-
98
+
89
99
if ( observer ) {
90
100
observer . disconnect ( ) ;
91
101
}
92
-
102
+
93
103
if ( dropdownObserver ) {
94
104
dropdownObserver . disconnect ( ) ;
95
105
}
96
106
} ;
97
- }
107
+ }
0 commit comments