Permission Gate
Contextual permission guidance with primary actions for idle, denied, blocked, and granted states.
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 permission-gateSource files
Expand a file to read the source the installer copies into your app.
components/blocks/permission-gate/PermissionGate.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import { Alert, AlertDescription } from '@/components/ui/alert'
import { Button } from '@/components/ui/button'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'
export type PermissionGateStatus = 'idle' | 'denied' | 'blocked' | 'granted'
type PermissionAction = () => void | boolean | Promise<void | boolean>
const defaultLabels = {
idle: '',
denied: 'Access was denied. You can try again.',
blocked: 'To use this feature, enable access in your device settings.',
granted: 'Access granted.',
request: 'Continue',
retry: 'Try again',
settings: 'Open settings',
proceed: 'Continue to feature',
skip: 'Continue without access',
pending: 'Please wait…',
failed: 'This action could not be completed. Try again.',
}
export interface PermissionGateProps extends Omit<ViewProps, 'children'> {
className?: string
title: string
description: string
status: PermissionGateStatus
onRequest?: PermissionAction
onRetry?: PermissionAction
onOpenSettings?: PermissionAction
onContinue?: PermissionAction
onSkip?: () => void
disabled?: boolean
labels?: Partial<typeof defaultLabels>
}
export function PermissionGate({
className,
title,
description,
status,
onRequest,
onRetry,
onOpenSettings,
onContinue,
onSkip,
disabled = false,
labels,
...props
}: PermissionGateProps) {
const copy = { ...defaultLabels, ...labels }
const [pending, setPending] = React.useState<PermissionGateStatus>()
const [failed, setFailed] = React.useState<PermissionGateStatus>()
const inFlight = React.useRef(false)
const currentStatus = React.useRef(status)
React.useLayoutEffect(() => {
currentStatus.current = status
setFailed(undefined)
}, [status])
const action = {
idle: { label: copy.request, run: onRequest },
denied: { label: copy.retry, run: onRetry },
blocked: { label: copy.settings, run: onOpenSettings },
granted: { label: copy.proceed, run: onContinue },
}[status]
const isPending = pending === status
const isFailed = failed === status
const message = copy[status]
useAnnouncement(isFailed ? copy.failed : message || undefined)
async function proceed() {
if (disabled || inFlight.current || !action.run) return
inFlight.current = true
setPending(status)
setFailed(undefined)
try {
if ((await action.run()) === false && currentStatus.current === status) setFailed(status)
} catch {
if (currentStatus.current === status) setFailed(status)
} finally {
inFlight.current = false
setPending(undefined)
}
}
return (
<View {...props} className={cn('gap-4 rounded-xl border border-border bg-card p-4', className)}>
<View className="gap-1">
<Text accessibilityRole="header" size="xl">
{title}
</Text>
<Text variant="muted">{description}</Text>
</View>
{message ? (
<Text size="sm" className="font-medium">
{message}
</Text>
) : null}
{isFailed ? (
<Alert accessibilityRole="none" variant="destructive">
<AlertDescription>{copy.failed}</AlertDescription>
</Alert>
) : null}
{action.run ? (
<Button
accessibilityLabel={isPending ? copy.pending : action.label}
loading={isPending}
disabled={disabled || (!isPending && pending !== undefined)}
onPress={() => {
void proceed()
}}
>
{isPending ? copy.pending : action.label}
</Button>
) : null}
{onSkip && status !== 'granted' ? (
<Button
variant="ghost"
accessibilityLabel={copy.skip}
disabled={disabled || pending !== undefined}
onPress={() => {
if (!disabled && !inFlight.current) onSkip()
}}
>
{copy.skip}
</Button>
) : null}
</View>
)
}
components/blocks/permission-gate/index.ts
export { PermissionGate, type PermissionGateProps, type PermissionGateStatus } from './PermissionGate'