UIPackage React Native
Search

Share Button

Action Button wrapping React Native Share.share with rapid-press guards and raw platform ShareAction reporting.

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 share-button

Source files

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

components/ui/share-button/ShareButton.tsx
import * as React from 'react'
import { Share, type GestureResponderEvent, type ShareAction, type ShareContent, type ShareOptions } from 'react-native'
import { Button, type ButtonProps } from '@/components/ui/button'

export interface ShareButtonProps extends ButtonProps {
  /** Payload passed to `Share.share`. */
  content: ShareContent
  /** Optional platform share options passed to `Share.share`. */
  options?: ShareOptions
  /**
   * Receives the raw `ShareAction` returned by `Share.share`, or `undefined` on React Native Web.
   *
   * On Android, `Share.share` always resolves with `Share.sharedAction` even when the chooser is
   * dismissed. On iOS, cancelling the sheet resolves with `Share.dismissedAction`.
   */
  onResult?: (result: ShareAction | undefined) => void
  /** Called when `Share.share` rejects. */
  onError?: (error: Error) => void
}

/**
 * Triggers the system share sheet via React Native `Share.share`.
 *
 * Platform note: Android always resolves `Share.sharedAction` even if the user dismisses the
 * chooser; iOS can resolve `Share.dismissedAction`; React Native Web may resolve `undefined`.
 */
const ShareButton = React.forwardRef<React.ComponentRef<typeof Button>, ShareButtonProps>(
  ({ content, options, onResult, onError, onPress, disabled, loading = false, children = 'Share', ...props }, ref) => {
    const [pending, setPending] = React.useState(false)
    const mountedRef = React.useRef(true)
    const pendingRef = React.useRef(false)

    React.useEffect(() => {
      mountedRef.current = true
      return () => {
        mountedRef.current = false
      }
    }, [])

    async function handlePress(event: GestureResponderEvent) {
      if (disabled || loading || pendingRef.current) return
      onPress?.(event)
      if (event?.defaultPrevented) return

      pendingRef.current = true
      setPending(true)
      let result: ShareAction | undefined
      let nativeError: Error | undefined
      try {
        result = (await Share.share(content, options)) as ShareAction | undefined
      } catch (error) {
        nativeError = error instanceof Error ? error : new Error(String(error))
      }

      pendingRef.current = false
      if (!mountedRef.current) return
      setPending(false)

      if (nativeError) {
        onError?.(nativeError)
        return
      }
      onResult?.(result)
    }

    return (
      <Button ref={ref} disabled={disabled} loading={loading || pending} onPress={handlePress} {...props}>
        {children}
      </Button>
    )
  },
)
ShareButton.displayName = 'ShareButton'

export { ShareButton }
components/ui/share-button/index.ts
export { ShareButton, type ShareButtonProps } from './ShareButton'

Dependencies

Registry items

Browse controls and interactions