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 @crossbuildui/alert
cbui-cli
globally and authenticated your account. This will ensure you can access all the necessary features.Import
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
.
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
, and faded
.
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</View>
Colors
Set the thematic color using the color
prop. Supported colors include primary
, secondary
, success
, warning
, danger
, and default
(default).
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
.
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>
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.
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
).
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
).
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).
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.
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
PROP | TYPE | DEFAULT | DESCRIPTION |
---|---|---|---|
title | ReactNode | - | Main title of the alert. |
description | ReactNode | - | Detailed description text. |
icon | React.ReactElement | - | Custom icon element. |
color | AlertColor | 'default' | Semantic color. |
variant | AlertVariant | 'solid' | Visual style. |
radius | AlertRadius | 'md' | Border radius. |
startContent | ReactNode | - | Content at the start. |
endContent | ReactNode | - | Content at the end. |
isVisible | boolean | true | Controls visibility. |
isClosable | boolean | false | Show close button. |
hideIcon | boolean | false | Hide the icon. |
hideIconWrapper | boolean | false | Hide icon's wrapper styling. |
closeButtonProps | PressableProps & { icon?: ReactNode } | - | Props for the close button. |
style | StyleProp<ViewStyle> | - | Styles for the container. |
onClose | () => void | - | Callback when closed. |
onVisibleChange | (isVisible: boolean) => void | - | Callback for visibility change intent. |
Styling
Customize the Alert appearance using the style
prop for the main container or the styles
prop (if available, though not explicitly shown in the provided Alert.tsx for slots) for more granular control. The provided Alert.tsx
uses a flat StyleSheet
. For slot-based styling, the component would need to expose a styles
prop that maps to internal elements.
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 mainstyle
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.
Alert.tsx
component does not explicitly implement a styles
prop for slot-based styling like some other components. Customization is primarily through the main style
prop and individual props like closeButtonProps
. The example below demonstrates how one might style if such a prop existed or by targeting with the main style
prop and carefully considering internal structure.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
hasaccessibilityRole="alert"
. - If
isClosable
is true, the close button hasaccessibilityRole="button"
andaccessibilityLabel="Close alert"
. - Ensure that
title
anddescription
provide clear and concise information.