Files
ThreadNet-Web/packages/element-web-module-api/src/api/custom-components.ts
T

74 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-05-19 09:16:27 +01:00
import type { JSX } from "react";
2025-05-20 12:11:07 +01:00
import type { MatrixEvent } from "matrix-js-sdk";
2025-05-16 15:49:07 +01:00
/**
2025-05-19 09:16:27 +01:00
* Targets in Element for custom components.
2025-05-16 15:49:07 +01:00
* @public
*/
export enum CustomComponentTarget {
/**
2025-05-19 09:16:27 +01:00
* Component that renders "m.room.message" events in the room timeline.
2025-05-16 15:49:07 +01:00
*/
TextualBody = "TextualBody",
}
/**
* Properties for the render component.
* @public
*/
export type CustomComponentProps = {
[CustomComponentTarget.TextualBody]: {
/**
2025-05-19 09:16:27 +01:00
* The Matrix event for this textual body.
2025-05-16 15:49:07 +01:00
*/
2025-05-20 12:11:07 +01:00
mxEvent: MatrixEvent;
2025-05-16 15:49:07 +01:00
/**
2025-05-19 11:56:53 +01:00
* Words to highlight on (e.g. from search results).
2025-05-19 09:16:27 +01:00
* May be undefined if the client does not need to highlight
2025-05-16 15:49:07 +01:00
*/
highlights?: string[];
/**
* Should previews be shown for this event
*/
showUrlPreview?: boolean;
/**
* Is this event being rendered to a static export
*/
forExport?: boolean;
};
};
/**
* Render function. Returning null skips this function and passes it onto the next registered renderer.
* @public
*/
export type CustomComponentRenderFunction<T extends CustomComponentTarget> = (
2025-05-19 09:16:27 +01:00
/**
* Properties from the given target to be used for rendering.
*/
2025-05-16 15:49:07 +01:00
props: CustomComponentProps[T],
2025-05-19 09:16:27 +01:00
/**
2025-05-20 12:11:07 +01:00
* Render function for the original component.
2025-05-19 09:16:27 +01:00
*/
2025-05-20 12:11:07 +01:00
originalComponent: () => JSX.Element,
2025-05-16 15:49:07 +01:00
) => JSX.Element | null;
/**
2025-05-19 09:16:27 +01:00
* API for inserting custom components into Element.
2025-05-16 15:49:07 +01:00
* @public
*/
export interface CustomComponentsApi {
/**
* Register a renderer for a component type.
2025-05-19 09:16:27 +01:00
* The render function should either return a rendered component, or `null` if the
* component should not be overidden.
*
* Multiple render function may be registered for a single target, however the first
* non-null result will be used. If all results are null, or no registrations exist
* for a target then the original component is used.
*
* @param target - The target location for the component.
2025-05-16 15:49:07 +01:00
* @param renderer - The render method.
*/
register<T extends CustomComponentTarget>(target: T, renderer: CustomComponentRenderFunction<T>): void;
}