summaryrefslogtreecommitdiffstatshomepage
path: root/src/htmx.d.ts
blob: d3c3bba499c48d77ee6356f3373fd397ac85dc48 (plain) (blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// https://htmx.org/reference/#api

/**
 * This method adds a class to the given element.
 *
 * https://htmx.org/api/#addClass
 *
 * @param elt the element to add the class to
 * @param clazz the class to add
 * @param delay the delay (in milliseconds before class is added)
 */
export function addClass(elt: Element, clazz: string, delay?: number): void;

/**
 * Issues an htmx-style AJAX request
 *
 * https://htmx.org/api/#ajax
 *
 * @param verb 'GET', 'POST', etc.
 * @param path the URL path to make the AJAX
 * @param element the element to target (defaults to the **body**)
 * @returns Promise that resolves immediately if no request is sent, or when the request is complete
 */
export function ajax(verb: string, path: string, element?: Element): Promise<void>;

/**
 * Issues an htmx-style AJAX request
 *
 * https://htmx.org/api/#ajax
 *
 * @param verb 'GET', 'POST', etc.
 * @param path the URL path to make the AJAX
 * @param selector a selector for the target
 * @returns Promise that resolves immediately if no request is sent, or when the request is complete
 */
export function ajax(verb: string, path: string, selector: string): Promise<void>;

/**
 * Issues an htmx-style AJAX request
 *
 * https://htmx.org/api/#ajax
 *
 * @param verb 'GET', 'POST', etc.
 * @param path the URL path to make the AJAX
 * @param context a context object that contains any of the following
 * @returns Promise that resolves immediately if no request is sent, or when the request is complete
 */
export function ajax(
    verb: string,
    path: string,
    context: Partial<{ source: any; event: any; handler: any; target: any; swap: any; values: any; headers: any; select: any }>
): Promise<void>;

/**
 * Finds the closest matching element in the given elements parentage, inclusive of the element
 *
 * https://htmx.org/api/#closest
 *
 * @param elt the element to find the selector from
 * @param selector the selector to find
 */
export function closest(elt: Element, selector: string): Element | null;

/**
 * A property holding the configuration htmx uses at runtime.
 *
 * Note that using a [meta tag](https://htmx.org/docs/#config) is the preferred mechanism for setting these properties.
 *
 * https://htmx.org/api/#config
 */
export var config: HtmxConfig;

/**
 * A property used to create new [Server Sent Event](https://htmx.org/docs/#sse) sources. This can be updated to provide custom SSE setup.
 *
 * https://htmx.org/api/#createEventSource
 */
export var createEventSource: (url: string) => EventSource;

/**
 * A property used to create new [WebSocket](https://htmx.org/docs/#websockets). This can be updated to provide custom WebSocket setup.
 *
 * https://htmx.org/api/#createWebSocket
 */
export var createWebSocket: (url: string) => WebSocket;

/**
 * Defines a new htmx [extension](https://htmx.org/extensions).
 *
 * https://htmx.org/api/#defineExtension
 *
 * @param name the extension name
 * @param ext the extension definition
 */
export function defineExtension(name: string, ext: HtmxExtension): void;

/**
 * Finds an element matching the selector
 *
 * https://htmx.org/api/#find
 *
 * @param selector the selector to match
 */
export function find(selector: string): Element | null;

/**
 * Finds an element matching the selector
 *
 * https://htmx.org/api/#find
 *
 * @param elt the root element to find the matching element in, inclusive
 * @param selector the selector to match
 */
export function find(elt: Element, selector: string): Element | null;

/**
 * Finds all elements matching the selector
 *
 * https://htmx.org/api/#findAll
 *
 * @param selector the selector to match
 */
export function findAll(selector: string): NodeListOf<Element>;

/**
 * Finds all elements matching the selector
 *
 * https://htmx.org/api/#findAll
 *
 * @param elt the root element to find the matching elements in, inclusive
 * @param selector the selector to match
 */
export function findAll(elt: Element, selector: string): NodeListOf<Element>;

/**
 * Log all htmx events, useful for debugging.
 *
 * https://htmx.org/api/#logAll
 */
export function logAll(): void;

/**
 * The logger htmx uses to log with
 *
 * https://htmx.org/api/#logger
 */
export var logger: (elt: Element, eventName: string, detail: any) => void | null;

/**
 * Removes an event listener from an element
 *
 * https://htmx.org/api/#off
 *
 * @param eventName the event name to remove the listener from
 * @param listener the listener to remove
 */
export function off(eventName: string, listener: (evt: Event) => void): (evt: Event) => void;

/**
 * Removes an event listener from an element
 *
 * https://htmx.org/api/#off
 *
 * @param target the element to remove the listener from
 * @param eventName the event name to remove the listener from
 * @param listener the listener to remove
 */
export function off(target: string, eventName: string, listener: (evt: Event) => void): (evt: Event) => void;

/**
 * Adds an event listener to an element
 *
 * https://htmx.org/api/#on
 *
 * @param eventName the event name to add the listener for
 * @param listener the listener to add
 */
export function on(eventName: string, listener: (evt: Event) => void): (evt: Event) => void;

/**
 * Adds an event listener to an element
 *
 * https://htmx.org/api/#on
 *
 * @param target the element to add the listener to
 * @param eventName the event name to add the listener for
 * @param listener the listener to add
 */
export function on(target: string, eventName: string, listener: (evt: Event) => void): (evt: Event) => void;

/**
 * Adds a callback for the **htmx:load** event. This can be used to process new content, for example initializing the content with a javascript library
 *
 * https://htmx.org/api/#onLoad
 *
 * @param callback the callback to call on newly loaded content
 */
export function onLoad(callback: (element: Element) => void): void;

/**
 * Parses an interval string consistent with the way htmx does. Useful for plugins that have timing-related attributes.
 *
 * Caution: Accepts an int followed by either **s** or **ms**. All other values use **parseFloat**
 *
 * https://htmx.org/api/#parseInterval
 *
 * @param str timing string
 */
export function parseInterval(str: string): number;

/**
 * Processes new content, enabling htmx behavior. This can be useful if you have content that is added to the DOM outside of the normal htmx request cycle but still want htmx attributes to work.
 *
 * https://htmx.org/api/#process
 *
 * @param element element to process
 */
export function process(element: Element): void;

/**
 * Removes an element from the DOM
 *
 * https://htmx.org/api/#remove
 *
 * @param elt element to remove
 * @param delay the delay (in milliseconds before element is removed)
 */
export function remove(elt: Element, delay?: number): void;

/**
 * Removes a class from the given element
 *
 * https://htmx.org/api/#removeClass
 *
 * @param elt element to remove the class from
 * @param clazz the class to remove
 * @param delay the delay (in milliseconds before class is removed)
 */
export function removeClass(elt: Element, clazz: string, delay?: number): void;

/**
 * Removes the given extension from htmx
 *
 * https://htmx.org/api/#removeExtension
 *
 * @param name the name of the extension to remove
 */
export function removeExtension(name: string): void;

/**
 * Takes the given class from its siblings, so that among its siblings, only the given element will have the class.
 *
 * https://htmx.org/api/#takeClass
 *
 * @param elt the element that will take the class
 * @param clazz the class to take
 */
export function takeClass(elt: Element, clazz: string): void;

/**
 * Toggles the given class on an element
 *
 * https://htmx.org/api/#toggleClass
 *
 * @param elt the element to toggle the class on
 * @param clazz the class to toggle
 */
export function toggleClass(elt: Element, clazz: string): void;

/**
 * Triggers a given event on an element
 *
 * https://htmx.org/api/#trigger
 *
 * @param elt the element to trigger the event on
 * @param name the name of the event to trigger
 * @param detail details for the event
 */
export function trigger(elt: Element, name: string, detail: any): void;

/**
 * Returns the input values that would resolve for a given element via the htmx value resolution mechanism
 *
 * https://htmx.org/api/#values
 *
 * @param elt the element to resolve values on
 * @param requestType the request type (e.g. **get** or **post**) non-GET's will include the enclosing form of the element. Defaults to **post**
 */
export function values(elt: Element, requestType?: string): any;

export const version: string;

export interface HtmxConfig {
    /**
     * The attributes to settle during the settling phase.
     * @default ["class", "style", "width", "height"]
     */
    attributesToSettle?: ["class", "style", "width", "height"] | string[];
    /**
     * If the focused element should be scrolled into view.
     * @default false
    */
    defaultFocusScroll?: boolean;
    /**
     * The default delay between completing the content swap and settling attributes.
     * @default 20
     */
    defaultSettleDelay?: number;
    /**
     * The default delay between receiving a response from the server and doing the swap.
     * @default 0
     */
    defaultSwapDelay?: number;
    /**
     * The default swap style to use if **[hx-swap](https://htmx.org/attributes/hx-swap)** is omitted.
     * @default "innerHTML"
     */
    defaultSwapStyle?: "innerHTML" | string;
    /**
     * The number of pages to keep in **localStorage** for history support.
     * @default 10
     */
    historyCacheSize?: number;
    /**
     * Whether or not to use history.
     * @default true
     */
    historyEnabled?: boolean;
    /**
     * If true, htmx will inject a small amount of CSS into the page to make indicators invisible unless the **htmx-indicator** class is present.
     * @default true
     */
    includeIndicatorStyles?: boolean;
    /**
     * The class to place on indicators when a request is in flight.
     * @default "htmx-indicator"
     */
    indicatorClass?: "htmx-indicator" | string;
    /**
     * The class to place on triggering elements when a request is in flight.
     * @default "htmx-request"
     */
    requestClass?: "htmx-request" | string;
    /**
     * The class to temporarily place on elements that htmx has added to the DOM.
     * @default "htmx-added"
     */
    addedClass?: "htmx-added" | string;
    /**
     * The class to place on target elements when htmx is in the settling phase.
     * @default "htmx-settling"
     */
    settlingClass?: "htmx-settling" | string;
    /**
     * The class to place on target elements when htmx is in the swapping phase.
     * @default "htmx-swapping"
     */
    swappingClass?: "htmx-swapping" | string;
    /**
     * Allows the use of eval-like functionality in htmx, to enable **hx-vars**, trigger conditions & script tag evaluation. Can be set to **false** for CSP compatibility.
     * @default true
     */
    allowEval?: boolean;
    /**
     * Use HTML template tags for parsing content from the server. This allows you to use Out of Band content when returning things like table rows, but it is *not* IE11 compatible.
     * @default false
     */
    useTemplateFragments?: boolean;
    /**
     * Allow cross-site Access-Control requests using credentials such as cookies, authorization headers or TLS client certificates.
     * @default false
     */
    withCredentials?: boolean;
    /**
     * The default implementation of **getWebSocketReconnectDelay** for reconnecting after unexpected connection loss by the event code **Abnormal Closure**, **Service Restart** or **Try Again Later**.
     * @default "full-jitter"
     */
    wsReconnectDelay?: "full-jitter" | string | ((retryCount: number) => number);
    // following don't appear in the docs
    /** @default false */
    refreshOnHistoryMiss?: boolean;
    /** @default 0 */
    timeout?: number;
    /** @default "[hx-disable], [data-hx-disable]" */
    disableSelector?: "[hx-disable], [data-hx-disable]" | string;
    /** @default "smooth" */
    scrollBehavior?: "smooth" | "auto";
    /**
     * If set to false, disables the interpretation of script tags.
     * @default true
     */
    allowScriptTags?: boolean;
    /**
     * If set to true, disables htmx-based requests to non-origin hosts.
     * @default false
     */
    selfRequestsOnly?: boolean;
    /**
     * Whether or not the target of a boosted element is scrolled into the viewport.
     * @default true
     */
    scrollIntoViewOnBoost?: boolean;
    /**
     * If set, the nonce will be added to inline scripts.
     * @default ''
     */
    inlineScriptNonce?: string;
    /**
     * The type of binary data being received over the WebSocket connection
     * @default 'blob'
     */
    wsBinaryType?: 'blob' | 'arraybuffer'; 
    /**
     * If set to true htmx will include a cache-busting parameter in GET requests to avoid caching partial responses by the browser
     * @default false 
     */
    getCacheBusterParam?: boolean;
    /**
     * If set to true, htmx will use the View Transition API when swapping in new content.
     * @default false 
     */
    globalViewTransitions?: boolean;
    /**
     * htmx will format requests with these methods by encoding their parameters in the URL, not the request body
     * @default ["get"] 
     */
    methodsThatUseUrlParams?: ('get' | 'head' | 'post' | 'put' | 'delete' | 'connect' | 'options' | 'trace' | 'patch' )[];
    /**
     * If set to true htmx will not update the title of the document when a title tag is found in new content
     * @default false 
     */
    ignoreTitle:? boolean;
    /**
     * The cache to store evaluated trigger specifications into.
     * You may define a simple object to use a never-clearing cache, or implement your own system using a [proxy object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Proxy)
     * @default null
     */
    triggerSpecsCache?: {[trigger: string]: HtmxTriggerSpecification[]};
}

export type HtmxEvent = "htmx:abort"
    | "htmx:afterOnLoad"
    | "htmx:afterProcessNode"
    | "htmx:afterRequest"
    | "htmx:afterSettle"
    | "htmx:afterSwap"
    | "htmx:beforeCleanupElement"
    | "htmx:beforeOnLoad"
    | "htmx:beforeProcessNode"
    | "htmx:beforeRequest"
    | "htmx:beforeSwap"
    | "htmx:beforeSend"
    | "htmx:configRequest"
    | "htmx:confirm"
    | "htmx:historyCacheError"
    | "htmx:historyCacheMiss"
    | "htmx:historyCacheMissError"
    | "htmx:historyCacheMissLoad"
    | "htmx:historyRestore"
    | "htmx:load"
    | "htmx:noSSESourceError"
    | "htmx:onLoadError"
    | "htmx:oobAfterSwap"
    | "htmx:oobBeforeSwap"
    | "htmx:oobErrorNoTarget"
    | "htmx:prompt"
    | "htmx:pushedIntoHistory"
    | "htmx:responseError"
    | "htmx:sendError"
    | "htmx:sseError"
    | "htmx:sseOpen"
    | "htmx:swapError"
    | "htmx:targetError"
    | "htmx:timeout"
    | "htmx:validation:validate"
    | "htmx:validation:failed"
    | "htmx:validation:halted"
    | "htmx:xhr:abort"
    | "htmx:xhr:loadend"
    | "htmx:xhr:loadstart"
    | "htmx:xhr:progress"
;

/**
 * https://htmx.org/extensions/#defining
 */
export interface HtmxExtension {
    onEvent?: (name: HtmxEvent, evt: CustomEvent) => any;
    transformResponse?: (text: any, xhr: XMLHttpRequest, elt: any) => any;
    isInlineSwap?: (swapStyle: any) => any;
    handleSwap?: (swapStyle: any, target: any, fragment: any, settleInfo: any) => any;
    encodeParameters?: (xhr: XMLHttpRequest, parameters: any, elt: any) => any;
}