Skip to content

Visualize Your Ideas With Code

feat(2d): add textAlign property (#451)

Jacob3d15825f3cc5
diff --git a/packages/2d/src/components/Layout.ts b/packages/2d/src/components/Layout.ts
index a204bb03..b9be34fc 100644
--- a/packages/2d/src/components/Layout.ts
+++ b/packages/2d/src/components/Layout.ts
@@ -94,6 +94,7 @@ export interface LayoutProps extends NodeProps {
letterSpacing?: SignalValue<number>;
textWrap?: SignalValue<TextWrap>;
textDirection?: SignalValue<CanvasDirection>;
+ textAlign?: SignalValue<CanvasTextAlign>;
size?: SignalValue<PossibleVector2<Length>>;
offsetX?: SignalValue<number>;
@@ -192,6 +193,9 @@ export class Layout extends Node {
@initial('inherit')
@signal()
public declare readonly textDirection: SimpleSignal<CanvasDirection, this>;
+ @defaultStyle('text-align')
+ @signal()
+ public declare readonly textAlign: SimpleSignal<CanvasTextAlign, this>;
@cloneable(false)
@inspectable(false)
@@ -793,6 +797,10 @@ export class Layout extends Node {
? ''
: `${this.letterSpacing()}px`;
+ this.element.style.textAlign = this.textAlign.isInitial()
+ ? ''
+ : this.textAlign();
+
if (this.textWrap.isInitial()) {
this.element.style.whiteSpace = '';
} else {
diff --git a/packages/2d/src/components/Txt.ts b/packages/2d/src/components/Txt.ts
index 852c2fbb..a95620ae 100644
--- a/packages/2d/src/components/Txt.ts
+++ b/packages/2d/src/components/Txt.ts
@@ -113,6 +113,13 @@ export class Txt extends Shape {
this.applyFont();
this.applyFlex();
+ // Make sure the text is aligned correctly even if the text is smaller than
+ // the container.
+ if (this.justifyContent.isInitial()) {
+ this.element.style.justifyContent =
+ this.styles.getPropertyValue('text-align');
+ }
+
const wrap =
this.styles.whiteSpace !== 'nowrap' && this.styles.whiteSpace !== 'pre';