UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

10 lines (8 loc) 839 B
/** * 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. * */ "use strict";var e=require("@lexical/react/LexicalComposerContext"),t=require("react");const i="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement?t.useLayoutEffect:t.useEffect;function n(e){return{initialValueFn:()=>e.isEditable(),subscribe:t=>e.registerEditableListener(t)}}exports.useLexicalEditable=function(){return function(n){const[r]=e.useLexicalComposerContext(),u=t.useMemo((()=>n(r)),[r,n]),[c,o]=t.useState((()=>u.initialValueFn())),s=t.useRef(c);return i((()=>{const{initialValueFn:e,subscribe:t}=u,i=e();return s.current!==i&&(s.current=i,o(i)),t((e=>{s.current=e,o(e)}))}),[u,n]),c}(n)};