Skip to content

Added interactable terminal #50

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

Merged
merged 3 commits into from
Jun 8, 2025
Merged
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
1 change: 0 additions & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions frontend/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Image from 'next/image';
import Link from 'next/link';
import { useEffect, useState } from 'react';
import Hamburger from './Hamburger';
import Terminal from './Terminal';

const Navbar = () => {
const [path, setPath] = useState<string[]>([]);
Expand All @@ -13,22 +14,25 @@ const Navbar = () => {

return (
<nav className="sticky top-0 flex justify-between items-center relative z-10 shadow-lg rounded-md bg-black/15 backdrop-blur-md xl:px-24 md:px-10 px-5 py-6">
<Link href="/">
<Image
src="/assets/csesoc_logo.svg"
alt="CSESoc Logo"
width={200}
height={200}
draggable={false}
/>
<div>
<Link href="/">
<Image
src="/assets/csesoc_logo.svg"
alt="CSESoc Logo"
width={200}
height={200}
draggable={false}
/>
</Link>
<p className="font-mono mt-3 font-bold">
<span className="text-green-500">csesoc@unsw</span>
<span>:</span>
<span className="text-blue-500">~{path.map(segment => '/' + segment.toLowerCase())}</span>
<span>$ </span>
<span id="cursor" className="text-gray-400 inline-block animate-blink">_</span>
{/* The interactive terminal that allows changing pages using 'cd' */}
<Terminal/>
</p>
</Link>
</div>
<div>
<div className="md:flex xl:gap-18 lg:gap-10 md:gap-5 text-right font-bold hidden">
<Link href="/about">
Expand Down
100 changes: 100 additions & 0 deletions frontend/src/components/Terminal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { useRouter } from "next/router";
import { useRef, useState } from "react";

const Terminal = () => {
// Store the value of the input
const [value, setValue] = useState("");

// Automatically select the end of the input as the custom
// cursor only works at the end of the input.
const inputRef = useRef<HTMLInputElement>(null);
const setInputEnd = () => {
if (inputRef.current) {
const len = inputRef.current.value.length;
inputRef.current.setSelectionRange(len, len);
}
}

// Keep track of if the input is focused
const [inputFocused, setInputFocused] = useState(false);

// Using the router to change pages seamlessly
const router = useRouter();
const goToPage = (target: string) => {
router.push(target);
};

// Checking for "Enter" and if so, changing to
// the inputted page
const handleKey = (key: string) => {
if (key !== "Enter") return;

if (value.toLowerCase() === "~"
|| value.toLowerCase() === "cd"
|| value.toLowerCase() === "cd ~"
|| value.toLowerCase() === "cd .."
) {
goToPage("/");
} else if (value.toLowerCase() === "cd about"
|| value.toLowerCase() === "cd about us"
|| value.toLowerCase() === "cd about_us"
) {
goToPage("/about");
} else if (value.toLowerCase() === "cd events"
|| value.toLowerCase() === "cd event"
) {
goToPage("/events");
} else if (value.toLowerCase() === "cd resources"
|| value.toLowerCase() === "cd resource"
) {
goToPage("/resources");
} else if (value.toLowerCase() === "cd sponsors"
|| value.toLowerCase() === "cd sponsor"
) {
goToPage("/sponsors");
} else if (value.toLowerCase() === "cd contact"
|| value.toLowerCase() === "cd contacts"
|| value.toLowerCase() === "cd contact us"
|| value.toLowerCase() === "cd contact_us"
) {
goToPage("/contact-us");
}

clearInput()
};

const clearInput = () => {
setValue("");
};

return (
// Using relative + absolute to overlap the `input` and `span`
<span className="relative">
{/* The input */}
<input type="text" id="input" value={value} ref={inputRef} maxLength={40}
className="absolute text-blue-500 p-0 m-0 bg-transparent outline-none caret-transparent w-[50vw] z-10"
onKeyDown={(e) => {
handleKey(e.key)
setInputEnd()
}}
onChange={(e) => setValue(e.target.value)}
onFocus={() => setInputFocused(true)}
onBlur={() => {
clearInput()
setInputFocused(false)
}}
></input>
{/* The custom cursor */}
<span className="absolute w-[60vw] p-0 m-0 z-0">
{/* The invisable span that is the same length as the input */}
<span
className="invisible whitespace-pre pointer-events-none text-base"
>{value}</span>
{/* The custom cursor */}
<span id="cursor" className={`text-${inputFocused ? "white" : "gray-500"} pointer-events-none inline-block animate-blink p-0 m-0`}>_</span>
</span>
</span>
)
}

export default Terminal
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading