@kano/kbc-dialog

2.0.105 • Public • Published

kbc-dialog

A basic modal dialog plus Context Provider, or HOC injectable into Kano boilerplate apps.

Usage

import { DialogProvider, DialogConfig, DialogContext, KbcDialog } from '@kano/kbc-dialog';

const Component = (props) => {
    return (
        <DialogProvider>
            ...

            <DialogContext.Consumer>
                {(config: DialogConfig) => (
                    <KbcDialog config={config} title="Your message" />
                )}
            </DialogContext.Consumer>
            
            ...
            
            <DialogContext.Consumer>
                {(config: DialogConfig) => (
                    <button onClick={config.showDialog}>Open Dialog</button>
                )}
            </DialogContext.Consumer>

            ...
        </DialogProvider>
    )
}

If using the optional backdrop, nest the KbcDialog directly within the component div you want to be covered by the backdrop.

For Example (in Pixel Motion app):

You 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.

import { withDialog, IDialogAPI } from '@kano/kbc-dialog';

class OutsideOfNavAndBulkOfApp {
    renderDialogs = () => {
        if (this.state.currentDialog) {
            return this.state.currentDialog();
        }
        return '';
    }

    render() {
        return (
            <Main>
                ...
                <this.renderDialogs />
            </Main>
        );
    }
}

export default withDialog(OutsideOfNavAndBulkOfApp);

And with the DialogProvider being wrapper around the whole App, e.g.:

import { DialogProvider } from '@kano/kbc-dialog';

const render = (messages?: any, optRoot?: HTMLElement) => {
    ReactDOM.render(
        <DialogProvider>
            <AppProvider config={config} />
        </DialogProvider>
        optRoot || MOUNT_NODE,
    );
};

Props for KbcDialogComponent

  • config: DialogConfig - from the Context Provider
  • modifier?: string - can modify styles on the dialog container (styles should be added at app level)
  • title?: string - h4 dialog title
  • message?: string - smaller text for the dialog
  • children?: ReactNode - allows a fully customisable dialog
  • backdrop?: boolean - whether to have an overlay under the dialog, default true
  • onConfirm?: function - optional function on user selecting confirm action
  • onCancel?: function - as above, but with cancellation
  • onClose?: function - as above, but on either confirm or cancel

Custom Dialogs

There are also a selection of custom dialogs:

  • NextChallengeDialog for calling a next challenge, displays name, image and start button
  • 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
  • WarningDialog simply to warn a user of the something, for example their browser is incompatable with exporting. Only includes dismiss button.

Tracking

Component (location) Function Event / Error Name Extra Info
KbcDialog confirmHandler confirm_dialog module: kbc-dialog, action: click
KbcDialog cancelHandler cancel_dialog module: kbc-dialog, action: click
ClearDialog dismiss dismiss_dialog module: clear-dialog, action: click
ClearDialog fireCallback confirm_callback module: clear-dialog, action: click
WarningDialog okay warning_acknowledged module: warning-dialog, action: click
RewardsDialog next next_challange_requested module: rewards-dialog, action: click
RewardsDialog close close_dialog module: rewards-dialog, action: click
NextChallengeDialog fireCallback confirm_callback module: next-challenge-dialog, action: click

Readme

Keywords

none

Package Sidebar

Install

npm i @kano/kbc-dialog

Weekly Downloads

79

Version

2.0.105

License

ISC

Unpacked Size

226 kB

Total Files

56

Last publish

Collaborators

  • thefijimonster
  • dianlin61
  • jacobmorgan99
  • alexnklein
  • ellenw3lsh
  • camuskano
  • alexfiennes
  • kanocomputing
  • jamie_old_plantain