Skip to content

Commit

Permalink
updated render cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
PrathamLalwani committed Nov 27, 2023
1 parent 82fab63 commit b0cc279
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 57 deletions.
10 changes: 7 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="pragma" content="no-cache" />
<meta
http-equiv="Cache-Control"
content="no-cache, no-store, must-revalidate"
/>
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

<!-- https://github.com/aws/aws-amplify/issues/678 fix: -->
<script>
window.global = window;
Expand Down
21 changes: 13 additions & 8 deletions src/components/Project.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Props = {
const Project = (props: Props) => {
const getImgUrl = (name: string) =>
new URL(`../assets/ProjectPictures/${name}`, import.meta.url).href;
const [overflowActive, setOverflowActive] = useState<boolean>(false);
const [overflowActive, setOverflowActive] = useState<boolean>(true);
const [showMore, setShowMore] = useState<boolean>(false);
const overflowingText = useRef<HTMLSpanElement | null>(null);
const checkOverflow = (textContainer: HTMLSpanElement | null): boolean => {
Expand All @@ -35,8 +35,7 @@ const Project = (props: Props) => {
return;
}
setOverflowActive(false);
}, [overflowActive]);

}, [overflowActive, window.innerWidth]);
return (
<div className="m-5 flex h-full w-max flex-col items-center rounded-2xl border-2 border-slate-400/50 bg-background-50 bg-opacity-10 px-3 py-5 backdrop-blur-md transition-all ease-in-out sm:p-5 md:items-start">
<div className="h-52 w-52 shrink-0 rounded-md sm:h-64 sm:w-64 md:h-96 md:w-96 ">
Expand All @@ -52,12 +51,12 @@ const Project = (props: Props) => {
{" "}
<span
ref={overflowingText}
className={`relative line-clamp-2 w-full overflow-hidden overflow-ellipsis text-sm leading-6 text-text sm:text-base ${
className={`relative line-clamp-2 w-full overflow-hidden overflow-ellipsis text-sm text-text sm:text-base ${
showMore
? "line-clamp-none h-fit overflow-visible"
: overflowActive
? "h-12"
: "h-[4.5rem]"
? "h-10 sm:h-12"
: "h-10 sm:h-[4.5rem]"
}`}
>
{props.description}
Expand All @@ -74,14 +73,16 @@ const Project = (props: Props) => {
{props.techStack.map((tech) => (
<li
key={tech}
className="text-md my-2 mr-1 max-w-full rounded-lg px-2 py-1 text-text"
className="text-md my-2 mr-1 w-max rounded-lg px-2 py-1 text-text"
title={tech}
>
{
<SvgIcon
key={tech}
iconName={tech}
size={0}
size={40}
onError={console.error}
onCompleted={console.log}
className=" h-5 w-5 fill-text sm:h-6 sm:w-6"
/>
}
Expand All @@ -95,6 +96,8 @@ const Project = (props: Props) => {
<SvgIcon
iconName="GitHub"
size={24}
onError={console.error}
onCompleted={console.log}
className="fill-secondary-950"
></SvgIcon>
</a>
Expand All @@ -105,6 +108,8 @@ const Project = (props: Props) => {
<SvgIcon
iconName="live"
size={24}
onError={console.error}
onCompleted={console.log}
className="fill-secondary-950"
></SvgIcon>
</a>
Expand Down
58 changes: 28 additions & 30 deletions src/components/SvgIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
/// <reference types="vite-plugin-svgr/client" />
import { useEffect, useState } from "react";
import { useDynamicSvgImport } from "./useDynamicSvgImport";
import { useEffect, useState } from "react";
interface IProps {
iconName: string;
size: number;
onCompleted: (
name: string,
SvgIcon: React.FC<React.SVGProps<SVGSVGElement>> | undefined,
) => void;
onError: (err: unknown) => void;
className?: string;
}

const SvgIcon = (props: IProps) => {
const { iconName, size, className } = props;

const { error, loading, SvgIcon } = useDynamicSvgImport(iconName);
const [isMounted, setIsMounted] = useState(false);
const { iconName, onCompleted, onError, size, className } = props;
const [refreshed, setRefreshed] = useState<boolean>(false);
useEffect(() => {
if (SvgIcon) {
setIsMounted(true);
} else {
setIsMounted(false);
}
}, [SvgIcon, iconName]);
setTimeout(() => {
setRefreshed(true);
}, 1);
}, []);

return (
<>
{loading && (
<div className="h-8 w-8 animate-pulse rounded-full bg-slate-400">
{iconName}
</div>
)}
{error && (
<div className="h-8 w-8 animate-pulse rounded-full bg-slate-400">
{iconName}
</div>
)}
{SvgIcon && isMounted ? (
<SvgIcon className={className} width={size} height={size} />
) : (
iconName
)}
</>
);
const { error, loading, SvgIcon } = useDynamicSvgImport(iconName, {
onCompleted,
onError,
});
if (error) {
return <p>error</p>;
}
if (loading) {
return <p>Loading...</p>;
}
if (SvgIcon) {
return (
refreshed && <SvgIcon width={size} height={size} className={className} />
);
}
return null;
};

export default SvgIcon;
43 changes: 27 additions & 16 deletions src/components/useDynamicSvgImport.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
/// <reference types="vite-plugin-svgr/client" />
import React, { useEffect, useRef, useState } from "react";

export function useDynamicSvgImport(iconName: string) {
const importedIconRef = useRef<React.FC<React.SVGProps<SVGElement>>>();
interface UseDynamicSVGImportOptions {
onCompleted?: (
name: string,
SvgIcon: React.FC<React.SVGProps<SVGSVGElement>> | undefined,
) => void;
onError?: (err: unknown) => void;
}
export function useDynamicSvgImport(
iconName: string,
options: UseDynamicSVGImportOptions,
) {
const ImportedIconRef = useRef<React.FC<React.SVGProps<SVGSVGElement>>>();

const [loading, setLoading] = useState(false);
const [error, setError] = useState<unknown>(null);
const [error, setError] = useState<unknown>();

const { onCompleted, onError } = options;
useEffect(() => {
setLoading(true);
const importSvgIcon = async (): Promise<void> => {
const importIcon = async () => {
try {
importedIconRef.current = (
ImportedIconRef.current = (
await import(`../assets/techicons/${iconName}.svg`)
).default;
console.log(ImportedIconRef.current);
if (onCompleted) {
onCompleted(iconName, ImportedIconRef.current);
}
} catch (err) {
if (onError) {
onError(err);
}
setError(err);
console.error(err);
} finally {
setLoading(false);
if (importedIconRef.current === undefined) {
console.error(`Icon ${iconName} not found`);
} else {
console.log(`Icon ${iconName} loaded`);
console.log(importedIconRef.current);
}
}
};
importIcon();
}, [iconName, onCompleted, onError]);

importSvgIcon();
}, [iconName]);

return { error, loading, SvgIcon: importedIconRef.current };
return { error, loading, SvgIcon: ImportedIconRef.current };
}

0 comments on commit b0cc279

Please sign in to comment.