Files
dx4/src/trash/trash-components.jsx
T
2025-11-29 20:53:09 +03:00

302 lines
9.9 KiB
React

import React, { useEffect, useState } from "react";
import { BiCheck, BiLeftArrowAlt, BiRightArrowAlt, BiX } from "react-icons/bi";
import { formatDate, getDateTags } from "./dengi.js";
import { AnimatePresence, motion, LayoutGroup } from "motion/react";
export function Switch({
checked,
defaultChecked = false,
onChange,
disabled = false,
ariaLabel,
className = "",
size = "md", // "sm" | "md" | "lg"
}) {
const isControlled = checked !== undefined;
const [internalChecked, setInternalChecked] = useState(defaultChecked);
const current = isControlled ? checked : internalChecked;
useEffect(() => {
if (!isControlled) return;
setInternalChecked(checked);
}, [checked, isControlled]);
const sizes = {
sm: { track: "w-9 h-5", thumb: "w-3 h-3", translate: "translate-x-4" },
md: { track: "w-11 h-6", thumb: "w-4 h-4", translate: "translate-x-5" },
lg: { track: "w-14 h-8", thumb: "w-6 h-6", translate: "translate-x-6" },
};
const s = sizes[size] || sizes.md;
const toggle = (next) => {
if (disabled) return;
const newState = typeof next === "boolean" ? next : !current;
if (!isControlled) setInternalChecked(newState);
onChange?.(newState);
};
return (
<button
type="button"
role="switch"
aria-checked={current}
aria-label={ariaLabel}
disabled={disabled}
tabIndex={disabled ? -1 : 0}
onClick={() => toggle()}
className={[
"inline-flex items-center p-0 focus:outline-none",
// focus-visible: shows ring only when focused by keyboard (better UX)
"focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-offset-0.5 focus-visible:ring-offset-transparent",
disabled ? "opacity-60" : "cursor-pointer",
current ? "bg-zinc-500" : "bg-gray-300/10",
"rounded-full transition-colors duration-150",
s.track,
className,
].join(" ")}
>
<span
className={[
"relative block rounded-full bg-white shadow transform transition-transform ease-out duration-150",
s.thumb,
current ? s.translate : "",
size === "sm" ? "m-1" : "m-1",
].join(" ")}
/>
</button>
);
}
export function LoadingSpinner(props) {
return (
<svg
aria-hidden="true"
className="animate-spin ease-out text-zinc-600 fill-white"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
width={props.size}
height={props.size}
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
);
}
function DatePickerButton({ next = false, date, setDate }) {
return (
<span
className="cursor-pointer lg:hover:*:fill-black lg:hover:bg-white transition *:transition-colors rounded-full"
onClick={() => {
const d = new Date(date);
d.setDate(d.getDate() + (next ? 1 : -1));
setDate(d);
}}
>
{!next ? <BiLeftArrowAlt size={34} /> : <BiRightArrowAlt size={34} />}
</span>
);
}
export const DatePicker = React.memo((props) => {
const dateTag = getDateTags(props.date);
return (
<LayoutGroup>
<div className="flex items-center justify-between px-2 py-1 w-full bg-grain border-zinc-800/40 border rounded-2xl relative">
<DatePickerButton date={props.date} setDate={props.setDate} />
<AnimatePresence mode="popLayout">
<motion.span
key={formatDate(props.date)}
className="cursor-pointer flex items-center gap-1 justify-center w-[80%] text-zinc-300 lg:hover:text-black *:transition-colors transition-colors p-1 rounded-3xl lg:hover:bg-white px-3 overflow-hidden lg:hover:*:text-black"
variants={{
a: { opacity: 0, filter: "blur(1px)" },
b: { opacity: 1, filter: "blur(0px)" },
}}
initial="a"
animate="b"
exit="a"
onClick={() => {
props.setDate(new Date());
}}
>
<motion.span
className={
"block min-w-2 min-h-2 rounded-full mx-1 transition-colors" +
(props.indicator
? " bg-zinc-300 animate-pulse"
: " bg-transparent")
}
layoutId="datepicker_indicator"
layout="position"
/>
<motion.span
layoutId="datepicker_date"
layout="position"
className="font-semibold"
>
{formatDate(props.date)}
</motion.span>
{dateTag.length > 0 && (
<>
<motion.div
className="flex"
layoutId="datepicker_date_div"
layout="position"
>
<span className="h-[1px] -rotate-50 bg-zinc-800 w-[20px] " />
</motion.div>
<motion.span
initial={{ x: 10, opacity: 0 }}
transition={{ delay: 0.1 }}
animate={{ x: 0, opacity: 1 }}
className="text-zinc-600 italic"
>
{dateTag}
</motion.span>
</>
)}
</motion.span>
</AnimatePresence>
<DatePickerButton next date={props.date} setDate={props.setDate} />
</div>
</LayoutGroup>
);
});
export function DengiButton({
onClick,
children,
disabled = false,
className = "",
}) {
return (
<span
role="button"
className={
"flex bg-black items-center justify-center border p-1 gap-2 px-2 border-white " +
(disabled
? "cursor-not-allowed text-zinc-700 border-zinc-900 md:hover:border-red-950/50 "
: "not-disabled:cursor-pointer md:not-hover:border-zinc-700 md:not-hover:text-zinc-400 text-white ") +
"rounded-xl transition-colors h-10 min-w-10 not-md:p-2 " +
(className ? className : "")
}
aria-disabled={disabled}
onClick={disabled ? null : onClick}
>
{children}
</span>
);
}
// BAD IDEA::
//
// export function CoolMouseBg() {
// const x = useSpring(0, { stiffness: 200, damping: 40 });
// const y = useSpring(0, { stiffness: 200, damping: 40 });
// const [visible, setVisible] = useState(false);
// useEffect(() => {
// const move = (e) => {
// if (visible) {
// x.set(e.clientX - 25);
// y.set(e.clientY - 25);
// } else {
// setVisible(true);
// }
// };
// window.addEventListener("mousemove", move);
// return () => {
// window.removeEventListener("mousemove", move);
// };
// }, [visible]);
// return (
// <div className="not-md:hidden w-full h-full -z-2 overflow-hidden absolute top-0 left-0">
// <motion.span
// className="w-[50px] h-[50px] blur-3xl bg-white absolute rounded-full"
// style={{
// top: y,
// left: x,
// }}
// initial={{ opacity: 0 }}
// animate={{ opacity: visible ? 1 : 0 }}
// transition={{ duration: 0.5, delay: 1 }}
// />
// </div>
// );
// }
export function DengiYesOrNoDialog({
content = {
message: "?",
yesBtn: "da",
noBtn: "net",
bgDissmis: true,
yesIcon: <BiCheck />,
noIcon: <BiX />,
noBtnStyle: "",
yesBtnStyle: "",
},
show,
onAnswer,
}) {
return (
<>
{/* Background */}
<AnimatePresence>
{show && (
<motion.div
variants={{ on: { opacity: 1 }, off: { opacity: 0 } }}
initial="off"
animate="on"
exit="off"
className="fixed z-99 bg-black/50 backdrop-blur-xl top-0 left-0 w-full h-full overflow-hidden flex items-center justify-center"
onClick={(e) => {
if (e.target === e.currentTarget && content.bgDissmis)
onAnswer(false);
}}
>
<motion.div
variants={{
on: { filter: "blur(0px)", scale: 1, y: 0, opacity: 1 },
off: { filter: "blur(2px) ", scale: 0.7, y: 10, opacity: 0 },
}}
initial="off"
animate="on"
exit="off"
className="flex flex-col justify-center gap-4 p-4 border bg-black border-zinc-400 rounded-2xl"
>
<span>{content.message}</span>
<div className="flex gap-2 justify-stretch">
<DengiButton
className={content.yesBtnStyle + " w-full"}
onClick={() => onAnswer(true)}
>
{content.yesIcon}
{content.yesBtn}
</DengiButton>
<DengiButton
className={content.noBtnStyle + " w-full"}
onClick={() => onAnswer(false)}
>
{content.noIcon}
{content.noBtn}
</DengiButton>
</div>
</motion.div>
</motion.div>
)}
</AnimatePresence>
</>
);
}