Skip to content
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

Add mechanism to extend the JSX namespace for custom elements (and attributes) #2807

Closed
wants to merge 4 commits into from
Closed
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
48 changes: 48 additions & 0 deletions site/development/themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,51 @@ export function load(app: Application) {
```

[RendererHooks]: https://typedoc.org/api/interfaces/RendererHooks.html

## Registering your own custom elements/attributes

Start by writing a `jsx.d.ts` file somewhere.

````ts
// src/jsx.d.ts
import { JSX as TypeDocJSX } from "typedoc";

declare module "typedoc" {
namespace JSX {
namespace JSX {
Comment on lines +116 to +117
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a typo but the result of a test

interface IntrinsicAttributes {
popover?: boolean;
popovertarget?: string;
popovertargetaction?: "hide" | "show" | "toggle";
}
interface IntrinsicElements {
// add your custom elements here, ie:
/**
* @example
* ```tsx
* <drop-down trigger="#my-trigger" class="header-menu">
* <button>Option #1</button>
* <button>Option #2</button>
* </drop-down>
* ```
*/
"drop-down": IntrinsicAttributes & {
/**
* A query selector, ie: '#my-trigger'
*/
trigger: string;
};
}
}
}
}
````

Then in your plugin entry point, reference the `jsx.d.ts` file with a triple-slash directive.

```ts
// src/index.ts
/// <reference types="./jsx.d.ts" />

export function load(app: Application) { … }
```
13 changes: 13 additions & 0 deletions src/lib/utils/jsx.elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ export interface JsxHtmlGlobalProps {
tabIndex?: number;
title?: string;
translate?: boolean;

// popover attributes
/**
* Default: 'auto'. true and 'auto' are equivalent
*
* See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Popover_API) for more details
*/
popover?: boolean | "auto" | "manual";
/**
* It must be the popover element id, see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Popover_API)
*/
popovertarget?: string;
popovertargetaction?: "hide" | "show" | "toggle";
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/lib/utils/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
JsxElement,
JsxChildren,
JsxComponent,
JsxHtmlGlobalProps,
} from "./jsx.elements.js";
import { JsxFragment } from "./jsx.elements.js";

Expand Down Expand Up @@ -50,7 +51,11 @@ export function Raw(_props: { html: string }) {
* @hidden
*/
export declare namespace JSX {
export { IntrinsicElements, JsxElement as Element };
export {
IntrinsicElements,
JsxElement as Element,
JsxHtmlGlobalProps as IntrinsicAttributes,
};
}

const voidElements = new Set([
Expand Down
Loading