UNPKG

@pmndrs/uikit

Version:

Build performant 3D user interfaces with Three.js and yoga.

59 lines (58 loc) 2.56 kB
import { Parent, createParentContextSignal, bindHandlers, setupParentContextSignal } from './utils.js'; import { effect, signal, untracked } from '@preact/signals-core'; import { createSvgState, setupSvg } from '../components/index.js'; export class Svg extends Parent { 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) { this.contextSignal.value = undefined; return; } const abortController = new AbortController(); this.internals = createSvgState(parentContext, { current: this }, this.styleSignal, this.propertiesSignal, this.defaultPropertiesSignal); setupSvg(this.internals, parentContext, this.styleSignal, this.propertiesSignal, this, this.childrenContainer, abortController.signal); this.mergedProperties = this.internals.mergedProperties; this.contextSignal.value = Object.assign(this.internals, { fontFamiliesSignal: parentContext.fontFamiliesSignal }); super.add(this.internals.interactionPanel); super.add(this.internals.centerGroup); bindHandlers(this.internals.handlers, this, abortController.signal); return () => { this.remove(this.internals.interactionPanel); this.remove(this.internals.centerGroup); 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(); } }