UIPackage React Native
Search

Chip

Static, selectable, or removable chip with caller-owned selection and a separate removal target.

Install

First install? Set up the registry in your app.

npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add chip

Source files

Expand a file to read the source the installer copies into your app.

components/ui/chip/Chip.tsx
import * as React from 'react'
import { Pressable, View, type PressableProps, type ViewProps } from 'react-native'
import { Check, X } from 'lucide-react-native'
import { IconButton } from '@/components/ui/icon-button'
import { Text } from '@/components/ui/text'
import { useThemeColors } from '@/lib/theme'
import { cn } from '@/lib/utils'

export interface ChipProps extends Omit<PressableProps, 'children' | 'style'> {
  label: string
  selected?: boolean
  onRemove?: () => void
  removeLabel?: string
  prefixIcon?: React.ReactNode
  className?: string
  style?: ViewProps['style']
}

export const Chip = React.forwardRef<React.ComponentRef<typeof Pressable>, ChipProps>(
  (
    {
      label,
      selected,
      onRemove,
      removeLabel = `Remove ${label}`,
      prefixIcon,
      disabled,
      onPress,
      onLongPress,
      className,
      style,
      ...props
    },
    ref,
  ) => {
    const colors = useThemeColors()
    const content = (
      <>
        {selected ? <Check size={18} color={colors.secondaryForeground} aria-hidden /> : prefixIcon}
        <Text size="sm" className="min-w-0 shrink text-secondary-foreground">
          {label}
        </Text>
      </>
    )
    const bodyClassName = 'min-h-[48px] min-w-0 shrink flex-row items-center gap-[8px] px-3'
    return (
      <View
        className={cn(
          'max-w-full flex-row items-center self-start rounded-full border bg-secondary',
          selected ? 'border-primary' : 'border-transparent',
          disabled && 'opacity-50',
          className,
        )}
        style={style}
      >
        {onPress || onLongPress ? (
          <Pressable
            ref={ref}
            {...props}
            accessibilityRole="button"
            accessibilityLabel={props.accessibilityLabel ?? label}
            accessibilityState={{ ...props.accessibilityState, selected, disabled: !!disabled }}
            disabled={disabled}
            onPress={(event) => {
              if (!disabled) onPress?.(event)
            }}
            onLongPress={onLongPress}
            className={cn(bodyClassName, 'rounded-full active:opacity-80')}
          >
            {content}
          </Pressable>
        ) : (
          <View ref={ref} {...props} className={bodyClassName}>
            {content}
          </View>
        )}
        {onRemove ? (
          <IconButton
            icon={X}
            iconSize={18}
            iconColor={colors.secondaryForeground}
            accessibilityLabel={removeLabel}
            disabled={disabled}
            onPress={onRemove}
            className="rounded-full"
          />
        ) : null}
      </View>
    )
  },
)
Chip.displayName = 'Chip'
components/ui/chip/index.ts
export * from './Chip'

Dependencies

Registry items

npm packages

  • lucide-react-native
Browse controls and interactions