UNPKG

4.89 kBMarkdownView Raw
1# `kbc-dialog`
2
3A basic modal dialog plus Context Provider, or HOC injectable into Kano boilerplate apps.
4
5## Usage
6
7```
8import { DialogProvider, DialogConfig, DialogContext, KbcDialog } from '@kano/kbc-dialog';
9
10const Component = (props) => {
11 return (
12 <DialogProvider>
13 ...
14
15 <DialogContext.Consumer>
16 {(config: DialogConfig) => (
17 <KbcDialog config={config} title="Your message" />
18 )}
19 </DialogContext.Consumer>
20
21 ...
22
23 <DialogContext.Consumer>
24 {(config: DialogConfig) => (
25 <button onClick={config.showDialog}>Open Dialog</button>
26 )}
27 </DialogContext.Consumer>
28
29 ...
30 </DialogProvider>
31 )
32}
33```
34
35If using the optional backdrop, nest the KbcDialog directly within the component div you want to be covered by the backdrop.
36
37#### For Example (in Pixel Motion app):
38
39You can wrap the main part of the app, since you will probably want to overlay everything (including Nav), in a HOC called `withDialog`, this will provide the hiding and showing functionality, and can be passed down as props if needed elsewhere. You can also pass up KbcDialog components to set the current dialog you want to show before triggering the dialog. If you have multiple dialogs, you can also set an array of dialogs here and trigger a specific one.
40
41```
42import { withDialog, IDialogAPI } from '@kano/kbc-dialog';
43
44class OutsideOfNavAndBulkOfApp {
45 renderDialogs = () => {
46 if (this.state.currentDialog) {
47 return this.state.currentDialog();
48 }
49 return '';
50 }
51
52 render() {
53 return (
54 <Main>
55 ...
56 <this.renderDialogs />
57 </Main>
58 );
59 }
60}
61
62export default withDialog(OutsideOfNavAndBulkOfApp);
63
64```
65
66And with the `DialogProvider` being wrapper around the whole `App`, e.g.:
67
68
69```
70import { DialogProvider } from '@kano/kbc-dialog';
71
72const render = (messages?: any, optRoot?: HTMLElement) => {
73 ReactDOM.render(
74 <DialogProvider>
75 <AppProvider config={config} />
76 </DialogProvider>
77 optRoot || MOUNT_NODE,
78 );
79};
80
81```
82
83
84## Props for KbcDialogComponent
85* __config__: _DialogConfig_ - from the Context Provider
86* __modifier__?: _string_ - can modify styles on the dialog container (styles should be added at app level)
87* __title__?: _string_ - h4 dialog title
88* __message__?: _string_ - smaller text for the dialog
89* __children__?: _ReactNode_ - allows a fully customisable dialog
90* __backdrop__?: _boolean_ - whether to have an overlay under the dialog, default true
91* __onConfirm__?: _function_ - optional function on user selecting confirm action
92* __onCancel__?: _function_ - as above, but with cancellation
93* __onClose__?: _function_ - as above, but on either confirm or cancel
94
95
96## Custom Dialogs
97
98There are also a selection of custom dialogs:
99
100* __NextChallengeDialog__ for calling a next challenge, displays name, image and start button
101* __ClearDialog__ for clearing the editor, letting a user decide if they want to lose what they are currently working on, includes confirm and dismiss buttons
102* __WarningDialog__ simply to warn a user of the something, for example their browser is incompatable with exporting. Only includes dismiss button.
103
104
105## Tracking
106
107| Component (location) | Function | Event / Error Name | Extra Info |
108| -------------------- | --------------------------------- | ----------------------------- | ---------------------------------------------------- |
109| KbcDialog | `confirmHandler` | `confirm_dialog` | module: `kbc-dialog`, action: `click` |
110| KbcDialog | `cancelHandler` | `cancel_dialog` | module: `kbc-dialog`, action: `click` |
111| ClearDialog | `dismiss` | `dismiss_dialog` | module: `clear-dialog`, action: `click` |
112| ClearDialog | `fireCallback` | `confirm_callback` | module: `clear-dialog`, action: `click` |
113| WarningDialog | `okay` | `warning_acknowledged` | module: `warning-dialog`, action: `click` |
114| RewardsDialog | `next` | `next_challange_requested` | module: `rewards-dialog`, action: `click` |
115| RewardsDialog | `close` | `close_dialog` | module: `rewards-dialog`, action: `click` |
116| NextChallengeDialog | `fireCallback` | `confirm_callback` | module: `next-challenge-dialog`, action: `click` |
\No newline at end of file