Skip to content

docs(www): add code tabs and copy functionality #4897

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 3 commits 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
6 changes: 6 additions & 0 deletions projects/www/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MenuComponent } from './components/menu.component';
import { MarkdownSymbolLinkComponent } from './components/docs/markdown-symbol-link.component';
import { AlertComponent } from './components/docs/alert.component';
import { CodeExampleComponent } from './components/docs/code-example.component';
import { CodeTabsComponent } from './components/docs/code-tabs.component';
import { StackblitzComponent } from './components/docs/stackblitz.component';
import { FooterComponent } from './components/footer.component';

Expand Down Expand Up @@ -74,6 +75,11 @@ export class AppComponent {
});
customElements.define('ngrx-code-example', codeExampleElement);

const codeTabsElement = createCustomElement(CodeTabsComponent, {
injector: this.injector,
});
customElements.define('ngrx-code-tabs', codeTabsElement);

const stackblitzElement = createCustomElement(StackblitzComponent, {
injector: this.injector,
});
Expand Down
98 changes: 96 additions & 2 deletions projects/www/src/app/components/docs/code-example.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { Component, Input } from '@angular/core';
import { Component, Input, ElementRef, signal, viewChild } from '@angular/core';
import { MatIcon } from '@angular/material/icon';

@Component({
selector: 'ngrx-code-example',
standalone: true,
imports: [MatIcon],
template: `
@if (header) {
<div class="header">{{ header }}</div>
}

<div class="body">
<ng-content></ng-content>
<button
class="copy-button"
[class.copied]="copied()"
(click)="copyCode()"
(animationend)="onAnimationEnd($event)"
[title]="copied() ? 'Copied!' : 'Copy code'"
[attr.aria-label]="
copied() ? 'Code copied to clipboard' : 'Copy code to clipboard'
"
>
@if(copied()) {
<mat-icon>done</mat-icon>} @else {
<mat-icon>content_copy</mat-icon>
}
</button>
<div #codeBody>
<ng-content></ng-content>
</div>
</div>
`,
styles: [
Expand All @@ -30,9 +52,81 @@ import { Component, Input } from '@angular/core';
padding: 0 0px;
overflow-x: wrap;
}

.copy-button {
position: absolute;
top: 8px;
right: 8px;
cursor: pointer;
padding: 3px;
transition: all 0.2s ease;
display: flex;
align-items: center;
justify-content: center;
}

.copy-button.copied {
animation: copyFeedback 2s ease-in-out;
}

@keyframes copyFeedback {
0% {
background: rgba(207, 143, 197, 0.8);
border-color: rgba(207, 143, 197, 0.6);
color: rgba(255, 255, 255, 1);
transform: scale(1);
}
15% {
background: rgba(207, 143, 197, 0.8);
border-color: rgba(207, 143, 197, 0.6);
color: rgba(255, 255, 255, 1);
transform: scale(1.1);
}
30% {
background: rgba(207, 143, 197, 0.8);
border-color: rgba(207, 143, 197, 0.6);
color: rgba(255, 255, 255, 1);
transform: scale(1);
}
80% {
background: rgba(207, 143, 197, 0.8);
border-color: rgba(207, 143, 197, 0.6);
color: rgba(255, 255, 255, 1);
transform: scale(1);
}
100% {
background: rgba(0, 0, 0, 0.7);
border-color: rgba(255, 255, 255, 0.2);
color: rgba(255, 255, 255, 0.8);
transform: scale(1);
}
}

.copy-button:hover {
border-color: rgba(255, 255, 255, 0.4);
color: rgba(255, 255, 255, 1);
}

.copy-button:active {
transform: scale(0.95);
}
`,
],
})
export class CodeExampleComponent {
@Input() header: string = '';
codeBody = viewChild.required<ElementRef>('codeBody');
copied = signal(false);

copyCode() {
if (navigator.clipboard && window.isSecureContext) {
const codeText = this.codeBody().nativeElement.textContent?.trim() || '';
navigator.clipboard.writeText(codeText);
this.copied.set(true);
}
}

onAnimationEnd(event: AnimationEvent) {
this.copied.set(false);
}
}
64 changes: 64 additions & 0 deletions projects/www/src/app/components/docs/code-tabs.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
Component,
OnInit,
ElementRef,
inject,
signal,
viewChild,
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatTabsModule } from '@angular/material/tabs';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { CodeExampleComponent } from './code-example.component';

@Component({
selector: 'ngrx-code-tabs',
standalone: true,
imports: [CommonModule, MatTabsModule, CodeExampleComponent],
template: `
<div #content style="display: none"><ng-content></ng-content></div>

<mat-tab-group [preserveContent]="true">
@for (tab of tabs(); track tab) {
<mat-tab [label]="tab.header">
<ngrx-code-example [innerHTML]="tab.code"> </ngrx-code-example>
Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe not the best implementation, but it works.
The idea is to reuse the ngrx-code-example in the content, and re-render it here using it's own rendered HTML.
The ngrx-code-example is re-used here to have the copy functionality.

</mat-tab>
}
</mat-tab-group>
`,
styles: [
`
ngrx-code-example {
margin: 0;
}
`,
],
})
export class CodeTabsComponent implements OnInit {
private domSanitizer = inject(DomSanitizer);
private content = viewChild.required<ElementRef>('content');
protected tabs = signal<TabInfo[]>([]);

ngOnInit() {
const codeExamples =
this.content().nativeElement.querySelectorAll('ngrx-code-example') ?? [];
const examples: TabInfo[] = [...codeExamples].map((example) =>
this.extractTabInfo(example)
);
this.tabs.set(examples);
}

private extractTabInfo(tabContent: HTMLElement): TabInfo {
return {
code: this.domSanitizer.bypassSecurityTrustHtml(
tabContent.querySelector('pre')?.parentElement?.innerHTML ?? ''
),
header: tabContent.getAttribute('header') || '',
};
}
}

export interface TabInfo {
code: SafeHtml;
header: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ export class MarkdownArticleComponent implements OnDestroy {
}

private watchHeadings() {
console.log('watchHeadings');
if (this.intersectionObserver) {
this.intersectionObserver.disconnect();
this.intersectionObserver = undefined;
Expand Down
63 changes: 30 additions & 33 deletions projects/www/src/app/pages/guide/component-store/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,13 @@ You can see the full example at StackBlitz: <ngrx-docs-stackblitz name="componen

</ngrx-docs-alert>

<code-tabs linenums="true">
<code-pane
header="src/app/slide-toggle.component.ts"
path="component-store-slide-toggle/src/app/slide-toggle.component.ts">
</code-pane>
<code-pane
header="src/app/slide-toggle.html"
path="component-store-slide-toggle/src/app/slide-toggle.html">
</code-pane>
</code-tabs>
<ngrx-code-tabs>
<ngrx-code-example header="src/app/slide-toggle.component.ts">
</ngrx-code-example>

<ngrx-code-example header="src/app/slide-toggle.component.html">
</ngrx-code-example>
</ngrx-code-tabs>

Below are the steps of integrating `ComponentStore` into a component.

Expand Down Expand Up @@ -151,7 +148,7 @@ When it is called with a callback, the state is updated.
header="src/app/slide-toggle.component.ts"
path="component-store-slide-toggle/src/app/slide-toggle.component.ts"
region="init">

```ts
constructor(
private readonly componentStore: ComponentStore<SlideToggleState>
Expand All @@ -177,7 +174,7 @@ When a user clicks the toggle (triggering a 'change' event), instead of calling
header="src/app/slide-toggle.component.ts"
path="component-store-slide-toggle/src/app/slide-toggle.component.ts"
region="updater">

```ts
@Input() set checked(value: boolean) {
this.setChecked(value);
Expand Down Expand Up @@ -210,6 +207,21 @@ Finally, the state is aggregated with selectors into two properties:

This example does not have a lot of business logic, however it is still fully reactive.

<ngrx-code-tabs linenums="true">
<ngrx-code-example
header="PaginatorComponent with PaginatorStore Service"
path="component-store-paginator-service/src/app/paginator.component.ts">
</ngrx-code-example>
<ngrx-code-example
header="PaginatorComponent providing ComponentStore"
path="component-store-paginator/src/app/paginator.component.ts">
</ngrx-code-example>
<ngrx-code-example
header="src/app/paginator.store.ts"
path="component-store-paginator-service/src/app/paginator.store.ts">
</ngrx-code-example>
</ngrx-code-tabs>

### Example 2: Service extending ComponentStore

`SlideToggleComponent` is a fairly simple component and having `ComponentStore` within the component itself is still manageable. When components takes more Inputs and/or has more events within its template, it becomes larger and harder to read/maintain.
Expand Down Expand Up @@ -239,21 +251,6 @@ You can see the examples at StackBlitz:

</ngrx-docs-alert>

<code-tabs linenums="true">
<code-pane
header="PaginatorComponent with PaginatorStore Service"
path="component-store-paginator-service/src/app/paginator.component.ts">
</code-pane>
<code-pane
header="PaginatorComponent providing ComponentStore"
path="component-store-paginator/src/app/paginator.component.ts">
</code-pane>
<code-pane
header="src/app/paginator.store.ts"
path="component-store-paginator-service/src/app/paginator.store.ts">
</code-pane>
</code-tabs>

#### Updating the state

With `ComponentStore` extracted into `PaginatorStore`, the developer is now using updaters and effects to update the state. `@Input` values are passed directly into `updater`s as their arguments.
Expand Down Expand Up @@ -316,19 +313,19 @@ changePageSize(newPageSize: number) {

`PaginatorStore` exposes the two properties: `vm$` for an aggregated _ViewModel_ to be used in the template and `page$` that would emit whenever data aggregated from a `PageEvent` changes.

<code-tabs>
<code-pane
<ngrx-code-tabs>
<ngrx-code-example
header="src/app/paginator.component.ts"
path="component-store-paginator-service/src/app/paginator.component.ts"
region="selectors"
>
</code-pane>
<code-pane
</ngrx-code-example>
<ngrx-code-example
header="src/app/paginator.store.ts"
path="component-store-paginator-service/src/app/paginator.store.ts"
region="selectors">
</code-pane>
</code-tabs>
</ngrx-code-example>
</ngrx-code-tabs>

<ngrx-docs-alert type="help">

Expand Down
14 changes: 8 additions & 6 deletions projects/www/src/app/pages/guide/signals/signal-state.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ export class Counter {

### Example 2: SignalState in a Service

<code-tabs linenums="true">
<code-pane header="book-list-store.ts">

<ngrx-code-tabs linenums="true">
<ngrx-code-example header="books.store.ts">


```ts
import { inject, Injectable } from '@angular/core';
Expand Down Expand Up @@ -204,9 +206,9 @@ export class BookListStore {
}
```

</code-pane>
</ngrx-code-example>

<code-pane header="book-list.ts">
<ngrx-code-example header="book-list.ts">

```ts
import {
Expand Down Expand Up @@ -244,5 +246,5 @@ export class BookList {
}
```

</code-pane>
</code-tabs>
</ngrx-code-example>
</ngrx-code-tabs>
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
SignalStore allows defining private members that cannot be accessed from outside the store by using the `_` prefix.
This includes root-level state slices, properties, and methods.

<code-tabs linenums="false">
<code-pane header="counter-store.ts">
<ngrx-code-tabs linenums="false">
<ngrx-code-example header="counter-store.ts">


```ts
import { computed } from '@angular/core';
Expand Down Expand Up @@ -46,9 +47,10 @@ export const CounterStore = signalStore(
);
```

</code-pane>
</ngrx-code-example>

<ngrx-code-example header="counter.ts">

<code-pane header="counter.ts">

```ts
import { Component, inject, OnInit } from '@angular/core';
Expand All @@ -75,7 +77,8 @@ export class Counter implements OnInit {
this.store._increment2(); // ❌
}
}
}
```

</code-pane>
</code-tabs>
</ngrx-code-example>
</ngrx-code-tabs>