Visualize Your Ideas With Code
feat(2d): switch to signal-based inheritance (#1097)
Jacob11e2e52e851a
diff --git a/packages/2d/src/lib/components/Layout.ts b/packages/2d/src/lib/components/Layout.tsindex 3c4c4f62..d5baf71d 100644--- a/packages/2d/src/lib/components/Layout.ts+++ b/packages/2d/src/lib/components/Layout.ts@@ -255,32 +255,32 @@ export class Layout extends Node {return this.gap.y;}- @defaultStyle('font-family')+ @defaultStyle('Roboto')@signal()public declare readonly fontFamily: SimpleSignal<string, this>;- @defaultStyle('font-size', parseFloat)+ @defaultStyle(48)@signal()public declare readonly fontSize: SimpleSignal<number, this>;- @defaultStyle('font-style')+ @defaultStyle('normal')@signal()public declare readonly fontStyle: SimpleSignal<string, this>;- @defaultStyle('font-weight', parseInt)+ @defaultStyle(500)@signal()public declare readonly fontWeight: SimpleSignal<number, this>;- @defaultStyle('line-height', parseFloat)+ @defaultStyle('120%')@signal()public declare readonly lineHeight: SimpleSignal<Length, this>;- @defaultStyle('letter-spacing', i => (i === 'normal' ? 0 : parseFloat(i)))+ @defaultStyle(0)@signal()public declare readonly letterSpacing: SimpleSignal<number, this>;- @defaultStyle('white-space', i => (i === 'pre' ? 'pre' : i === 'normal'))+ @defaultStyle(false)@signal()public declare readonly textWrap: SimpleSignal<TextWrap, this>;- @initial('inherit')+ @initial('ltr')@signal()public declare readonly textDirection: SimpleSignal<CanvasDirection, this>;- @defaultStyle('text-align')+ @defaultStyle('start')@signal()public declare readonly textAlign: SimpleSignal<CanvasTextAlign, this>;@@ -993,44 +993,25 @@ export class Layout extends Node {@computed()protected applyFont() {- this.element.style.fontFamily = this.fontFamily.isInitial()- ? ''- : this.fontFamily();- this.element.style.fontSize = this.fontSize.isInitial()- ? ''- : `${this.fontSize()}px`;- this.element.style.fontStyle = this.fontStyle.isInitial()- ? ''- : this.fontStyle();- if (this.lineHeight.isInitial()) {- this.element.style.lineHeight = '';+ this.element.style.fontFamily = this.fontFamily();+ this.element.style.fontSize = `${this.fontSize()}px`;+ this.element.style.fontStyle = this.fontStyle();++ const lineHeight = this.lineHeight();+ this.element.style.lineHeight =+ typeof lineHeight === 'number'+ ? `${lineHeight}px`+ : (parseFloat(lineHeight as string) / 100).toString();++ this.element.style.fontWeight = this.fontWeight().toString();+ this.element.style.letterSpacing = `${this.letterSpacing()}px`;+ this.element.style.textAlign = this.textAlign();++ const wrap = this.textWrap();+ if (typeof wrap === 'boolean') {+ this.element.style.whiteSpace = wrap ? 'normal' : 'nowrap';} else {- const lineHeight = this.lineHeight();- this.element.style.lineHeight =- typeof lineHeight === 'string'- ? (parseFloat(lineHeight as string) / 100).toString()- : `${lineHeight}px`;- }- this.element.style.fontWeight = this.fontWeight.isInitial()- ? ''- : this.fontWeight().toString();- this.element.style.letterSpacing = this.letterSpacing.isInitial()- ? ''- : `${this.letterSpacing()}px`;-- this.element.style.textAlign = this.textAlign.isInitial()- ? ''- : this.textAlign();-- if (this.textWrap.isInitial()) {- this.element.style.whiteSpace = '';- } else {- const wrap = this.textWrap();- if (typeof wrap === 'boolean') {- this.element.style.whiteSpace = wrap ? 'normal' : 'nowrap';- } else {- this.element.style.whiteSpace = wrap;- }+ this.element.style.whiteSpace = wrap;}}diff --git a/packages/2d/src/lib/components/View2D.ts b/packages/2d/src/lib/components/View2D.tsindex 46a9de1e..85415134 100644--- a/packages/2d/src/lib/components/View2D.ts+++ b/packages/2d/src/lib/components/View2D.ts@@ -43,11 +43,6 @@ export class View2D extends Rect {public constructor(props: View2DProps) {super({composite: true,- fontFamily: 'Roboto',- fontSize: 48,- lineHeight: '120%',- textWrap: false,- fontStyle: 'normal',...props,});this.view2D = this;diff --git a/packages/2d/src/lib/decorators/defaultStyle.ts b/packages/2d/src/lib/decorators/defaultStyle.tsindex d9662b75..8c8bc8f1 100644--- a/packages/2d/src/lib/decorators/defaultStyle.ts+++ b/packages/2d/src/lib/decorators/defaultStyle.ts@@ -1,18 +1,15 @@import {capitalize} from '@motion-canvas/core';import {Layout} from '../components';-export function defaultStyle<T>(- styleName: string,- parse: (value: string) => T = value => value as T,-): PropertyDecorator {+export function defaultStyle<T>(initial?: T): PropertyDecorator {return (target: any, key) => {target[`getDefault${capitalize(<string>key)}`] = function (this: Layout) {- this.requestLayoutUpdate();- const old = (<any>this.element.style)[styleName];- (<any>this.element.style)[styleName] = '';- const ret = parse.call(this, this.styles.getPropertyValue(styleName));- (<any>this.element.style)[styleName] = old;- return ret;+ const parent = this.parentTransform();+ if (parent && this.layout() !== false && parent.layoutEnabled()) {+ return (parent as any)[key]();+ }++ return initial;};};}