14.2.2 • Published 3 days ago

@rmwc/dialog v14.2.2

Weekly downloads
5,651
License
MIT
Repository
github
Last release
3 days ago

Dialogs

Dialogs inform users about a specific task and may contain critical information, require decisions, or involve multiple tasks.

  • Module @rmwc/dialog
  • Import styles:
    • Using CSS Loader
      • import '@rmwc/dialog/styles';
    • Or include stylesheets
      • '@material/dialog/dist/mdc.dialog.css'
      • '@material/button/dist/mdc.button.css'
      • '@material/ripple/dist/mdc.ripple.css'
  • MDC Docs: https://material.io/develop/web/components/dialogs/

Standard Usage

function Example() {
  const [open, setOpen] = React.useState(false);
  return (
    <>
      <Dialog
        open={open}
        onClose={(evt) => {
          console.log(evt.detail.action);
          setOpen(false);
        }}
        onClosed={(evt) => console.log(evt.detail.action)}
      >
        <DialogTitle>Dialog Title</DialogTitle>
        <DialogContent>This is a standard dialog.</DialogContent>
        <DialogActions>
          <DialogButton action="close">Cancel</DialogButton>
          <DialogButton action="accept" isDefaultAction>
            Sweet!
          </DialogButton>
        </DialogActions>
      </Dialog>

      <Button raised onClick={() => setOpen(true)}>
        Open standard Dialog
      </Button>
    </>
  );
}

Simplified Usage

Material Dialogs are a complex component. RMWC contains an additional SimpleDialog component for ease of use that internally contains the default structure already built out. Illustrated below is both the standard and simple dialog usage.

function Example() {
  const [open, setOpen] = React.useState(false);
  return (
    <>
      <SimpleDialog
        title="This is a simple dialog"
        body="You can pass the body prop or children."
        open={open}
        onClose={(evt) => {
          console.log(evt.detail.action);
          setOpen(false);
        }}
      />

      <Button raised onClick={() => setOpen(true)}>
        Open Simple Dialog
      </Button>
    </>
  );
}

Usage with DialogQueue

Some dialog interactions are complex, but a lot of the time you just need a simple alert or confirm dialog. DialogQueue allows you to open dialogs from anywhere in your app and emulates the browsers built in alert, confirm and prompt dialogs. If you've used the SnackbarQueue, the DialogQueue is very similar.

Setup is nice and easy, create a queue object you can pass around in your code base, pass the queues dialogs to the DialogQueuecomponent, and then use the alert, prompt or confirm api to open dialogs.

  `
// Create a file that exports your queue
// myQueue.js
import { createDialogQueue } from '@rmwc/dialog';

export const queue = createDialogQueue();
  `
// Somewhere at the top level of your app
// Render the DialogQueue
import React from 'react';
import { queue } from './myQueue';

export default function App() {
  return (
    <div>
      ...
      <DialogQueue
        dialogs={queue.dialogs}
        // You can also pass default options to pass to your dialogs
        // ie, prevent all dialogs from dismissing from a click on the background scrim
        preventOutsideDismiss
      />
    </div>
  )
}

The alert, confirm, and prompt functions were designed to mimic the the built-in browser methods with a couple of small difference. First, they all return a promise. The promise will always resolve successfully with the response indicating the appropriate action. alert the response will be accept for clicking the ok button, or close. confirm will resolve true or false, and prompt will resolve with the value entered into the input, or null if the closed the dialog. Second, all methods the methods accept any valid prop for SimpleDialog.

  `
// Somewhere else in your app
// Could be a view, your redux store, anywhere you want...
import { queue } from './myQueue';

queue.alert({
  title: 'Hi there',
  body: 'Whats going on?'
});

queue.confirm({
  title: <b>Are you positive?</b>,
  body: 'You have selected pizza instead icecream!',
  acceptLabel: 'CONFIRM'
});

queue.prompt({
  title: 'Whats your name?',
  body: 'Anything will do',
  acceptLabel: 'Submit',
  cancelLabel: 'Skip',
  // For prompts only, you can pass props to the input
  inputProps: {
    outlined: true
  }
});
() => {
  const { dialogs, alert, confirm, prompt } = createDialogQueue();

  function App() {
    const [response, setResponse] = React.useState('____________');

    const fireAlert = () =>
      alert({ title: 'Hello!' }).then((res) => setResponse(res));

    const fireConfirm = () =>
      confirm({}).then((res) => setResponse(res));

    const firePrompt = () =>
      prompt({ inputProps: { outlined: true } }).then((res) =>
        setResponse(res)
      );

    return (
      <div>
        <Button label="Alert" onClick={fireAlert} />
        <Button label="Confirm" onClick={fireConfirm} />
        <Button label="Prompt" onClick={firePrompt} />
        <Button
          label="In Sequence"
          onClick={() => {
            fireAlert();
            fireConfirm();
            firePrompt();
          }}
        />

        <p>
          Response: <b>{String(response)}</b>
        </p>
        <DialogQueue dialogs={dialogs} />
      </div>
    );
  }
  return <App />;
}

Rendering through Portals

Occasionally, you may find your dialog being cut off from being inside a container that is styled to be overflow:hidden. RMWC provides a renderToPortal prop that lets you use React's portal functionality to render the menu dropdown in a different container.

You can specify any element or selector you want, but the simplest method is to pass true and use RMWC's built in Portal component.

  `
  // Somewhere at the top level of your app
  // Render the RMWC Portal
  // You only have to do this once
  import React from 'react';
  import { Portal } from '@rmwc/base';

  export default function App() {
    return (
      <div>
        ...
        <Portal />
      </div>
    )
  }
`

Now you can use the renderToPortal prop. Below is a contrived example of a dialog being cut off due to overflow: hidden.

function Example() {
  const [renderToPortal, setRenderToPortal] = React.useState(true);
  const [open, setOpen] = React.useState(false);
  return (
    <div
      id="dialog-portal-example"
      style={{
        transform: 'translateZ(0)',
        height: '20rem',
        overflow: 'hidden'
      }}
    >
      <SimpleDialog
        title={`This is a ${renderToPortal ? 'working!' : 'broken :/'}`}
        renderToPortal={renderToPortal}
        body="Use `renderToPortal` to get around `overflow:hidden` and layout issues."
        open={open}
        onClose={(evt) => {
          console.log(evt.detail.action);
          setOpen(false);
        }}
      />

      <Button
        raised
        onClick={() => {
          setRenderToPortal(false);
          setOpen(true);
        }}
      >
        Open Broken :/
      </Button>

      <Button
        raised
        onClick={() => {
          setRenderToPortal(true);
          setOpen(true);
        }}
      >
        Open in Portal
      </Button>
    </div>
  );
}

Dialog

A Dialog component.

Props

NameTypeDescription
foundationRefRef<MDCDialogFoundation<>>Advanced: A reference to the MDCFoundation.
onClose(evt: DialogOnCloseEventT) => voidCallback for when the Dialog beings to close. evt.detail = { action?: string }
onClosed(evt: DialogOnCloseEventT) => voidCallback for when the Dialog finishes closing. evt.detail = { action?: string }
onOpen(evt: DialogOnOpenEventT) => voidCallback for when the Dialog opens.
onOpened(evt: DialogOnOpenedEventT) => voidCallback for when the Dialog finishes opening
openbooleanWhether or not the Dialog is showing.
preventOutsideDismissbooleanPrevent the dialog from closing when the scrim is clicked or escape key is pressed.
renderToPortalPortalPropTRenders the dialog to a portal. Useful for situations where the dialog might be cutoff by an overflow: hidden container. You can pass "true" to render to the default RMWC portal.

DialogTitle

The Dialog title.

DialogContent

The Dialog content.

DialogActions

Actions container for the Dialog.

DialogButton

Action buttons for the Dialog.

Props

NameTypeDescription
actionstringAn action returned in evt.detail.action to the onClose handler.
childrenReactNodeContent specified as children.
dangerbooleanUsed to indicate a dangerous action.
densebooleanMake the Button dense.
disabledbooleanMake the button disabled
iconIconPropTAn Icon for the Button
isDefaultActionbooleanIndicates this is the default selected action when pressing enter
labelanyContent specified as a label prop.
outlinedbooleanMake the button outlined.
raisedbooleanMake the Button raised.
rippleRipplePropTAdds a ripple effect to the component
touchbooleanMakes the button more touch friendly. This will automatically be set true if used inside of TouchTargetWrapper.
trailingIconIconPropTA trailing icon for the Button
unelevatedbooleanMake the button unelevated.

SimpleDialog

A SimpleDialog component for ease of use.

Props

NameTypeDescription
acceptLabelReactNodeCreates an accept button for the default Dialog template with a given label. You can pass

null to remove the button. | | body | ReactNode | Body content for the default Dialog template, rendered before children. | | cancelLabel | ReactNode | Creates an cancel button for the default Dialog with a given label. You can pass null to remove the button. | | children | ReactNode | Any children will be rendered in the body of the default Dialog template. | | footer | ReactNode | Additional footer content for the default Dialog template, rendered before any buttons. | | foundationRef | Ref<MDCDialogFoundation<>> | Advanced: A reference to the MDCFoundation. | | header | ReactNode | Additional Dialog header content for the default Dialog template. | | onClose | (evt: DialogOnCloseEventT) => void | Callback for when the Dialog beings to close. evt.detail = { action?: string } | | onClosed | (evt: DialogOnCloseEventT) => void | Callback for when the Dialog finishes closing. evt.detail = { action?: string } | | onOpen | (evt: DialogOnOpenEventT) => void | Callback for when the Dialog opens. | | onOpened | (evt: DialogOnOpenedEventT) => void | Callback for when the Dialog finishes opening | | open | boolean | Whether or not the Dialog is showing. | | preventOutsideDismiss | boolean | Prevent the dialog from closing when the scrim is clicked or escape key is pressed. | | renderToPortal | PortalPropT | Renders the dialog to a portal. Useful for situations where the dialog might be cutoff by an overflow: hidden container. You can pass "true" to render to the default RMWC portal. | | title | ReactNode | A title for the default Dialog template. |

14.2.2

3 days ago

14.2.0

11 days ago

14.2.1

10 days ago

14.1.5

11 days ago

14.1.4

24 days ago

14.1.3

1 month ago

14.1.2

1 month ago

14.1.1

2 months ago

14.1.0

2 months ago

14.0.12

2 months ago

14.0.11

2 months ago

14.0.10

3 months ago

14.0.9

3 months ago

14.0.8

3 months ago

14.0.7

3 months ago

14.0.6

4 months ago

14.0.5

4 months ago

14.0.4

5 months ago

14.0.1-alpha.0

8 months ago

14.0.2-alpha.3

6 months ago

14.0.2-alpha.0

8 months ago

14.0.2-alpha.1

6 months ago

14.0.2-alpha.6

6 months ago

14.0.2-alpha.7

6 months ago

14.0.2-alpha.4

6 months ago

14.0.2-alpha.5

6 months ago

14.0.0

5 months ago

14.0.1

5 months ago

14.0.0-alpha.0

8 months ago

14.0.2

5 months ago

14.0.3

5 months ago

8.0.8

11 months ago

8.0.7

1 year ago

8.0.6

1 year ago

8.0.5

1 year ago

8.0.4

1 year ago

8.0.3

2 years ago

8.0.2

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

7.0.3

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

7.0.0

2 years ago

6.1.4

4 years ago

6.1.3

4 years ago

6.1.2

4 years ago

6.1.0

4 years ago

6.0.14

4 years ago

6.0.13

4 years ago

6.0.12

4 years ago

6.0.11

4 years ago

6.0.10

4 years ago

6.0.9

4 years ago

6.0.5

4 years ago

6.0.4

4 years ago

6.0.0-rc.5

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

6.0.3

4 years ago

6.0.2

4 years ago

6.0.0-rc.4

4 years ago

6.0.0-rc.3

4 years ago

6.0.0-rc.2

4 years ago

6.0.0-rc.1

4 years ago

6.0.0-rc.0

4 years ago

6.0.0-alpha.16

4 years ago

6.0.0-alpha.14

4 years ago

6.0.0-alpha.15

4 years ago

6.0.0-alpha.13

4 years ago

6.0.0-alpha.12

4 years ago

6.0.0-alpha.11

4 years ago

6.0.0-alpha.7

4 years ago

5.7.2

4 years ago

6.0.0-alpha.6

4 years ago

6.0.0-alpha.5

4 years ago

6.0.0-alpha.3

4 years ago

5.7.0

5 years ago

5.6.0

5 years ago

5.5.2

5 years ago

5.5.1

5 years ago

5.5.0

5 years ago

5.4.3

5 years ago

5.4.2

5 years ago

5.4.1

5 years ago

5.4.0

5 years ago

5.3.1

5 years ago

5.3.0

5 years ago

5.2.2

5 years ago

5.2.1

5 years ago

5.2.0

5 years ago

5.2.0-alpha.0

5 years ago

5.1.8

5 years ago

5.1.7

5 years ago

5.1.6

5 years ago

5.1.5

5 years ago

5.1.4

5 years ago

5.1.3

5 years ago

5.1.2

5 years ago

5.1.1

5 years ago

5.1.0

5 years ago

5.0.30-rc.0

5 years ago

5.0.29-rc.0

5 years ago

5.0.28-rc.0

5 years ago

5.0.27-rc.0

5 years ago

5.0.26-rc.0

5 years ago

5.0.25-rc.0

5 years ago

5.0.24-rc.0

5 years ago

5.0.23-rc.0

5 years ago

5.0.23-alpha.0

5 years ago

5.0.22-alpha.0

5 years ago

5.0.21-alpha.0

5 years ago

5.0.20-alpha.0

5 years ago

5.0.19-alpha.0

5 years ago

5.0.18-alpha.0

5 years ago

5.0.17-alpha.0

5 years ago

5.0.16-alpha.0

5 years ago

5.0.15-alpha.0

5 years ago

5.0.14-alpha.0

5 years ago

5.0.13-alpha.0

5 years ago

5.0.12-alpha.0

5 years ago

5.0.11-alpha.0

5 years ago

5.0.8-alpha.0

5 years ago

5.0.7-alpha.0

5 years ago

5.0.6-alpha.0

5 years ago

5.0.5-alpha.0

5 years ago

5.0.4-alpha.0

5 years ago

5.0.3-alpha.0

5 years ago

5.0.2-alpha.0

5 years ago

5.0.1-alpha.0

5 years ago

5.0.0-alpha.0

5 years ago

4.0.6

5 years ago

4.0.5

5 years ago

4.0.4

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.0.11

5 years ago

3.0.10

5 years ago

3.0.9

5 years ago

3.0.8

5 years ago

3.0.7

6 years ago

3.0.6

6 years ago

3.0.5

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.0

6 years ago

2.2.2

6 years ago

2.2.0

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

2.0.0-alpha.7

6 years ago

2.0.0-alpha.6

6 years ago

2.0.0-alpha.5

6 years ago

2.0.0-alpha.4

6 years ago

2.0.0-alpha.3

6 years ago

2.0.0-alpha.2

6 years ago

2.0.0-alpha.1

6 years ago

2.0.0-alpha.0

6 years ago

1.10.1-alpha.0

6 years ago

1.10.0-alpha.0

6 years ago