Dialog
Dialogs inform users about a task and can contain critical information, require decisions, or involve multiple tasks.
install | version | usage | style |
---|---|---|---|
yarn add @ciceksepeti/cui-dialog | 1.0.0 | import {Dialog} from '@ciceksepeti/cui-dialog' | import '@ciceksepeti/cui-dialog/style.css' |
Content​
Shows the relevant content in a Dialog in accordance with accessibility rules. Dialog is a polymorphic component. That's why can be rendered as any valid HTML tag
Examples​
Default​
Example of a default behavior of Dialog component. Renders as 'div' by default.
Default one works as sticky dialog window there is no close option. You should refresh the page in order to clear dialog window or you can use escape.
import React, { useState } from 'react';
import { Dialog } from '@ciceksepeti/cui-dialog';
import '@ciceksepeti/cui-dialog/styles.css';
const Example = () => {
const [open, setOpen] = useState(false);
return (
<>
<button onClick={() => setOpen(true)}>show 'default' dialog</button>
<Dialog
removeScrollBar="false"
autoFocusToLast="false"
autoFocusToFirst="false"
disableFocusTrap="false"
enableRemoveScroll="false"
restoreFocusOnUnmount="true"
open={open}
>
I am a dialog
</Dialog>
</>
);
};
Escape​
Example of when escape is pressed, it should close dialog if onEscapeKey is provided
import React, { useState } from 'react';
import { Dialog } from '@ciceksepeti/cui-dialog';
import '@ciceksepeti/cui-dialog/styles.css';
const Example = () => {
const [open, setOpen] = useState(false);
const onEscapeKey = () => {
setOpen(false);
};
const onClickOutside = () => {
setOpen(false);
};
return (
<>
<button onClick={() => setOpen(true)}>show 'escape' dialog</button>
<Dialog
removeScrollBar="false"
autoFocusToLast="false"
autoFocusToFirst="false"
disableFocusTrap="false"
enableRemoveScroll="false"
restoreFocusOnUnmount="true"
open={open}
onEscapeKey={onEscapeKey}
onClickOutside={onClickOutside}
>
<p>I am a dialog</p>
</Dialog>
</>
);
};
Trapped​
Example of rendering Focus Trapped Dialog component and large height to demonstrate scroll behavior when Dialog is open
import React, { useState } from 'react';
import { Dialog } from '@ciceksepeti/cui-dialog';
import '@ciceksepeti/cui-dialog/styles.css';
const Example = () => {
const [open, setOpen] = useState(false);
const dialogStyles = {
display: 'flex',
flexDirection: 'column',
};
return (
<div>
<button>unfocusable button1</button>
<button onClick={() => setOpen(true)}>show dialog</button>
<Dialog
removeScrollBar="true"
autoFocusToLast="false"
autoFocusToFirst="true"
disableFocusTrap="false"
enableRemoveScroll="true"
restoreFocusOnUnmount="true"
style={dialogStyles}
open={open}
>
<p>focusing with tab should not leave the dialog!</p>
<button>should be auto focused</button>
<button>second button</button>
<button onClick={() => setOpen(false)}>close button</button>
</Dialog>
</div>
);
};
Props​
Dialog Props​
Name | Type | Default | Required | Description |
as | string | div | - | Changes html tag of dialog component which renders to DOM |
open | boolean | false | required | Show/hide dialog component |
removeScrollBar | boolean | true | - | Removes scroll bar from UI |
autoFocusToLast | boolean | false | - | Auto focus to last element of dialog |
autoFocusToFirst | boolean | true | - | Auto focus to first element of dialog |
disableFocusTrap | boolean | false | - | Disables focus trap functionality |
enableRemoveScroll | boolean | true | - | Enables disabling scroll events when dialog is mounted |
restoreFocusOnUnmount | boolean | true | - | Restores focus to the element that had focus before the dialog was opened |
children | React.ReactNode | - | required | The element which shows content of a Dialog |
Dialog Events​
Name | Type | Description |
onEscapeKey | ( event: KeyboardEvent | React.KeyboardEvent<HTMLElement> ) => void | Gets called when escape button is pressed |
onClickOutside | ( event: React.MouseEvent<HTMLElement, MouseEvent | TouchEvent> ) => void | Gets called when outside of dialog is clicked |
Dialog Accessibility​
info
Name | Type | Default | Description |
aria-label | string | To provide an accessible name when the native HTML labeling element is not supported by the control. | |
aria-labelledby | string | Gives the dialog an accessible name by referring to the element that provides the dialog title. |
Playground​
Styling​
Element Selectors​
tip
Name | Type |
[data-cui-dialog-overlay] | Element Selector |
[data-cui-dialog-content] | Element Selector |
Default Selector Values​
[data-cui-dialog-overlay] {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
overflow: auto;
background: rgba(0, 0, 0, 0.22);
}
[data-cui-dialog-content] {
width: 50vw;
padding: 1rem;
margin: 10vh auto;
background-color: white;
}