-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
timdeschryver
wants to merge
3
commits into
main
Choose a base branch
from
docs/code-blocks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
projects/www/src/app/components/docs/code-tabs.component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,16 +82,13 @@ You can see the full example at StackBlitz: <live-example name="component-store- | |
|
||
</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. | ||
|
||
|
@@ -102,9 +99,6 @@ First, the state for the component needs to be identified. In `SlideToggleCompon | |
<ngrx-code-example header="src/app/slide-toggle.component.ts" | ||
path="component-store-slide-toggle/src/app/slide-toggle.component.ts" | ||
region="state"> | ||
|
||
`ts` | ||
|
||
</ngrx-code-example> | ||
|
||
Then we need to provide `ComponentStore` in the component's providers, so that each new instance of `SlideToggleComponent` has its own `ComponentStore`. It also has to be injected into the constructor. | ||
|
@@ -119,9 +113,6 @@ In this example `ComponentStore` is provided directly in the component. This wor | |
header="src/app/slide-toggle.component.ts" | ||
path="component-store-slide-toggle/src/app/slide-toggle.component.ts" | ||
region="providers"> | ||
|
||
`ts` | ||
|
||
</ngrx-code-example> | ||
|
||
Next, the default state for the component needs to be set. It could be done lazily, however it needs to be done before any of `updater`s are executed, because they rely on the state to be present and would throw an error if the state is not initialized by the time they are invoked. | ||
|
@@ -141,9 +132,6 @@ When it is called with a callback, the state is updated. | |
<ngrx-code-example header="src/app/slide-toggle.component.ts" | ||
path="component-store-slide-toggle/src/app/slide-toggle.component.ts" | ||
region="init"> | ||
|
||
`ts` | ||
|
||
</ngrx-code-example> | ||
|
||
#### Step 2. Updating state | ||
|
@@ -158,9 +146,6 @@ 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` | ||
|
||
</ngrx-code-example> | ||
|
||
#### Step 3. Reading the state | ||
|
@@ -173,13 +158,25 @@ Finally, the state is aggregated with selectors into two properties: | |
<ngrx-code-example header="src/app/slide-toggle.component.ts" | ||
path="component-store-slide-toggle/src/app/slide-toggle.component.ts" | ||
region="selector"> | ||
|
||
`ts` | ||
|
||
</ngrx-code-example> | ||
|
||
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. | ||
|
@@ -209,31 +206,13 @@ 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> | ||
|
||
Comment on lines
-242
to
-256
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is order of code examples changed here? |
||
#### 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. | ||
|
||
<ngrx-code-example header="src/app/paginator.store.ts" | ||
path="component-store-paginator-service/src/app/paginator.component.ts" | ||
region="inputs"> | ||
|
||
`ts` | ||
|
||
</ngrx-code-example> | ||
|
||
Not all `updater`s have to be called in the `@Input`. For example, `changePageSize` is called from the template. | ||
|
@@ -243,28 +222,25 @@ Effects are used to perform additional validation and get extra information from | |
<ngrx-code-example header="src/app/paginator.store.ts" | ||
path="component-store-paginator-service/src/app/paginator.component.ts" | ||
region="updating-state"> | ||
|
||
`ts` | ||
|
||
</ngrx-code-example> | ||
|
||
#### Reading the state | ||
|
||
`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-examp> | ||
timdeschryver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<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"> | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.