Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
import { type LucideIcon, MoreHorizontal } from "lucide-react";
import {
type CSSProperties,
useLayoutEffect,
useState,
useSyncExternalStore,
} from "react";
import { useTranslation } from "react-i18next";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { LAYER_SURFACE_CLASS } from "@/components/ui/layer";
import { Separator } from "@/components/ui/separator";
import { Tooltip } from "@/components/ui/tooltip";
import { cn } from "@/lib/utils";
export interface SelectionAction {
/** Stable key for React reconciliation. */
key: string;
label: string;
icon?: LucideIcon;
onClick: () => void;
disabled?: boolean;
/**
* When `disabled` and this is set, the reason is shown as a tooltip
* on hover/focus.
*/
disabledReason?: string;
/** When true, the action is omitted from the bar entirely. */
hidden?: boolean;
/**
* Visual tone. "destructive" applies a red-text/border override on top
* of the outline variant. Default: "neutral".
*/
tone?: "neutral" | "destructive";
}
export interface SelectionActionBarProps {
/** Number of selected items. The bar renders only when count > 0. */
count: number;
/**
* Pre-formatted label (e.g. "2 selected", "3 databases selected").
* The call site owns i18n + pluralization.
*/
label: string;
/**
* True when every visible item on the current page is selected. Drives
* the checkbox's checked vs. indeterminate state.
*/
allSelected: boolean;
/**
* Toggles between "select every visible item" and "clear selection".
* Called by the leading checkbox.
*/
onToggleSelectAll: () => void;
/** Declarative actions. Rendered in order. Hidden actions are omitted. */
actions?: SelectionAction[];
/**
* Override the default responsive cap (1 / 3 / 5 by viewport).
* Useful when a call site has only 1–2 actions and never wants a
* More menu — set this to a high number like 99.
*/
maxVisibleActions?: number;
/** Renders the bar even when `count` is 0. */
forceVisible?: boolean;
/** `viewport` centers over the main content; `container` anchors to parent. */
placement?: "viewport" | "container";
/** Hide the selected-count label for narrow containers. */
hideLabel?: boolean;
/** Compact spacing for constrained surfaces. */
density?: "default" | "compact";
}
const MQ_SM = "(min-width: 640px)";
const MQ_LG = "(min-width: 1024px)";
function subscribeMatchMedia(query: string) {
return (cb: () => void) => {
if (typeof window === "undefined") return () => {};
const mql = window.matchMedia(query);
mql.addEventListener("change", cb);
return () => mql.removeEventListener("change", cb);
};
}
function matchesMediaQuery(query: string): boolean {
if (typeof window === "undefined") return true;
return window.matchMedia(query).matches;
}
function useSelectionMaxVisible(): number {
const isLg = useSyncExternalStore(
subscribeMatchMedia(MQ_LG),
() => matchesMediaQuery(MQ_LG),
() => true
);
const isSm = useSyncExternalStore(
subscribeMatchMedia(MQ_SM),
() => matchesMediaQuery(MQ_SM),
() => true
);
if (isLg) return 5;
if (isSm) return 3;
return 1;
}
const DESTRUCTIVE_TONE_CLASS =
"border-error text-error hover:bg-error/10 hover:text-error focus-visible:ring-error";
function useFixedPositionStyle(enabled: boolean): CSSProperties | undefined {
const [style, setStyle] = useState<CSSProperties>();
useLayoutEffect(() => {
if (!enabled) return;
const main = document.getElementById("bb-layout-main");
if (!main) {
setStyle({ left: "50%", maxWidth: "calc(100vw - 2rem)" });
return;
}
const update = () => {
const rect = main.getBoundingClientRect();
setStyle({
left: rect.left + rect.width / 2,
maxWidth: Math.max(0, rect.width - 32),
});
};
update();
// Observing the main pane tracks sidebar collapse/expand as well as
// window resizes; the extra resize listener covers pure horizontal
// shifts where the pane's size stays the same.
const observer = new ResizeObserver(update);
observer.observe(main);
window.addEventListener("resize", update);
return () => {
observer.disconnect();
window.removeEventListener("resize", update);
};
}, [enabled]);
return enabled ? style : undefined;
}
export function SelectionActionBar({
count,
label,
allSelected,
onToggleSelectAll,
actions,
maxVisibleActions,
forceVisible = false,
placement = "viewport",
hideLabel = false,
density = "default",
}: SelectionActionBarProps) {
const { t } = useTranslation();
const defaultMaxVisible = useSelectionMaxVisible();
const maxVisible = maxVisibleActions ?? defaultMaxVisible;
const visible = count > 0 || forceVisible;
const fixedStyle = useFixedPositionStyle(visible && placement === "viewport");
if (!visible) return null;
const visibleActions = (actions ?? []).filter((a) => !a.hidden);
const inlineActions = visibleActions.slice(0, maxVisible);
const overflowActions = visibleActions.slice(maxVisible);
const compact = density === "compact";
let checkboxState: boolean | "indeterminate" = false;
if (count > 0) {
checkboxState = allSelected ? true : "indeterminate";
}
const showLabel = !hideLabel;
const showSeparator =
showLabel && (inlineActions.length > 0 || overflowActions.length > 0);
const checkbox = (
<Checkbox
aria-label={hideLabel ? label : undefined}
checked={checkboxState}
onCheckedChange={() => onToggleSelectAll()}
/>
);
return (
<div
className={cn(
placement === "viewport"
? "fixed bottom-6 w-fit max-w-full -translate-x-1/2"
: "absolute right-2 bottom-3 left-2 max-w-none",
"flex items-center justify-center rounded-full bg-background border border-control-border shadow-lg",
placement === "container" ? "flex-wrap" : "flex-nowrap",
compact ? "gap-x-1.5 gap-y-1 px-2 py-1.5" : "gap-x-3 px-4 py-2",
LAYER_SURFACE_CLASS
)}
style={fixedStyle}
>
{/* Leading cluster — never shrinks; stays visible on narrow viewports
even when the actions group has to scroll horizontally. */}
{hideLabel ? <Tooltip content={label}>{checkbox}</Tooltip> : checkbox}
{showLabel && (
<span className="shrink-0 text-sm font-medium text-control whitespace-nowrap">
{label}
</span>
)}
{showSeparator && (
<Separator orientation="vertical" className="h-5 shrink-0" />
)}
{/* Actions cluster — inline buttons + optional trailing More dropdown. */}
<div
className={cn(
"flex min-w-0 items-center",
placement === "container"
? "max-w-full flex-wrap justify-center"
: "shrink-0",
compact ? "gap-x-1.5 gap-y-1" : "gap-x-3"
)}
>
{inlineActions.map((action) => {
const Icon = action.icon;
const button = (
<Button
appearance="outline"
size={compact ? "xs" : "sm"}
disabled={action.disabled}
onClick={action.onClick}
className={cn(
"min-w-0 max-w-full rounded-full",
action.tone === "destructive" && DESTRUCTIVE_TONE_CLASS
)}
>
{Icon && (
<Icon
className={cn(compact ? "size-3.5" : "size-4")}
aria-hidden
/>
)}
<span className="truncate">{action.label}</span>
</Button>
);
if (action.disabled && action.disabledReason) {
return (
<Tooltip key={action.key} content={action.disabledReason}>
{button}
</Tooltip>
);
}
return (
<div key={action.key} className="min-w-0 max-w-full">
{button}
</div>
);
})}
{overflowActions.length > 0 && (
<DropdownMenu>
<DropdownMenuTrigger
render={
<Button
appearance="outline"
size={compact ? "xs" : "sm"}
className="rounded-full"
aria-label={t("common.more")}
>
<MoreHorizontal
className={cn(compact ? "size-3.5" : "size-4")}
aria-hidden
/>
</Button>
}
/>
<DropdownMenuContent align="end">
{overflowActions.map((action) => {
const Icon = action.icon;
const item = (
<DropdownMenuItem
key={action.key}
disabled={action.disabled}
onClick={action.onClick}
className={cn(
action.tone === "destructive" &&
"text-error data-highlighted:bg-error/10 data-highlighted:text-error"
)}
>
{Icon && <Icon className="size-4" aria-hidden />}
{action.label}
</DropdownMenuItem>
);
if (action.disabled && action.disabledReason) {
return (
<Tooltip key={action.key} content={action.disabledReason}>
<div>{item}</div>
</Tooltip>
);
}
return item;
})}
</DropdownMenuContent>
</DropdownMenu>
)}
</div>
</div>
);
}