0.13.10 • Published 1 year ago

dialogic-mithril v0.13.10

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Dialogic for Mithril

Manage dialogs and notifications.

API

See: Main documentation

Demo

Codesandbox examples:

Demo code in this repo:

  • ./packages/demo-dialogic-mithril
  • ./packages/demo-dialogic-mithril-router

Installation

npm install dialogic-mithril

For useDialog also install mithril-hooks:

npm install mithril-hooks

Usage

Dialog

// index.js
import m from 'mithril';
import { dialog, Dialog } from 'dialogic-mithril';

const App = {
  view: () => [
    m(
      'button',
      {
        onclick: () => {
          dialog.show({
            dialogic: {
              component: DialogView, // any component; see example below
              className: 'dialog',
            },
            title: 'Dialog Title',
          });
        },
      },
      'Show dialog',
    ),
    m(Dialog), // dialog will be drawn by this component
  ],
};

const DialogView = {
  view: ({ attrs }) =>
    m('.dialog', [
      m('.dialog-background', {
        onclick: () => dialog.hide(),
      }),
      m('.dialog-content', [m('h3', attrs.title), m('div', 'Dialog content')]),
    ]),
};
/* index.css */
.dialog {
  transition: opacity 350ms ease-in-out;
  opacity: 0;
}
.dialog-show-start {}
.dialog-show-end {
  opacity: 1;
}
.dialog-hide-start {}
.dialog-hide-end {
  opacity: 0;
}

Notification

Live example

// index.js
import m from 'mithril';
import { notification, Notification } from 'dialogic-mithril';

const App = {
  view: () => [
    m(
      '.button',
      {
        onclick: () => {
          notification.show({
            dialogic: {
              component: NotificationView, // any component; see example below
              className: 'notification',
            },
            title: 'Notification Title',
          });
        },
      },
      'Show notification',
    ),
    m(Notification), // notification will be drawn by this component
  ],
};

const NotificationView = {
  view: ({ attrs }) =>
    m('.notification', [
      m('h3', attrs.title),
      m('div', [
        m('span', 'Message'),
        // Optionally using pause/resume/isPaused:
        m(
          'button',
          {
            onclick: () =>
              notification.isPaused()
                ? notification.resume({ minimumDuration: 2000 })
                : notification.pause(),
          },
          notification.isPaused() ? 'Continue' : 'Wait',
        ),
      ]),
    ]),
};
/* index.css */
.notification {
  transition: opacity 350ms;
  opacity: 0;
}
.notification-show-start {}
.notification-show-end {
  opacity: 1;
}
.notification-hide-start {}
.notification-hide-end {
  opacity: 0;
}

useDialog

Live example

See also: useNotification and useDialogic.

useDialog requires mithril-hooks to use the React Hook API. The Mithril component is a simple view function without lifecycle hooks.

In the following example the dialog is shown when the current route matches a given path:

import m from 'mithril';
import { useDialog } from 'dialogic-mithril';
import { withHooks } from 'mithril-hooks';
import { MyDialog } from './MyDialog';

const MyComponentFn = () => {
  const dialogPath = '/profile/edit';
  const returnPath = '/profile';
  const isRouteMatch = m.route.get() === dialogPath;

  useDialog({
    isShow: isRouteMatch,
    props: {
      dialogic: {
        component: MyDialog,
        className: 'dialog',
      },
      // Props that will be passed to the MyDialog component
      returnPath,
    },
  });

  return m('div', '...');
};

export const MyComponent = withHooks(MyComponentFn);

With TypeScript

useDialog has a generic type to match the values passed to the component.

import m from 'mithril';
import { useDialog } from 'dialogic-mithril';
import { withHooks } from 'mithril-hooks';
import { MyDialog, TDialogProps } from './MyDialog';

type TProps = {
  // ...
}
const MyComponentFn = (attrs: TProps) => {
  const dialogPath = '/profile/edit';
  const returnPath = '/profile';
  const isRouteMatch = m.route.get() === dialogPath;

  useDialog<TDialogProps>({
    isShow: isRouteMatch,
    props: {
      dialogic: {
        component: MyDialog,
        className: 'dialog',
      },
      // Props that will be passed to the MyDialog component
      returnPath,
    },
  });

  return m('div', '...');
};

export const MyComponent: m.Component<TProps> = withHooks(MyComponentFn);

Calling show and hide directly

In the example below:

  • show is used to show the dialog
  • Component MyDialog receives props hideDialog to explicitly hide the dialog
  • deps includes the URL location - whenever it changes the dialog is hidden
import m from 'mithril';
import { useDialog } from 'dialogic-mithril';
import { MyDialog } from './MyDialog';

const MyComponent = () => {
  const { show, hide } = useDialog({
    deps: [window.location.href], // as soon this value changes ...
    hide: true,                   // ... hide
    props: {
      dialogic: {
        component: MyDialog,
        className: 'dialog',
      },
      // Props that will be passed to the MyDialog component
      returnPath,
      hideDialog: () => hide(),
    }
  });

  return m('button', {
    onclick: () => show()
  }, 'Show dialog');
};

useRemaining

Hook to fetch the current remaining time. This is an alternative to getRemaining

import { notification, useDialogicState, useRemaining } from 'dialogic-mithril';

const MyComponent = (attrs) => {
  const [remainingSeconds] = useRemaining({ instance: notification, roundToSeconds: true });
  // ...
}

Sizes

┌──────────────────────────────────────────────┐
│                                              │
│   Bundle Name:  dialogic-mithril.module.js   │
│   Bundle Size:  31.25 KB                     │
│   Minified Size:  15.39 KB                   │
│   Gzipped Size:  5.02 KB                     │
│                                              │
└──────────────────────────────────────────────┘

┌───────────────────────────────────────────┐
│                                           │
│   Bundle Name:  dialogic-mithril.umd.js   │
│   Bundle Size:  34.22 KB                  │
│   Minified Size:  13.2 KB                 │
│   Gzipped Size:  4.72 KB                  │
│                                           │
└───────────────────────────────────────────┘

┌────────────────────────────────────────┐
│                                        │
│   Bundle Name:  dialogic-mithril.cjs   │
│   Bundle Size:  31.58 KB               │
│   Minified Size:  15.72 KB             │
│   Gzipped Size:  5.06 KB               │
│                                        │
└────────────────────────────────────────┘
0.13.7

1 year ago

0.13.10

1 year ago

0.13.6

2 years ago

0.13.5

2 years ago

0.13.4

2 years ago

0.13.1

3 years ago

0.13.2

3 years ago

0.13.3

3 years ago

0.13.0

3 years ago

0.12.7

3 years ago

0.12.6

3 years ago

0.12.4

3 years ago

0.12.5

3 years ago

0.12.0

3 years ago

0.12.2

3 years ago

0.12.3

3 years ago

0.11.0

4 years ago

0.10.3

4 years ago

0.10.2

4 years ago

0.10.1

4 years ago

0.9.1

4 years ago

0.9.0

4 years ago

0.8.5

4 years ago

0.8.4

4 years ago

0.8.3

4 years ago

0.8.2

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.4.10

4 years ago

0.4.9

4 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.5

4 years ago

0.4.6

4 years ago

0.4.4

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.3

4 years ago

0.2.1

4 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago