Text
Themed text that receives its colour from the nearest styled parent.
Installation
Terminal
npx magic-native-ui@latest add textUsage
The example running in the preview above. Imports use the default aliases from components.json.
text-demo.tsx
import { View } from 'react-native'
import { Text } from '@/components/ui/text'
export function TextDemo() {
return (
<View className="w-full gap-2">
<Text className="text-2xl font-bold">Heading</Text>
<Text>Body copy inherits colour from its parent.</Text>
<Text className="text-muted-foreground">Muted supporting text.</Text>
</View>
)
}Component source
What the CLI copies into your project. Once it is there, the code is yours to edit.
registry/ui/text.tsx
import * as React from 'react'
import { Text as RNText } from 'react-native'
import { cn } from '@/registry/lib/utils'
/**
* React Native has no CSS inheritance, so a `<Text>` nested inside a styled
* parent knows nothing about that parent's colour or size. Components like
* Button and Badge publish their text classes through this context instead,
* which is what lets `<Button><Text>Save</Text></Button>` render correctly.
*/
const TextClassContext = React.createContext<string | undefined>(undefined)
function Text({ className, ...props }: React.ComponentProps<typeof RNText>) {
const textClass = React.useContext(TextClassContext)
return <RNText className={cn('text-base text-foreground', textClass, className)} {...props} />
}
export { Text, TextClassContext }