brew-js-react 0.1.0-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +5 -0
  2. package/app.js +11 -0
  3. package/dialog.d.ts +33 -0
  4. package/dialog.js +83 -0
  5. package/dist/brew-js-react.js +1060 -0
  6. package/dist/brew-js-react.js.map +1 -0
  7. package/dist/brew-js-react.min.js +2 -0
  8. package/dist/brew-js-react.min.js.map +1 -0
  9. package/entry.js +2 -0
  10. package/hooks.d.ts +20 -0
  11. package/hooks.js +34 -0
  12. package/include/zeta-dom/dom.js +3 -0
  13. package/include/zeta-dom/domUtil.js +1 -0
  14. package/include/zeta-dom/events.js +1 -0
  15. package/include/zeta-dom/util.js +1 -0
  16. package/index.d.ts +4 -0
  17. package/index.js +4 -0
  18. package/mixin.d.ts +48 -0
  19. package/mixin.js +64 -0
  20. package/mixins/AnimateMixin.d.ts +9 -0
  21. package/mixins/AnimateMixin.js +31 -0
  22. package/mixins/AnimateSequenceItemMixin.d.ts +5 -0
  23. package/mixins/AnimateSequenceItemMixin.js +15 -0
  24. package/mixins/AnimateSequenceMixin.d.ts +6 -0
  25. package/mixins/AnimateSequenceMixin.js +29 -0
  26. package/mixins/ClassNameMixin.d.ts +12 -0
  27. package/mixins/ClassNameMixin.js +41 -0
  28. package/mixins/FlyoutMixin.d.ts +14 -0
  29. package/mixins/FlyoutMixin.js +95 -0
  30. package/mixins/FocusStateMixin.d.ts +3 -0
  31. package/mixins/FocusStateMixin.js +24 -0
  32. package/mixins/LoadingStateMixin.d.ts +3 -0
  33. package/mixins/LoadingStateMixin.js +27 -0
  34. package/mixins/Mixin.d.ts +47 -0
  35. package/mixins/Mixin.js +61 -0
  36. package/mixins/ScrollableMixin.d.ts +17 -0
  37. package/mixins/ScrollableMixin.js +46 -0
  38. package/mixins/StatefulMixin.d.ts +23 -0
  39. package/mixins/StatefulMixin.js +71 -0
  40. package/mixins/StaticAttributeMixin.d.ts +5 -0
  41. package/mixins/StaticAttributeMixin.js +13 -0
  42. package/package.json +46 -0
  43. package/view.d.ts +33 -0
  44. package/view.js +92 -0
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # brew.js for React
2
+
3
+ Brew.js for React is a utility that allow brew.js features to be used on React App.
4
+
5
+ See documentation at https://hackmd.io/@misonou/brew-js-react.
package/app.js ADDED
@@ -0,0 +1,11 @@
1
+ import brew from "brew-js";
2
+
3
+ /** @type {Brew.AppInstance<Brew.WithRouter & Brew.WithI18n>} */
4
+ export var app;
5
+
6
+ brew.install('react', function (app_) {
7
+ // @ts-ignore: type inference issue
8
+ app = app_;
9
+ });
10
+
11
+ brew.defaults.react = true;
package/dialog.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ export type DialogCloseCallback<T> = (value?: T) => void;
2
+
3
+ export interface DialogState<T> {
4
+ readonly root: HTMLElement;
5
+ open: () => Promise<T | undefined>;
6
+ close: DialogCloseCallback<T>;
7
+ }
8
+
9
+ export interface DialogBaseProps<T> {
10
+ title?: string;
11
+ className?: string;
12
+ showCloseButton?: boolean;
13
+ modal?: boolean;
14
+ onCommit?: (value: T | undefined) => void;
15
+ onOpen?: (root: HTMLElement) => void;
16
+ onClose?: (root: HTMLElement) => void;
17
+ }
18
+
19
+ export interface DialogRenderComponentProps<T> extends DialogBaseProps<T> {
20
+ closeDialog: DialogCloseCallback<T>;
21
+ }
22
+
23
+ export interface DialogOptions<T> extends DialogBaseProps<T> {
24
+ onRender: React.FC<DialogRenderComponentProps<T>>;
25
+ }
26
+
27
+ export interface DialogProps<T> extends React.PropsWithChildren<DialogBaseProps<T>> {
28
+ isOpen: boolean;
29
+ }
30
+
31
+ export function createDialog<T = any>(props: DialogOptions<T>): DialogState<T>;
32
+
33
+ export function Dialog<T>(props: DialogProps<T>): JSX.Element;
package/dialog.js ADDED
@@ -0,0 +1,83 @@
1
+ import React, { useEffect, useState } from "react";
2
+ import ReactDOM from "react-dom";
3
+ import brew from "brew-js";
4
+ import { always, either, extend, noop } from "./include/zeta-dom/util.js";
5
+ import { containsOrEquals, removeNode } from "./include/zeta-dom/domUtil.js";
6
+ import dom from "./include/zeta-dom/dom.js";
7
+
8
+ /**
9
+ * @param {Partial<import("./dialog").DialogOptions<any>>} props
10
+ */
11
+ export function createDialog(props) {
12
+ var root = document.createElement('div');
13
+ var closing = false;
14
+ var promise;
15
+
16
+ function closeDialog(value) {
17
+ if (!closing) {
18
+ closing = true;
19
+ brew.closeFlyout(root, value).then(function () {
20
+ closing = false;
21
+ removeNode(root);
22
+ (props.onClose || noop)(root);
23
+ if (props.onRender) {
24
+ ReactDOM.unmountComponentAtNode(root);
25
+ }
26
+ });
27
+ }
28
+ }
29
+
30
+ return {
31
+ root: root,
32
+ close: closeDialog,
33
+ open: function () {
34
+ if (promise) {
35
+ return promise;
36
+ }
37
+ root.className = props.className || '';
38
+ document.body.appendChild(root);
39
+ dom.retainFocus(dom.activeElement, root);
40
+ if (props.modal) {
41
+ root.setAttribute('is-modal', '');
42
+ dom.setModal(root);
43
+ }
44
+ if (props.onRender) {
45
+ ReactDOM.render(React.createElement(props.onRender, extend({ closeDialog }, props)), root);
46
+ }
47
+
48
+ promise = brew.openFlyout(root);
49
+ always(promise, function () {
50
+ promise = null;
51
+ });
52
+ promise.then(props.onCommit);
53
+ (props.onOpen || noop)(root);
54
+ return promise;
55
+ }
56
+ };
57
+ }
58
+
59
+ /**
60
+ * @param {import("./dialog").DialogProps} props
61
+ */
62
+ export function Dialog(props) {
63
+ const _props = useState(props)[0];
64
+ const dialog = useState(function () {
65
+ return createDialog(_props);
66
+ })[0];
67
+ extend(_props, props);
68
+
69
+ useEffect(function () {
70
+ var opened = containsOrEquals(dom.root, dialog.root);
71
+ if (either(opened, _props.isOpen)) {
72
+ if (!opened) {
73
+ dialog.open();
74
+ } else {
75
+ dialog.close();
76
+ }
77
+ }
78
+ }, [_props.isOpen])
79
+ useEffect(function () {
80
+ return dialog.close;
81
+ }, [dialog]);
82
+ return ReactDOM.createPortal(props.children, dialog.root);
83
+ }