diff --git a/docs/6.x/docs/guides/migration.md b/docs/6.x/docs/guides/migration.md index 4004b584d8..0101eb5334 100644 --- a/docs/6.x/docs/guides/migration.md +++ b/docs/6.x/docs/guides/migration.md @@ -573,23 +573,20 @@ const buttonRef = React.useRef>(null); ```diff - -- -- Alert -- +- - -+ -+ Alert -+ ++ ``` -The dialog now has the `dialog` role. On web, the dialog's accessible name is set automatically by `Dialog.Title`. You can specify a different name with the new `aria-label` prop, e.g. when the dialog has no title: +The dialog now has the `dialog` role. Its accessible name is set automatically from the `title` prop. A string title is used as the `aria-label` on all platforms. Any other title is linked through `aria-labelledby`, which applies on web only, so on iOS and Android the dialog has no name unless you set `aria-label`. You can specify a different name with the new `aria-label` prop, e.g. when the dialog has no title: ```jsx - - - Are you sure? - - + ``` `Dialog` follows the same back button, Escape key and focus behavior as `Modal`, including the `initialFocusRef` and `restoreFocusRef` props. As with `Modal`, focus returns on iOS and Android only when you pass `restoreFocusRef`. @@ -599,6 +596,60 @@ When the dialog is `dismissable`, screen reader users can dismiss it with a visu - The default elevation changed from level `1` to level `3`. - The `style` prop no longer configures the background color or border radius. You can override `theme.colors.surfaceContainerHigh` and `theme.shapes.corner.extraLarge` using the `theme` prop instead. +#### Prop-based API + +The Paper 6.x `Dialog` now supports a simplified prop-based API for common dialog layouts. If your dialog previously composed `Dialog.Title`, `Dialog.Content`, and `Dialog.Actions` via `children`, migrate to the dedicated `icon`, `title`, `content`, and `actions` props. + +**Removed props** + +- **`children`** was removed from `Dialog` + +Use the dedicated props instead: + +- **`Dialog.Icon`** → **`icon`** +- **`Dialog.Title`** → **`title`** +- **`Dialog.Content`** → **`content`** +- **`Dialog.Actions`** → **`actions`** + +The new **`actions`** prop is more customizable because it accepts **`React.ReactNode[]`**. Unlike the previous `Dialog.Actions` children behavior, the dialog no longer injects **`compact`** and **`uppercase`** into action components automatically. When **`actions`** is provided, the passed components are rendered through **`Dialog.Actions`** internally. + +**New props** + +- **`scrollable`** renders the dialog content inside a scrollable area. +- **`contentProps`** passes props to the internal `Dialog.Content` when `scrollable` is not enabled. +- **`scrollAreaProps`** passes props to the internal `Dialog.ScrollArea` when `scrollable` is enabled. +- **`scrollViewProps`** passes props to the internal `ScrollView` when `scrollable` is enabled. +- **`aria-label`** overrides the title as the dialog's accessible name. Set it when the `title` isn't a string, since iOS and Android can't use `aria-labelledby`. +- **`aria-labelledby`** is the `nativeID` of an element that provides the dialog's accessible name. It takes precedence over the title and is applied on web only. + +```tsx +// Before (v5) + + + Alert + + This is a simple dialog + + + + + + + +// After (v6) + + Done + , + ]} +/> +``` + #### `Dialog.Actions` `Dialog.Actions` no longer injects `compact` on its buttons, and the default [`Button`](#button) mode is now `filled`. Add `mode="text"` to each button. At the default `small` size, the buttons are **40dp** tall, the Material Design 3 size for dialog actions. The `compact` text buttons in 5.x were **38dp** tall. For a more compact look, also add `size="extra-small"`, which is **32dp** tall: @@ -610,11 +661,13 @@ When the dialog is `dismissable`, screen reader users can dismiss it with a visu // After (v6) - - - + + Done + , + ]} +/> ``` ### Menu diff --git a/example/src/DrawerItems.tsx b/example/src/DrawerItems.tsx index 6b758e0895..6e7555c6c3 100644 --- a/example/src/DrawerItems.tsx +++ b/example/src/DrawerItems.tsx @@ -239,9 +239,11 @@ function DrawerItems() { )} - - Changing to RTL - + Due to Expo Go limitations it is impossible to change RTL dynamically. To do so, you need to create a development build of @@ -250,13 +252,13 @@ function DrawerItems() { app.json within{' '} example directory. - - - - - + } + actions={[ + , + ]} + /> ); } diff --git a/example/src/Examples/DialogExample.tsx b/example/src/Examples/DialogExample.tsx index b12cbb5c84..2936afffb8 100644 --- a/example/src/Examples/DialogExample.tsx +++ b/example/src/Examples/DialogExample.tsx @@ -113,10 +113,12 @@ const DialogExample = () => { visible={_getVisible('dialog6')} close={_toggleDialog('dialog6')} /> - + {Platform.OS === 'android' && ( + + )} & { - isSubheading?: boolean; -}; - -export const TextComponent = ({ isSubheading = false, ...props }: Props) => { - const theme = useTheme(); - - return ( - - ); -}; diff --git a/example/src/Examples/Dialogs/DialogWithCustomColors.tsx b/example/src/Examples/Dialogs/DialogWithCustomColors.tsx index e688913d11..2a82c950ba 100644 --- a/example/src/Examples/Dialogs/DialogWithCustomColors.tsx +++ b/example/src/Examples/Dialogs/DialogWithCustomColors.tsx @@ -1,6 +1,6 @@ -import { Button, Dialog, Palette } from 'react-native-paper'; +import { StyleSheet } from 'react-native'; -import { TextComponent } from './DialogTextComponent'; +import { Button, Dialog, Palette, Text } from 'react-native-paper'; const DialogWithCustomColors = ({ visible, @@ -12,26 +12,41 @@ const DialogWithCustomColors = ({ return ( - Alert - - + title={ + + Alert + + } + content={ + This is a dialog with custom colors - - - - - - + , + ]} + /> ); }; +const styles = StyleSheet.create({ + text: { + color: Palette.primary95, + }, +}); + export default DialogWithCustomColors; diff --git a/example/src/Examples/Dialogs/DialogWithDismissableBackButton.tsx b/example/src/Examples/Dialogs/DialogWithDismissableBackButton.tsx index dd52bd5d92..0c7637dc3b 100644 --- a/example/src/Examples/Dialogs/DialogWithDismissableBackButton.tsx +++ b/example/src/Examples/Dialogs/DialogWithDismissableBackButton.tsx @@ -1,7 +1,5 @@ import { Button, Dialog, Palette } from 'react-native-paper'; -import { TextComponent } from './DialogTextComponent'; - const DialogWithDismissableBackButton = ({ visible, close, @@ -14,23 +12,22 @@ const DialogWithDismissableBackButton = ({ visible={visible} dismissable={false} dismissableBackButton - > - Alert - - - This is an undismissable dialog, however you can use hardware back - button to close it! - - - - - , + - - + , + ]} + /> ); export default DialogWithDismissableBackButton; diff --git a/example/src/Examples/Dialogs/DialogWithIcon.tsx b/example/src/Examples/Dialogs/DialogWithIcon.tsx index 5a577343cd..adb728b9ff 100644 --- a/example/src/Examples/Dialogs/DialogWithIcon.tsx +++ b/example/src/Examples/Dialogs/DialogWithIcon.tsx @@ -1,41 +1,32 @@ -import { StyleSheet } from 'react-native'; - import { Button, Dialog, Palette } from 'react-native-paper'; -import { TextComponent } from './DialogTextComponent'; - const DialogWithIcon = ({ visible, close, }: { visible: boolean; close: () => void; -}) => { - return ( - - - Dialog with Icon - - - This is a dialog with new component called DialogIcon. When icon is - displayed you should center the header. - - - - - - - - ); -}; +}) => ( + + Disagree + , + , + ]} + /> +); -const styles = StyleSheet.create({ - title: { - textAlign: 'center', - }, -}); export default DialogWithIcon; diff --git a/example/src/Examples/Dialogs/DialogWithLoadingIndicator.tsx b/example/src/Examples/Dialogs/DialogWithLoadingIndicator.tsx index f70c93d576..5ff5461369 100644 --- a/example/src/Examples/Dialogs/DialogWithLoadingIndicator.tsx +++ b/example/src/Examples/Dialogs/DialogWithLoadingIndicator.tsx @@ -1,8 +1,6 @@ import { ActivityIndicator, Platform, StyleSheet, View } from 'react-native'; -import { Dialog, Palette } from 'react-native-paper'; - -import { TextComponent } from './DialogTextComponent'; +import { Dialog, Palette, Text, useTheme } from 'react-native-paper'; const DialogWithLoadingIndicator = ({ visible, @@ -11,25 +9,32 @@ const DialogWithLoadingIndicator = ({ visible: boolean; close: () => void; }) => { + const theme = useTheme(); + const textColor = { color: theme.colors.onSurfaceVariant }; + return ( - - Progress Dialog - - + - Loading..... + + Loading..... + - - + } + /> ); }; const styles = StyleSheet.create({ - flexing: { + content: { flexDirection: 'row', alignItems: 'center', }, diff --git a/example/src/Examples/Dialogs/DialogWithLongText.tsx b/example/src/Examples/Dialogs/DialogWithLongText.tsx index bd0dd349e4..b19d219529 100644 --- a/example/src/Examples/Dialogs/DialogWithLongText.tsx +++ b/example/src/Examples/Dialogs/DialogWithLongText.tsx @@ -1,8 +1,6 @@ -import { Dimensions, ScrollView, StyleSheet } from 'react-native'; +import { Dimensions, StyleSheet } from 'react-native'; -import { Button, Dialog } from 'react-native-paper'; - -import { TextComponent } from './DialogTextComponent'; +import { Dialog, Button } from 'react-native-paper'; const DialogWithLongText = ({ visible, @@ -15,64 +13,26 @@ const DialogWithLongText = ({ onDismiss={close} visible={visible} style={{ maxHeight: 0.6 * Dimensions.get('window').height }} - > - Alert - - - - Material is the metaphor - {'\n'} - {'\n'}A material metaphor is the unifying theory of a rationalized - space and a system of motion. The material is grounded in tactile - reality, inspired by the study of paper and ink, yet technologically - advanced and open to imagination and magic. - {'\n'} - {'\n'} - Surfaces and edges of the material provide visual cues that are - grounded in reality. The use of familiar tactile attributes helps - users quickly understand affordances. Yet the flexibility of the - material creates new affordances that supersede those in the physical - world, without breaking the rules of physics. - {'\n'} - {'\n'} - The fundamentals of light, surface, and movement are key to conveying - how objects move, interact, and exist in space and in relation to each - other. Realistic lighting shows seams, divides space, and indicates - moving parts. - {'\n'} - {'\n'}A material metaphor is the unifying theory of a rationalized - space and a system of motion. The material is grounded in tactile - reality, inspired by the study of paper and ink, yet technologically - advanced and open to imagination and magic. - {'\n'} - {'\n'} - Surfaces and edges of the material provide visual cues that are - grounded in reality. The use of familiar tactile attributes helps - users quickly understand affordances. Yet the flexibility of the - material creates new affordances that supersede those in the physical - world, without breaking the rules of physics. - {'\n'} - {'\n'} - The fundamentals of light, surface, and movement are key to conveying - how objects move, interact, and exist in space and in relation to each - other. Realistic lighting shows seams, divides space, and indicates - moving parts. - - - - - - - + , + ]} + /> ); const styles = StyleSheet.create({ - smallPadding: { + scrollArea: { paddingHorizontal: 0, }, - biggerPadding: { + scrollViewContent: { paddingHorizontal: 24, }, }); diff --git a/example/src/Examples/Dialogs/DialogWithRadioBtns.tsx b/example/src/Examples/Dialogs/DialogWithRadioBtns.tsx index 73b61b14eb..a53c30d149 100644 --- a/example/src/Examples/Dialogs/DialogWithRadioBtns.tsx +++ b/example/src/Examples/Dialogs/DialogWithRadioBtns.tsx @@ -1,15 +1,15 @@ import * as React from 'react'; -import { ScrollView, View, StyleSheet } from 'react-native'; +import { View, StyleSheet } from 'react-native'; import { - Button, Dialog, RadioButton, + Text, TouchableRipple, + useTheme, + Button, } from 'react-native-paper'; -import { TextComponent } from './DialogTextComponent'; - type Props = { visible: boolean; close: () => void; @@ -19,77 +19,81 @@ type CheckedState = 'normal' | 'first' | 'second' | 'third' | 'fourth'; const DialogWithRadioBtns = ({ visible, close }: Props) => { const [checked, setChecked] = React.useState('normal'); + const theme = useTheme(); + const optionTextColor = { color: theme.colors.onSurfaceVariant }; return ( - - Choose an option - - - - setChecked('normal')}> - - - - - - Option 1 - + + setChecked('normal')}> + + + - - setChecked('second')}> - - - - - - Option 2 - + + Option 1 + + + + setChecked('second')}> + + + - - setChecked('third')}> - - - - - - Option 3 - + + Option 2 + + + + setChecked('third')}> + + + - - setChecked('fourth')}> - - - - - - Option 4 - + + Option 3 + + + + setChecked('fourth')}> + + + - - - - - - - , + - - + , + ]} + /> ); }; diff --git a/example/src/Examples/Dialogs/DialogWithUndismissableBackButton.tsx b/example/src/Examples/Dialogs/DialogWithUndismissableBackButton.tsx index deae141b13..f9ea5d7abf 100644 --- a/example/src/Examples/Dialogs/DialogWithUndismissableBackButton.tsx +++ b/example/src/Examples/Dialogs/DialogWithUndismissableBackButton.tsx @@ -1,7 +1,5 @@ import { Button, Dialog, Palette } from 'react-native-paper'; -import { TextComponent } from './DialogTextComponent'; - const DialogWithUndismissableBackButton = ({ visible, close, @@ -9,23 +7,27 @@ const DialogWithUndismissableBackButton = ({ visible: boolean; close: () => void; }) => ( - - Alert - - - This dialog can be dismissed by tapping outside, however the hardware - back button will not close it! - - - - - , + - - + , + ]} + /> ); export default DialogWithUndismissableBackButton; diff --git a/example/src/Examples/Dialogs/UndismissableDialog.tsx b/example/src/Examples/Dialogs/UndismissableDialog.tsx index fa10e5c6a4..4087a3f749 100644 --- a/example/src/Examples/Dialogs/UndismissableDialog.tsx +++ b/example/src/Examples/Dialogs/UndismissableDialog.tsx @@ -1,7 +1,5 @@ import { Button, Dialog, Palette } from 'react-native-paper'; -import { TextComponent } from './DialogTextComponent'; - const UndismissableDialog = ({ visible, close, @@ -9,20 +7,27 @@ const UndismissableDialog = ({ visible: boolean; close: () => void; }) => ( - - Alert - - This is an undismissable dialog!! - - - - , + - - + , + ]} + /> ); export default UndismissableDialog; diff --git a/src/components/Dialog/Dialog.tsx b/src/components/Dialog/Dialog.tsx index 0c04801b80..7012e2518c 100644 --- a/src/components/Dialog/Dialog.tsx +++ b/src/components/Dialog/Dialog.tsx @@ -1,20 +1,25 @@ import * as React from 'react'; -import { Platform, StyleSheet } from 'react-native'; -import type { StyleProp } from 'react-native'; +import { Platform, ScrollView, StyleSheet } from 'react-native'; +import type { ScrollViewProps, StyleProp } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import DialogActions from './DialogActions'; import DialogContent from './DialogContent'; +import type { Props as DialogContentProps } from './DialogContent'; import DialogIcon from './DialogIcon'; import DialogScrollArea from './DialogScrollArea'; +import type { Props as DialogScrollAreaProps } from './DialogScrollArea'; import DialogTitle from './DialogTitle'; +import type { Props as DialogTitleProps } from './DialogTitle'; import { DialogTitleIdContext } from './utils'; import { useInternalTheme } from '../../core/theming'; import type { Elevation, ThemeProp } from '../../theme/types'; import type { OverlayFocusTarget } from '../../utils/useOverlayFocus'; +import type { IconSource } from '../Icon'; import Modal from '../Modal'; import type { SurfaceStyle } from '../Surface'; +import Text from '../Typography/Text'; export type Props = { /** @@ -34,9 +39,18 @@ export type Props = { */ dismissAccessibilityLabel?: string; /** - * Accessible name for the dialog. On web, defaults to the text of `Dialog.Title`. + * Accessible name for the dialog. Defaults to the `title` when it is a + * string. With any other title, iOS and Android have no name unless you set + * this prop, because `aria-labelledby` is web-only. */ 'aria-label'?: string; + /** + * `nativeID` of the element that provides the accessible name for the dialog. + * Only applied on web. Defaults to the dialog's title when no `aria-label` + * is set. On iOS and Android, use `aria-label` to name a dialog with a + * non-string title. + */ + 'aria-labelledby'?: string; /** * Determines Whether the dialog is visible. */ @@ -71,9 +85,44 @@ export type Props = { */ restoreFocusRef?: React.RefObject; /** - * Content of the `Dialog`. + * Icon to display above the dialog title. + */ + icon?: IconSource; + /** + * Title of the dialog. */ - children: React.ReactNode; + title?: React.ReactNode; + /** + * Props passed to `Dialog.Title` when `title` is provided. + */ + titleProps?: Omit; + /** + * Content of the dialog. Strings and numbers are rendered as Material 3 + * supporting text. + */ + content?: React.ReactNode; + /** + * Action buttons displayed at the bottom of the dialog. + * Keep their order stable between renders. + */ + actions?: React.ReactNode[]; + /** + * Whether to render the content in a `ScrollView` within the dialog scroll + * area. + */ + scrollable?: boolean; + /** + * Props passed to `Dialog.Content` when `scrollable` is not enabled. + */ + contentProps?: Omit; + /** + * Props passed to `Dialog.ScrollArea` when `scrollable` is enabled. + */ + scrollAreaProps?: Omit; + /** + * Props passed to the `ScrollView` when `scrollable` is enabled. + */ + scrollViewProps?: Omit; style?: StyleProp; /** * @optional @@ -97,8 +146,7 @@ const DIALOG_ELEVATION: Elevation = 3; * ## Usage * ```js * import * as React from 'react'; - * import { View } from 'react-native'; - * import { Button, Dialog, PaperProvider, Text } from 'react-native-paper'; + * import { Button, Dialog, PaperProvider } from 'react-native-paper'; * * const MyComponent = () => { * const [visible, setVisible] = React.useState(false); @@ -109,18 +157,18 @@ const DIALOG_ELEVATION: Elevation = 3; * * return ( * - * - * - * - * Alert - * - * This is simple dialog - * - * - * - * - * - * + * + * + * Done + * , + * ]} + * /> * * ); * }; @@ -129,12 +177,12 @@ const DIALOG_ELEVATION: Elevation = 3; * ``` */ const Dialog = ({ - children, dismissable = true, dismissableBackButton = dismissable, onDismiss, dismissAccessibilityLabel, 'aria-label': ariaLabel, + 'aria-labelledby': ariaLabelledBy, visible = false, initialFocusRef, restoreFocusRef, @@ -142,6 +190,15 @@ const Dialog = ({ theme: themeOverrides, testID, overlayTestID, + actions, + content, + icon, + scrollable, + contentProps, + scrollAreaProps, + scrollViewProps, + title, + titleProps, }: Props) => { const { right, left } = useSafeAreaInsets(); @@ -152,10 +209,26 @@ const Dialog = ({ const titleId = React.useId(); + const contentNode = + typeof content === 'string' || typeof content === 'number' ? ( + + {content} + + ) : ( + content + ); + return ( - {children} + {icon ? : null} + + {title ? ( + + {title} + + ) : null} + + {scrollable ? ( + + {contentNode} + + ) : ( + + {contentNode} + + )} + + {actions?.length ? {actions} : null} ); @@ -196,6 +297,9 @@ Dialog.ScrollArea = DialogScrollArea; Dialog.Icon = DialogIcon; const styles = StyleSheet.create({ + wrapper: { + alignItems: 'center', + }, container: { /** * This prevents the shadow from being clipped on Android since Android @@ -206,7 +310,15 @@ const styles = StyleSheet.create({ */ marginVertical: Platform.OS === 'android' ? 44 : 0, justifyContent: 'flex-start', - paddingTop: 24, + width: '100%', + minWidth: 280, + maxWidth: 560, + }, + titleWithIcon: { + textAlign: 'center', + }, + firstChild: { + marginTop: 24, }, }); diff --git a/src/components/Dialog/DialogActions.tsx b/src/components/Dialog/DialogActions.tsx index 29ab90006f..cae3d68f80 100644 --- a/src/components/Dialog/DialogActions.tsx +++ b/src/components/Dialog/DialogActions.tsx @@ -31,12 +31,14 @@ export type Props = ViewProps & { * const hideDialog = () => setVisible(false); * * return ( - * - * - * - * - * - * + * Disagree, + * , + * ]} + * /> * ); * }; * diff --git a/src/components/Dialog/DialogContent.tsx b/src/components/Dialog/DialogContent.tsx index 9af1742608..c44d8ee904 100644 --- a/src/components/Dialog/DialogContent.tsx +++ b/src/components/Dialog/DialogContent.tsx @@ -16,7 +16,7 @@ export type Props = ViewProps & { * ## Usage * ```js * import * as React from 'react'; - * import { Dialog, Text } from 'react-native-paper'; + * import { Dialog } from 'react-native-paper'; * * const MyComponent = () => { * const [visible, setVisible] = React.useState(false); @@ -24,11 +24,11 @@ export type Props = ViewProps & { * const hideDialog = () => setVisible(false); * * return ( - * - * - * This is simple dialog - * - * + * * ); * }; * diff --git a/src/components/Dialog/DialogIcon.tsx b/src/components/Dialog/DialogIcon.tsx index 56cdfa0d64..50b3e40736 100644 --- a/src/components/Dialog/DialogIcon.tsx +++ b/src/components/Dialog/DialogIcon.tsx @@ -30,14 +30,12 @@ export type Props = { }; /** - * @supported Available in v5.x with theme version 3 * A component to show an icon in a Dialog. * * ## Usage * ```js * import * as React from 'react'; - * import { StyleSheet } from 'react-native'; - * import { Dialog, Text } from 'react-native-paper'; + * import { Dialog } from 'react-native-paper'; * * const MyComponent = () => { * const [visible, setVisible] = React.useState(false); @@ -45,22 +43,15 @@ export type Props = { * const hideDialog = () => setVisible(false); * * return ( - * - * - * This is a title - * - * This is simple dialog - * - * + * * ); * }; * - * const styles = StyleSheet.create({ - * title: { - * textAlign: 'center', - * }, - * }) - * * export default MyComponent; * ``` */ @@ -74,7 +65,7 @@ const DialogIcon = ({ const theme = useInternalTheme(themeOverrides); const { colors } = theme; - //@ts-ignore + // @ts-ignore const iconColor = color || colors.secondary; return ( @@ -91,7 +82,7 @@ const styles = StyleSheet.create({ alignItems: 'center', justifyContent: 'center', marginBottom: 16, - paddingTop: 0, + paddingTop: 24, }, }); diff --git a/src/components/Dialog/DialogScrollArea.tsx b/src/components/Dialog/DialogScrollArea.tsx index bc0e03792d..e2a45ae640 100644 --- a/src/components/Dialog/DialogScrollArea.tsx +++ b/src/components/Dialog/DialogScrollArea.tsx @@ -24,8 +24,7 @@ export type Props = ViewProps & { * ## Usage * ```js * import * as React from 'react'; - * import { ScrollView } from 'react-native'; - * import { Dialog, Text } from 'react-native-paper'; + * import { Dialog } from 'react-native-paper'; * * const MyComponent = () => { * const [visible, setVisible] = React.useState(false); @@ -33,13 +32,12 @@ export type Props = ViewProps & { * const hideDialog = () => setVisible(false); * * return ( - * - * - * - * This is a scrollable area - * - * - * + * * ); * }; * @@ -50,7 +48,7 @@ const DialogScrollArea = (props: Props) => { const theme = useInternalTheme(props.theme); const { colors } = theme; const borderStyles = { - borderColor: colors.surfaceVariant, + borderColor: colors.outlineVariant, borderTopWidth: 1, borderBottomWidth: 1, }; diff --git a/src/components/Dialog/DialogTitle.tsx b/src/components/Dialog/DialogTitle.tsx index f60672f78e..c7dcc5d825 100644 --- a/src/components/Dialog/DialogTitle.tsx +++ b/src/components/Dialog/DialogTitle.tsx @@ -25,7 +25,7 @@ export type Props = React.ComponentPropsWithRef & { * ## Usage * ```js * import * as React from 'react'; - * import { Dialog, Text } from 'react-native-paper'; + * import { Dialog } from 'react-native-paper'; * * const MyComponent = () => { * const [visible, setVisible] = React.useState(false); @@ -33,12 +33,12 @@ export type Props = React.ComponentPropsWithRef & { * const hideDialog = () => setVisible(false); * * return ( - * - * This is a title - * - * This is simple dialog - * - * + * * ); * }; * diff --git a/src/components/Dialog/utils.ts b/src/components/Dialog/utils.ts index 2d546489f5..8ef4abe307 100644 --- a/src/components/Dialog/utils.ts +++ b/src/components/Dialog/utils.ts @@ -1,10 +1,5 @@ import * as React from 'react'; -import type { StyleProp, ViewStyle } from 'react-native'; export const DialogTitleIdContext = React.createContext( undefined ); - -export type DialogChildProps = { - style?: StyleProp; -}; diff --git a/src/components/__tests__/Dialog.test.tsx b/src/components/__tests__/Dialog.test.tsx index 6fb88f7281..57446aa5a3 100644 --- a/src/components/__tests__/Dialog.test.tsx +++ b/src/components/__tests__/Dialog.test.tsx @@ -26,12 +26,10 @@ afterEach(() => { }); describe('Dialog', () => { - it('renders passed children', async () => { + it('should render passed content', async () => { await render( - - This is simple dialog - + ); @@ -45,9 +43,12 @@ describe('Dialog', () => { await render( - - This is simple dialog - + ); @@ -70,9 +71,8 @@ describe('Dialog', () => { onDismiss={onDismiss} dismissable={false} overlayTestID="backdrop" - > - This is simple dialog - + content="This is simple dialog" + /> ); @@ -100,9 +100,8 @@ describe('Dialog', () => { dismissable={false} overlayTestID="backdrop" dismissableBackButton - > - This is simple dialog - + content="This is simple dialog" + /> ); @@ -127,67 +126,108 @@ describe('Dialog', () => { it('does not add a top margin to a title-first dialog', async () => { await render( - - - Test Dialog Content - - + This is simple dialog} + content="This is simple dialog" + /> ); - expect(screen.getByTestId('dialog-title')).toHaveStyle({ - marginTop: 0, + const element = screen.getByTestId('dialog-title').parent; + + expect(element).toHaveStyle({ + marginTop: 24, }); }); - it('should keep the bottom padding on a content-first dialog', async () => { + it('adds a top padding to an icon-first dialog', async () => { await render( - - - Test Dialog Content - - + Icon} + title="Alert" + content="This is simple dialog" + /> ); - expect(screen.getByTestId('dialog-content')).toHaveStyle({ - paddingBottom: 24, - }); + const element = screen.getByTestId('dialog-icon').parent; + + expect(element).toHaveStyle({ paddingTop: 24 }); }); - it('should not add a top padding to an icon-first dialog', async () => { + it('does not add a top margin to the content of an icon dialog without a title', async () => { await render( - - - + Icon} + content="This is simple dialog" + contentProps={{ testID: 'dialog-content' }} + /> ); - expect(screen.getByTestId('dialog-icon')).toHaveStyle({ - paddingTop: 0, + expect(screen.getByTestId('dialog-content')).not.toHaveStyle({ + marginTop: 24, }); }); - it('should preserve the icon-to-title spacing for an icon dialog', async () => { + it('centers the title of a dialog with an icon', async () => { await render( - - - - Test Dialog Content - - + Icon} + title="Alert" + titleProps={{ testID: 'dialog-title' }} + content="This is simple dialog" + /> ); - expect(screen.getByTestId('dialog-icon')).toHaveStyle({ - marginBottom: 16, - paddingTop: 0, - }); expect(screen.getByTestId('dialog-title')).toHaveStyle({ - marginTop: 0, + textAlign: 'center', + }); + }); + + it('passes titleProps to the title', async () => { + await render( + + + + ); + + const title = screen.getByTestId('dialog-title'); + + expect(title).toHaveProp('numberOfLines', 2); + expect(title).toHaveStyle({ color: 'red' }); + }); + + it('should keep the bottom padding on a content-first dialog', async () => { + await render( + + Test Dialog Content} + contentProps={{ testID: 'dialog-content' }} + /> + + ); + + expect(screen.getByTestId('dialog-content')).toHaveStyle({ + paddingBottom: 24, }); }); @@ -196,10 +236,7 @@ describe('Dialog', () => { await render( - - Alert - This is simple dialog - + ); @@ -209,9 +246,155 @@ describe('Dialog', () => { it('uses aria-label over the title as the accessible name', async () => { await render( - - Alert - + + + ); + + expect(screen.getByLabelText('Confirm deletion')).toHaveProp( + 'role', + 'dialog' + ); + expect(screen.queryByLabelText('Alert')).not.toBeOnTheScreen(); + }); + + it('labels the dialog by a non-string title on web', async () => { + jest.replaceProperty(Platform, 'OS', 'web'); + + await render( + + Custom title} + content="This is simple dialog" + /> + + ); + + // No `aria-label` fallback exists for a node title, so the name can only + // come from `aria-labelledby` pointing at the rendered title. + expect(screen.getByLabelText('Custom title')).toHaveProp('role', 'dialog'); + }); + + it('prefers an explicit aria-labelledby over the title on web', async () => { + jest.replaceProperty(Platform, 'OS', 'web'); + + await render( + + External heading + Custom title} + content="This is simple dialog" + /> + + ); + + expect(screen.getByLabelText('External heading')).toHaveProp( + 'role', + 'dialog' + ); + expect(screen.queryByLabelText('Custom title')).not.toBeOnTheScreen(); + }); + + it('prefers aria-label over the title when both are set on web', async () => { + jest.replaceProperty(Platform, 'OS', 'web'); + + await render( + + Custom title} + content="This is simple dialog" + /> + + ); + + expect(screen.getByLabelText('Confirm deletion')).toHaveProp( + 'role', + 'dialog' + ); + expect(screen.queryByLabelText('Custom title')).not.toBeOnTheScreen(); + }); + + it('prefers an explicit aria-labelledby over a string title on web', async () => { + jest.replaceProperty(Platform, 'OS', 'web'); + + await render( + + External heading + + + ); + + expect(screen.getByLabelText('External heading')).toHaveProp( + 'role', + 'dialog' + ); + }); + + it('ignores aria-labelledby and uses a string title on native', async () => { + jest.replaceProperty(Platform, 'OS', 'ios'); + + await render( + + + + ); + + expect(screen.getByLabelText('Alert')).toHaveProp('role', 'dialog'); + }); + + it('prefers aria-labelledby over aria-label on web', async () => { + jest.replaceProperty(Platform, 'OS', 'web'); + + await render( + + External heading + + + ); + + expect(screen.getByLabelText('External heading')).toHaveProp( + 'role', + 'dialog' + ); + }); + + it('ignores aria-labelledby and uses aria-label on native', async () => { + jest.replaceProperty(Platform, 'OS', 'ios'); + + await render( + + ); @@ -221,15 +404,92 @@ describe('Dialog', () => { ); expect(screen.queryByLabelText('Alert')).not.toBeOnTheScreen(); }); + + it('uses the title as aria-label on native', async () => { + jest.replaceProperty(Platform, 'OS', 'ios'); + + await render( + + + + ); + + expect(screen.getByLabelText('Alert')).toHaveProp('role', 'dialog'); + }); + + it('should render a content', async () => { + await render( + + + Ok + , + ]} + /> + + ); + + expect(screen.getByText('Content')).toBeOnTheScreen(); + }); + + it('should render string content in a scroll area', async () => { + await render( + + + + ); + + expect(screen.getByTestId('dialog-scroll-area')).toBeOnTheScreen(); + expect(screen.getByTestId('dialog-scroll-view')).toBeOnTheScreen(); + expect(screen.getByText('Scrollable content')).toBeOnTheScreen(); + }); + + it('should render passed action', async () => { + await render( + + jest.fn()} testID="cancel-btn"> + Cancel + , + ]} + /> + + ); + + expect(screen.getByTestId('cancel-btn')).toBeOnTheScreen(); + }); }); describe('DialogActions', () => { - it('renders passed children', async () => { + it('should render passed actions', async () => { await render( - - - - + + + Cancel + , + , + ]} + /> + ); expect(screen.getByTestId('button-cancel')).toBeOnTheScreen(); diff --git a/src/components/__tests__/MenuInDialog.test.tsx b/src/components/__tests__/MenuInDialog.test.tsx index 3196f0121a..3438200380 100644 --- a/src/components/__tests__/MenuInDialog.test.tsx +++ b/src/components/__tests__/MenuInDialog.test.tsx @@ -38,8 +38,11 @@ const MenuInDialog = ({ onDismissMenu: () => void; }) => ( - - + - - + } + /> ); diff --git a/src/components/__tests__/Portal.test.tsx b/src/components/__tests__/Portal.test.tsx index 64ff1121bb..999355ba74 100644 --- a/src/components/__tests__/Portal.test.tsx +++ b/src/components/__tests__/Portal.test.tsx @@ -100,9 +100,7 @@ it('keeps dialog content accessible above a modal when both are visible', async {}}> modal content - {}}> - dialog content - + {}} content="dialog content" /> );