import { createContext, forwardRef, useContext, useId } from 'react'; import { Text as RNText, View } from 'react-native'; import * as Slot from '../slot'; import type { ContentProps, ContentRef, DescriptionProps, DescriptionRef, IndicatorProps, IndicatorRef, RootContext, RootProps, RootRef, TitleProps, TitleRef, } from './alert.types'; const AlertContext = createContext<(RootContext & { nativeID: string }) | null>( null ); /** * Hook to access alert root context. * Throws when used outside Alert.Root. */ function useRootContext(): RootContext & { nativeID: string } { const context = useContext(AlertContext); if (!context) { throw new Error( 'Alert compound components cannot be rendered outside the Alert component' ); } return context; } // -------------------------------------------------- const Root = forwardRef( ({ asChild, id, status = 'default', ...viewProps }, ref) => { const generatedId = useId(); const nativeID = id != null ? String(id) : generatedId; const Component = asChild ? Slot.View : View; return ( ); } ); Root.displayName = 'HeroUINative.Primitive.Alert.Root'; // -------------------------------------------------- const Indicator = forwardRef( ({ asChild, ...props }, ref) => { const Component = asChild ? Slot.View : View; return ( ); } ); Indicator.displayName = 'HeroUINative.Primitive.Alert.Indicator'; // -------------------------------------------------- const Content = forwardRef( ({ asChild, ...props }, ref) => { const Component = asChild ? Slot.View : View; return ; } ); Content.displayName = 'HeroUINative.Primitive.Alert.Content'; // -------------------------------------------------- const Title = forwardRef(({ asChild, ...props }, ref) => { const { nativeID } = useRootContext(); const Component = asChild ? Slot.Text : RNText; return ( ); }); Title.displayName = 'HeroUINative.Primitive.Alert.Title'; // -------------------------------------------------- const Description = forwardRef( ({ asChild, ...props }, ref) => { const { nativeID } = useRootContext(); const Component = asChild ? Slot.Text : RNText; return ; } ); Description.displayName = 'HeroUINative.Primitive.Alert.Description'; // -------------------------------------------------- export { Content, Description, Indicator, Root, Title, useRootContext };