DirectiveAdded in v16Optional

wrFitText

Shrink a block whose text wraps to the width of its longest line.

Installation

Import the class, and @use the entry point in your global styles. Nothing warns you when the @use is missing.

import { Component } from '@angular/core';
import { WrTag } from 'ngwr/badge';
import { WrFitText } from 'ngwr/directives';

@Component({ imports: [WrTag, WrFitText] })
export class MyComponent {}

Usage

A wrapped block keeps the width it was offered, so a tinted pill carries an empty band beside its text. wrFitText sets the host's inline width to its widest line. The line breaks stay where they were.

<wr-tag class="wrap" wrFitText transparent color="success">
  Charging finished, waiting for the next shift to collect
</wr-tag>

<p [wrFitText]="fit()">…</p>

Without

Charging finished, waiting for the next shift to collect

wrFitText

Charging finished, waiting for the next shift to collect

What it needs

A host that takes a width and wraps: a block, an inline-block, a flex or grid item. wr-tag is one line by default, so let it wrap first.

.wrap {
  display: inline-block;
  max-inline-size: 100%;
  text-align: start;
  white-space: normal;
}
WhenWhat happens
The text changesMeasured again on the next frame.
The parent gets wider or narrowerMeasured again on the next frame.
The text fits on one lineNo width is written.
[wrFitText]="false"The width it wrote is removed.
Server renderingNothing: the width is written in the browser.

API

NameDescriptionTypeDefault
[wrFitText]Sets the inline width to the widest line. false switches it off.booleantrue

See also