2025-12-13 12:25:01 +02:00
|
|
|
import "./Badge.css";
|
|
|
|
|
|
|
|
|
|
import clsx from "clsx";
|
2025-12-15 16:37:33 +02:00
|
|
|
import { ComponentChildren, HTMLAttributes } from "preact";
|
2025-12-13 12:25:01 +02:00
|
|
|
import { useRef } from "preact/hooks";
|
|
|
|
|
|
|
|
|
|
import Dropdown, { DropdownProps } from "./Dropdown";
|
|
|
|
|
import { useStaticTooltip } from "./hooks";
|
|
|
|
|
import Icon from "./Icon";
|
|
|
|
|
|
|
|
|
|
interface SimpleBadgeProps {
|
2025-08-06 12:12:37 +03:00
|
|
|
className?: string;
|
2025-12-14 19:36:53 +02:00
|
|
|
title: ComponentChildren;
|
2025-08-06 12:12:37 +03:00
|
|
|
}
|
|
|
|
|
|
2025-12-15 16:37:33 +02:00
|
|
|
interface BadgeProps extends Pick<HTMLAttributes<HTMLDivElement>, "onClick" | "style"> {
|
2025-12-15 16:47:53 +02:00
|
|
|
text?: ComponentChildren;
|
2025-12-13 12:25:01 +02:00
|
|
|
icon?: string;
|
|
|
|
|
className?: string;
|
|
|
|
|
tooltip?: string;
|
|
|
|
|
href?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function SimpleBadge({ title, className }: SimpleBadgeProps) {
|
|
|
|
|
return <span class={`badge ${className ?? ""}`}>{title}</span>;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-15 16:37:33 +02:00
|
|
|
export function Badge({ icon, className, text, tooltip, href, ...containerProps }: BadgeProps) {
|
2025-12-13 12:25:01 +02:00
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
useStaticTooltip(containerRef, {
|
|
|
|
|
placement: "bottom",
|
|
|
|
|
fallbackPlacements: [ "bottom" ],
|
|
|
|
|
animation: false,
|
|
|
|
|
html: true,
|
|
|
|
|
title: tooltip
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const content = <>
|
|
|
|
|
{icon && <><Icon icon={icon} /> </>}
|
|
|
|
|
<span class="text">{text}</span>
|
|
|
|
|
</>;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
ref={containerRef}
|
2025-12-15 16:37:33 +02:00
|
|
|
className={clsx("ext-badge", className, { "clickable": !!containerProps.onClick })}
|
|
|
|
|
{...containerProps}
|
2025-12-13 12:25:01 +02:00
|
|
|
>
|
|
|
|
|
{href ? <a href={href}>{content}</a> : <span>{content}</span>}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-15 16:47:53 +02:00
|
|
|
export function BadgeWithDropdown({ text, children, tooltip, className, dropdownOptions, ...props }: BadgeProps & {
|
2025-12-13 12:25:01 +02:00
|
|
|
children: ComponentChildren,
|
|
|
|
|
dropdownOptions?: Partial<DropdownProps>
|
|
|
|
|
}) {
|
|
|
|
|
return (
|
|
|
|
|
<Dropdown
|
|
|
|
|
className={`dropdown-badge dropdown-${className}`}
|
2025-12-15 16:47:53 +02:00
|
|
|
text={<Badge
|
2025-12-15 17:17:01 +02:00
|
|
|
text={<>{text} <Icon className="arrow" icon="bx bx-chevron-down" /></>}
|
2025-12-15 16:47:53 +02:00
|
|
|
className={className}
|
|
|
|
|
{...props}
|
|
|
|
|
/>}
|
2025-12-13 12:25:01 +02:00
|
|
|
noDropdownListStyle
|
|
|
|
|
noSelectButtonStyle
|
|
|
|
|
hideToggleArrow
|
|
|
|
|
title={tooltip}
|
|
|
|
|
titlePosition="bottom"
|
|
|
|
|
{...dropdownOptions}
|
|
|
|
|
dropdownOptions={{
|
|
|
|
|
...dropdownOptions?.dropdownOptions,
|
|
|
|
|
popperConfig: {
|
|
|
|
|
...dropdownOptions?.dropdownOptions?.popperConfig,
|
|
|
|
|
placement: "bottom", strategy: "fixed"
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>{children}</Dropdown>
|
|
|
|
|
);
|
|
|
|
|
}
|