UIPackage React Native
Search

Control Group

Structural connected control group with corner-radius ownership, optional hairline dividers, and shared disabled propagation without imposing selection semantics.

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

Source files

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

components/ui/control-group/ControlGroup.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import { cn } from '@/lib/utils'

export type ControlGroupOrientation = 'horizontal' | 'vertical'
export type ControlGroupItemPosition = 'first' | 'middle' | 'last' | 'only'

export interface ControlGroupContextValue {
  orientation: ControlGroupOrientation
  disabled: boolean
}

export interface ControlGroupItemContextValue {
  position: ControlGroupItemPosition
  orientation: ControlGroupOrientation
  disabled: boolean
}

const ControlGroupContext = React.createContext<ControlGroupContextValue>({
  orientation: 'horizontal',
  disabled: false,
})

const ControlGroupItemContext = React.createContext<ControlGroupItemContextValue>({
  position: 'only',
  orientation: 'horizontal',
  disabled: false,
})

export function useControlGroup(): ControlGroupContextValue {
  return React.useContext(ControlGroupContext)
}

export function useControlGroupItem(): ControlGroupItemContextValue {
  return React.useContext(ControlGroupItemContext)
}

function cornerClassFor(position: ControlGroupItemPosition, orientation: ControlGroupOrientation): string {
  if (position === 'only') return 'rounded-md'
  if (position === 'middle') return 'rounded-none'
  if (orientation === 'horizontal') {
    return position === 'first' ? 'rounded-l-md rounded-r-none' : 'rounded-r-md rounded-l-none'
  }
  return position === 'first' ? 'rounded-t-md rounded-b-none' : 'rounded-b-md rounded-t-none'
}

export interface ControlGroupProps extends ViewProps {
  orientation?: ControlGroupOrientation
  /** Disables all items inside the group without imposing selection semantics. */
  disabled?: boolean
  /** Renders hairline dividers between adjacent items. Defaults to `true`. */
  dividers?: boolean
  className?: string
}

const ControlGroup = React.forwardRef<React.ComponentRef<typeof View>, ControlGroupProps>(
  ({ orientation = 'horizontal', disabled = false, dividers = true, className, children, ...props }, ref) => {
    const validChildren = React.Children.toArray(children).filter(React.isValidElement)
    const count = validChildren.length

    const groupContext = React.useMemo<ControlGroupContextValue>(
      () => ({ orientation, disabled }),
      [orientation, disabled],
    )

    return (
      <ControlGroupContext.Provider value={groupContext}>
        <View
          ref={ref}
          className={cn(
            'overflow-hidden rounded-md border border-border',
            orientation === 'horizontal' ? 'flex-row items-stretch' : 'flex-col',
            disabled && 'opacity-50',
            className,
          )}
          {...props}
        >
          {validChildren.map((child, index) => {
            const position: ControlGroupItemPosition =
              count <= 1 ? 'only' : index === 0 ? 'first' : index === count - 1 ? 'last' : 'middle'
            const itemKey = child.key ?? index

            return (
              <React.Fragment key={itemKey}>
                {dividers && index > 0 ? (
                  <View
                    aria-hidden
                    accessibilityElementsHidden
                    importantForAccessibility="no-hide-descendants"
                    className={cn(
                      'shrink-0 bg-border',
                      orientation === 'horizontal' ? 'w-px self-stretch' : 'h-px w-full',
                    )}
                  />
                ) : null}
                <ControlGroupItemContext.Provider value={{ position, orientation, disabled }}>
                  {child}
                </ControlGroupItemContext.Provider>
              </React.Fragment>
            )
          })}
        </View>
      </ControlGroupContext.Provider>
    )
  },
)
ControlGroup.displayName = 'ControlGroup'

export interface ControlGroupItemProps extends ViewProps {
  disabled?: boolean
  className?: string
}

const ControlGroupItem = React.forwardRef<React.ComponentRef<typeof View>, ControlGroupItemProps>(
  ({ disabled, className, children, ...props }, ref) => {
    const group = useControlGroup()
    const item = useControlGroupItem()
    const effectiveDisabled = Boolean(group.disabled || disabled)

    const mergedContext = React.useMemo<ControlGroupItemContextValue>(
      () => ({
        position: item.position,
        orientation: item.orientation,
        disabled: effectiveDisabled,
      }),
      [item.position, item.orientation, effectiveDisabled],
    )

    const resolvedChildren = React.useMemo(() => {
      if (React.isValidElement<{ disabled?: boolean }>(children) && children.type !== React.Fragment) {
        const mergedDisabled = Boolean(effectiveDisabled || children.props.disabled)
        if (mergedDisabled || 'disabled' in children.props) {
          return React.cloneElement(children, { disabled: mergedDisabled })
        }
      }
      return children
    }, [children, effectiveDisabled])

    return (
      <ControlGroupItemContext.Provider value={mergedContext}>
        <View
          ref={ref}
          className={cn(
            'overflow-hidden',
            cornerClassFor(item.position, item.orientation),
            !group.disabled && effectiveDisabled && 'opacity-50',
            className,
          )}
          {...props}
        >
          {resolvedChildren}
        </View>
      </ControlGroupItemContext.Provider>
    )
  },
)
ControlGroupItem.displayName = 'ControlGroupItem'

export { ControlGroup, ControlGroupItem }
components/ui/control-group/index.ts
export {
  ControlGroup,
  ControlGroupItem,
  useControlGroup,
  useControlGroupItem,
  type ControlGroupContextValue,
  type ControlGroupItemContextValue,
  type ControlGroupItemPosition,
  type ControlGroupItemProps,
  type ControlGroupOrientation,
  type ControlGroupProps,
} from './ControlGroup'

Dependencies

Registry items

Browse layout and navigation