Registry / web-framework / react-kapsule

react-kapsule

JSON →
library2.5.7jsnpmunverified

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.

npm install react-kapsule
INSTALL
IMPORT
SIG · REACT-KAPSULE
R
react-kapsule
web-frameworkjavascriptv2.5.7
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fromKapsule
import fromKapsule from 'react-kapsule';
import { fromKapsule } from 'react-kapsule';
`fromKapsule` is the default export of the 'react-kapsule' package.
fromKapsule (CJS)
const fromKapsule = require('react-kapsule');
const { fromKapsule } = require('react-kapsule');
For CommonJS environments, `fromKapsule` is the default export when requiring the package.
Kapsule (from parent library)
import Kapsule from 'kapsule';
import { Kapsule } from 'kapsule';
While react-kapsule wraps Kapsule components, Kapsule itself is typically imported from the 'kapsule' package, where it is also a default export.

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"); }
Debug
Known issues
gotchaProps specified in `options.initPropNames` are only used during the initial instantiation of the Kapsule component. Modifying these props after the component has mounted will have no effect on the underlying Kapsule component's configuration.
fix
Ensure that props intended for dynamic updates are NOT listed in `initPropNames`. Only use `initPropNames` for static, initial configuration values.
affects: >=1.0.0
gotchaMethods of the underlying Kapsule component, when exposed via `options.methodNames`, are accessible only through a React ref to the wrapped component instance. They are not directly available as props.
fix
Use `useRef` or `createRef` to get a reference to the `MyReactKapsuleComponent`. Call methods like `myRef.current.myMethod()` instead of trying to pass them as props.
affects: >=1.0.0
breakingThis package lists 'react' as a peer dependency, requiring `react@>=16.13.1`. Using an incompatible version of React (e.g., older than 16.13.1) can lead to unexpected behavior or runtime errors.
fix
Ensure your project's `react` and `react-dom` versions satisfy the peer dependency requirement by upgrading to `react@^16.13.1` or newer (e.g., `npm install react@^16.13.1 react-dom@^16.13.1`).
affects: <16.13.1 (for React)
Errors
Common errors & fixes
TypeError: (0, react_kapsule__WEBPACK_IMPORTED_MODULE_0__.fromKapsule) is not a function
Attempting to import `fromKapsule` as a named export instead of a default export in an ES module environment.
fix
Change the import statement to `import fromKapsule from 'react-kapsule';` (removing the curly braces).
Module not found: Can't resolve 'react-kapsule'
The 'react-kapsule' package is not installed or incorrectly referenced in your project's dependencies.
fix
Install the package using your package manager: `npm install react-kapsule` or `yarn add react-kapsule`. Ensure it's listed in `package.json`.
Cannot read properties of undefined (reading 'increment')
Attempting to call a Kapsule method (e.g., `increment`) on the wrapped React component without a valid ref, or before the ref has been assigned (e.g., during initial render before mount).
fix
Ensure the ref is correctly attached to the `MyReactKapsuleComponent` and checked for `null` or `undefined` before attempting to call methods. Methods are only available after the component has mounted.
Upgrade
Version history
2.5.7latest on npm
Audit
Dependencies
reactrequiredPeer dependency for React component integration; required for rendering in a React application.
Agent activity
10 hits · last 30 days
node
8
Resources
react-kapsule — npm install react-kapsule · libregistry