docs: finish transparent elements after demo

This commit is contained in:
Corbin Crutchley
2023-12-26 14:03:14 -08:00
parent 74ee5aad5d
commit efdce0850b
23 changed files with 16774 additions and 0 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,71 @@
{
"$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"
}
}
}
}
}
}

View File

@@ -0,0 +1,31 @@
{
"name": "@ffg-fundamentals/angular-transparent-files-after-51",
"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 Transparent Files After - Example #51 - FFG Fundamentals</title>
</head>
<body>
<file-list></file-list>
</body>
</html>

View File

@@ -0,0 +1,161 @@
import "zone.js";
import { bootstrapApplication } from "@angular/platform-browser";
import {
Component,
Input,
EventEmitter,
Output,
OnInit,
OnDestroy,
} from "@angular/core";
import { NgFor, NgIf, DatePipe } from "@angular/common";
@Component({
selector: "file-date",
standalone: true,
imports: [DatePipe],
template: `
<span [attr.aria-label]="inputDate | date: 'MMMM d, Y'">
{{ inputDate | date }}
</span>
`,
})
class FileDateComponent {
@Input() inputDate!: Date;
}
@Component({
selector: "file-item",
standalone: true,
imports: [FileDateComponent, NgIf],
template: `
<button
(click)="selected.emit()"
[style]="
isSelected
? 'background-color: blue; color: white'
: 'background-color: white; color: blue'
"
>
{{ fileName }}
<span *ngIf="isFolder; else fileDisplay">Type: Folder</span>
<ng-template #fileDisplay><span>Type: File</span></ng-template>
<file-date *ngIf="!isFolder" [inputDate]="inputDate" />
</button>
`,
})
class FileComponent implements OnInit, OnDestroy {
@Input() fileName!: string;
@Input() href!: string;
@Input() isSelected!: boolean;
@Input() isFolder!: boolean;
@Output() selected = new EventEmitter();
inputDate = new Date();
interval: any = null;
ngOnInit() {
// Check if it's a new day every 10 minutes
this.interval = setInterval(
() => {
const newDate = new Date();
if (this.inputDate.getDate() === newDate.getDate()) return;
this.inputDate = newDate;
},
10 * 60 * 1000,
);
}
ngOnDestroy() {
clearInterval(this.interval);
}
}
@Component({
selector: "file-list",
standalone: true,
imports: [FileComponent, NgFor, NgIf],
template: `
<div>
<button (click)="toggleOnlyShow()">Only show files</button>
<ul>
<ng-container
*ngFor="let file of filesArray; let i = index; trackBy: fileTrackBy"
>
<li *ngIf="onlyShowFiles ? !file.isFolder : true">
<file-item
(selected)="onSelected(i)"
[isSelected]="selectedIndex === i"
[fileName]="file.fileName"
[href]="file.href"
[isFolder]="file.isFolder"
/>
</li>
</ng-container>
</ul>
</div>
`,
})
class FileListComponent {
selectedIndex = -1;
fileTrackBy(index: number, file: File) {
return file.id;
}
onSelected(idx: number) {
if (this.selectedIndex === idx) {
this.selectedIndex = -1;
return;
}
this.selectedIndex = idx;
}
onlyShowFiles = false;
toggleOnlyShow() {
this.onlyShowFiles = !this.onlyShowFiles;
}
filesArray: File[] = [
{
fileName: "File one",
href: "/file/file_one",
isFolder: false,
id: 1,
},
{
fileName: "File two",
href: "/file/file_two",
isFolder: false,
id: 2,
},
{
fileName: "File three",
href: "/file/file_three",
isFolder: false,
id: 3,
},
{
fileName: "Folder one",
href: "/file/folder_one/",
isFolder: true,
id: 4,
},
{
fileName: "Folder two",
href: "/file/folder_two/",
isFolder: true,
id: 5,
},
];
}
interface File {
fileName: string;
href: string;
isFolder: boolean;
id: number;
}
bootstrapApplication(FileListComponent);

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

@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

View File

@@ -0,0 +1,9 @@
<html>
<head>
<title>React Transparent Files After - Example #51 - FFG Fundamentals</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>

View File

@@ -0,0 +1,19 @@
{
"name": "@ffg-fundamentals/react-transparent-files-after-51",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.0.4",
"vite": "^4.4.9"
}
}

View File

@@ -0,0 +1,158 @@
import { createRoot } from "react-dom/client";
import { useState, useEffect, useMemo, Fragment } from "react";
const FileDate = ({ inputDate }) => {
const dateStr = useMemo(() => formatDate(inputDate), [inputDate]);
const labelText = useMemo(() => formatReadableDate(inputDate), [inputDate]);
return <span aria-label={labelText}>{dateStr}</span>;
};
const File = ({ href, fileName, isSelected, onSelected, isFolder }) => {
const [inputDate, setInputDate] = useState(new Date());
useEffect(() => {
// Check if it's a new day every 10 minutes
const timeout = setTimeout(
() => {
const newDate = new Date();
if (inputDate.getDate() === newDate.getDate()) return;
setInputDate(newDate);
},
10 * 60 * 1000,
);
return () => clearTimeout(timeout);
}, [inputDate]);
return (
<button
onClick={onSelected}
style={
isSelected
? { backgroundColor: "blue", color: "white" }
: { backgroundColor: "white", color: "blue" }
}
>
{fileName}
{isFolder ? <span>Type: Folder</span> : <span>Type: File</span>}
{!isFolder && <FileDate inputDate={inputDate} />}
</button>
);
};
const filesArray = [
{
fileName: "File one",
href: "/file/file_one",
isFolder: false,
id: 1,
},
{
fileName: "File two",
href: "/file/file_two",
isFolder: false,
id: 2,
},
{
fileName: "File three",
href: "/file/file_three",
isFolder: false,
id: 3,
},
{
fileName: "Folder one",
href: "/file/folder_one/",
isFolder: true,
id: 4,
},
{
fileName: "Folder two",
href: "/file/folder_two/",
isFolder: true,
id: 5,
},
];
const FileList = () => {
const [selectedIndex, setSelectedIndex] = useState(-1);
const onSelected = (idx) => {
if (selectedIndex === idx) {
setSelectedIndex(-1);
return;
}
setSelectedIndex(idx);
};
const [onlyShowFiles, setOnlyShowFiles] = useState(false);
const toggleOnlyShow = () => setOnlyShowFiles(!onlyShowFiles);
return (
<div>
<button onClick={toggleOnlyShow}>Only show files</button>
<ul>
{filesArray.map((file, i) => (
<Fragment key={file.id}>
{(!onlyShowFiles || !file.isFolder) && (
<li>
<File
isSelected={selectedIndex === i}
onSelected={() => onSelected(i)}
fileName={file.fileName}
href={file.href}
isFolder={file.isFolder}
/>
</li>
)}
</Fragment>
))}
</ul>
</div>
);
};
createRoot(document.getElementById("root")).render(<FileList />);
function formatDate(inputDate) {
// Month starts at 0, annoyingly
const month = inputDate.getMonth() + 1;
const date = inputDate.getDate();
const year = inputDate.getFullYear();
return month + "/" + date + "/" + year;
}
function formatReadableDate(inputDate) {
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const monthStr = months[inputDate.getMonth()];
const dateSuffixStr = dateSuffix(inputDate.getDate());
const yearNum = inputDate.getFullYear();
return monthStr + " " + dateSuffixStr + "," + yearNum;
}
function dateSuffix(dayNumber) {
const lastDigit = dayNumber % 10;
if (lastDigit == 1 && dayNumber != 11) {
return dayNumber + "st";
}
if (lastDigit == 2 && dayNumber != 12) {
return dayNumber + "nd";
}
if (lastDigit == 3 && dayNumber != 13) {
return dayNumber + "rd";
}
return dayNumber + "th";
}

View File

@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
});

View File

@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

View File

@@ -0,0 +1,9 @@
<html>
<head>
<title>Vue Transparent Files After - Example #51 - FFG Fundamentals</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>

View File

@@ -0,0 +1,18 @@
{
"name": "@ffg-fundamentals/vue-transparent-files-after-51",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.3.4"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.2.3",
"vite": "^4.4.9"
}
}

View File

@@ -0,0 +1,44 @@
<!-- File.vue -->
<script setup>
import { ref, onMounted, onUnmounted } from "vue";
import FileDate from "./FileDate.vue";
const props = defineProps(["isSelected", "isFolder", "fileName", "href"]);
const emit = defineEmits(["selected"]);
const inputDate = ref(new Date());
const interval = ref(null);
onMounted(() => {
// Check if it's a new day every 10 minutes
interval.value = setInterval(
() => {
const newDate = new Date();
if (inputDate.value.getDate() === newDate.getDate()) return;
inputDate.value = newDate;
},
10 * 60 * 1000,
);
});
onUnmounted(() => {
clearInterval(interval.value);
});
</script>
<template>
<button
v-on:click="emit('selected')"
:style="
isSelected
? 'background-color: blue; color: white'
: 'background-color: white; color: blue'
"
>
{{ fileName }}
<span v-if="isFolder">Type: Folder</span>
<span v-else>Type: File</span>
<FileDate v-if="!isFolder" :inputDate="inputDate" />
</button>
</template>

View File

@@ -0,0 +1,56 @@
<!-- FileDate.vue -->
<script setup>
import { ref, watch, computed } from "vue";
function formatDate(inputDate) {
// Month starts at 0, annoyingly
const monthNum = inputDate.getMonth() + 1;
const dateNum = inputDate.getDate();
const yearNum = inputDate.getFullYear();
return monthNum + "/" + dateNum + "/" + yearNum;
}
function formatReadableDate(inputDate) {
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const monthStr = months[inputDate.getMonth()];
const dateSuffixStr = dateSuffix(inputDate.getDate());
const yearNum = inputDate.getFullYear();
return monthStr + " " + dateSuffixStr + "," + yearNum;
}
function dateSuffix(dayNumber) {
const lastDigit = dayNumber % 10;
if (lastDigit == 1 && dayNumber != 11) {
return dayNumber + "st";
}
if (lastDigit == 2 && dayNumber != 12) {
return dayNumber + "nd";
}
if (lastDigit == 3 && dayNumber != 13) {
return dayNumber + "rd";
}
return dayNumber + "th";
}
const props = defineProps(["inputDate"]);
const dateStr = computed(() => formatDate(props.inputDate));
const labelText = computed(() => formatReadableDate(props.inputDate));
</script>
<template>
<span :aria-label="labelText">{{ dateStr }}</span>
</template>

View File

@@ -0,0 +1,73 @@
<!-- FileList.vue -->
<script setup>
import { ref } from "vue";
import File from "./File.vue";
const filesArray = [
{
fileName: "File one",
href: "/file/file_one",
isFolder: false,
id: 1,
},
{
fileName: "File two",
href: "/file/file_two",
isFolder: false,
id: 2,
},
{
fileName: "File three",
href: "/file/file_three",
isFolder: false,
id: 3,
},
{
fileName: "Folder one",
href: "/file/folder_one/",
isFolder: true,
id: 4,
},
{
fileName: "Folder two",
href: "/file/folder_two/",
isFolder: true,
id: 5,
},
];
const selectedIndex = ref(-1);
function onSelected(idx) {
if (selectedIndex.value === idx) {
selectedIndex.value = -1;
return;
}
selectedIndex.value = idx;
}
const onlyShowFiles = ref(false);
function toggleOnlyShow() {
onlyShowFiles.value = !onlyShowFiles.value;
}
</script>
<template>
<div>
<button @click="toggleOnlyShow()">Only show files</button>
<ul>
<template v-for="(file, i) of filesArray" :key="file.id">
<li v-if="onlyShowFiles ? !file.isFolder : true">
<File
@selected="onSelected(i)"
:isSelected="selectedIndex === i"
:fileName="file.fileName"
:href="file.href"
:isFolder="file.isFolder"
/>
</li>
</template>
</ul>
</div>
</template>

View File

@@ -0,0 +1,5 @@
// main.js
import { createApp } from "vue";
import FileList from "./FileList.vue";
createApp(FileList).mount("#root");

View File

@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
export default defineConfig({
plugins: [vue()],
});