badge-overflow
diceui-radix/badge-overflowFreeComponent
diceui/radix publishes no description for this item.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/sadmann7/diceui/HEAD/docs/public/r/styles/radix-nova/badge-overflow.jsonSource
1"use client";23import { Slot as SlotPrimitive } from "radix-ui";4import * as React from "react";5import { useComposedRefs } from "@/lib/compose-refs";6import { cn } from "@/lib/utils";78interface GetBadgeLabel<T> {9 /**10 * Callback that returns a label string for each badge item.11 * Optional for primitive arrays (strings, numbers), required for object arrays.12 * @example getBadgeLabel={(item) => item.name}13 */14 getBadgeLabel: (item: T) => string;15}1617type BadgeOverflowElement = React.ComponentRef<typeof BadgeOverflow>;1819type BadgeOverflowProps<T = string> = React.ComponentProps<"div"> &20 (T extends object ? GetBadgeLabel<T> : Partial<GetBadgeLabel<T>>) & {21 items: T[];22 lineCount?: number;23 renderBadge: (item: T, label: string) => React.ReactNode;24 renderOverflow?: (count: number) => React.ReactNode;25 asChild?: boolean;26 };2728function BadgeOverflow<T = string>(props: BadgeOverflowProps<T>) {29 const {30 items,31 getBadgeLabel: getBadgeLabelProp,32 lineCount = 1,33 renderBadge,34 renderOverflow,35 asChild,36 className,37 style,38 ref,39 ...rootProps40 } = props;4142 const getBadgeLabel = React.useCallback(43 (item: T): string => {44 if (typeof item === "object" && !getBadgeLabelProp) {45 throw new Error(46 "`getBadgeLabel` is required when using array of objects",47 );48 }49 return getBadgeLabelProp ? getBadgeLabelProp(item) : (item as string);50 },51 [getBadgeLabelProp],52 );5354 const rootRef = React.useRef<BadgeOverflowElement | null>(null);55 const composedRef = useComposedRefs(ref, rootRef);56 const measureRef = React.useRef<HTMLDivElement | null>(null);57 const [containerWidth, setContainerWidth] = React.useState(0);58 const [badgeGap, setBadgeGap] = React.useState(4);59 const [badgeHeight, setBadgeHeight] = React.useState(20);60 const [overflowBadgeWidth, setOverflowBadgeWidth] = React.useState(40);61 const [isMeasured, setIsMeasured] = React.useState(false);62 const [badgeWidths, setBadgeWidths] = React.useState<Map<string, number>>(63 new Map(),64 );6566 React.useLayoutEffect(() => {67 if (!rootRef.current || !measureRef.current) return;6869 function measureContainer() {70 if (!rootRef.current || !measureRef.current) return;7172 const computedStyle = getComputedStyle(rootRef.current);7374 const gapValue = computedStyle.gap;75 const gap = gapValue ? parseFloat(gapValue) : 4;76 setBadgeGap(gap);7778// … truncated
What it pulls in
npm packages
Files it writes
More from diceui/radix
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| action-baraction-bar | diceui/radix | Components | Free | 1 dep · 2 files | |
| angle-sliderangle-slider | diceui/radix | Components | Free | 1 dep · 3 files | |
| avatar-groupavatar-group | diceui/radix | Components | Free | 1 dep | |
| bannerbanner | diceui/radix | Components | Free | 1 dep | |
| checkbox-groupcheckbox-group | diceui/radix | Components | Free | 1 dep | |
| circular-progresscircular-progress | diceui/radix | Components | Free | 1 dep | |
| client-onlyclient-only | diceui/radix | Components | Free | no deps | |
| color-pickercolor-picker | diceui/radix | Components | Free | 1 dep · 3 files |
