@@ -11,7 +11,8 @@ import { constructClientAgents } from './agents'
11
11
* @param {string } options.hostUrl URL where Meilisearch instance is hosted
12
12
* @param {string } options.apiKey Read-only API key
13
13
* @param {string } options.indexUid UID of the index to target
14
- * @param {string } options.inputSelector CSS selector that targets the input
14
+ * @param {string } [options.inputSelector] CSS selector that targets the input
15
+ * @param {Element } [options.inputElement] Input element
15
16
* @param {boolean } [options.debug] When set to true, the dropdown will not be closed on blur
16
17
* @param {Object } [options.meilisearchOptions] Options to pass the underlying Meilisearch client
17
18
* @param {function } [options.queryDataCallback] This function will be called when querying Meilisearch
@@ -29,7 +30,8 @@ const usage = `Usage:
29
30
hostUrl,
30
31
apiKey,
31
32
indexUid,
32
- inputSelector,
33
+ [ inputSelector ],
34
+ [ inputElement ],
33
35
[ debug ],
34
36
[ meilisearchOptions ],
35
37
[ queryDataCallback ],
@@ -46,7 +48,8 @@ class DocsSearchBar {
46
48
hostUrl,
47
49
apiKey,
48
50
indexUid,
49
- inputSelector,
51
+ inputSelector = '' ,
52
+ inputElement = null ,
50
53
debug = false ,
51
54
meilisearchOptions = { } ,
52
55
queryDataCallback = null ,
@@ -64,6 +67,7 @@ class DocsSearchBar {
64
67
apiKey,
65
68
indexUid,
66
69
inputSelector,
70
+ inputElement,
67
71
debug,
68
72
meilisearchOptions,
69
73
queryDataCallback,
@@ -80,7 +84,9 @@ class DocsSearchBar {
80
84
this . apiKey = apiKey
81
85
this . hostUrl = hostUrl
82
86
this . indexUid = indexUid
83
- this . input = DocsSearchBar . getInputFromSelector ( inputSelector )
87
+ this . input = inputElement
88
+ ? $ ( inputElement )
89
+ : DocsSearchBar . getInputFromSelector ( inputSelector )
84
90
this . meilisearchOptions = {
85
91
limit : 5 ,
86
92
attributesToHighlight : [ '*' ] ,
@@ -121,7 +127,11 @@ class DocsSearchBar {
121
127
clientAgents : constructClientAgents ( clientAgents ) ,
122
128
} )
123
129
124
- DocsSearchBar . addThemeWrapper ( inputSelector , this . enableDarkMode )
130
+ DocsSearchBar . addThemeWrapper (
131
+ inputElement ,
132
+ inputSelector ,
133
+ this . enableDarkMode ,
134
+ )
125
135
126
136
if ( enhancedSearchInput ) {
127
137
this . input = DocsSearchBar . injectSearchBox ( this . input )
@@ -176,17 +186,18 @@ class DocsSearchBar {
176
186
/**
177
187
* Wraps input selector in a docs-searchbar-js div
178
188
* @function addThemeWrapper
189
+ * @param {Element } inputElement Input Element
179
190
* @param {string } inputSelector Selector of the input element
180
191
* @param {boolean|'auto' } enableDarkMode Allows you to enforce, light theme, dark theme, or auto mode on the searchbar.
181
192
* @returns {void }
182
193
*/
183
- static addThemeWrapper ( inputSelector , enableDarkMode ) {
184
- const inputElement = document . querySelector ( inputSelector )
185
- const parent = inputElement . parentNode
194
+ static addThemeWrapper ( inputElement , inputSelector , enableDarkMode ) {
195
+ const input = inputElement || document . querySelector ( inputSelector )
196
+ const parent = input . parentNode
186
197
const wrapper = document . createElement ( 'div' )
187
198
wrapper . className += 'docs-searchbar-js'
188
- parent . replaceChild ( wrapper , inputElement )
189
- wrapper . appendChild ( inputElement )
199
+ parent . replaceChild ( wrapper , input )
200
+ wrapper . appendChild ( input )
190
201
191
202
let isSystemInDarkMode = Boolean ( enableDarkMode )
192
203
if ( enableDarkMode === 'auto' && window . matchMedia ) {
@@ -219,17 +230,24 @@ class DocsSearchBar {
219
230
* @returns {void }
220
231
*/
221
232
static checkArguments ( args ) {
222
- if ( ! args . inputSelector || ! args . indexUid || ! args . hostUrl ) {
233
+ if (
234
+ ( ! args . inputSelector && ! args . inputElement ) ||
235
+ ! args . indexUid ||
236
+ ! args . hostUrl
237
+ ) {
223
238
throw new Error ( usage )
224
239
}
225
240
226
- if ( typeof args . inputSelector !== 'string' ) {
241
+ if ( args . inputSelector !== null && typeof args . inputSelector !== 'string' ) {
227
242
throw new Error (
228
243
`Error: inputSelector:${ args . inputSelector } must be a string. Each selector must match only one element and separated by ','` ,
229
244
)
230
245
}
231
246
232
- if ( ! DocsSearchBar . getInputFromSelector ( args . inputSelector ) ) {
247
+ if (
248
+ ! args . inputElement &&
249
+ ! DocsSearchBar . getInputFromSelector ( args . inputSelector )
250
+ ) {
233
251
throw new Error (
234
252
`Error: No input element in the page matches ${ args . inputSelector } ` ,
235
253
)
@@ -342,7 +360,7 @@ class DocsSearchBar {
342
360
* @function getInputFromSelector
343
361
* @param {string } selector CSS selector that matches the search
344
362
* input of the page
345
- * @returns {void }
363
+ * @returns {zepto.Z|null } Matching input or null
346
364
*/
347
365
static getInputFromSelector ( selector ) {
348
366
const input = $ ( selector ) . filter ( 'input' )
0 commit comments