Skip to content

Answer:59 #1280

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
@@ -1,11 +1,36 @@
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { NgTemplateOutlet } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
contentChild,
Directive,
signal,
TemplateRef,
} from '@angular/core';

@Directive({
selector: '[cardContentTemplate]',
})
export class CardContentTemplateDirective {
static ngTemplateContextGuard(
dir: CardContentTemplateDirective,
ctx: any,
): ctx is CardContentTemplateContext {
return true;
}
}

type CardContentTemplateContext = {
$implicit: { expanded: boolean };
};

@Component({
selector: 'app-expandable-card',
template: `
<button
class="text-fg-subtle hover:bg-button-secondary-bg-hover active:bg-button-secondary-bg-active focus:outline-button-border-highlight flex w-fit items-center gap-1 py-2 focus:outline focus:outline-2 focus:outline-offset-1"
(click)="isExpanded.set(!isExpanded())"
#expandContentButton
data-cy="expandable-panel-toggle">
@if (isExpanded()) {
<svg
Expand Down Expand Up @@ -41,14 +66,29 @@ import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
class="overflow-hidden transition-[max-height] duration-500"
[class.max-h-0]="!isExpanded()"
[class.max-h-[1000px]]="isExpanded()">
<ng-content />
@defer (on interaction(expandContentButton)) {
Copy link
Owner

Choose a reason for hiding this comment

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

yes, yes and yes
I was looking for this solution. There were a hint in the title. But I think I will create a second challenge for people to guess this solution.

Great job. 🔥

<ng-container
[ngTemplateOutlet]="cardContentTemplate()"
[ngTemplateOutletContext]="{
$implicit: { expanded: isExpanded() }
}"></ng-container>
} @placeholder {
<div>This is the card content placeholder</div>
}
</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [NgTemplateOutlet],
host: {
class: 'flex flex-col gap-2 ',
},
})
export class ExpandableCard {
public isExpanded = signal(false);
public cardContentTemplate = contentChild.required<
CardContentTemplateDirective,
TemplateRef<CardContentTemplateContext>
>(CardContentTemplateDirective, {
read: TemplateRef,
});
}
58 changes: 44 additions & 14 deletions apps/angular/59-content-projection-defer/src/app/page-2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ import { httpResource } from '@angular/common/http';
import {
ChangeDetectionStrategy,
Component,
computed,
Directive,
ResourceStatus,
viewChild,
} from '@angular/core';
import { ExpandableCard } from './expandable-card';
import {
CardContentTemplateDirective,
ExpandableCard,
} from './expandable-card';

interface Post {
id: number;
Expand All @@ -13,31 +19,55 @@ interface Post {
userId: number;
}

@Directive({
selector: '[postResource]',
})
export class PostResourceDirective {
readonly isLoading = computed(() => this.#postHttpResource.isLoading());
readonly status = computed(() => this.#postHttpResource.status());
readonly value = computed(() => this.#postHttpResource.value());

readonly #postHttpResource = httpResource<Post[]>(
() => 'https://jsonplaceholder.typicode.com/posts',
);
}

@Component({
selector: 'app-page-2',
template: `
page2
<app-expandable-card>
<div title>Load Post</div>
<div>
@if (postResource.isLoading()) {
Loading...
} @else if (postResource.status() === ResourceStatus.Error) {
Error...
} @else {
@for (post of postResource.value(); track post.id) {
<div>{{ post.title }}</div>
}
}
<ng-template [cardContentTemplate] let-state>
<div postResource>
@if (state.expanded) {
@if (postResourceDir().isLoading()) {
<div>Loading...</div>
} @else if (postResourceDir().status() === ResourceStatus.Error) {
<div>Error...</div>
} @else {
@for (post of postResourceDir().value(); track post.id) {
<div>{{ post.title }}</div>
}
}
}
</div>
</ng-template>
</div>
</app-expandable-card>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [ExpandableCard],
imports: [
ExpandableCard,
PostResourceDirective,
CardContentTemplateDirective,
],
})
export class Page2 {
public postResource = httpResource<Post[]>(
'https://jsonplaceholder.typicode.com/posts',
);
protected readonly ResourceStatus = ResourceStatus;

protected readonly postResourceDir = viewChild.required(
PostResourceDirective,
);
}