Sensitive Value
Explicitly reveal and hide a formatted value, with an accessible toggle and hidden-state label.
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 sensitive-valueSource files
Expand a file to read the source the installer copies into your app.
components/ui/sensitive-value/SensitiveValue.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'
export interface SensitiveValueProps extends Omit<ViewProps, 'children'> {
className?: string
label: string
value: string
visible?: boolean
defaultVisible?: boolean
onVisibilityChange?: (visible: boolean) => void
disabled?: boolean
mask?: string
hiddenLabel?: string
showLabel?: string
hideLabel?: string
}
/** Presentation control. A hidden value is absent from the rendered and accessibility trees. */
export function SensitiveValue({
className,
label,
value,
visible,
defaultVisible = false,
onVisibilityChange,
disabled = false,
mask = '••••',
hiddenLabel = 'Hidden',
showLabel = 'Show',
hideLabel = 'Hide',
...props
}: SensitiveValueProps) {
const [internal, setInternal] = React.useState(defaultVisible)
const shown = visible ?? internal
const action = shown ? hideLabel : showLabel
return (
<View {...props} className={cn('flex-row flex-wrap items-center justify-between gap-3', className)}>
<View
accessible
accessibilityLabel={`${label}, ${shown ? value : hiddenLabel}`}
className="min-w-[96px] flex-1 gap-1"
>
<Text size="sm" variant="muted" aria-hidden>
{label}
</Text>
<Text size="xl" style={{ fontVariant: ['tabular-nums'] }} aria-hidden>
{shown ? value : mask}
</Text>
</View>
<Button
variant="outline"
disabled={disabled}
accessibilityLabel={`${action} ${label}`}
accessibilityState={{ expanded: shown }}
onPress={() => {
if (disabled) return
if (visible === undefined) setInternal(!shown)
onVisibilityChange?.(!shown)
}}
>
{action}
</Button>
</View>
)
}
components/ui/sensitive-value/index.ts
export { SensitiveValue, type SensitiveValueProps } from './SensitiveValue'