• Stars
    star
    634
  • Rank 70,925 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Pure JavaScript React-Native dialog

react-native-dialog

npm version

A flexible pure JavaScript React-Native dialog that follows closely the native UI guidelines.

Features

  • Support for iOS and Android (JavaScript API)
  • A flexible declarative API
  • Follows closely the UI of native dialogs/alerts
  • Can be used both as an alert and as an input prompt
  • Can be injected with any component
  • Supports light/dark mode

Demo

Setup

Install the library using npm or yarn:

# Using npm:
$ npm install react-native-dialog
# Using yarn:
$ yarn add react-native-dialog

Usage

React-native-dialog exposes a set of components that can be used to build the UI of the dialog:

  • Dialog.Container: This component is the root component of the dialog and all the other components should be nested inside it.
  • Dialog.Title: A Text component styled as a native dialog title.
  • Dialog.Description: A Text component styled as a native dialog description.
  • Dialog.Button: A component styled as a native dialog button.
  • Dialog.Input: A TextInput component styled as a native dialog input.
  • Dialog.CodeInput: A TextInput component styled as one time code input.
  • Dialog.Switch: A native Switch component with an optional label.
  1. Import react-native-dialog:
import Dialog from "react-native-dialog";
  1. Create a dialog and nest its content inside of it:
return (
  <View>
    <Dialog.Container>
      <Dialog.Title>Account delete</Dialog.Title>
      <Dialog.Description>
        Do you want to delete this account? You cannot undo this action.
      </Dialog.Description>
      <Dialog.Button label="Cancel" />
      <Dialog.Button label="Delete" />
    </Dialog.Container>
  </View>
);
  1. Then simply show it by setting the visible prop to true:
return (
  <View>
    <Dialog.Container visible={true}>
      <Dialog.Title>Account delete</Dialog.Title>
      <Dialog.Description>
        Do you want to delete this account? You cannot undo this action.
      </Dialog.Description>
      <Dialog.Button label="Cancel" />
      <Dialog.Button label="Delete" />
    </Dialog.Container>
  </View>
);

The visible prop is the only prop you'll really need to make the dialog work: you should control this prop value by saving it in your state and setting it to true or false when needed.

A complete example

The following example consists in a component (DialogTester) with a button and a dialog. The dialog is controlled by the dialogVisible state variable and it is initially hidden since its value is false. Pressing the button sets dialogVisible to true, making the dialog visible. Inside the dialog there are two buttons that, when pressed, set dialogVisible to false, hiding the dialog.

import React, { useState } from "react";
import { Button, StyleSheet, View } from "react-native";
import Dialog from "react-native-dialog";

export default function App() {
  const [visible, setVisible] = useState(false);

  const showDialog = () => {
    setVisible(true);
  };

  const handleCancel = () => {
    setVisible(false);
  };

  const handleDelete = () => {
    // The user has pressed the "Delete" button, so here you can do your own logic.
    // ...Your logic
    setVisible(false);
  };

  return (
    <View style={styles.container}>
      <Button title="Show dialog" onPress={showDialog} />
      <Dialog.Container visible={visible}>
        <Dialog.Title>Account delete</Dialog.Title>
        <Dialog.Description>
          Do you want to delete this account? You cannot undo this action.
        </Dialog.Description>
        <Dialog.Button label="Cancel" onPress={handleCancel} />
        <Dialog.Button label="Delete" onPress={handleDelete} />
      </Dialog.Container>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
});

Available props

Dialog.Button props

Name Type Default Description
label string REQUIRED The label text
color string #007ff9 on iOS, #169689 on Android The label color
bold bool false Show the label with a bold font weight?
disabled bool false Disable the button?
onPress func REQUIRED Called when the button is pressed

Dialog.Description props

Name Type Default Description
children string REQUIRED The description text

Dialog.Container props

Name Type Default Description
blurComponentIOS node A low-opacity The blur component used in iOS
visible bool REQUIRED Show the dialog?
children node REQUIRED The dialog content
contentStyle any undefined Extra style applied to the dialog content
headerStyle any undefined Extra style applied to the dialog header
footerStyle any undefined Extra style applied to the dialog footer
buttonSeparatorStyle any undefined Extra style applied to the dialog button separator
onBackdropPress func undefined Callback invoked when the backdrop is pressed
onRequestClose func undefined Callback invoked when the hardware back button on Android or the menu button on Apple TV is pressed
keyboardVerticalOffset number undefined keyboardVerticalOffset for iOS
verticalButtons bool false Renders button vertically
useNativeDriver bool false Defines if animations should use native driver

Dialog.Input props

Name Type Default Description
label string undefined The input floating label
wrapperStyle any undefined The style applied to the input wrapper View
textInputRef ref undefined Ref to the input
unstableLabelStyle any undefined Likely to be removed in a future version. See issue #141 for discussion

Dialog.Input also accepts all the React-Native's TextInput component props.

Dialog.CodeInput props

Name Type Default Description
wrapperStyle any undefined The style applied to the input wrapper View
digitContainerStyle any undefined The style applied to the digit container View
digitContainerFocusedStyle any undefined The style applied to the digit container View when in focus
digitStyle any undefined The style applied to the digit text
codeLength number 4 The total number of digits
onCodeChange func undefined Called when the input changed

Dialog.CodeInput also accepts all the React-Native's TextInput component props.

Dialog.Title props

Name Type Default Description
children string REQUIRED The title text

Dialog.Title also accepts all the React-Native's Text component props.

Dialog.Switch props

Name Type Default Description
label string undefined The switch description text
unstableLabelStyle any undefined Likely to be removed in a future version. See issue #141 for discussion

Dialog.Switch also accepts all the React-Native's Switch component props.

Frequently Asked Questions

How can I use a custom blur component as the dialog background on iOS?

To achieve a look even closer to the native iOS dialog you can provide your own component in the blurComponentIOS prop of a Dialog.Container and it will be injected in the dialog to be used as a background. The blurComponentIOS can be useful for example if you want to apply a native blur effect to the dialog. Here is an example using react-native-blur:

const blurComponentIOS = (
  <BlurView style={StyleSheet.absoluteFill} blurType="xlight" blurAmount={50} />
);
return (
  <View style={styles.container}>
    <Dialog.Container visible={visible} blurComponentIOS={blurComponentIOS}>
      <Dialog.Title>Account delete</Dialog.Title>
      <Dialog.Description>
        Do you want to delete this account? You cannot undo this action.
      </Dialog.Description>
      <Dialog.Button label="Cancel" onPress={handleCancel} />
      <Dialog.Button label="Delete" onPress={handleConfirm} />
    </Dialog.Container>
  </View>
);

How can I add a 'tap outside dialog' callback?

react-native-dialog uses a thin abstraction on top of the React-Native's modal component. Any properties you add to Dialog.Container are mapped through to the modal. The modal has an onBackdropPress property that can be used to register clicks on the backdrop.

Below is an example of how you can close the dialog by tapping outside.

const [visible, setVisible] = useState(true);

const handleCancel = () => {
  setVisible(false);
};

return (
  <Dialog.Container visible={visible} onBackdropPress={handleCancel}>
    <Dialog.Title>Title</Dialog.Title>
    <Dialog.Button label="Cancel" onPress={handleCancel} />
  </Dialog.Container>
);

Acknowledgments

Thanks to the user @honaf who has kindly offered the react-native-dialog namespace. Also thanks to the user @leecade who offered the namespace react-native-alert (which has not been used since "Dialog" seems to suit better this component) and to @tyxou for the entire codebase refactoring to hooks.

More Repositories

1

react-native-modal-datetime-picker

A React-Native datetime-picker for Android and iOS
JavaScript
2,809
star
2

react-native-universal-monorepo

React Native boilerplate supporting multiple platforms: Android, iOS, macOS, Windows, web, browser extensions, Electron.
JavaScript
1,619
star
3

breathly-app

A tiny breath training app built with React-Native
TypeScript
477
star
4

ordinary-puzzles-app

Mobile and web puzzle game built with React-Native
TypeScript
469
star
5

react-native-login-animation-example

A React-Native login animation example
JavaScript
350
star
6

tangerine-monorepo

A "fast" TypeScript-based Node.js monorepo setup powered by esbuild & turborepo
TypeScript
265
star
7

tap-the-number

A simple React-Native game for iOS
JavaScript
157
star
8

react-native-monorepo-tools

Tools and utils to support a React Native monorepo built with Yarn Workspaces
JavaScript
115
star
9

top-github

Android app for browsing GitHub top repositories
Java
95
star
10

firebase-recyclerview

Generic Firebase recyclerview list for Android
Java
91
star
11

react-native-beacons-android

A React-Native library for detecting beacons on Android
Java
73
star
12

the-starter-app

An in-depth tutorial about building mobile apps using React-Native
TypeScript
67
star
13

eslint-plugin-react-app

ESLint configuration used by Create React App
JavaScript
51
star
14

react-native-radio-button

Just a simple radio button
JavaScript
48
star
15

just-tap

A first test drive of hooks, redux and redux-saga in React-Native!
TypeScript
39
star
16

easy-bookmarks

Bookmarks saving and sharing app for Android (with Firebase)
Java
38
star
17

create-react-app-electron-boilerplate

JavaScript
34
star
18

alfred-jira-search

An Alfred workflow to search for Jira tickets
Shell
26
star
19

jira-express

Browser extension to quickly access your Jira tickets.
TypeScript
21
star
20

react-native-starter

React-Native/Redux/Parse
JavaScript
20
star
21

url-metadata-scraper

Tiny Vercel serverless function to scrape metadata from a URL
JavaScript
15
star
22

serverino

Tiny CLI-based static server
JavaScript
13
star
23

trees-and-tents-sample

React-Native implementation of the Trees and Tents puzzle game
JavaScript
13
star
24

build-it-bigger

Udacity Android Nanodegree: Project 4
Java
12
star
25

pinboard-api-proxy

Tiny proxy server to allow all origins to fetch the Pinboard API
JavaScript
11
star
26

reason-react-material-ui-demo

A simple form built with Reason React and Material UI
Reason
8
star
27

react-native-trello-login

Use Trello's API token-based authentication in React-Native
JavaScript
8
star
28

img-resizable

Resizable polymer <img>
HTML
6
star
29

which-one

A small React + Typescript + MobX game
TypeScript
6
star
30

some-react-native-components

components components components components components components components
4
star
31

visual-ui-testing-example

Using Playwright and GitHub action to automate visual UI testing
TypeScript
4
star
32

bookmarkpack-server

NodeJS/Express/Mongoose REST API server
JavaScript
3
star
33

multi-pacman

A multiplayer pacman for Android (Libgdx + Google Play Games Services)
Java
3
star
34

bookmarkpack-client-angular

JavaScript
2
star
35

touch-delay

Fixing the small (~100ms) touch delay on mobile
JavaScript
2
star
36

breathly-website

Breathly app landing page
HTML
2
star
37

polymer-scroll-threshold

aka core-scroll-threshold for polymer 1.0
HTML
2
star
38

miniexec

Minimalist approach to running shell commands in Deno
TypeScript
1
star
39

spotify-streamer-stage-2

Udacity Android Nanodegree: Project 2
Java
1
star
40

dotfiles

Simple dotfiles
Shell
1
star
41

react-toolbox-postcss-boilerplate

A minimal boilerplate for react-toolbox + postcss
JavaScript
1
star
42

quick-reminders-cli

Tiny Deno CLI to create new reminders in the Apple's Reminders app using a natural language parser
TypeScript
1
star