Expandable Text
A native paragraph that offers a disclosure action when its text exceeds the selected line limit.
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 expandable-textSource files
Expand a file to read the source the installer copies into your app.
components/ui/expandable-text/ExpandableText.tsx
import * as React from 'react'
import { View, useWindowDimensions, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import { Text, type TextProps } from '@/components/ui/text'
import { cn } from '@/lib/utils'
export interface ExpandableTextProps extends Omit<ViewProps, 'children'> {
className?: string
text: string
maxLines?: number
expanded?: boolean
defaultExpanded?: boolean
onExpandedChange?: (expanded: boolean) => void
moreLabel?: string
lessLabel?: string
textClassName?: string
textSize?: TextProps['size']
textVariant?: TextProps['variant']
maxFontSizeMultiplier?: number
}
export function ExpandableText({
className,
text,
maxLines = 3,
expanded,
defaultExpanded = false,
onExpandedChange,
moreLabel = 'Show more',
lessLabel = 'Show less',
textClassName,
textSize,
textVariant,
maxFontSizeMultiplier,
...props
}: ExpandableTextProps) {
const { width, fontScale } = useWindowDimensions()
const lines = Number.isSafeInteger(maxLines) && maxLines > 0 ? maxLines : 3
const [internal, setInternal] = React.useState(defaultExpanded)
const shownExpanded = expanded ?? internal
const measurement = [text, width, fontScale, textSize, textVariant, textClassName, maxFontSizeMultiplier].join('\0')
const activeMeasurement = React.useRef(measurement)
React.useLayoutEffect(() => {
activeMeasurement.current = measurement
}, [measurement])
const [layout, setLayout] = React.useState<{ measurement: string; lines: number }>()
const canExpand = layout?.measurement === measurement && layout.lines > lines
const action = shownExpanded ? lessLabel : moreLabel
const typography = { className: textClassName, size: textSize, variant: textVariant, maxFontSizeMultiplier }
return (
<View {...props} className={cn('gap-1', className)}>
<Text {...typography} numberOfLines={shownExpanded ? undefined : lines}>
{text}
</Text>
{text ? (
<View
pointerEvents="none"
aria-hidden
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
className="absolute inset-x-0 top-0 opacity-0"
>
<Text
{...typography}
key={measurement}
onTextLayout={({ nativeEvent }) => {
if (activeMeasurement.current !== measurement) return
const count = nativeEvent.lines.length
setLayout((previous) =>
previous?.measurement === measurement && previous.lines === count
? previous
: { measurement, lines: count },
)
}}
>
{text}
</Text>
</View>
) : null}
{canExpand ? (
<Button
variant="ghost"
className="self-start"
accessibilityLabel={action}
accessibilityState={{ expanded: shownExpanded }}
onPress={() => {
if (expanded === undefined) setInternal(!shownExpanded)
onExpandedChange?.(!shownExpanded)
}}
>
{action}
</Button>
) : null}
</View>
)
}
components/ui/expandable-text/index.ts
export { ExpandableText, type ExpandableTextProps } from './ExpandableText'