@pmndrs/uikit
Version:
Build performant 3D user interfaces with Three.js and yoga.
55 lines (54 loc) • 2.3 kB
JavaScript
import { createParentContextSignal, setupParentContextSignal, bindHandlers, Component } from './utils.js';
import { effect, signal, untracked } from '@preact/signals-core';
import { createInputState, setupInput } from '../components/input.js';
export class Input extends Component {
mergedProperties;
styleSignal = signal(undefined);
propertiesSignal;
defaultPropertiesSignal;
parentContextSignal = createParentContextSignal();
unsubscribe;
internals;
constructor(properties, defaultProperties) {
super();
this.matrixAutoUpdate = false;
setupParentContextSignal(this.parentContextSignal, this);
this.propertiesSignal = signal(properties);
this.defaultPropertiesSignal = signal(defaultProperties);
this.unsubscribe = effect(() => {
const parentContext = this.parentContextSignal.value?.value;
if (parentContext == null) {
return;
}
const abortController = new AbortController();
this.internals = createInputState(parentContext, parentContext.fontFamiliesSignal, this.styleSignal, this.propertiesSignal, this.defaultPropertiesSignal);
setupInput(this.internals, parentContext, this.styleSignal, this.propertiesSignal, this.defaultPropertiesSignal, this, abortController.signal);
this.mergedProperties = this.internals.mergedProperties;
super.add(this.internals.interactionPanel);
bindHandlers(this.internals.handlers, this, abortController.signal);
return () => {
this.remove(this.internals.interactionPanel);
abortController.abort();
};
});
}
getComputedProperty(key) {
return untracked(() => this.mergedProperties?.value.read(key, undefined));
}
getStyle() {
return this.styleSignal.peek();
}
setStyle(style, replace) {
this.styleSignal.value = replace ? style : { ...this.styleSignal.value, ...style };
}
setProperties(properties) {
this.propertiesSignal.value = properties;
}
setDefaultProperties(properties) {
this.defaultPropertiesSignal.value = properties;
}
destroy() {
this.parent?.remove(this);
this.unsubscribe();
}
}