React Kapsule Wrapper

2.5.7 · active · verified Sun Apr 19

react-kapsule is a utility library designed to integrate Kapsule-style web components into React applications. It provides a higher-order component, `fromKapsule`, which acts as a bridge, transforming a Kapsule component definition (often a closure-based functional component following Mike Bostock's reusable charts pattern) into a standard React component. The current stable version is 2.5.7, with the last publish being approximately a year ago, indicating a mature and stable library rather than one with frequent, breaking updates. It ships with TypeScript types, ensuring robust type checking for consumers. Its core utility lies in abstracting the imperative nature of Kapsule components, allowing them to be declaratively used within React's component tree, handling prop updates and method invocations transparently. This enables developers to leverage existing Kapsule components or build new ones with Kapsule, while still utilizing React for their main application UI.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates wrapping a Kapsule component with `fromKapsule`, passing initial props, exposing Kapsule methods, and interacting with them via a React ref to update the component imperatively.

import React, { useRef, useEffect } from 'react';
import ReactDOM from 'react-dom/client';
import Kapsule from 'kapsule';
import fromKapsule from 'react-kapsule';

// Define a simple Kapsule component
const myKapsuleComponent = Kapsule({
  props: {
    message: { default: 'Hello' },
    count: { default: 0 }
  },
  methods: {
    increment: function(state) { state.count++; },
    setMessage: function(state, msg) { state.message = msg; }
  },
  init(domNode, state) {
    state.container = document.createElement('div');
    domNode.appendChild(state.container);
  },
  update(state) {
    state.container.innerHTML = `<span>${state.message} from Kapsule! Count: ${state.count}</span>`;
  }
});

// Wrap the Kapsule component for React
const MyReactKapsuleComponent = fromKapsule(myKapsuleComponent, {
  methodNames: ['increment', 'setMessage'] // Expose Kapsule methods as React component methods
});

function App() {
  const kapsuleRef = useRef(null);

  useEffect(() => {
    const interval = setInterval(() => {
      if (kapsuleRef.current) {
        (kapsuleRef.current as any).increment(); // Call exposed Kapsule method
      }
    }, 1000);
    return () => clearInterval(interval);
  }, []);

  return (
    <div>
      <h1>React Kapsule Example</h1>
      <MyReactKapsuleComponent
        ref={kapsuleRef}
        message="Greetings"
        count={10}
      />
      <button onClick={() => (kapsuleRef.current as any)?.setMessage('New message!')}>
        Change Message
      </button>
    </div>
  );
}

const rootElement = document.getElementById('root');
if (rootElement) {
  const root = ReactDOM.createRoot(rootElement);
  root.render(<App />);
} else {
  console.error("Root element not found");
}

view raw JSON →