@pmndrs/uikit
Version:
Build performant 3D user interfaces with Three.js and yoga.
55 lines (54 loc) • 2.31 kB
JavaScript
import { createParentContextSignal, setupParentContextSignal, bindHandlers, Component } from './utils.js';
import { effect, signal, untracked } from '@preact/signals-core';
import { createIconState, setupIcon } from '../components/icon.js';
export class Icon extends Component {
styleSignal = signal(undefined);
propertiesSignal;
defaultPropertiesSignal;
parentContextSignal = createParentContextSignal();
unsubscribe;
internals;
constructor(text, svgWidth, svgHeight, 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 = createIconState(parentContext, text, svgWidth, svgHeight, this.styleSignal, this.propertiesSignal, this.defaultPropertiesSignal);
setupIcon(this.internals, parentContext, this.styleSignal, this.propertiesSignal, this, abortController.signal);
super.add(this.internals.interactionPanel);
super.add(this.internals.iconGroup);
bindHandlers(this.internals.handlers, this, abortController.signal);
return () => {
this.remove(this.internals.interactionPanel);
this.remove(this.internals.iconGroup);
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();
}
}