json-joy
Version:
Collection of libraries for building collaborative editing apps.
33 lines • 1.1 kB
JavaScript
import { createElement, useMemo, useLayoutEffect } from 'react';
import { bind } from 'collaborative-input';
class CollaborativeInputState {
props;
_unbind;
constructor(props) {
this.props = props;
}
unbind = () => {
this._unbind?.();
this._unbind = void 0;
};
stop = this.unbind;
start = () => this.stop;
ref = (el) => {
if (!el)
return this.unbind();
const { str, polling } = this.props;
this._unbind = bind(str, el, !!polling);
};
}
export const CollaborativeInput = (props) => {
const { str, polling, inp: _inp, input, ...rest } = props;
// biome-ignore lint: hook dependency list manually managed
const state = useMemo(() => new CollaborativeInputState(props), [str, polling]);
// biome-ignore lint: hook dependency list manually managed
useLayoutEffect(state.start, [state]);
if (input)
return input(state.ref);
rest.ref = state.ref;
return createElement(rest.multiline ? 'textarea' : 'input', rest);
};
//# sourceMappingURL=CollaborativeInput.js.map