CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/npm-remirror--react

Hooks and components for consuming remirror with your fave framework React.

Overall
score

36%

Evaluation36%

1.09x

Agent success when using this tile

Overview
Eval results
Files

utilities.mddocs/

Utilities

React-specific utility functions for element manipulation, type checking, and React component handling.

Capabilities

Element Utilities

Functions for working with React elements and their properties.

/**
 * Add key prop to React element for use in arrays
 * @param element - React element to add key to
 * @param key - Key value to add
 * @returns Element with key prop added
 */
function addKeyToElement(element: ReactElement, key: string): ReactElement;

/**
 * Extract props from JSX element
 * @param element - React element to extract props from
 * @returns Props object or empty object if not valid element
 */
function getElementProps(element: ReactElement): Record<string, any>;

Usage Example:

import React from 'react';
import { addKeyToElement, getElementProps } from '@remirror/react';

function DynamicList({ items }) {
  const elementList = items.map((item, index) => {
    const element = <div>{item.content}</div>;
    return addKeyToElement(element, `item-${index}`);
  });

  const firstElementProps = getElementProps(elementList[0]);
  console.log('First element props:', firstElementProps);

  return <div>{elementList}</div>;
}

Type Checking Utilities

Functions for validating React elements and components.

/**
 * Check if value is a React DOM element (not a custom component)
 * @param element - Value to check
 * @returns True if element is a React DOM element
 */
function isReactDOMElement(element: unknown): element is ReactElement<any, string>;

/**
 * Check if value is a React Fragment
 * @param element - Value to check
 * @returns True if element is a React Fragment
 */
function isReactFragment(element: unknown): element is ReactElement<any, typeof React.Fragment>;

/**
 * Drop-in replacement for React.isValidElement with enhanced type checking
 * @param element - Value to check
 * @returns True if element is a valid React element
 */
function isValidElement(element: unknown): element is ReactElement;

Usage Example:

import React from 'react';
import { 
  isReactDOMElement, 
  isReactFragment, 
  isValidElement 
} from '@remirror/react';

function ElementAnalyzer({ children }) {
  const analyzeChild = (child) => {
    if (!isValidElement(child)) {
      return 'Not a React element';
    }
    
    if (isReactFragment(child)) {
      return 'React Fragment';
    }
    
    if (isReactDOMElement(child)) {
      return `DOM Element: ${child.type}`;
    }
    
    return `Custom Component: ${child.type.name || 'Anonymous'}`;
  };

  return (
    <div>
      {React.Children.map(children, (child, index) => (
        <div key={index}>
          Child {index}: {analyzeChild(child)}
        </div>
      ))}
    </div>
  );
}

Prop Validation Utilities

Functions for validating component props and function props.

/**
 * Validate that a prop is a function, throws error if not
 * @param prop - Property value to validate
 * @param propName - Name of the property for error messages
 * @throws Error if prop is not a function
 */
function propIsFunction(prop: unknown, propName: string): asserts prop is Function;

Usage Example:

import React from 'react';
import { propIsFunction } from '@remirror/react';

interface CallbackComponentProps {
  onAction: Function;
  onError?: Function;
  data: any;
}

function CallbackComponent({ onAction, onError, data }: CallbackComponentProps) {
  // Validate required function props
  propIsFunction(onAction, 'onAction');
  
  // Validate optional function props
  if (onError) {
    propIsFunction(onError, 'onError');
  }

  const handleClick = () => {
    try {
      onAction(data);
    } catch (error) {
      onError?.(error);
    }
  };

  return <button onClick={handleClick}>Execute Action</button>;
}

Advanced Element Manipulation

More sophisticated utilities for working with React elements.

/**
 * Clone element with additional props, preserving existing props
 * @param element - Element to clone
 * @param additionalProps - Props to add or override
 * @returns Cloned element with merged props
 */
function cloneElementWithProps(
  element: ReactElement,
  additionalProps: Record<string, any>
): ReactElement;

/**
 * Recursively map over React children, including nested structures
 * @param children - React children to map over
 * @param mapFn - Function to apply to each child
 * @returns Mapped children
 */
function deepMapChildren(
  children: React.ReactNode,
  mapFn: (child: ReactElement, index: number, depth: number) => ReactElement
): React.ReactNode;

/**
 * Filter React children based on a predicate function
 * @param children - React children to filter
 * @param predicate - Function to test each child
 * @returns Filtered children
 */
function filterChildren(
  children: React.ReactNode,
  predicate: (child: ReactElement, index: number) => boolean
): React.ReactNode;

/**
 * Find first child element matching a predicate
 * @param children - React children to search
 * @param predicate - Function to test each child
 * @returns First matching child or undefined
 */
function findChild(
  children: React.ReactNode,
  predicate: (child: ReactElement, index: number) => boolean
): ReactElement | undefined;

Usage Example:

import React from 'react';
import { 
  cloneElementWithProps,
  deepMapChildren,
  filterChildren,
  findChild
} from '@remirror/react';

function EnhancedContainer({ children, theme }) {
  // Add theme prop to all child elements
  const themedChildren = deepMapChildren(children, (child, index, depth) => {
    return cloneElementWithProps(child, {
      theme,
      depth,
      'data-index': index,
    });
  });

  // Filter out hidden children
  const visibleChildren = filterChildren(themedChildren, (child) => {
    return child.props.hidden !== true;
  });

  // Find first button child
  const firstButton = findChild(visibleChildren, (child) => {
    return child.type === 'button' || child.type?.name === 'Button';
  });

  return (
    <div className="enhanced-container">
      {firstButton && (
        <div className="primary-action">
          {cloneElementWithProps(firstButton, { primary: true })}
        </div>
      )}
      <div className="other-children">
        {visibleChildren}
      </div>
    </div>
  );
}

Component Analysis Utilities

Functions for analyzing React component structures and hierarchies.

/**
 * Get the display name of a React component
 * @param component - Component to get name from
 * @returns Component display name or 'Anonymous'
 */
function getComponentName(component: ComponentType): string;

/**
 * Check if component is a class component
 * @param component - Component to check
 * @returns True if component is a class component
 */
function isClassComponent(component: ComponentType): boolean;

/**
 * Check if component is a function component
 * @param component - Component to check
 * @returns True if component is a function component
 */
function isFunctionComponent(component: ComponentType): boolean;

/**
 * Check if component is a memo component
 * @param component - Component to check
 * @returns True if component is wrapped with React.memo
 */
function isMemoComponent(component: ComponentType): boolean;

/**
 * Check if component is a forwardRef component
 * @param component - Component to check
 * @returns True if component is wrapped with React.forwardRef
 */
function isForwardRefComponent(component: ComponentType): boolean;

Usage Example:

import React from 'react';
import { 
  getComponentName,
  isClassComponent,
  isFunctionComponent,
  isMemoComponent,
  isForwardRefComponent
} from '@remirror/react';

function ComponentInspector({ component }) {
  const analysis = {
    name: getComponentName(component),
    isClass: isClassComponent(component),
    isFunction: isFunctionComponent(component),
    isMemo: isMemoComponent(component),
    isForwardRef: isForwardRefComponent(component),
  };

  return (
    <div className="component-analysis">
      <h3>Component Analysis</h3>
      <ul>
        <li>Name: {analysis.name}</li>
        <li>Type: {analysis.isClass ? 'Class' : 'Function'}</li>
        <li>Memoized: {analysis.isMemo ? 'Yes' : 'No'}</li>
        <li>Forward Ref: {analysis.isForwardRef ? 'Yes' : 'No'}</li>
      </ul>
    </div>
  );
}

Event Utilities

Helper functions for working with React events and event handlers.

/**
 * Create a debounced event handler
 * @param handler - Original event handler
 * @param delay - Debounce delay in milliseconds
 * @returns Debounced event handler
 */
function debounceEventHandler<T extends (...args: any[]) => void>(
  handler: T,
  delay: number
): T;

/**
 * Create a throttled event handler
 * @param handler - Original event handler
 * @param interval - Throttle interval in milliseconds
 * @returns Throttled event handler
 */
function throttleEventHandler<T extends (...args: any[]) => void>(
  handler: T,
  interval: number
): T;

/**
 * Combine multiple event handlers into one
 * @param handlers - Array of event handlers
 * @returns Combined event handler
 */
function combineEventHandlers<T extends (...args: any[]) => void>(
  ...handlers: (T | undefined)[]
): T;

/**
 * Stop event propagation and prevent default
 * @param event - React event or native event
 */
function stopEvent(event: React.SyntheticEvent | Event): void;

Usage Example:

import React, { useState } from 'react';
import { 
  debounceEventHandler,
  throttleEventHandler,
  combineEventHandlers,
  stopEvent
} from '@remirror/react';

function SmartInput({ onChange, onFocus, onBlur, onKeyDown }) {
  const [value, setValue] = useState('');

  // Debounce onChange to avoid excessive API calls
  const debouncedOnChange = debounceEventHandler(onChange, 300);

  // Throttle scroll event handler
  const throttledScroll = throttleEventHandler((e) => {
    console.log('Scroll position:', e.target.scrollTop);
  }, 100);

  // Combine multiple key handlers
  const combinedKeyHandler = combineEventHandlers(
    onKeyDown,
    (e) => {
      if (e.key === 'Escape') {
        stopEvent(e);
        setValue('');
      }
    }
  );

  const handleChange = (e) => {
    setValue(e.target.value);
    debouncedOnChange(e.target.value);
  };

  return (
    <input
      value={value}
      onChange={handleChange}
      onFocus={onFocus}
      onBlur={onBlur}
      onKeyDown={combinedKeyHandler}
      onScroll={throttledScroll}
    />
  );
}

Type Definitions

/**
 * Generic component type that accepts any props
 */
type ComponentType<P = {}> = React.ComponentType<P>;

/**
 * Props with children
 */
interface PropsWithChildren<P = {}> extends P {
  children?: React.ReactNode;
}

/**
 * Props with optional className
 */
interface PropsWithClassName<P = {}> extends P {
  className?: string;
}

/**
 * Element properties for HTML elements
 */
type HTMLElementProps<T = HTMLElement> = React.HTMLAttributes<T>;

/**
 * Event handler type for React events
 */
type EventHandler<E = React.SyntheticEvent> = (event: E) => void;

/**
 * Ref type for React elements
 */
type ElementRef<T = HTMLElement> = React.RefObject<T> | React.MutableRefObject<T>;

Performance Utilities

/**
 * Memoize a component with custom comparison
 * @param component - Component to memoize
 * @param areEqual - Custom equality function
 * @returns Memoized component
 */
function memoizeComponent<P>(
  component: ComponentType<P>,
  areEqual?: (prevProps: P, nextProps: P) => boolean
): React.MemoExoticComponent<ComponentType<P>>;

/**
 * Create a stable callback that doesn't change on re-renders
 * @param callback - Callback function
 * @param deps - Dependency array
 * @returns Stable callback
 */
function useStableCallback<T extends (...args: any[]) => any>(
  callback: T,
  deps: React.DependencyList
): T;

/**
 * Lazy load a component with optional loading fallback
 * @param importFn - Dynamic import function
 * @param fallback - Loading component
 * @returns Lazy component
 */
function lazyComponent<P>(
  importFn: () => Promise<{ default: ComponentType<P> }>,
  fallback?: ComponentType
): React.LazyExoticComponent<ComponentType<P>>;

Install with Tessl CLI

npx tessl i tessl/npm-remirror--react@2.0.0
What are skills?

docs

advanced-hooks.md

component-extensions.md

content-rendering.md

core-integration.md

editor-hooks.md

index.md

table-extensions.md

ui-components.md

utilities.md

tile.json