UIPackage React Native
Search

Toggle Group

Composable single or multiple native toggle selection.

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 toggle-group

Source files

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

components/ui/toggle-group/ToggleGroup.tsx
import * as React from 'react'
import { cssInterop } from 'react-native-css-interop'
import * as Primitive from '@rn-primitives/toggle-group'
import { renderTextChildren } from '@/lib/label-children'
import { cn } from '@/lib/utils'

export type ToggleGroupProps = Primitive.RootProps & { className?: string }
export const ToggleGroup = React.forwardRef<Primitive.RootRef, ToggleGroupProps>((props, ref) => (
  <Primitive.Root {...props} ref={ref} className={cn('flex-row flex-wrap gap-[8px]', props.className)} />
))
ToggleGroup.displayName = 'ToggleGroup'
export type ToggleGroupItemProps = Omit<Primitive.ItemProps, 'children'> & {
  children?: React.ReactNode
  className?: string
}
export const ToggleGroupItem = React.forwardRef<Primitive.ItemRef, ToggleGroupItemProps>(
  ({ children, className, ...props }, ref) => {
    const group = Primitive.useRootContext()
    const selected = Array.isArray(group.value) ? group.value.includes(props.value) : group.value === props.value
    return (
      <Primitive.Item
        {...props}
        ref={ref}
        className={cn(
          'min-h-[48px] min-w-[48px] flex-row items-center justify-center gap-[8px] rounded-md border border-border px-[16px] py-[12px]',
          selected && 'bg-secondary',
          (group.disabled || props.disabled) && 'opacity-50',
          className,
        )}
      >
        {renderTextChildren(children, 'font-medium')}
      </Primitive.Item>
    )
  },
)
ToggleGroupItem.displayName = 'ToggleGroupItem'

// The published headless components forward className outside the NativeWind JSX transform.
cssInterop(Primitive.Root, { className: 'style' })
cssInterop(Primitive.Item, { className: 'style' })
components/ui/toggle-group/index.ts
export * from './ToggleGroup'

Dependencies

Registry items

npm packages

  • react-native-css-interop
  • @rn-primitives/toggle-group
Browse controls and interactions