@tiptap/react 2.12.0 → 2.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { BubbleMenuPlugin } from '@tiptap/extension-bubble-menu';
2
- import React, { forwardRef, useState, useDebugValue, useLayoutEffect, useEffect, useRef, createContext, useContext } from 'react';
2
+ import React, { forwardRef, useState, useDebugValue, useLayoutEffect, useEffect, useRef, createContext, useContext, version, createRef, memo, createElement } from 'react';
3
3
  import ReactDOM, { flushSync } from 'react-dom';
4
4
  import { Editor, NodeView, getRenderedAttributes } from '@tiptap/core';
5
5
  export * from '@tiptap/core';
@@ -1212,6 +1212,26 @@ function isForwardRefComponent(Component) {
1212
1212
  return !!(typeof Component === 'object'
1213
1213
  && ((_a = Component.$$typeof) === null || _a === void 0 ? void 0 : _a.toString()) === 'Symbol(react.forward_ref)');
1214
1214
  }
1215
+ /**
1216
+ * Check if we're running React 19+ by detecting if function components support ref props
1217
+ * @returns {boolean}
1218
+ */
1219
+ function isReact19Plus() {
1220
+ // React 19 is detected by checking React version if available
1221
+ // In practice, we'll use a more conservative approach and assume React 18 behavior
1222
+ // unless we can definitively detect React 19
1223
+ try {
1224
+ // @ts-ignore
1225
+ if (version) {
1226
+ const majorVersion = parseInt(version.split('.')[0], 10);
1227
+ return majorVersion >= 19;
1228
+ }
1229
+ }
1230
+ catch {
1231
+ // Fallback to React 18 behavior if we can't determine version
1232
+ }
1233
+ return false;
1234
+ }
1215
1235
  /**
1216
1236
  * The ReactRenderer class. It's responsible for rendering React components inside the editor.
1217
1237
  * @example
@@ -1257,13 +1277,30 @@ class ReactRenderer {
1257
1277
  const Component = this.component;
1258
1278
  const props = this.props;
1259
1279
  const editor = this.editor;
1260
- if (isClassComponent(Component) || isForwardRefComponent(Component)) {
1261
- // @ts-ignore This is a hack to make the ref work
1262
- props.ref = (ref) => {
1263
- this.ref = ref;
1264
- };
1280
+ // Handle ref forwarding with React 18/19 compatibility
1281
+ const isReact19 = isReact19Plus();
1282
+ const isClassComp = isClassComponent(Component);
1283
+ const isForwardRefComp = isForwardRefComponent(Component);
1284
+ const elementProps = { ...props };
1285
+ if (!elementProps.ref) {
1286
+ if (isReact19) {
1287
+ // React 19: ref is a standard prop for all components
1288
+ // @ts-ignore - Setting ref prop for React 19 compatibility
1289
+ elementProps.ref = (ref) => {
1290
+ this.ref = ref;
1291
+ };
1292
+ }
1293
+ else if (isClassComp || isForwardRefComp) {
1294
+ // React 18 and prior: only set ref for class components and forwardRef components
1295
+ // @ts-ignore - Setting ref prop for React 18 class/forwardRef components
1296
+ elementProps.ref = (ref) => {
1297
+ this.ref = ref;
1298
+ };
1299
+ }
1300
+ // For function components in React 18, we can't use ref - the component won't receive it
1301
+ // This is a limitation we have to accept for React 18 function components without forwardRef
1265
1302
  }
1266
- this.reactElement = React.createElement(Component, { ...props });
1303
+ this.reactElement = React.createElement(Component, { ...elementProps });
1267
1304
  (_a = editor === null || editor === void 0 ? void 0 : editor.contentComponent) === null || _a === void 0 ? void 0 : _a.setRenderer(this.id, this);
1268
1305
  }
1269
1306
  /**
@@ -1312,6 +1349,7 @@ class ReactNodeView extends NodeView {
1312
1349
  getPos: () => this.getPos(),
1313
1350
  updateAttributes: (attributes = {}) => this.updateAttributes(attributes),
1314
1351
  deleteNode: () => this.deleteNode(),
1352
+ ref: createRef(),
1315
1353
  };
1316
1354
  if (!this.component.displayName) {
1317
1355
  const capitalizeFirstChar = (string) => {
@@ -1329,8 +1367,8 @@ class ReactNodeView extends NodeView {
1329
1367
  const Component = this.component;
1330
1368
  // For performance reasons, we memoize the provider component
1331
1369
  // And all of the things it requires are declared outside of the component, so it doesn't need to re-render
1332
- const ReactNodeViewProvider = React.memo(componentProps => {
1333
- return (React.createElement(ReactNodeViewContext.Provider, { value: context }, React.createElement(Component, componentProps)));
1370
+ const ReactNodeViewProvider = memo(componentProps => {
1371
+ return (React.createElement(ReactNodeViewContext.Provider, { value: context }, createElement(Component, componentProps)));
1334
1372
  });
1335
1373
  ReactNodeViewProvider.displayName = 'ReactNodeView';
1336
1374
  if (this.node.isLeaf) {