@pmndrs/uikit
Version:
Build performant 3D user interfaces with Three.js and yoga.
54 lines (53 loc) • 2.32 kB
JavaScript
import { createImageState, setupImage } from '../components/image.js';
import { Parent, createParentContextSignal, setupParentContextSignal, bindHandlers } from './utils.js';
import { effect, signal, untracked } from '@preact/signals-core';
export class Image extends Parent {
styleSignal = signal(undefined);
propertiesSignal;
defaultPropertiesSignal;
parentContextSignal = createParentContextSignal();
unsubscribe;
internals;
constructor(properties, defaultProperties) {
super();
setupParentContextSignal(this.parentContextSignal, this);
this.matrixAutoUpdate = false;
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 = createImageState(parentContext, { current: this }, this.styleSignal, this.propertiesSignal, this.defaultPropertiesSignal);
setupImage(this.internals, parentContext, this.styleSignal, this.propertiesSignal, this, this.childrenContainer, abortController.signal);
this.contextSignal.value = Object.assign(this.internals, { fontFamiliesSignal: parentContext.fontFamiliesSignal });
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.internals.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();
}
}