Chip
tetra-ui/chipFreeComponent
Displays a compact, rounded chip element.
Live preview
live · rendered by the registry
Rendered by tetra-ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://tetra-ui.com/r/chip.jsonSource
1import { cva, type VariantProps } from "class-variance-authority";2import {3 Children,4 cloneElement,5 createContext,6 useContext,7 useMemo,8} from "react";9import { Pressable, Text } from "react-native";10import { cn } from "../lib/utils";1112// Types13type InternalChipContextType = VariantProps<typeof chipVariants> & {14 disabled?: boolean;15};1617type ChipChildProps = {18 children: React.ReactNode;19 className?: string;20};2122export type ChipProps = React.ComponentProps<typeof Pressable> &23 InternalChipContextType & {24 children: React.ReactNode;25 };2627// Context28const ChipContext = createContext<InternalChipContextType | null>(null);2930const useChipContext = () => {31 const context = useContext(ChipContext);32 if (!context) {33 throw new Error("useChipContext must be used within a Chip component");34 }35 return context;36};3738// Components39export const Chip = ({40 accessibilityRole = "button",41 children,42 className,43 disabled,44 variant,45 ...props46}: ChipProps) => {47 const ctx = useMemo(() => {48 return {49 disabled,50 variant,51 };52 }, [disabled, variant]);5354 return (55 <ChipContext.Provider value={ctx}>56 <Pressable57 accessibilityRole={accessibilityRole}58 accessibilityState={{ disabled }}59 className={cn(chipVariants({ className, variant }))}60 {...props}61 >62 {Children.map(children, (child) => {63 if (typeof child === "string") {64 return <ChipText>{child}</ChipText>;65 }6667 return child;68 })}69 </Pressable>70 </ChipContext.Provider>71 );72};7374export const ChipText = (props: ChipChildProps) => {75 const ctx = useChipContext();7677 return (78 <Text {...props} className={cn(chipTextVariants(ctx), props.className)} />79 );80};8182export const ChipIcon = ({ children, ...props }: ChipChildProps) => {83 const ctx = useChipContext();8485 const child = Children.only(children);8687 if (!child) {88 if (__DEV__) {89 throw new Error("ChipIcon expects a single React element as children");90 }91 return null;92 }9394 return cloneElement(child as React.ReactElement<ChipChildProps>, {95 ...props,96 className: cn(chipIconVariants(ctx), props.className),97 });98};99100// Styles101const chipVariants = cva(102 "flex w-fit shrink-0 flex-row items-center justify-center gap-1 self-start overflow-hidden whitespace-nowrap rounded-full px-2.5 py-1.5 font-medium text-xs",103 {104 defaultVariants: {105 variant: "default",106 },107 variants: {108 variant: {109// … truncated
Files it writes
More from tetra-ui
All 41 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | tetra-ui | Components | Free | 1 dep | |
| Action Inputaction-input | tetra-ui | Components | Free | no deps | |
| Alertalert | tetra-ui | Components | Free | no deps | |
| Avataravatar | tetra-ui | Components | Free | no deps | |
| Badgebadge | tetra-ui | Components | Free | no deps | |
| Bottom Sheetbottom-sheet | tetra-ui | Components | Free | 4 deps · 7 files | |
| Buttonbutton | tetra-ui | Components | Free | no deps | |
| Cardcard | tetra-ui | Components | Free | no deps |
