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
26 changes: 25 additions & 1 deletion packages/element-web-module-api/element-web-module-api.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { ComponentType } from 'react';
import { JSX } from 'react';
import { ModuleApi } from '@matrix-org/react-sdk-module-api';
import { ReactNode } from 'react';
import { Root } from 'react-dom/client';
import { RuntimeModule } from '@matrix-org/react-sdk-module-api';

Expand Down Expand Up @@ -109,10 +110,29 @@ export interface ConfigApi {

// @alpha
export interface CustomComponentsApi {
registerLoginComponent(renderer: CustomLoginRenderFunction): void;
registerMessageRenderer(eventTypeOrFilter: string | ((mxEvent: MatrixEvent) => boolean), renderer: CustomMessageRenderFunction, hints?: CustomMessageRenderHints): void;
registerRoomPreviewBar(renderer: CustomRoomPreviewBarRenderFunction): void;
}

// @alpha
export type CustomLoginComponentProps = {
serverConfig: CustomLoginComponentPropsServerConfig;
fragmentAfterLogin?: string;
children?: ReactNode;
onLoggedIn(data: AccountAuthInfo): void;
onServerConfigChange(config: CustomLoginComponentPropsServerConfig): void;
};

// @alpha
export interface CustomLoginComponentPropsServerConfig {
hsName: string;
hsUrl: string;
}

// @alpha
export type CustomLoginRenderFunction = ExtendablePropsRenderFunction<CustomLoginComponentProps>;

// @alpha
export type CustomMessageComponentProps = {
mxEvent: MatrixEvent;
Expand Down Expand Up @@ -171,6 +191,11 @@ export interface DirectoryCustomisations {
requireCanonicalAliasAccessToPublish?(): boolean;
}

// @alpha
export type ExtendablePropsRenderFunction<BaseProps> = <P extends BaseProps>(
props: P,
originalComponent: (props: P) => JSX.Element) => JSX.Element;

// @alpha
export interface ExtrasApi {
getVisibleRoomBySpaceKey(spaceKey: string, cb: () => string[]): void;
Expand Down Expand Up @@ -476,4 +501,3 @@ export interface WidgetVariablesCustomisations {
// (No @packageDocumentation comment for this package)

```

83 changes: 82 additions & 1 deletion packages/element-web-module-api/src/api/custom-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/

import type { JSX } from "react";
import type { JSX, ReactNode } from "react";
import type { MatrixEvent } from "../models/event";
import type { AccountAuthInfo } from "./auth.ts";

/**
* Properties for all message components.
Expand Down Expand Up @@ -91,6 +92,71 @@ export type CustomRoomPreviewBarRenderFunction = (
originalComponent: (props: CustomRoomPreviewBarComponentProps) => JSX.Element,
) => JSX.Element;

/**
* Authentication server config object.
* @alpha Subject to change.
*/
export interface CustomLoginComponentPropsServerConfig {
/**
* The URL of the homeserver's client-server API
*/
hsUrl: string;
/**
* The name of the homeserver to present to the user
*/
hsName: string;
}

/**
* Properties for login component.
* @alpha Subject to change.
*/
export type CustomLoginComponentProps = {
/**
* The details of the currently chosen Matrix homeserver
*/
serverConfig: CustomLoginComponentPropsServerConfig;
/**
* The URL fragment to send the user to after authentication is complete
*/
fragmentAfterLogin?: string;
/**
* Additional components to render as children
*/
children?: ReactNode;
/**
* Function to complete login
* @param data - the data to authenticate the user with
*/
onLoggedIn(data: AccountAuthInfo): void;
/**
* Function to change the selected server
* @param config - new server configuration details
*/
onServerConfigChange(config: CustomLoginComponentPropsServerConfig): void;
};

/**
* Function used to render a component with a superset of the known props.
* @alpha Unlikely to change
*/
export type ExtendablePropsRenderFunction<BaseProps> = <P extends BaseProps>(
/**
* Properties for the component to be rendered.
*/
props: P,
/**
* Render function for the original component.
*/
originalComponent: (props: P) => JSX.Element,
) => JSX.Element;

/**
* Function used to render a login component.
* @alpha Unlikely to change
*/
export type CustomLoginRenderFunction = ExtendablePropsRenderFunction<CustomLoginComponentProps>;

/**
* API for inserting custom components into Element.
* @alpha Subject to change.
Expand Down Expand Up @@ -143,4 +209,19 @@ export interface CustomComponentsApi {
* ```
*/
registerRoomPreviewBar(renderer: CustomRoomPreviewBarRenderFunction): void;

/**
* Register a renderer for the login component.
*
* The render function should return a rendered component.
*
* @param renderer - The render function for the login component.
* @example
* ```
* customComponents.registerLoginComponent((props, OriginalComponent) => {
* return <YourCustomComponent onLoggedIn={props.onLoggedIn} />;
* });
* ```
*/
registerLoginComponent(renderer: CustomLoginRenderFunction): void;
}