UIPackage React Native
Search
Menu

Swipeable Row

Gesture-driven row actions with visible controls and imperative close/open methods.

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 swipeable-row

Source files

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

components/ui/swipeable-row/SwipeableRow.tsx
import * as React from 'react'
import { Pressable, View, type ViewProps } from 'react-native'
import { GestureHandlerRootView } from 'react-native-gesture-handler'
import NativeSwipeable, {
  type SwipeableMethods,
  type SwipeableProps,
} from 'react-native-gesture-handler/ReanimatedSwipeable'
import { Button, type ButtonProps } from '@/components/ui/button'
import { renderTextChildren } from '@/lib/label-children'
import { cn } from '@/lib/utils'

interface RowState {
  disabled: boolean
  open: boolean
  canOpen: (direction: 'left' | 'right') => boolean
  show: (direction: 'left' | 'right') => void
  close: () => void
}
const Context = React.createContext<RowState | null>(null)
function useRow() {
  const row = React.useContext(Context)
  if (!row) throw new Error('SwipeableRow parts must be inside SwipeableRow')
  return row
}
export interface SwipeableRowProps extends Omit<SwipeableProps, 'ref'> {
  children?: React.ReactNode
  disabled?: boolean
  className?: string
  style?: ViewProps['style']
}
export const SwipeableRow = React.forwardRef<SwipeableMethods, SwipeableRowProps>(
  (
    { children, disabled = false, enabled = true, className, style, onSwipeableOpen, onSwipeableClose, ...props },
    ref,
  ) => {
    const native = React.useRef<SwipeableMethods>(null)
    const [open, setOpen] = React.useState(false)
    const isDisabled = disabled || !enabled
    const canOpen = (direction: 'left' | 'right') =>
      !isDisabled && !!(direction === 'left' ? props.renderLeftActions : props.renderRightActions)
    const show = (direction: 'left' | 'right') => {
      if (!canOpen(direction) || !native.current) return
      setOpen(true)
      if (direction === 'left') native.current?.openLeft()
      else native.current?.openRight()
    }
    const close = () => {
      setOpen(false)
      native.current?.close()
    }
    React.useImperativeHandle(ref, () => ({
      close,
      openLeft: () => show('left'),
      openRight: () => show('right'),
      reset: () => {
        native.current?.reset()
        setOpen(false)
      },
    }))
    React.useEffect(() => {
      if (isDisabled) {
        native.current?.reset()
        setOpen(false)
      }
    }, [isDisabled])
    return (
      <Context.Provider value={{ disabled: isDisabled, open, canOpen, show, close }}>
        <View className={cn('w-full overflow-hidden rounded-lg border border-border', className)} style={style}>
          <GestureHandlerRootView style={{ flex: 0 }}>
            <NativeSwipeable
              overshootLeft={false}
              overshootRight={false}
              {...props}
              ref={native}
              enabled={!isDisabled}
              onSwipeableOpen={(direction) => {
                setOpen(true)
                onSwipeableOpen?.(direction)
              }}
              onSwipeableClose={(direction) => {
                setOpen(false)
                onSwipeableClose?.(direction)
              }}
            >
              <View className="min-h-[48px] bg-card">{renderTextChildren(children)}</View>
            </NativeSwipeable>
          </GestureHandlerRootView>
        </View>
      </Context.Provider>
    )
  },
)
SwipeableRow.displayName = 'SwipeableRow'
export interface SwipeableRowOpenProps extends ButtonProps {
  direction?: 'left' | 'right'
}
export const SwipeableRowOpen = React.forwardRef<React.ComponentRef<typeof Pressable>, SwipeableRowOpenProps>(
  ({ direction = 'right', children, disabled, onPress, ...props }, ref) => {
    const row = useRow()
    const isDisabled = row.disabled || !!disabled || !row.canOpen(direction)
    return (
      <Button
        variant="outline"
        {...props}
        ref={ref}
        disabled={isDisabled}
        onPress={(event) => {
          if (isDisabled) return
          onPress?.(event)
          if (event.isDefaultPrevented()) return
          if (row.open) row.close()
          else row.show(direction)
        }}
      >
        {children ?? (row.open ? 'Close actions' : 'Show actions')}
      </Button>
    )
  },
)
SwipeableRowOpen.displayName = 'SwipeableRowOpen'
export interface SwipeableRowActionProps extends ButtonProps {
  closeOnPress?: boolean
}
export const SwipeableRowAction = React.forwardRef<React.ComponentRef<typeof Pressable>, SwipeableRowActionProps>(
  ({ children, closeOnPress = true, disabled, onPress, ...props }, ref) => {
    const row = useRow()
    const isDisabled = row.disabled || !!disabled
    return (
      <Button
        {...props}
        ref={ref}
        disabled={isDisabled}
        onPress={(event) => {
          if (isDisabled) return
          onPress?.(event)
          if (!event.isDefaultPrevented() && closeOnPress) row.close()
        }}
      >
        {children}
      </Button>
    )
  },
)
SwipeableRowAction.displayName = 'SwipeableRowAction'
export type { SwipeableMethods }
components/ui/swipeable-row/index.ts
export * from './SwipeableRow'

Dependencies

Registry items

npm packages

  • react-native-gesture-handler
  • react-native-reanimated
  • react-native-worklets
Browse more components