1
0
Fork 0

fix(docsearch-js): provide unmounting function (#2602)

This commit is contained in:
Xavier Ruiz 2025-08-27 04:10:04 -04:00 committed by GitHub
parent e2fe65e739
commit ba1573f2a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,6 @@
import type { DocSearchProps as DocSearchComponentProps } from '@docsearch/react';
import { DocSearch, version } from '@docsearch/react';
import React, { render } from 'preact/compat';
import React, { render, unmountComponentAtNode } from 'preact/compat';
function getHTMLElement(value: HTMLElement | string, environment: DocSearchProps['environment'] = window): HTMLElement {
if (typeof value === 'string') {
@ -15,7 +15,8 @@ interface DocSearchProps extends DocSearchComponentProps {
environment?: typeof window;
}
export function docsearch(props: DocSearchProps): void {
export function docsearch(props: DocSearchProps): () => void {
const containerElement = getHTMLElement(props.container, props.environment);
render(
<DocSearch
{...props}
@ -24,6 +25,9 @@ export function docsearch(props: DocSearchProps): void {
return props.transformSearchClient ? props.transformSearchClient(searchClient) : searchClient;
}}
/>,
getHTMLElement(props.container, props.environment),
containerElement,
);
return () => {
unmountComponentAtNode(containerElement);
};
}