docs: add Angular comp ref challenge example

This commit is contained in:
Corbin Crutchley
2024-01-21 00:14:29 -08:00
parent 433b623c81
commit d6b0806a1a
9 changed files with 13140 additions and 1 deletions

View File

@@ -0,0 +1,42 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
# Compiled output
/dist
/tmp
/out-tsc
/bazel-out
# Node
/node_modules
npm-debug.log
yarn-error.log
# IDEs and editors
.idea/
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# Visual Studio Code
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*
# Miscellaneous
/.angular/cache
.sass-cache/
/connect.lock
/coverage
/libpeerconnection.log
testem.log
/typings
# System files
.DS_Store
Thumbs.db

View File

@@ -0,0 +1,74 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"DemoApp": {
"projectType": "application",
"schematics": {},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/demo-app",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": ["zone.js"],
"tsConfig": "tsconfig.app.json",
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"outputHashing": "all"
},
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"browserTarget": "DemoApp:build:production"
},
"development": {
"browserTarget": "DemoApp:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "DemoApp:build"
}
}
}
}
},
"cli": {
"analytics": false
}
}

View File

@@ -0,0 +1,31 @@
{
"name": "@ffg-fundamentals/angular-comp-ref-challenge-69",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"dev": "ng serve",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development"
},
"private": true,
"dependencies": {
"@angular/animations": "^16.2.0",
"@angular/common": "^16.2.0",
"@angular/compiler": "^16.2.0",
"@angular/core": "^16.2.0",
"@angular/forms": "^16.2.0",
"@angular/platform-browser": "^16.2.0",
"@angular/platform-browser-dynamic": "^16.2.0",
"@angular/router": "^16.2.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.13.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "^16.2.10",
"@angular/cli": "^16.2.10",
"@angular/compiler-cli": "^16.2.0",
"typescript": "~5.1.3"
}
}

View File

@@ -0,0 +1,8 @@
<html>
<head>
<title>Angular Comp Ref Challenge - Example #69 - FFG Fundamentals</title>
</head>
<body>
<app-root></app-root>
</body>
</html>

View File

@@ -0,0 +1,129 @@
import "zone.js";
import { bootstrapApplication } from "@angular/platform-browser";
import {
Component,
EventEmitter,
Input,
OnDestroy,
OnInit,
Output,
ViewChild,
} from "@angular/core";
import { NgIf } from "@angular/common";
@Component({
selector: "app-layout",
standalone: true,
template: `
<div style="display: flex; flex-wrap: nowrap; min-height: 100vh">
<div
[style]="
'width: ' +
sidebarWidth +
'px;' +
'height: 100vh;' +
'overflow-y: scroll;' +
'border-right: 2px solid #bfbfbf;'
"
>
<ng-content select="[sidebar]" />
</div>
<div style="width: 1px; flex-grow: 1">
<ng-content />
</div>
</div>
`,
})
class LayoutComponent {
@Input() sidebarWidth!: number;
}
@Component({
selector: "app-sidebar",
standalone: true,
imports: [NgIf],
template: `
<button *ngIf="isCollapsed" (click)="toggleCollapsed()">Toggle</button>
<div *ngIf="!isCollapsed">
<button (click)="toggleCollapsed()">Toggle</button>
<ul style="padding: 1rem">
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
<li>List item 5</li>
<li>List item 6</li>
</ul>
</div>
`,
})
class SidebarComponent {
@Output() toggle = new EventEmitter<boolean>();
isCollapsed = false;
setAndToggle(v: boolean) {
this.isCollapsed = v;
this.toggle.emit(v);
}
collapse() {
this.setAndToggle(true);
}
expand() {
this.setAndToggle(false);
}
toggleCollapsed() {
this.setAndToggle(!this.isCollapsed);
}
}
@Component({
selector: "app-root",
standalone: true,
imports: [LayoutComponent, SidebarComponent],
template: `
<app-layout [sidebarWidth]="width">
<app-sidebar #sidebar sidebar (toggle)="onToggle($event)" />
<p style="padding: 1rem">Hi there!</p>
</app-layout>
`,
})
class AppComponent implements OnInit, OnDestroy {
@ViewChild("sidebar", { static: true }) sidebar!: SidebarComponent;
collapsedWidth = 100;
expandedWidth = 150;
widthToCollapseAt = 600;
width = this.expandedWidth;
onToggle(isCollapsed: boolean) {
if (isCollapsed) {
this.width = this.collapsedWidth;
return;
}
this.width = this.expandedWidth;
}
onResize = () => {
if (window.innerWidth < this.widthToCollapseAt) {
this.sidebar.collapse();
} else if (this.sidebar.isCollapsed) {
this.sidebar.expand();
}
};
ngOnInit() {
window.addEventListener("resize", this.onResize);
}
ngOnDestroy() {
window.removeEventListener("resize", this.onResize);
}
}
bootstrapApplication(AppComponent);

View File

@@ -0,0 +1,10 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": []
},
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"]
}

View File

@@ -0,0 +1,30 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "ES2022",
"module": "ES2022",
"useDefineForClassFields": false,
"lib": ["ES2022", "dom"]
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}

View File

@@ -1,6 +1,6 @@
<html>
<head>
<title>Vue Expose Comp Ref - Example #67c - FFG Fundamentals</title>
<title>Vue Focused Comp Ref - Example #68 - FFG Fundamentals</title>
</head>
<body>
<div id="root"></div>