@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
68 lines (59 loc) • 2.58 kB
JavaScript
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import { getExtensionDependencyFromEditor } from '@lexical/extension/getExtensionDependencyFromEditor';
import { getPeerDependencyFromEditor } from '@lexical/extension/getPeerDependencyFromEditor';
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
/**
* Returns the resolved {@link LexicalExtensionDependency} for `extension` from
* the current editor (read from the composer context), giving access to its
* config and output. The editor must include this extension.
*
* @returns The extension's dependency record.
*/
function useExtensionDependency(extension) {
return getExtensionDependencyFromEditor(useLexicalComposerContext()[0], extension);
}
/**
* Like {@link useExtensionDependency}, but returns `undefined` instead of
* throwing when `extension` is not present on the current editor. Useful for
* optionally integrating with an extension that may or may not be configured.
*
* @returns The extension's dependency record, or `undefined` if absent.
*/
function useOptionalExtensionDependency(extension) {
return usePeerExtensionDependency(extension.name);
}
/**
* Returns the {@link LexicalExtensionDependency} for a peer extension looked up
* by `extensionName`, or `undefined` if no extension with that name is present
* on the current editor. Use this when you only have the peer extension's name
* (for example to avoid a hard dependency on its module).
*
* @returns The peer extension's dependency record, or `undefined` if absent.
*/
function usePeerExtensionDependency(extensionName) {
return getPeerDependencyFromEditor(useLexicalComposerContext()[0], extensionName);
}
/**
* Use a Component from the given Extension that uses the ReactExtension convention
* of exposing a Component property in its output.
*
* @param extension - An extension with a Component property in the output
* @returns `getExtensionConfigFromEditor(useLexicalComposerContext()[0], extension).Component`
*/
function useExtensionComponent(extension) {
return useExtensionDependency(extension).output.Component;
}
export { useExtensionComponent, useExtensionDependency, useOptionalExtensionDependency, usePeerExtensionDependency };