Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions types/hotwired__turbo/hotwired__turbo-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
StreamMessage,
StreamSource,
TurboHistory,
TurboStreamAction,
TurboStreamActions,
Visit,
visit,
VisitOptions,
Expand Down Expand Up @@ -102,6 +104,19 @@ StreamActions.log = function() {
console.log(this.getAttribute("message"));
};

// Test TurboStreamActions / TurboStreamAction exports
// $ExpectType TurboStreamActions
StreamActions;

const customStreamAction: TurboStreamAction = function() {
// $ExpectType StreamElement
this;
};
StreamActions.custom = customStreamAction;

const allStreamActions: TurboStreamActions = StreamActions;
allStreamActions.log;

document.addEventListener("turbo:before-fetch-request", function(event) {
// $ExpectType FetchRequestHeaders
const headers = event.detail.fetchOptions.headers;
Expand Down Expand Up @@ -272,6 +287,12 @@ turboStream.templateElement = document.createElement("template");
// @ts-expect-error - templateContent is readonly
turboStream.templateContent = document.createDocumentFragment();

// Test StreamElement.targetElements
// $ExpectType Element[]
turboStream.targetElements;
// @ts-expect-error - targetElements is readonly
turboStream.targetElements = [];

const eventSource = new EventSource("https://example.com/stream");
const webSocket = new WebSocket("wss://example.com/stream");

Expand Down
27 changes: 21 additions & 6 deletions types/hotwired__turbo/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export class StreamElement extends HTMLElement {
* Gets a cloned copy of the template's content.
*/
readonly templateContent: DocumentFragment;

/**
* Gets the list of elements the stream action will be applied to,
* resolved from the `target` (an element ID) or `targets` (a CSS
* selector) attribute.
*/
readonly targetElements: Element[];
}

export class StreamSourceElement extends HTMLElement {
Expand Down Expand Up @@ -299,9 +306,19 @@ export interface TurboSession {
readonly restorationIdentifier: string;
}

export const StreamActions: {
[action: string]: (this: StreamElement) => void;
};
/**
* A stream action callback. Invoked with the matched `StreamElement` as
* `this`, allowing access to its attributes and target elements.
*/
export type TurboStreamAction = (this: StreamElement) => void;

/**
* A map of action names to their {@link TurboStreamAction} callbacks, as
* used by {@link StreamActions}.
*/
export type TurboStreamActions = Record<string, TurboStreamAction>;

export const StreamActions: TurboStreamActions;

export type Action = "advance" | "replace" | "restore";
export interface VisitOptions {
Expand Down Expand Up @@ -457,9 +474,7 @@ export interface TurboGlobal {
navigator: Navigator;
cache: Cache;
config: TurboConfig;
StreamActions: {
[action: string]: (this: StreamElement) => void;
};
StreamActions: TurboStreamActions;
}

declare global {
Expand Down