-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
introduce types.ts; improve types for internal functions with focus on hx-swap #2107
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/** | ||
* @fileoverview Internal types used inside htmx.js but are not part of | ||
* the public API. The main benefit of having this file is that you can | ||
* have better DX defining types in TypeScript and then just import them | ||
* into the JsDoc comments in the htmx.js file. */ | ||
|
||
interface HtmxTriggerSpecification { | ||
sseEvent?: string; | ||
trigger: string; | ||
root?: Element | Document | null; | ||
threshold?: string; | ||
delay?: number; | ||
pollInterval?: number; | ||
} | ||
|
||
export interface HtmxSwapSpecification { | ||
swapStyle: SwapStyle; | ||
swapDelay: number; | ||
settleDelay: number; | ||
|
||
scroll?: "top" | "bottom"; | ||
scrollTarget?: string | null; | ||
|
||
show?: "top" | "bottom"; | ||
showTarget?: string | null; | ||
|
||
transition?: boolean; | ||
ignoreTitle?: boolean; | ||
focusScroll?: boolean; | ||
} | ||
|
||
interface HtmxSettleInfo { | ||
title?: string; | ||
tasks?: (() => void)[]; | ||
elts?: Element[]; | ||
} | ||
|
||
interface ListenerInfo { | ||
listener: EventListener; | ||
on: HTMLElement; | ||
trigger: string; | ||
} | ||
|
||
/** the http verb used in the request (lowercase) */ | ||
type Verb = "get" | "post" | "put" | "delete" | "patch"; | ||
|
||
interface KnownInternalData { | ||
initHash?: number | null; | ||
listenerInfos?: ListenerInfo[]; | ||
path?: string; | ||
streamPaused?: boolean; | ||
streamReader?: ReadableStreamDefaultReader; | ||
verb?: Verb; | ||
lastButtonClicked?: Element | null; | ||
timeout?: number; | ||
webSocket?: WebSocket; | ||
sseEventSource?: EventSource; | ||
onHandlers?: { event: string; listener: EventListener }[]; | ||
xhr?: XMLHttpRequest; | ||
requestCount?: number; | ||
} | ||
|
||
type InternalData = KnownInternalData & Record<PropertyKey, any>; | ||
|
||
interface InputValues { | ||
errors: any[]; | ||
values: Record<string, string>; | ||
} | ||
|
||
interface Pollable { | ||
polling: boolean; | ||
} | ||
|
||
interface TriggerHandler { | ||
(elt: Element, evt: Event): void; | ||
(): void; | ||
} | ||
|
||
export type SwapStyle = | ||
| "innerHTML" | ||
| "outerHTML" | ||
| "beforebegin" | ||
| "afterbegin" | ||
| "beforeend" | ||
| "afterend" | ||
| "delete" | ||
| "none"; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should probably add export type SwapStyle =
| "innerHTML"
| "outerHTML"
| "afterbegin"
| "beforebegin"
| "afterend"
| "beforeend"
| "none"
| "delete"
| (string & NonNullable<unknown>); because there are other swap styles (like this way, editor autocomplete will still be there for known values, but any string can be passed. |
||
export interface HtmxInternalApi { | ||
addTriggerHandler( | ||
elt: Element, | ||
triggerSpec: HtmxTriggerSpecification, | ||
nodeData: Pollable, | ||
handler: TriggerHandler | ||
): void; | ||
bodyContains(elt: Node): boolean; | ||
canAccessLocalStorage(): boolean; | ||
findThisElement(elt: HTMLElement, attribute: string): HTMLElement | null; | ||
filterValues( | ||
inputValues: Record<string, string>, | ||
elt: HTMLElement | ||
): Record<string, string>; | ||
hasAttribute( | ||
elt: { hasAttribute: (arg0: string) => boolean }, | ||
qualifiedName: string | ||
): boolean; | ||
getAttributeValue(elt: HTMLElement, qualifiedName: string): string | null; | ||
getClosestAttributeValue( | ||
elt: HTMLElement, | ||
attributeName: string | ||
): string | null; | ||
getClosestMatch( | ||
elt: HTMLElement, | ||
condition: (e: HTMLElement) => boolean | ||
): HTMLElement | null; | ||
getExpressionVars(elt: HTMLElement): Record<string, string>; | ||
getHeaders( | ||
elt: HTMLElement, | ||
target: HTMLElement, | ||
prompt: string | ||
): Record<string, string>; | ||
getInputValues(elt: HTMLElement, verb: string): InputValues; | ||
getInternalData(elt: HTMLElement): InternalData; | ||
getSwapSpecification( | ||
elt: HTMLElement, | ||
swapInfoOverride?: string | ||
): HtmxSwapSpecification; | ||
getTriggerSpecs(elt: HTMLElement): HtmxTriggerSpecification[]; | ||
getTarget(elt: HTMLElement): Element | null; | ||
makeFragment(resp: any): Element | DocumentFragment; | ||
mergeObjects<A extends object, B extends object>(obj1: A, obj2: B): A & B; | ||
makeSettleInfo(target: Element): HtmxSettleInfo; | ||
oobSwap( | ||
oobValue: string, | ||
oobElement: HTMLElement, | ||
settleInfo: HtmxSettleInfo | ||
): string; | ||
querySelectorExt(eltOrSelector: any, selector: string): Element | null; | ||
selectAndSwap( | ||
swapStyle: SwapStyle, | ||
target: Element | null, | ||
elt: Element | null, | ||
responseText: string, | ||
settleInfo: HtmxSettleInfo, | ||
selectOverride?: string | null | ||
): void; | ||
settleImmediately(tasks: { call: () => void }[]): void; | ||
shouldCancel(evt: Event, elt: HTMLElement): boolean; | ||
triggerEvent( | ||
elt: Element | null, | ||
eventName: string, | ||
detail?: EventDetail | ||
): boolean; | ||
triggerErrorEvent( | ||
elt: HTMLElement, | ||
eventName: string, | ||
detail?: EventDetail | ||
): void; | ||
withExtensions( | ||
elt: HTMLElement, | ||
toDo: (extension: import("./htmx").HtmxExtension) => void | ||
): void; | ||
} | ||
|
||
type EventDetail = { | ||
[key: PropertyKey]: unknown; | ||
}; | ||
|
||
export declare function updateScrollState( | ||
content, | ||
swapSpec: HtmxSwapSpecification | ||
): void; | ||
|
||
export declare function splitOnWhitespace(trigger: string): string[]; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looked through and updated this with keys found in source: