Alert

The Alert component displays a short, important message in a way that attracts the user's attention without interrupting their task.

Installation

$ cbui-cli install alert

Import

index.tsx
1import { Alert } from '@/crossbuildui/alert'; 2// import Icon from 'react-native-vector-icons/MaterialIcons'; // Or your preferred icon library

Alert Component

Basic Usage

Provide a title and optionally a description.

MyComponent.tsx
1<Alert 2 title="System Update Available" 3 description="A new version of the software is ready to be installed." 4/>

Variants

Alerts support multiple visual variant options: solid (default), bordered, flat, faded, and glass.

MyComponent.tsx
1<View style={{gap: 16}}> 2 <Alert title="Solid Alert (Default)" variant="solid" color="primary" /> 3 <Alert title="Bordered Alert" variant="bordered" color="secondary" description="This alert has a border." /> 4 <Alert title="Flat Alert" variant="flat" color="success" description="This alert has a flat style." /> 5 <Alert title="Faded Alert" variant="faded" color="warning" description="This alert has a faded style." /> 6 <Alert title="Glass Alert" variant="glass" color="primary" description="This alert has a frosted glass effect." /> 7</View>

Colors

Set the thematic color using the color prop. Supported colors include primary, secondary, success, warning, danger, and default (default).

MyComponent.tsx
1<View style={{gap: 16}}> 2 <Alert title="Primary Alert" color="primary" description="This is a primary alert." /> 3 <Alert title="Secondary Alert" color="secondary" description="This is a secondary alert." /> 4 <Alert title="Success Alert" color="success" description="Operation completed successfully!" /> 5 <Alert title="Warning Alert" color="warning" description="Please check your input." /> 6 <Alert title="Danger Alert" color="danger" description="An error occurred!" /> 7 <Alert title="Default Alert" color="default" description="This is a default alert." /> 8</View>

Radius

Control the border radius with none, sm, md (default), lg, or full.

MyComponent.tsx
1<View style={{gap: 16}}> 2 <Alert title="No Radius" radius="none" color="primary" /> 3 <Alert title="Small Radius" radius="sm" color="secondary" /> 4 <Alert title="Medium Radius (Default)" radius="md" color="success" /> 5 <Alert title="Large Radius" radius="lg" color="warning" /> 6 <Alert title="Full Radius" radius="full" color="danger" /> 7</View>

Glass Variant

The glass variant applies a frosted glass effect to the alert background using expo-blur. Customize its appearance with glassIntensity (a number from 0 to 100) and glassTint ('light', 'dark', or 'default'). Text and default icon colors for this variant typically adapt to the theme's foreground color.

MyComponent.tsx
1<View style={{gap: 16}}> 2 <Alert 3 title="Light Glass Alert" 4 variant="glass" 5 glassIntensity={70} 6 glassTint="light" 7 // color="primary" // color prop has less visual impact on text/bg for glass 8 description="This alert uses a light glass effect. Text color defaults to theme foreground." 9 isClosable 10 /> 11 <Alert 12 title="Dark Glass Alert" 13 variant="glass" 14 glassIntensity={40} 15 glassTint="dark" 16 description="This alert uses a dark glass effect with a custom icon." 17 // Icon color should be set manually to contrast with the dark blur 18 icon={<Icon name="shield" size={20} color="white" />} // Ensure icon color contrasts 19 /> 20</View>

With Icon

Alerts can display a default icon based on their color (for success, warning, danger) or a custom icon. Use hideIcon to remove it.

MyComponent.tsx
1// Default icons are shown for success, warning, danger colors. 2<View style={{gap: 16}}> 3 <Alert title="Success with Default Icon" color="success" /> 4 <Alert title="Warning with Default Icon" color="warning" /> 5 <Alert title="Danger with Default Icon" color="danger" /> 6 <Alert 7 title="Custom Icon" 8 color="primary" 9 icon={<Icon name="info" size={20} color="white" />} // Assuming Icon component 10 description="This alert uses a custom info icon." 11 /> 12 <Alert title="Icon Hidden" color="secondary" hideIcon /> 13</View>

Closable

Set isClosable to true to display a close button. Manage visibility using isVisible and onVisibleChange (or onClose).

MyComponent.tsx
1function ClosableAlertExample() { 2 const [visible, setVisible] = React.useState(true); 3 4 if (!visible) { 5 return ( 6 <CrossBuildButton onPress={() => setVisible(true)}> 7 Show Alert Again 8 </CrossBuildButton> 9 ); 10 } 11 12 return ( 13 <Alert 14 title="Closable Alert" 15 description="You can close this alert by clicking the 'x' button." 16 color="info" // Assuming 'info' is a defined color or maps to 'default'/'primary' 17 isClosable 18 isVisible={visible} 19 onVisibleChange={setVisible} // Or onClose={() => setVisible(false)} 20 // Custom close button icon: 21 // closeButtonProps={{ icon: <Icon name="cancel" size={22} /> }} 22 /> 23 ); 24}

Start and End Content

Add custom elements before the icon/text (startContent) or after the text/before the close button (endContent).

MyComponent.tsx
1<View style={{gap: 16}}> 2 <Alert 3 title="Alert with Start Content" 4 color="primary" 5 startContent={<Icon name="star" size={24} color="gold" />} 6 description="This alert has a star at the beginning." 7 /> 8 <Alert 9 title="Alert with End Content" 10 color="secondary" 11 endContent={<CrossBuildButton size="sm" variant="flat" color="secondary">Action</CrossBuildButton>} 12 description="This alert has an action button at the end." 13 /> 14 <Alert 15 title="Alert with Both" 16 color="success" 17 startContent={<Icon name="thumb-up" size={20} color="white" />} 18 endContent={<Text style={{fontSize: 12, color: 'white'}}>Approved</Text>} 19 description="All good here!" 20 /> 21</View>

Controlled Visibility

The Alert component's visibility is controlled via the isVisible prop. Use a state variable in the parent component to manage this. The onVisibleChange callback informs the parent about the user's intent to close the alert (if isClosable is true).

MyComponent.tsx
1function ControlledVisibilityAlert() { 2 const [showAlert, setShowAlert] = React.useState(false); 3 4 return ( 5 <View style={{gap: 10}}> 6 <CrossBuildButton onPress={() => setShowAlert(!showAlert)}> 7 {showAlert ? 'Hide Alert' : 'Show Alert'} 8 </CrossBuildButton> 9 <Alert 10 title="Controlled Visibility" 11 description="This alert's visibility is managed by parent state." 12 color="primary" 13 isVisible={showAlert} 14 isClosable // Optional: allow user to close it too 15 onVisibleChange={setShowAlert} // Important to update parent state 16 /> 17 </View> 18 ); 19}

Hide Icon Wrapper

If your custom icon already includes spacing, you can use hideIconWrapper to remove the default padding around the icon.

MyComponent.tsx
1<Alert 2 title="Icon with No Wrapper Padding" 3 color="primary" 4 icon={<Icon name="settings" size={24} color="white" />} // Icon might have its own padding 5 hideIconWrapper 6 description="The icon's default wrapper styling is removed." 7/>

Props Overview

PROPTYPEDEFAULTDESCRIPTION
titleReactNode-Main title of the alert.
descriptionReactNode-Detailed description text.
iconReact.ReactElement-Custom icon element.
colorAlertColor ('primary' | ...)'default'Semantic color. For 'glass' variant, this primarily influences the choice of default icon; text and default icon colors typically default to theme foreground.
variantAlertVariant ('solid' | ... | 'glass')'solid'Visual style.
radiusAlertRadius'md'Border radius.
startContentReactNode-Content at the start.
endContentReactNode-Content at the end.
isVisiblebooleantrueControls visibility.
isClosablebooleanfalseShow close button.
hideIconbooleanfalseHide the icon.
hideIconWrapperbooleanfalseHide icon's wrapper styling.
closeButtonPropsPressableProps & { icon?: ReactNode }-Props for the close button.
styleStyleProp<ViewStyle>-Styles for the container.
onClose() => void-Callback when closed.
onVisibleChange(isVisible: boolean) => void-Callback for visibility change intent.
glassTint'default' | 'light' | 'dark'Theme-derivedTint for the 'glass' variant blur effect.
glassIntensitynumber50Intensity (0-100) for the 'glass' variant blur effect.

Styling

Customize the Alert appearance using the style prop for the main container. The Alert.tsx uses a flat StyleSheet for internal elements. For the glass variant, the background is rendered by BlurView from expo-blur, and its appearance is controlled by glassIntensity and glassTint props. The main style prop's backgroundColor will be overridden for the glass variant.

Based on the internal structure, potential styleable parts (if a styles prop were implemented) could be:

  • base: The main alert container. (Currently targeted by the main style prop).
  • iconWrapper: The view wrapping the icon.
  • textContent: The view wrapping title and description.
  • title: The title text.
  • description: The description text.
  • startContentWrapper: Wrapper for start content.
  • endContentWrapper: Wrapper for end content.
  • closeButton: The pressable close button.
MyComponent.tsx
1<Alert 2 title="Custom Styled Alert" 3 description="This alert has custom slot styling." 4 color="warning" 5 variant="bordered" 6 style={{ marginVertical: 10, elevation: 5 }} // Styles the outermost 'base' container 7 styles={{ 8 base: { shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.25, shadowRadius: 3.84 }, 9 iconWrapper: { backgroundColor: 'rgba(255, 165, 0, 0.1)', borderRadius: 999, padding: 6 }, 10 title: { color: 'darkorange', fontSize: 18, textTransform: 'uppercase' }, 11 description: { color: 'orange', fontStyle: 'italic' }, 12 closeButton: { backgroundColor: 'lightgray', borderRadius: 4, padding: 2 } 13 }} 14 isClosable 15 icon={<Icon name="build" size={20} color="orange" />} 16/>

Accessibility

The Alert component includes accessibility features:

  • The root View has accessibilityRole="alert".
  • If isClosable is true, the close button has accessibilityRole="button" and accessibilityLabel="Close alert".
  • Ensure that title and description provide clear and concise information.