analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
450 lines (447 loc) • 16.3 kB
JavaScript
// src/components/DropdownMenu/DropdownMenu.tsx
import { SignOut, User } from "phosphor-react";
import {
forwardRef,
useEffect,
useRef,
isValidElement,
Children,
cloneElement,
useState
} from "react";
import { create, useStore } from "zustand";
// src/utils/utils.ts
import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";
function cn(...inputs) {
return twMerge(clsx(inputs));
}
// src/components/Button/Button.tsx
import { jsx, jsxs } from "react/jsx-runtime";
var VARIANT_ACTION_CLASSES = {
solid: {
primary: "bg-primary-950 text-text border border-primary-950 hover:bg-primary-800 hover:border-primary-800 focus-visible:outline-none focus-visible:bg-primary-950 focus-visible:ring-2 focus-visible:ring-offset-0 focus-visible:ring-indicator-info active:bg-primary-700 active:border-primary-700 disabled:bg-primary-500 disabled:border-primary-500 disabled:opacity-40 disabled:cursor-not-allowed",
positive: "bg-success-500 text-text border border-success-500 hover:bg-success-600 hover:border-success-600 focus-visible:outline-none focus-visible:bg-success-500 focus-visible:ring-2 focus-visible:ring-offset-0 focus-visible:ring-indicator-info active:bg-success-700 active:border-success-700 disabled:bg-success-500 disabled:border-success-500 disabled:opacity-40 disabled:cursor-not-allowed",
negative: "bg-error-500 text-text border border-error-500 hover:bg-error-600 hover:border-error-600 focus-visible:outline-none focus-visible:bg-error-500 focus-visible:ring-2 focus-visible:ring-offset-0 focus-visible:ring-indicator-info active:bg-error-700 active:border-error-700 disabled:bg-error-500 disabled:border-error-500 disabled:opacity-40 disabled:cursor-not-allowed"
},
outline: {
primary: "bg-transparent text-primary-950 border border-primary-950 hover:bg-background-50 hover:text-primary-400 hover:border-primary-400 focus-visible:border-0 focus-visible:outline-none focus-visible:text-primary-600 focus-visible:ring-2 focus-visible:ring-offset-0 focus-visible:ring-indicator-info active:text-primary-700 active:border-primary-700 disabled:opacity-40 disabled:cursor-not-allowed",
positive: "bg-transparent text-success-500 border border-success-300 hover:bg-background-50 hover:text-success-400 hover:border-success-400 focus-visible:border-0 focus-visible:outline-none focus-visible:text-success-600 focus-visible:ring-2 focus-visible:ring-offset-0 focus-visible:ring-indicator-info active:text-success-700 active:border-success-700 disabled:opacity-40 disabled:cursor-not-allowed",
negative: "bg-transparent text-error-500 border border-error-300 hover:bg-background-50 hover:text-error-400 hover:border-error-400 focus-visible:border-0 focus-visible:outline-none focus-visible:text-error-600 focus-visible:ring-2 focus-visible:ring-offset-0 focus-visible:ring-indicator-info active:text-error-700 active:border-error-700 disabled:opacity-40 disabled:cursor-not-allowed"
},
link: {
primary: "bg-transparent text-primary-950 hover:text-primary-400 focus-visible:outline-none focus-visible:text-primary-600 focus-visible:ring-2 focus-visible:ring-offset-0 focus-visible:ring-indicator-info active:text-primary-700 disabled:opacity-40 disabled:cursor-not-allowed",
positive: "bg-transparent text-success-500 hover:text-success-400 focus-visible:outline-none focus-visible:text-success-600 focus-visible:ring-2 focus-visible:ring-offset-0 focus-visible:ring-indicator-info active:text-success-700 disabled:opacity-40 disabled:cursor-not-allowed",
negative: "bg-transparent text-error-500 hover:text-error-400 focus-visible:outline-none focus-visible:text-error-600 focus-visible:ring-2 focus-visible:ring-offset-0 focus-visible:ring-indicator-info active:text-error-700 disabled:opacity-40 disabled:cursor-not-allowed"
}
};
var SIZE_CLASSES = {
"extra-small": "text-xs px-3.5 py-2",
small: "text-sm px-4 py-2.5",
medium: "text-md px-5 py-2.5",
large: "text-lg px-6 py-3",
"extra-large": "text-lg px-7 py-3.5"
};
var Button = ({
children,
iconLeft,
iconRight,
size = "medium",
variant = "solid",
action = "primary",
className = "",
disabled,
type = "button",
...props
}) => {
const sizeClasses = SIZE_CLASSES[size];
const variantClasses = VARIANT_ACTION_CLASSES[variant][action];
const baseClasses = "inline-flex items-center justify-center rounded-full cursor-pointer font-medium";
return /* @__PURE__ */ jsxs(
"button",
{
className: cn(baseClasses, variantClasses, sizeClasses, className),
disabled,
type,
...props,
children: [
iconLeft && /* @__PURE__ */ jsx("span", { className: "mr-2 flex items-center", children: iconLeft }),
children,
iconRight && /* @__PURE__ */ jsx("span", { className: "ml-2 flex items-center", children: iconRight })
]
}
);
};
var Button_default = Button;
// src/components/DropdownMenu/DropdownMenu.tsx
import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
function createDropdownStore() {
return create((set) => ({
open: false,
setOpen: (open) => set({ open })
}));
}
var useDropdownStore = (externalStore) => {
if (!externalStore) {
throw new Error(
"Component must be used within a DropdownMenu (store is missing)"
);
}
return externalStore;
};
var injectStore = (children, store) => {
return Children.map(children, (child) => {
if (isValidElement(child)) {
const typedChild = child;
const newProps = {
store
};
if (typedChild.props.children) {
newProps.children = injectStore(typedChild.props.children, store);
}
return cloneElement(typedChild, newProps);
}
return child;
});
};
var DropdownMenu = ({
children,
open: propOpen,
onOpenChange
}) => {
const storeRef = useRef(null);
storeRef.current ??= createDropdownStore();
const store = storeRef.current;
const { open, setOpen: storeSetOpen } = useStore(store, (s) => s);
const setOpen = (newOpen) => {
storeSetOpen(newOpen);
};
const menuRef = useRef(null);
const handleArrowDownOrArrowUp = (event) => {
const menuContent = menuRef.current?.querySelector('[role="menu"]');
if (menuContent) {
event.preventDefault();
const items = Array.from(
menuContent.querySelectorAll(
'[role="menuitem"]:not([aria-disabled="true"])'
)
).filter((el) => el instanceof HTMLElement);
if (items.length === 0) return;
const focusedItem = document.activeElement;
const currentIndex = items.findIndex((item) => item === focusedItem);
let nextIndex;
if (event.key === "ArrowDown") {
nextIndex = currentIndex === -1 ? 0 : (currentIndex + 1) % items.length;
} else {
nextIndex = currentIndex === -1 ? items.length - 1 : (currentIndex - 1 + items.length) % items.length;
}
items[nextIndex]?.focus();
}
};
const handleDownkey = (event) => {
if (event.key === "Escape") {
setOpen(false);
} else if (event.key === "ArrowDown" || event.key === "ArrowUp") {
handleArrowDownOrArrowUp(event);
}
};
const handleClickOutside = (event) => {
if (menuRef.current && !menuRef.current.contains(event.target)) {
setOpen(false);
}
};
useEffect(() => {
if (open) {
document.addEventListener("mousedown", handleClickOutside);
document.addEventListener("keydown", handleDownkey);
}
return () => {
document.removeEventListener("mousedown", handleClickOutside);
document.removeEventListener("keydown", handleDownkey);
};
}, [open]);
useEffect(() => {
setOpen(open);
onOpenChange?.(open);
}, [open, onOpenChange]);
useEffect(() => {
if (propOpen) {
setOpen(propOpen);
}
}, [propOpen]);
return /* @__PURE__ */ jsx2("div", { className: "relative", ref: menuRef, children: injectStore(children, store) });
};
var DropdownMenuTrigger = ({
className,
children,
onClick,
store: externalStore,
...props
}) => {
const store = useDropdownStore(externalStore);
const open = useStore(store, (s) => s.open);
const toggleOpen = () => store.setState({ open: !open });
return /* @__PURE__ */ jsx2(
"button",
{
onClick: (e) => {
e.stopPropagation();
toggleOpen();
if (onClick) onClick(e);
},
"aria-expanded": open,
className: cn(className),
...props,
children
}
);
};
DropdownMenuTrigger.displayName = "DropdownMenuTrigger";
var ITEM_SIZE_CLASSES = {
small: "text-sm",
medium: "text-md"
};
var SIDE_CLASSES = {
top: "bottom-full",
right: "top-full",
bottom: "top-full",
left: "top-full"
};
var ALIGN_CLASSES = {
start: "left-0",
center: "left-1/2 -translate-x-1/2",
end: "right-0"
};
var MENUCONTENT_VARIANT_CLASSES = {
menu: "p-1",
profile: "p-6"
};
var MenuLabel = forwardRef(({ className, inset, store: _store, ...props }, ref) => {
return /* @__PURE__ */ jsx2(
"div",
{
ref,
className: cn("text-sm w-full", inset ? "pl-8" : "", className),
...props
}
);
});
MenuLabel.displayName = "MenuLabel";
var DropdownMenuContent = forwardRef(
({
className,
align = "start",
side = "bottom",
variant = "menu",
sideOffset = 4,
children,
store: externalStore,
...props
}, ref) => {
const store = useDropdownStore(externalStore);
const open = useStore(store, (s) => s.open);
const [isVisible, setIsVisible] = useState(open);
useEffect(() => {
if (open) {
setIsVisible(true);
} else {
const timer = setTimeout(() => setIsVisible(false), 200);
return () => clearTimeout(timer);
}
}, [open]);
if (!isVisible) return null;
const getPositionClasses = () => {
const vertical = SIDE_CLASSES[side];
const horizontal = ALIGN_CLASSES[align];
return `absolute ${vertical} ${horizontal}`;
};
const variantClasses = MENUCONTENT_VARIANT_CLASSES[variant];
return /* @__PURE__ */ jsx2(
"div",
{
ref,
role: "menu",
className: `
bg-background z-50 min-w-[210px] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md border-border-100
${open ? "animate-in fade-in-0 zoom-in-95" : "animate-out fade-out-0 zoom-out-95"}
${getPositionClasses()}
${variantClasses}
${className}
`,
style: {
marginTop: side === "bottom" ? sideOffset : void 0,
marginBottom: side === "top" ? sideOffset : void 0,
marginLeft: side === "right" ? sideOffset : void 0,
marginRight: side === "left" ? sideOffset : void 0
},
...props,
children
}
);
}
);
DropdownMenuContent.displayName = "DropdownMenuContent";
var DropdownMenuItem = forwardRef(
({
className,
size = "small",
children,
iconRight,
iconLeft,
disabled = false,
onClick,
variant = "menu",
store: externalStore,
...props
}, ref) => {
const store = useDropdownStore(externalStore);
const setOpen = useStore(store, (s) => s.setOpen);
const sizeClasses = ITEM_SIZE_CLASSES[size];
const handleClick = (e) => {
if (disabled) {
e.preventDefault();
e.stopPropagation();
return;
}
onClick?.(e);
setOpen(false);
};
const getVariantClasses = () => {
if (variant === "profile") {
return "relative flex flex-row justify-between select-none items-center gap-2 rounded-sm p-4 text-sm outline-none transition-colors [&>svg]:size-6 [&>svg]:shrink-0";
}
return "relative flex select-none items-center gap-2 rounded-sm p-3 text-sm outline-none transition-colors [&>svg]:size-4 [&>svg]:shrink-0";
};
const getVariantProps = () => {
return variant === "profile" ? { "data-variant": "profile" } : {};
};
return /* @__PURE__ */ jsxs2(
"div",
{
ref,
role: "menuitem",
...getVariantProps(),
"aria-disabled": disabled,
className: `
focus-visible:bg-background-50
${getVariantClasses()}
${sizeClasses}
${className}
${disabled ? "cursor-not-allowed text-text-400" : "cursor-pointer hover:bg-background-50 text-text-700 focus:bg-accent focus:text-accent-foreground hover:bg-accent hover:text-accent-foreground"}
`,
onClick: handleClick,
onKeyDown: (e) => {
if (e.key === "Enter" || e.key === " ") handleClick(e);
},
tabIndex: disabled ? -1 : 0,
...props,
children: [
iconLeft,
/* @__PURE__ */ jsx2("span", { className: "w-full text-md", children }),
iconRight
]
}
);
}
);
DropdownMenuItem.displayName = "DropdownMenuItem";
var DropdownMenuSeparator = forwardRef(({ className, store: _store, ...props }, ref) => /* @__PURE__ */ jsx2(
"div",
{
ref,
className: cn("my-1 h-px bg-border-200", className),
...props
}
));
DropdownMenuSeparator.displayName = "DropdownMenuSeparator";
var ProfileMenuTrigger = forwardRef(({ className, onClick, store: externalStore, ...props }, ref) => {
const store = useDropdownStore(externalStore);
const open = useStore(store, (s) => s.open);
const toggleOpen = () => store.setState({ open: !open });
return /* @__PURE__ */ jsx2(
"button",
{
ref,
className: cn(
"rounded-lg size-10 bg-primary-50 flex items-center justify-center cursor-pointer",
className
),
onClick: (e) => {
e.stopPropagation();
toggleOpen();
onClick?.(e);
},
"aria-expanded": open,
...props,
children: /* @__PURE__ */ jsx2("span", { className: "size-6 rounded-full bg-primary-100 flex items-center justify-center", children: /* @__PURE__ */ jsx2(User, { className: "text-primary-950", size: 18 }) })
}
);
});
ProfileMenuTrigger.displayName = "ProfileMenuTrigger";
var ProfileMenuHeader = forwardRef(({ className, name, email, store: _store, ...props }, ref) => {
return /* @__PURE__ */ jsxs2(
"div",
{
ref,
"data-component": "ProfileMenuHeader",
className: cn("flex flex-row gap-4 items-center", className),
...props,
children: [
/* @__PURE__ */ jsx2("span", { className: "size-16 bg-primary-100 rounded-full flex items-center justify-center", children: /* @__PURE__ */ jsx2(User, { size: 34, className: "text-primary-950" }) }),
/* @__PURE__ */ jsxs2("div", { className: "flex flex-col ", children: [
/* @__PURE__ */ jsx2("p", { className: "text-xl font-bold text-text-950", children: name }),
/* @__PURE__ */ jsx2("p", { className: "text-md text-text-600", children: email })
] })
]
}
);
});
ProfileMenuHeader.displayName = "ProfileMenuHeader";
var ProfileMenuSection = forwardRef(({ className, children, store: _store, ...props }, ref) => {
return /* @__PURE__ */ jsx2("div", { ref, className: cn("flex flex-col p-2", className), ...props, children });
});
ProfileMenuSection.displayName = "ProfileMenuSection";
var ProfileMenuFooter = ({
className,
disabled = false,
onClick,
store: externalStore,
...props
}) => {
const store = useDropdownStore(externalStore);
const setOpen = useStore(store, (s) => s.setOpen);
return /* @__PURE__ */ jsxs2(
Button_default,
{
variant: "outline",
className: cn("w-full", className),
disabled,
onClick: (e) => {
setOpen(false);
onClick?.(e);
},
...props,
children: [
/* @__PURE__ */ jsx2("span", { className: "mr-2 flex items-center", children: /* @__PURE__ */ jsx2(SignOut, {}) }),
/* @__PURE__ */ jsx2("span", { children: "Sair" })
]
}
);
};
ProfileMenuFooter.displayName = "ProfileMenuFooter";
var DropdownMenu_default = DropdownMenu;
export {
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
MenuLabel,
ProfileMenuFooter,
ProfileMenuHeader,
ProfileMenuSection,
ProfileMenuTrigger,
createDropdownStore,
DropdownMenu_default as default,
useDropdownStore
};
//# sourceMappingURL=index.mjs.map