Skip to content

Visualize Your Ideas With Code

feat(2d): unify layout properties (#355)

Jacob3cae97ea704d
diff --git a/packages/2d/src/components/CodeBlock.ts b/packages/2d/src/components/CodeBlock.ts
index 04fc4228..b4e56142 100644
--- a/packages/2d/src/components/CodeBlock.ts
+++ b/packages/2d/src/components/CodeBlock.ts
@@ -18,7 +18,7 @@ import {
tween,
} from '@motion-canvas/core/lib/tweening';
import {threadable} from '@motion-canvas/core/lib/decorators';
-import {Length} from '../partials';
+import {DesiredLength} from '../partials';
import {SerializedVector2, Vector2} from '@motion-canvas/core/lib/types';
import {
createComputedAsync,
@@ -131,7 +131,7 @@ export class CodeBlock extends Shape {
return new Vector2(width, parseFloat(this.styles.lineHeight));
}
- protected override desiredSize(): SerializedVector2<Length> {
+ protected override desiredSize(): SerializedVector2<DesiredLength> {
const custom = super.desiredSize();
const tokensSize = this.getTokensSize(this.parsed());
return {
diff --git a/packages/2d/src/components/Image.ts b/packages/2d/src/components/Image.ts
index 2998583b..e0f52775 100644
--- a/packages/2d/src/components/Image.ts
+++ b/packages/2d/src/components/Image.ts
@@ -8,7 +8,7 @@ import {
} from '@motion-canvas/core/lib/types';
import {drawImage} from '../utils';
import {Rect, RectProps} from './Rect';
-import {Length} from '../partials';
+import {DesiredLength} from '../partials';
import {
DependencyContext,
SignalValue,
@@ -39,7 +39,7 @@ export class Image extends Rect {
super(props);
}
- protected override desiredSize(): SerializedVector2<Length> {
+ protected override desiredSize(): SerializedVector2<DesiredLength> {
const custom = super.desiredSize();
if (custom.x === null && custom.y === null) {
const image = this.image();
diff --git a/packages/2d/src/components/Layout.ts b/packages/2d/src/components/Layout.ts
index 332aa1ae..77bde102 100644
--- a/packages/2d/src/components/Layout.ts
+++ b/packages/2d/src/components/Layout.ts
@@ -31,9 +31,11 @@ import {
FlexJustify,
FlexWrap,
LayoutMode,
- Length,
+ DesiredLength,
LetterSpacing,
TextWrap,
+ Length,
+ LengthLimit,
} from '../partials';
import {threadable} from '@motion-canvas/core/lib/decorators';
import {ThreadGenerator} from '@motion-canvas/core/lib/threading';
@@ -42,6 +44,7 @@ import {drawLine, lineTo} from '../utils';
import {spacingSignal} from '../decorators/spacingSignal';
import {
createSignal,
+ Signal,
SignalValue,
SimpleSignal,
} from '@motion-canvas/core/lib/signals';
@@ -52,10 +55,10 @@ export interface LayoutProps extends NodeProps {
width?: SignalValue<Length>;
height?: SignalValue<Length>;
- maxWidth?: SignalValue<Length>;
- maxHeight?: SignalValue<Length>;
- minWidth?: SignalValue<Length>;
- minHeight?: SignalValue<Length>;
+ maxWidth?: SignalValue<LengthLimit>;
+ maxHeight?: SignalValue<LengthLimit>;
+ minWidth?: SignalValue<LengthLimit>;
+ minHeight?: SignalValue<LengthLimit>;
ratio?: SignalValue<number>;
marginTop?: SignalValue<number>;
@@ -86,11 +89,11 @@ export interface LayoutProps extends NodeProps {
fontSize?: SignalValue<number>;
fontStyle?: SignalValue<string>;
fontWeight?: SignalValue<number>;
- lineHeight?: SignalValue<number>;
+ lineHeight?: SignalValue<Length>;
letterSpacing?: SignalValue<number>;
textWrap?: SignalValue<TextWrap>;
- size?: SignalValue<PossibleVector2>;
+ size?: SignalValue<PossibleVector2<Length>>;
offsetX?: SignalValue<number>;
offsetY?: SignalValue<number>;
offset?: SignalValue<PossibleVector2>;
@@ -104,16 +107,16 @@ export class Layout extends Node {
@initial(null)
@signal()
- public declare readonly maxWidth: SimpleSignal<Length, this>;
+ public declare readonly maxWidth: SimpleSignal<LengthLimit, this>;
@initial(null)
@signal()
- public declare readonly maxHeight: SimpleSignal<Length, this>;
+ public declare readonly maxHeight: SimpleSignal<LengthLimit, this>;
@initial(null)
@signal()
- public declare readonly minWidth: SimpleSignal<Length, this>;
+ public declare readonly minWidth: SimpleSignal<LengthLimit, this>;
@initial(null)
@signal()
- public declare readonly minHeight: SimpleSignal<Length, this>;
+ public declare readonly minHeight: SimpleSignal<LengthLimit, this>;
@initial(null)
@signal()
public declare readonly ratio: SimpleSignal<number | null, this>;
@@ -146,15 +149,15 @@ export class Layout extends Node {
@initial('normal')
@signal()
public declare readonly alignItems: SimpleSignal<FlexAlign, this>;
- @initial(null)
- @signal()
- public declare readonly gap: SimpleSignal<Length, this>;
- @initial(null)
- @signal()
- public declare readonly rowGap: SimpleSignal<Length, this>;
- @initial(null)
- @signal()
- public declare readonly columnGap: SimpleSignal<Length, this>;
+ @initial(0)
+ @vector2Signal({x: 'columnGap', y: 'rowGap'})
+ public declare readonly gap: Vector2LengthSignal<this>;
+ public get columnGap(): Signal<Length, number, this> {
+ return this.gap.x;
+ }
+ public get rowGap(): Signal<Length, number, this> {
+ return this.gap.y;
+ }
@defaultStyle('font-family')
@signal()
@@ -168,7 +171,7 @@ export class Layout extends Node {
@defaultStyle('font-weight', parseInt)
@signal()
public declare readonly fontWeight: SimpleSignal<number, this>;
- @initial('120%')
+ @defaultStyle('line-height', parseFloat)
@signal()
public declare readonly lineHeight: SimpleSignal<Length, this>;
@defaultStyle('letter-spacing', i => {
@@ -217,6 +220,11 @@ export class Layout extends Node {
this.customY(value);
}
+ @inspectable(false)
+ @initial(null)
+ @signal()
+ protected declare readonly customWidth: SimpleSignal<DesiredLength, this>;
+
protected getWidth(): number {
return this.computedSize().width;
}
@@ -257,6 +265,11 @@ export class Layout extends Node {
lock && this.releaseSize();
}
+ @inspectable(false)
+ @initial(null)
+ @signal()
+ protected declare readonly customHeight: SimpleSignal<DesiredLength, this>;
+
protected getHeight(): number {
return this.computedSize().height;
}
@@ -351,15 +364,22 @@ export class Layout extends Node {
@initial({x: null, y: null})
@vector2Signal({x: 'width', y: 'height'})
public declare readonly size: Vector2LengthSignal<this>;
+ public get width(): Signal<Length, number, this> {
+ return this.size.x;
+ }
+ public get height(): Signal<Length, number, this> {
+ return this.size.y;
+ }
- @inspectable(false)
- @signal()
- protected declare readonly customWidth: SimpleSignal<Length, this>;
- @inspectable(false)
- @signal()
- protected declare readonly customHeight: SimpleSignal<Length, this>;
+ /**
+ * Get the desired size of this node.
+ *
+ * @remarks
+ * This method can be used to control the size using external factors.
+ * By default, the returned size is the same as the one declared by the user.
+ */
@computed()
- protected desiredSize(): SerializedVector2<Length> {
+ protected desiredSize(): SerializedVector2<DesiredLength> {
return {
x: this.customWidth(),
y: this.customHeight(),
@@ -728,9 +748,8 @@ export class Layout extends Node {
this.element.style.justifyContent = this.justifyContent();
this.element.style.alignItems = this.alignItems();
- this.element.style.rowGap = this.parseLength(this.rowGap());
- this.element.style.columnGap = this.parseLength(this.columnGap());
- this.element.style.gap = this.parseLength(this.gap());
+ this.element.style.columnGap = this.parseLength(this.gap.x());
+ this.element.style.rowGap = this.parseLength(this.gap.y());
if (this.sizeLockCounter() > 0) {
this.element.style.flexGrow = '0';
@@ -752,10 +771,15 @@ export class Layout extends Node {
this.element.style.fontStyle = this.fontStyle.isInitial()
? ''
: this.fontStyle();
- this.element.style.lineHeight =
- typeof this.lineHeight() === 'string'
- ? (parseFloat(this.lineHeight() as string) / 100).toString()
- : `${this.lineHeight()}px`;
+ if (this.lineHeight.isInitial()) {
+ this.element.style.lineHeight = '';
+ } 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();
diff --git a/packages/2d/src/components/Line.ts b/packages/2d/src/components/Line.ts
index 25252527..7d8fa970 100644
--- a/packages/2d/src/components/Line.ts
+++ b/packages/2d/src/components/Line.ts
@@ -14,7 +14,7 @@ import {
Vector2,
} from '@motion-canvas/core/lib/types';
import {clamp} from '@motion-canvas/core/lib/tweening';
-import {Length} from '../partials';
+import {DesiredLength} from '../partials';
import {Layout} from './Layout';
import {
CurveDrawingInfo,
@@ -84,7 +84,7 @@ export class Line extends Shape {
this
>;
- protected override desiredSize(): SerializedVector2<Length> {
+ protected override desiredSize(): SerializedVector2<DesiredLength> {
return this.childrenRect().size;
}
diff --git a/packages/2d/src/components/Video.ts b/packages/2d/src/components/Video.ts
index 41311029..461b5266 100644
--- a/packages/2d/src/components/Video.ts
+++ b/packages/2d/src/components/Video.ts
@@ -8,7 +8,7 @@ import {useProject, useThread} from '@motion-canvas/core/lib/utils';
import {PlaybackState} from '@motion-canvas/core';
import {clamp} from '@motion-canvas/core/lib/tweening';
import {Rect, RectProps} from './Rect';
-import {Length} from '../partials';
+import {DesiredLength} from '../partials';
import {
DependencyContext,
SignalValue,
@@ -63,7 +63,7 @@ export class Video extends Rect {
return this.video().duration;
}
- protected override desiredSize(): SerializedVector2<Length> {
+ protected override desiredSize(): SerializedVector2<DesiredLength> {
const custom = super.desiredSize();
if (custom.x === null && custom.y === null) {
const image = this.video();
diff --git a/packages/2d/src/components/View2D.ts b/packages/2d/src/components/View2D.ts
index 9ab8c9f2..3e8d8983 100644
--- a/packages/2d/src/components/View2D.ts
+++ b/packages/2d/src/components/View2D.ts
@@ -25,7 +25,7 @@ export class View2D extends Layout {
composite: true,
fontFamily: 'Roboto',
fontSize: 48,
- lineHeight: 64,
+ lineHeight: '120%',
textWrap: false,
fontStyle: 'normal',
...props,
diff --git a/packages/2d/src/partials/types.ts b/packages/2d/src/partials/types.ts
index b3daa756..a526cdfc 100644
--- a/packages/2d/src/partials/types.ts
+++ b/packages/2d/src/partials/types.ts
@@ -46,9 +46,22 @@ export type LayoutMode = boolean | null;
* - `number` - the desired length in pixels
* - `${number}%` - a string with the desired length in percents, for example
* `'50%'`
- * - `null` - an automatic length (equivalent to `auto` in CSS)
*/
-export type Length = number | `${number}%` | null;
+export type Length = number | `${number}%`;
+
+/**
+ * Represents a desired length used internally by layout Nodes.
+ *
+ * @remarks
+ * When the desired length is set to `null` it represents a default value for
+ * whatever property it describes.
+ */
+export type DesiredLength = Length | null;
+
+/**
+ * Represents a length limit used by layout properties such as `max-width`.
+ */
+export type LengthLimit = Length | null | 'max-content' | 'min-content';
export type PossibleCanvasStyle = null | PossibleColor | Gradient | Pattern;
export type CanvasStyle = null | Color | Gradient | Pattern;