@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
96 lines (85 loc) • 3.06 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 { createContext, useContext } from 'react';
/**
* 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.
*
*/
// Do not require this module directly! Use normal `invariant` calls.
function formatDevErrorMessage(message) {
throw new Error(message);
}
/**
* The context value provided alongside a {@link LexicalEditor} by a
* {@link LexicalComposer}. It exposes a `getTheme()` function that resolves the
* active {@link EditorThemeClasses}, falling back to any parent composer's
* theme.
*/
/**
* A tuple of the {@link LexicalEditor} and its
* {@link LexicalComposerContextType}, as stored in {@link LexicalComposerContext}
* and returned by {@link useLexicalComposerContext}.
*/
/**
* The React context used to share the {@link LexicalEditor} and its
* {@link LexicalComposerContextType} with descendant plugins and components.
* Most code should read it through {@link useLexicalComposerContext} rather than
* consuming the context directly.
*/
const LexicalComposerContext =
// Annotated by hand: React's createContext is not a Lexical factory, so the
// build does not annotate it, and an unannotated module-scope call pins the
// module into every bundle.
/* @__PURE__ */
createContext(null);
/**
* Creates a {@link LexicalComposerContextType} for a composer. Theme resolution
* falls back to the optional `parent` context, so nested composers inherit the
* parent's theme unless they provide their own.
*
* @param parent - The parent composer context to inherit from, if any.
* @param theme - The theme classes for this composer, or `null`/`undefined` to
* inherit from `parent`.
* @returns The new composer context value.
*/
function createLexicalComposerContext(parent, theme) {
let parentContext = null;
if (parent != null) {
parentContext = parent[1];
}
function getTheme() {
if (theme != null) {
return theme;
}
return parentContext != null ? parentContext.getTheme() : null;
}
return {
getTheme
};
}
/**
* Returns the {@link LexicalEditor} and its {@link LexicalComposerContextType}
* from the nearest {@link LexicalComposer} (or nested composer). This is the
* primary way plugins and components access the editor instance.
*
* @returns The `[editor, context]` tuple for the current composer.
* @throws If called outside of a LexicalComposer.
*/
function useLexicalComposerContext() {
const composerContext = useContext(LexicalComposerContext);
if (composerContext == null) {
{
formatDevErrorMessage(`LexicalComposerContext.useLexicalComposerContext: cannot find a LexicalComposerContext`);
}
}
return composerContext;
}
export { LexicalComposerContext, createLexicalComposerContext, useLexicalComposerContext };