@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
10 lines (8 loc) • 2.43 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{useLexicalComposerContext as e}from"@lexical/react/LexicalComposerContext";import{useLayoutEffect as t,useEffect as i,forwardRef as a,useState as r,useCallback as n,useMemo as o}from"react";import{jsx as l,jsxs as d,Fragment as c}from"react/jsx-runtime";import{$canShowPlaceholderCurry as s}from"@lexical/text";import{mergeRegister as u}from"@lexical/utils";const m="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement?t:i;function f({editor:e,ariaActiveDescendant:t,ariaAutoComplete:i,ariaControls:a,ariaDescribedBy:d,ariaExpanded:c,ariaLabel:s,ariaLabelledBy:u,ariaMultiline:f,ariaOwns:b,ariaRequired:p,autoCapitalize:x,className:E,id:v,role:w="textbox",spellCheck:y=!0,style:C,tabIndex:h,"data-testid":L,...g},D){const[R,k]=r(e.isEditable()),q=n((t=>{t&&t.ownerDocument&&t.ownerDocument.defaultView?e.setRootElement(t):e.setRootElement(null)}),[e]),z=o((()=>function(...e){return t=>{e.forEach((e=>{"function"==typeof e?e(t):null!=e&&(e.current=t)}))}}(D,q)),[q,D]);return m((()=>(k(e.isEditable()),e.registerEditableListener((e=>{k(e)})))),[e]),l("div",{...g,"aria-activedescendant":R?t:void 0,"aria-autocomplete":R?i:"none","aria-controls":R?a:void 0,"aria-describedby":d,"aria-expanded":R&&"combobox"===w?!!c:void 0,"aria-label":s,"aria-labelledby":u,"aria-multiline":f,"aria-owns":R?b:void 0,"aria-readonly":!R||void 0,"aria-required":p,autoCapitalize:x,className:E,contentEditable:R,"data-testid":L,id:v,ref:z,role:R?w:void 0,spellCheck:y,style:C,tabIndex:h})}const b=a(f);function p(e){return e.getEditorState().read(s(e.isComposing()))}const x=a(E);function E(t,i){const{placeholder:a,...r}=t,[n]=e();return d(c,{children:[l(b,{editor:n,...r,ref:i}),null!=a&&l(v,{editor:n,content:a})]})}function v({content:e,editor:i}){const a=function(e){const[t,i]=r((()=>p(e)));return m((()=>{function t(){const t=p(e);i(t)}return t(),u(e.registerUpdateListener((()=>{t()})),e.registerEditableListener((()=>{t()})))}),[e]),t}(i),[n,o]=r(i.isEditable());if(t((()=>(o(i.isEditable()),i.registerEditableListener((e=>{o(e)})))),[i]),!a)return null;let d=null;return"function"==typeof e?d=e(n):null!==e&&(d=e),null===d?null:l("div",{"aria-hidden":!0,children:d})}export{x as ContentEditable};