Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kit): add auto lineHeight for TuiLineClamp #6806

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<tui-island class="huge">
<tui-line-clamp
content="Daenerys of the House Targaryen, the First of Her Name, The Unburnt, Queen of the Andals, the Rhoynar and the First Men, Queen of Meereen, Khaleesi of the Great Grass Sea, Protector of the Realm, Lady Regent of the Seven Kingdoms, Breaker of Chains and Mother of Dragons"
lineHeight="auto"
></tui-line-clamp>
</tui-island>

<tui-island class="small">
<tui-line-clamp
content="Daenerys of the House Targaryen, the First of Her Name, The Unburnt, Queen of the Andals, the Rhoynar and the First Men, Queen of Meereen, Khaleesi of the Great Grass Sea, Protector of the Realm, Lady Regent of the Seven Kingdoms, Breaker of Chains and Mother of Dragons"
lineHeight="auto"
></tui-line-clamp>
</tui-island>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.huge {
line-height: 40px;
}

.small {
line-height: 20px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Component} from '@angular/core';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';

@Component({
selector: 'tui-line-clamp-example-6',
templateUrl: './index.html',
styleUrls: ['./index.less'],
encapsulation,
changeDetection,
})
export class TuiLineClampExample6 {}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ export class ExampleTuiLineClampComponent {
HTML: import('./examples/5/index.html?raw'),
LESS: import('./examples/5/index.less?raw'),
};

readonly example6: TuiDocExample = {
TypeScript: import('./examples/6/index.ts?raw'),
HTML: import('./examples/6/index.html?raw'),
LESS: import('./examples/6/index.less?raw'),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {TuiLineClampExample2} from './examples/2';
import {TuiLineClampExample3} from './examples/3';
import {TuiLineClampExample4} from './examples/4';
import {TuiLineClampExample5} from './examples/5';
import {TuiLineClampExample6} from './examples/6';
import {ExampleTuiLineClampComponent} from './line-clamp.component';

@NgModule({
Expand All @@ -49,6 +50,7 @@ import {ExampleTuiLineClampComponent} from './line-clamp.component';
TuiLineClampExample3,
TuiLineClampExample4,
TuiLineClampExample5,
TuiLineClampExample6,
],
exports: [ExampleTuiLineClampComponent],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@
>
<tui-line-clamp-example-5></tui-line-clamp-example-5>
</tui-doc-example>

<tui-doc-example
id="clamp-auto-line-height"
heading="Clamp lineHeight auto"
[content]="example6"
>
<tui-line-clamp-example-6></tui-line-clamp-example-6>
</tui-doc-example>
</ng-template>

<ng-template pageTab>
Expand Down
19 changes: 17 additions & 2 deletions projects/kit/components/line-clamp/line-clamp.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Renderer2,
ViewChild,
} from '@angular/core';
import {WINDOW} from '@ng-web-apis/common';
import {tuiIsCurrentTarget, tuiPx, tuiTypedFromEvent, tuiZonefree} from '@taiga-ui/cdk';
import {TUI_HINT_COMPONENT, TuiHintDirective} from '@taiga-ui/core';
import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus';
Expand Down Expand Up @@ -55,7 +56,7 @@ export class TuiLineClampComponent implements DoCheck, AfterViewInit {
}

@Input()
lineHeight = 24;
lineHeight: number | 'auto' = 24;

@Input()
content: PolymorpheusContent;
Expand All @@ -82,6 +83,7 @@ export class TuiLineClampComponent implements DoCheck, AfterViewInit {
@Inject(Renderer2) private readonly renderer: Renderer2,
@Inject(ChangeDetectorRef) private readonly cd: ChangeDetectorRef,
@Inject(NgZone) private readonly zone: NgZone,
@Inject(WINDOW) private readonly window: Window,
@Inject(TUI_LINE_CLAMP_OPTIONS) private readonly options: TuiLineClampOptions,
) {
this.skipInitialTransition();
Expand All @@ -103,6 +105,19 @@ export class TuiLineClampComponent implements DoCheck, AfterViewInit {
return this.options.showHint && this.overflown ? this.content : '';
}

private get computedLineHeight(): number {
if (this.lineHeight === 'auto') {
return (
parseInt(
this.window.getComputedStyle(this.el.nativeElement).lineHeight,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We did this initially and it turned out to be very slow when there are multiple instances of it. I think we better not add this option. One day we will be able to use lh CSS units :harold_hides_the_pain:

10,
) || 24
);
}

return this.lineHeight;
}

@HostListener('transitionend')
updateView(): void {
this.cd.detectChanges();
Expand Down Expand Up @@ -139,7 +154,7 @@ export class TuiLineClampComponent implements DoCheck, AfterViewInit {
this.renderer.setStyle(
this.el.nativeElement,
'max-height',
tuiPx(this.lineHeight * this.linesLimit$.value),
tuiPx(this.computedLineHeight * this.linesLimit$.value),
);
}
}
Expand Down
Loading