Skip to content

Commit

Permalink
feat(design): create DaffTextSnippetComponent (#2945)
Browse files Browse the repository at this point in the history
This adds a new text snippet component to show a snippet of text, with the ability to show or hide content beyond one line of text. You can use it like this:

```html
<daff-text-snippet>
	Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</daff-text-snippet>
```
  • Loading branch information
xelaint authored Nov 5, 2024
1 parent 618a630 commit 8656d7e
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libs/design/text-snippet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Text Snippet
The text snippet component shows a snippet of text, with the ability to show or hide content beyond one line of text.
7 changes: 7 additions & 0 deletions libs/design/text-snippet/examples/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "../../../../node_modules/ng-packagr/ng-entrypoint.schema.json",
"lib": {
"entryFile": "src/index.ts",
"styleIncludePaths": ["../../src/scss"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<daff-text-snippet>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</daff-text-snippet>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
ChangeDetectionStrategy,
Component,
} from '@angular/core';

import { DaffTextSnippetComponent } from '@daffodil/design/text-snippet';

@Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: 'basic-text-snippet',
templateUrl: './basic-text-snippet.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [
DaffTextSnippetComponent,
],
})
export class BasicTextSnippetComponent {}
1 change: 1 addition & 0 deletions libs/design/text-snippet/examples/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './public_api';
5 changes: 5 additions & 0 deletions libs/design/text-snippet/examples/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { BasicTextSnippetComponent } from './basic-text-snippet/basic-text-snippet.component';

export const TEXT_SNIPPET_EXAMPLES = [
BasicTextSnippetComponent,
];
7 changes: 7 additions & 0 deletions libs/design/text-snippet/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "../../../node_modules/ng-packagr/ng-entrypoint.schema.json",
"lib": {
"entryFile": "src/index.ts",
"styleIncludePaths": ["../src/scss"]
}
}
1 change: 1 addition & 0 deletions libs/design/text-snippet/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './public_api';
1 change: 1 addition & 0 deletions libs/design/text-snippet/src/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './text-snippet.component';
8 changes: 8 additions & 0 deletions libs/design/text-snippet/src/text-snippet.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div class="daff-text-snippet__content daff-text-snippet__html" #htmlEl [class.condensed]="condensed" [innerHtml]="html" *ngIf="html"></div>
<div class="daff-text-snippet__content daff-text-snippet__ngcontent" #contentEl [class.condensed]="condensed" *ngIf="!html">
<ng-content></ng-content>
</div>
<button daff-underline-button color="theme-contrast" [attr.aria-expanded]="!condensed ? true : false" (click)="toggleSnippet()">
<ng-container *ngIf="condensed">Show More</ng-container>
<ng-container *ngIf="!condensed">Show Less</ng-container>
</button>
45 changes: 45 additions & 0 deletions libs/design/text-snippet/src/text-snippet.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@mixin line-clamp() {
display: -webkit-box; // stylelint-disable-line value-no-vendor-prefix
line-clamp: 1;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
overflow: hidden;
}

:host {
display: block;
position: relative;

::ng-deep { // stylelint-disable-line selector-pseudo-element-no-unknown
.daff-text-snippet__content {
> {
* {
&:first-child {
margin-top: 0;
}

&:last-child {
margin-bottom: 0;
}
}
}
}

h2 {
&:first-of-type {
margin-top: 0;
}
}
}
}

.daff-text-snippet {
&__content {
display: block;

&.condensed { // stylelint-disable-line selector-class-pattern
@include line-clamp();
width: 100%;
}
}
}
99 changes: 99 additions & 0 deletions libs/design/text-snippet/src/text-snippet.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {
Component,
DebugElement,
} from '@angular/core';
import {
TestBed,
ComponentFixture,
waitForAsync,
} from '@angular/core/testing';
import { By } from '@angular/platform-browser';

import { DaffButtonModule } from '@daffodil/design/button';

import { DaffTextSnippetComponent } from './text-snippet.component';

@Component({
template: '<daff-text-snippet [condensed]="condensed" [html]="html">content</daff-text-snippet>',
standalone: true,
imports: [
DaffButtonModule,
DaffTextSnippetComponent,
],
})

class WrapperComponent {
condensed = true;
html = '';
}

describe('DaffTextSnippetComponent', () => {
let fixture: ComponentFixture<WrapperComponent>;
let wrapper: WrapperComponent;
let component: DaffTextSnippetComponent;
let componentDe: DebugElement;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
WrapperComponent,
],
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(WrapperComponent);
wrapper = fixture.componentInstance;
componentDe = fixture.debugElement.query(By.css('daff-text-snippet'));
component = componentDe.componentInstance;
});

it('should create', () => {
expect(fixture).toBeTruthy();
});

it('should pass through any html as `innerHtml` on the `html` div inside the component', () => {
wrapper.html = '<h1>Some Content</h1>';
fixture.detectChanges();
const htmlHolder = fixture.debugElement.query(By.css('.daff-text-snippet__html'));
expect(htmlHolder).toBeTruthy();
expect(htmlHolder.nativeElement.innerHTML).toEqual(wrapper.html);
});

it('should securely pass-through html onto the `html` div', () => {
wrapper.html = '<script>alert("hello")</script>';
fixture.detectChanges();
const htmlHolder = fixture.debugElement.query(By.css('.daff-text-snippet__html'));
expect(htmlHolder).toBeTruthy();
expect(htmlHolder.nativeElement.innerHTML).toEqual('');
});

it('should hide the other content if `html` is set', () => {
wrapper.html = '<div></div>';
fixture.detectChanges();
const contentHolder = fixture.debugElement.query(By.css('.daff-text-snippet__ngcontent'));
expect(contentHolder).toBeFalsy();
});

it('should show the other content if `html` is falsy', () => {
wrapper.html = '';
fixture.detectChanges();
const contentHolder = fixture.debugElement.query(By.css('.daff-text-snippet__ngcontent'));
expect(contentHolder).toBeTruthy();
});

it('should hide the html holder if `html` is falsy', () => {
wrapper.html = '';
fixture.detectChanges();
const contentHolder = fixture.debugElement.query(By.css('.daff-text-snippet__html'));
expect(contentHolder).toBeFalsy();
});

it('should apply a `condensed` class to the content when `condensed` is true', () => {
wrapper.condensed = true;
fixture.detectChanges();

expect(componentDe.query(By.css('.daff-text-snippet__content')).classes['condensed']).toBeTruthy();
});
});
47 changes: 47 additions & 0 deletions libs/design/text-snippet/src/text-snippet.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { NgIf } from '@angular/common';
import {
Component,
Input,
ChangeDetectionStrategy,
EventEmitter,
Output,
ElementRef,
ViewChild,
} from '@angular/core';

import { DAFF_BUTTON_COMPONENTS } from '@daffodil/design/button';

@Component({
selector: 'daff-text-snippet',
templateUrl: './text-snippet.component.html',
styleUrls: ['./text-snippet.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [
NgIf,
DAFF_BUTTON_COMPONENTS,
],
})
export class DaffTextSnippetComponent {

/**
* A property to track whether or not the component
* should render a condensed version of the content.
*/
@Input() condensed = true;

@Input() html = '';

@ViewChild('contentEl', { read: ElementRef }) contentRef: ElementRef;
@ViewChild('htmlEl', { read: ElementRef }) htmlRef: ElementRef;

/**
* An output event that can be used to track the state of the component externally.
*/
@Output() toggle: EventEmitter<boolean> = new EventEmitter();

toggleSnippet() {
this.condensed = !this.condensed;
this.toggle.emit(this.condensed);
}
}
3 changes: 3 additions & 0 deletions libs/design/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
"@daffodil/design/sidebar": [
"libs/design/sidebar/src"
],
"@daffodil/design/text-snippet": [
"libs/design/text-snippet/src"
],
"@daffodil/design/toast": [
"libs/design/toast/src"
],
Expand Down

0 comments on commit 8656d7e

Please sign in to comment.