Skip to content

Commit

Permalink
added hover, and edited font size in Button element (no self-resizing)
Browse files Browse the repository at this point in the history
  • Loading branch information
khanheric2003 committed Feb 11, 2024
2 parents 5631e95 + b6bc532 commit 54dd3cc
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.21.4",
"@types/jest": "^29.5.0",
"@types/react": "^18.0.27",
"@types/react": "^18.2.51",
"@types/react-dom": "^18.0.10",
"@types/react-test-renderer": "^18.0.0",
"@vitejs/plugin-react": "^3.1.0",
Expand Down
12 changes: 10 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { createBrowserRouter, RouterProvider } from "react-router-dom";

// routes
import NavigaionTest from "./routes/NavigationTest";
import Test from "@/routes/Test";

import ButtonTestRoute from "./routes/ButtonTestRoute";

import Test from "./routes/Test";
import DropdownItem from "./components/DropdownItem";
import SortDropdownListTestRoute from "./routes/SortDropdownListTestRoute";
import NavigaionTest from "./routes/NavigationTest";

const router = createBrowserRouter([
{
path: "/",
Expand All @@ -18,6 +22,10 @@ const router = createBrowserRouter([
path: "/testButton",
element: <ButtonTestRoute />,
},
{
path: "/SortDropdownListTestRoute",
element: <SortDropdownListTestRoute />,
},
{
path: "/nav-test",
element: <NavigaionTest />,
Expand Down
28 changes: 6 additions & 22 deletions src/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState } from "react";
import React, { useRef } from "react";
import { Plus } from "@phosphor-icons/react";

interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
Expand All @@ -8,6 +8,7 @@ interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
rounded?: boolean;
fill?: boolean;
status?: boolean;
fontSize?: string;
}

const Button: React.FC<ButtonProps> = ({
Expand All @@ -17,39 +18,22 @@ const Button: React.FC<ButtonProps> = ({
rounded = false,
fill = true,
status = true,
fontSize = "1em",
}) => {
const buttonRef = useRef<HTMLButtonElement>(null);
const [fontSize, setFontSize] = useState("1em");

useEffect(() => {
if (text) {
const width = buttonRef.current?.offsetWidth;

// size adjustment could be error since hand adjusted it
// probably adjust it later or ask for advise
if (width) {
if (width > 200) {
setFontSize(`${width / 14}px`);
} else if (width > 100) {
setFontSize(`${width / 11}px`);
} else {
setFontSize(`${width / 7}px`);
}
}
}
}, [text]);
const buttonClassName = `flex flex-row items-center justify-center p-4 ${
!status
? "bg-neutrals-light-200 text-gray-400"
: fill
? "bg-primary-main text-white"
: "text-primary-dark border-primary-dark border-2"
? "bg-primary-main text-white hover:bg-primary-light active:bg-primary-dark "
: "text-primary-dark border-primary-dark border-2 hover:bg-neutrals-dark-100 active:bg-primary-dark active:text-white "
} ${rounded ? "rounded-full" : "rounded-lg"} w-full h-full`;

return (
<button
ref={buttonRef}
style={{ fontSize }}
style={{ fontSize: fontSize }}
onClick={onClick}
className={buttonClassName}
disabled={!status}
Expand Down
18 changes: 18 additions & 0 deletions src/components/DropdownItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// As shown in TestSquareProps
export interface DropdownItemProps {
text: string;
isSelected: boolean;
onSelect: () => void;
}

function DropdownItem({ text, isSelected, onSelect }:DropdownItemProps) {
return (
<li
className={`py-1 pr-4 pl-2 rounded-lg h-8 hover:bg-neutrals-light-500 cursor-pointer ${isSelected ? 'font-bold' : 'font-normal'}`} onClick={onSelect}>

<a className=" text-neutrals-dark-500 font-display">{text}</a>
</li>
);
}

export default DropdownItem;
82 changes: 82 additions & 0 deletions src/components/SortDropdownList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { useEffect, useRef, useState } from "react";
import DropdownItem from "./DropdownItem";
import { CaretDown } from "@phosphor-icons/react";

function SortDropdownList() {
// Options that can be later add in
const items = [
"Newest to Oldest",
"Oldest to Newest",
"Most-Least Storage",
"Recently Updated",
];

// State to keep track of the currently selected option
const [selectedItem, setSelectedItem] = useState<string | null>(
null
); // useState expect either string or null var

// State to keep track of the menu is being open or not
const [open, setOpen] = useState(false);

// Check if user click outside of the menu to close it
let menuRef = useRef<HTMLInputElement>(null); // Typescript like this

useEffect(() => {
let handler = (e: any) => {
if (menuRef.current && !menuRef.current.contains(e.target)) {
// Check menuRef.current is not null before calling .contains
setOpen(false);
}
};

document.addEventListener("mousedown", handler);

return () => {
// Clean up to prevent memory leaks and avoid running outdated event handlers
document.removeEventListener("mousedown", handler);
};
}, []);

return (
<div className="relative w-full" ref={menuRef}>
{/* For the menu trigger */}
<div
className={`w-full cursor-pointer font-body h-10 bg-status-blue-light flex justify-between items-center px-4 py-2 ${
open ? "rounded-t-lg mb-2" : "rounded-lg"
} `}
onClick={() => setOpen(!open)}
>
Sort
{/* The arrow symbol */}
<CaretDown size={24} />
</div>

{/* For the dropdown options */}
<div
className={`bg-status-blue-light p-4 rounded-b-lg z-10 absolute w-full ${
open ? "visible opacity-1" : "invisible opacity-0"
}`}
>
<div className={`flex flex-col gap-2`}>
<ul>
{items.map((item, index) => (
<DropdownItem
key={index}
text={item}
isSelected={selectedItem === item} // Check if matched with the selected option
onSelect={() => setSelectedItem(item)} // set the selectedItem to that option and when it re-render it will be check by the line above
/>
))}
</ul>
{/* for fun */}
<button className="text-white border bg-primary-main">
Reset
</button>
</div>
</div>
</div>
);
}

export default SortDropdownList;
31 changes: 16 additions & 15 deletions src/routes/ButtonTestRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ function ButtonTestRoute() {
return (
<div className="space-x-4 flex flex-row">
<div className="space-y-4 flex flex-col justify-center items-center min-h-screen">
<div className="w-64 h-12">
{/* Put font size inside, depending on you */}
<div style={{ fontSize: "20px" }} className=" w-72 h-64">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -16,7 +17,7 @@ function ButtonTestRoute() {
icon={true}
/>
</div>
<div className="w-48 h-12">
<div className="text-sm w-48 h-12">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -28,7 +29,7 @@ function ButtonTestRoute() {
icon={false}
/>
</div>
<div className="w-22 h-8">
<div className="text-sm w-22 h-8">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -40,7 +41,7 @@ function ButtonTestRoute() {
icon={true}
/>
</div>
<div className="w-22 h-8">
<div className="text-sm w-22 h-8">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -52,7 +53,7 @@ function ButtonTestRoute() {
icon={false}
/>
</div>
<div className="w-12 h-12">
<div className="text-sm w-12 h-12">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -64,7 +65,7 @@ function ButtonTestRoute() {
icon={true}
/>
</div>
<div className="w-12 h-12">
<div className="text-sm w-12 h-12">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -78,7 +79,7 @@ function ButtonTestRoute() {
</div>
</div>
<div className="space-y-4 flex flex-col justify-center items-center min-h-screen">
<div className="w-64 h-12">
<div className="text-sm w-64 h-12">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -90,7 +91,7 @@ function ButtonTestRoute() {
icon={true}
/>
</div>
<div className="w-48 h-12">
<div className="text-sm w-48 h-12">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -102,7 +103,7 @@ function ButtonTestRoute() {
icon={false}
/>
</div>
<div className="w-48 h-12">
<div className="text-sm w-48 h-12">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -114,7 +115,7 @@ function ButtonTestRoute() {
icon={true}
/>
</div>
<div className="w-12 h-12">
<div className="text-sm w-12 h-12">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -126,7 +127,7 @@ function ButtonTestRoute() {
icon={true}
/>
</div>
<div className="w-12 h-12">
<div className="text-sm w-12 h-12">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -140,7 +141,7 @@ function ButtonTestRoute() {
</div>
</div>
<div className="space-y-4 flex flex-col justify-center items-center min-h-screen">
<div className="w-64 h-12">
<div className="text-sm w-64 h-12">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -152,7 +153,7 @@ function ButtonTestRoute() {
icon={true}
/>
</div>
<div className="w-48 h-12">
<div className="text-sm w-48 h-12">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -164,7 +165,7 @@ function ButtonTestRoute() {
icon={false}
/>
</div>
<div className="w-12 h-12">
<div className="text-sm w-12 h-12">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand All @@ -176,7 +177,7 @@ function ButtonTestRoute() {
icon={true}
/>
</div>
<div className="w-12 h-12">
<div className="text-sm w-12 h-12">
<Button
onClick={function (): void {
throw new Error("Function not implemented.");
Expand Down
19 changes: 19 additions & 0 deletions src/routes/SortDropdownListTestRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import DropdownItem from "../components/DropdownItem";
import SortDropdownList from "../components/SortDropdownList";

function SortDropdownListTestRoute() {
return (
<div className="flex flex-col items-center justify-center min-h-screen">
<div className="text-h2">Hello</div>
<div className="text-body-reg">
Is this the sort dropdown list that you looking for?😩
</div>
<div className="w-1/4">
<SortDropdownList />
</div>
<div>What happen If I do this?</div>
</div>
);
}

export default SortDropdownListTestRoute;

0 comments on commit 54dd3cc

Please sign in to comment.