@alifd/chat 0.1.6 → 0.1.8

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.
@@ -83,7 +83,7 @@ export function useAutoHide(dom, { enable, leftSize, interactiveEdges, }) {
83
83
  setHide(hide);
84
84
  };
85
85
  const debounceTriggerHide = useDebounce(triggerHide, 100);
86
- const throttleSetHideByMouse = useThrottle(setHideByMouse, 250);
86
+ const throttleSetHideByMouse = useThrottle(setHideByMouse, 50);
87
87
  const throttleSetHideByApi = useThrottle(setHideByApi, 250);
88
88
  useEffect(() => {
89
89
  if (!dom) {
@@ -6,8 +6,8 @@ export declare function useNestleEdge(dom: HTMLElement | null, { enable, safeAre
6
6
  enable?: boolean;
7
7
  safeAreaMargin?: Margin;
8
8
  interactiveEdges?: Edge[];
9
- onUpdate?: (position: Position) => void | false;
10
- onUpdateEnd?: (position: Position) => void;
9
+ onUpdate?: (position: Position, edge: Edge) => void | false;
10
+ onUpdateEnd?: (position: Position, edge: Edge) => void;
11
11
  }): {
12
- update: (animation?: boolean) => void;
12
+ update: (animation?: boolean | undefined) => void;
13
13
  };
@@ -1,17 +1,10 @@
1
- import { useEffect, useLayoutEffect, useRef } from 'react';
2
- import { useDebounce } from '../../utils';
1
+ import { useEffect, useLayoutEffect } from 'react';
2
+ import { throttle, useDebounce, useThrottle } from '../../utils';
3
3
  import { getScreenSize, getNearlyEdge, adjustBySafeAreaMargin } from '../util';
4
4
  /**
5
5
  * 吸边逻辑
6
6
  */
7
7
  export function useNestleEdge(dom, { enable, safeAreaMargin, interactiveEdges, onUpdate, onUpdateEnd, }) {
8
- const eventTimerRef = useRef();
9
- const triggerUpdateEnd = (position) => {
10
- clearTimeout(eventTimerRef.current);
11
- eventTimerRef.current = setTimeout(() => {
12
- onUpdateEnd === null || onUpdateEnd === void 0 ? void 0 : onUpdateEnd(position);
13
- }, 300);
14
- };
15
8
  const update = (animation = true) => {
16
9
  if (!enable || !dom) {
17
10
  return;
@@ -20,7 +13,7 @@ export function useNestleEdge(dom, { enable, safeAreaMargin, interactiveEdges, o
20
13
  const { left, top, width, height } = rect;
21
14
  const { width: screenWidth, height: screenHeight } = getScreenSize();
22
15
  const [edge] = getNearlyEdge(dom, interactiveEdges, rect);
23
- const position = { left, top };
16
+ let position = { left, top };
24
17
  switch (edge) {
25
18
  case 'top': {
26
19
  position.top = 0;
@@ -38,30 +31,45 @@ export function useNestleEdge(dom, { enable, safeAreaMargin, interactiveEdges, o
38
31
  position.left = 0;
39
32
  break;
40
33
  }
41
- // no default
34
+ default: {
35
+ return;
36
+ }
42
37
  }
43
- if (position.left !== left || position.left !== top) {
44
- const { left, top } = adjustBySafeAreaMargin(position, dom.getBoundingClientRect(), safeAreaMargin) ||
45
- position;
46
- const shouldContinue = onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({ left, top });
47
- if (shouldContinue === false) {
38
+ position = adjustBySafeAreaMargin(position, { width, height }, safeAreaMargin) || position;
39
+ if (position.left !== left || position.top !== top) {
40
+ if ((onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(position, edge)) === false) {
48
41
  return;
49
42
  }
50
43
  dom.style.left = '0px';
51
44
  dom.style.top = '0px';
52
45
  if (animation) {
53
46
  dom.style.transition = 'transform 0.25s';
47
+ let isRemoved = false;
48
+ const onEnd = throttle(() => {
49
+ onUpdateEnd === null || onUpdateEnd === void 0 ? void 0 : onUpdateEnd(position, edge);
50
+ dom.removeEventListener('transitionend', onEnd);
51
+ isRemoved = true;
52
+ }, 10);
53
+ dom.addEventListener('transitionend', onEnd);
54
+ // 超时 500ms 移除监听
55
+ setTimeout(() => {
56
+ if (!isRemoved) {
57
+ dom.removeEventListener('transitionend', onEnd);
58
+ }
59
+ }, 500);
60
+ dom.style.transform = `translate(${position.left}px, ${position.top}px)`;
54
61
  }
55
62
  else {
56
63
  dom.style.transition = '';
64
+ dom.style.transform = `translate(${position.left}px, ${position.top}px)`;
65
+ onUpdateEnd === null || onUpdateEnd === void 0 ? void 0 : onUpdateEnd(position, edge);
57
66
  }
58
- dom.style.transform = `translate(${left}px, ${top}px)`;
59
- triggerUpdateEnd({ left, top });
60
67
  }
61
68
  };
62
69
  const debounceUpdate = useDebounce(() => update(), 100);
70
+ const throttleUpdate = useThrottle(update, 16);
63
71
  useLayoutEffect(() => {
64
- update(false);
72
+ throttleUpdate(false);
65
73
  });
66
74
  useEffect(() => {
67
75
  window.addEventListener('resize', debounceUpdate);
@@ -70,6 +78,6 @@ export function useNestleEdge(dom, { enable, safeAreaMargin, interactiveEdges, o
70
78
  };
71
79
  }, []);
72
80
  return {
73
- update,
81
+ update: throttleUpdate,
74
82
  };
75
83
  }
@@ -164,6 +164,19 @@ export interface FloatButtonProps extends CommonProps {
164
164
  * @defaultValue true
165
165
  */
166
166
  autoNestleEdge?: boolean;
167
+ /**
168
+ * 当触发边缘吸附前的回调
169
+ * @param position - 吸附位置
170
+ * @param edge - 吸附的边
171
+ * @returns false 则不允许吸附
172
+ */
173
+ beforeNestleEdge?: (position: Position, edge: Edge) => false | void;
174
+ /**
175
+ * 吸附到边缘后的回调
176
+ * @param position - 吸附位置
177
+ * @param edge - 吸附的边
178
+ */
179
+ afterNestleEdge?: (position: Position, edge: Edge) => void;
167
180
  /**
168
181
  * 在屏幕边缘时是否自动隐藏
169
182
  */
@@ -181,10 +194,6 @@ export interface FloatButtonProps extends CommonProps {
181
194
  * 触发节点
182
195
  */
183
196
  trigger: NonNullable<BalloonProps['trigger']>;
184
- /**
185
- * 隐藏时的触发节点,默认为 trigger
186
- */
187
- hiddenTrigger?: BalloonProps['trigger'];
188
197
  /**
189
198
  * 触发方式
190
199
  * @defaultValue 'click'
@@ -5,4 +5,7 @@ export declare function getScreenSize(): {
5
5
  };
6
6
  export declare function getNearlyEdge(el: HTMLElement, interactiveEdges?: Edge[], rect?: DOMRect): [] | [nearestEdge: Edge, subNearestEdge?: Edge];
7
7
  export declare function isInScreen(dom: HTMLElement): boolean;
8
- export declare function adjustBySafeAreaMargin(position: Position | undefined, rect: DOMRect | null, safeAreaMargin?: Margin): Position | undefined;
8
+ export declare function adjustBySafeAreaMargin(position: Position | undefined, rect: {
9
+ width: number;
10
+ height: number;
11
+ } | null, safeAreaMargin?: Margin): Position | undefined;
@@ -27,7 +27,7 @@ import { useDragable } from '../hooks/useDragable';
27
27
  * | SPACE | Trigger the onClick event |
28
28
  */
29
29
  const FloatButton = forwardRef((props, ref) => {
30
- const { className, style, addonAfter, addonBefore, dragable, safeAreaMargin = [10, 10, 10, 10], autoNestleEdge = true, interactiveEdges = ['left', 'right'], autoHide, autoAlign, leftSizeOfHidden, defaultPosition, trigger, triggerType = 'click', showClose = false, align, balloonProps, children, _renderView } = props, others = __rest(props, ["className", "style", "addonAfter", "addonBefore", "dragable", "safeAreaMargin", "autoNestleEdge", "interactiveEdges", "autoHide", "autoAlign", "leftSizeOfHidden", "defaultPosition", "trigger", "triggerType", "showClose", "align", "balloonProps", "children", "_renderView"]);
30
+ const { className, style, addonAfter, addonBefore, dragable, safeAreaMargin = [10, 10, 10, 10], autoNestleEdge = true, interactiveEdges = ['left', 'right'], autoHide, autoAlign, leftSizeOfHidden, defaultPosition, trigger, triggerType = 'click', showClose = false, align, balloonProps, children, _renderView, beforeNestleEdge, afterNestleEdge } = props, others = __rest(props, ["className", "style", "addonAfter", "addonBefore", "dragable", "safeAreaMargin", "autoNestleEdge", "interactiveEdges", "autoHide", "autoAlign", "leftSizeOfHidden", "defaultPosition", "trigger", "triggerType", "showClose", "align", "balloonProps", "children", "_renderView", "beforeNestleEdge", "afterNestleEdge"]);
31
31
  const elRef = useRef(null);
32
32
  const triggerRef = useRef(null);
33
33
  const [el, setEl] = useState(null);
@@ -63,14 +63,18 @@ const FloatButton = forwardRef((props, ref) => {
63
63
  enable: !!autoNestleEdge,
64
64
  safeAreaMargin,
65
65
  interactiveEdges,
66
- onUpdate(position) {
66
+ onUpdate(position, edge) {
67
67
  if (isHideRef.current) {
68
68
  saveRestorePosition(position);
69
69
  return false;
70
70
  }
71
+ if ((beforeNestleEdge === null || beforeNestleEdge === void 0 ? void 0 : beforeNestleEdge(position, edge)) === false) {
72
+ return false;
73
+ }
71
74
  },
72
- onUpdateEnd() {
75
+ onUpdateEnd(position, edge) {
73
76
  updateAlign();
77
+ afterNestleEdge === null || afterNestleEdge === void 0 ? void 0 : afterNestleEdge(position, edge);
74
78
  },
75
79
  });
76
80
  useImperativeHandle(ref, () => ({
@@ -100,7 +104,7 @@ const FloatButton = forwardRef((props, ref) => {
100
104
  },
101
105
  onDragend() {
102
106
  if (!isHideRef.current) {
103
- Promise.resolve().then(() => updateNestleEdge());
107
+ updateNestleEdge();
104
108
  }
105
109
  },
106
110
  });
@@ -22,7 +22,7 @@ function getMountTarget(prefix, target) {
22
22
  parent = document.createElement('div');
23
23
  parent.classList.add(`${prefix}outer`);
24
24
  parent.dataset.chat = 'drawer';
25
- oldParent.appendChild(parent);
25
+ oldParent.insertBefore(parent, dom);
26
26
  parent.appendChild(dom);
27
27
  }
28
28
  let mountTarget = parent.childNodes[1];
@@ -35,11 +35,27 @@ function getMountTarget(prefix, target) {
35
35
  parent.appendChild(mountTarget);
36
36
  }
37
37
  dom.classList.add(`${prefix}inner-start`);
38
- return mountTarget;
38
+ return {
39
+ mountTarget,
40
+ unmount() {
41
+ // 这里直接根据 mountTarget 的 dom 结构来卸载,防止 dom 结构发生变化导致报错
42
+ if (!document.body.contains(mountTarget)) {
43
+ return;
44
+ }
45
+ const outer = mountTarget.parentElement;
46
+ if (!outer.dataset.chat) {
47
+ return;
48
+ }
49
+ const dom = outer.childNodes[0];
50
+ dom.classList.remove(`${prefix}inner-start`);
51
+ outer.parentElement.replaceChild(dom, outer);
52
+ },
53
+ };
39
54
  }
40
55
  export function InnerDrawer({ className, prefix, target, children, trigger, triggerType, visible, onVisibleChange, closable = true, title, cache = false, width = 400, beforeOpen, beforeClose, afterOpen, afterClose, }) {
41
56
  const cls = `${prefix}inner-drawer`;
42
57
  const [mountTarget, setMountTarget] = useState(null);
58
+ const effectsRef = useRef(new Map());
43
59
  const [showPane, setShowPane] = useState(!!visible);
44
60
  const propsRef = useRef({ visible, afterOpen, afterClose });
45
61
  propsRef.current = {
@@ -54,10 +70,29 @@ export function InnerDrawer({ className, prefix, target, children, trigger, trig
54
70
  };
55
71
  useEffect(() => {
56
72
  if (visible) {
57
- setMountTarget(getMountTarget(`${cls}-`, target));
73
+ const prefix = `${cls}-`;
74
+ const results = getMountTarget(prefix, target);
75
+ if (!results) {
76
+ return;
77
+ }
78
+ setMountTarget(pre => {
79
+ var _a;
80
+ if (pre !== results.mountTarget) {
81
+ // 当挂载节点发生变化时,清除上一次的副作用,保存本次的副作用清除函数
82
+ (_a = effectsRef.current.get('mount')) === null || _a === void 0 ? void 0 : _a();
83
+ effectsRef.current.set('mount', results.unmount);
84
+ }
85
+ return results.mountTarget;
86
+ });
58
87
  setShowPane(true);
59
88
  }
60
89
  }, [visible]);
90
+ useEffect(() => {
91
+ return () => {
92
+ // 组件卸载,清除副作用
93
+ effectsRef.current.forEach(f => f());
94
+ };
95
+ }, []);
61
96
  const handleVisibleChange = (nextVisible, type) => {
62
97
  onVisibleChange === null || onVisibleChange === void 0 ? void 0 : onVisibleChange(nextVisible, type);
63
98
  };
package/es/index.js CHANGED
@@ -5,4 +5,4 @@ export { default as FloatButton } from './float-button';
5
5
  export { default as Text } from './text';
6
6
  export { default as Tag } from './tag';
7
7
  export { default as Tab } from './tab';
8
- export const version = '0.1.6';
8
+ export const version = '0.1.8';
@@ -86,7 +86,7 @@ function useAutoHide(dom, { enable, leftSize, interactiveEdges, }) {
86
86
  setHide(hide);
87
87
  };
88
88
  const debounceTriggerHide = (0, utils_1.useDebounce)(triggerHide, 100);
89
- const throttleSetHideByMouse = (0, utils_1.useThrottle)(setHideByMouse, 250);
89
+ const throttleSetHideByMouse = (0, utils_1.useThrottle)(setHideByMouse, 50);
90
90
  const throttleSetHideByApi = (0, utils_1.useThrottle)(setHideByApi, 250);
91
91
  (0, react_1.useEffect)(() => {
92
92
  if (!dom) {
@@ -6,8 +6,8 @@ export declare function useNestleEdge(dom: HTMLElement | null, { enable, safeAre
6
6
  enable?: boolean;
7
7
  safeAreaMargin?: Margin;
8
8
  interactiveEdges?: Edge[];
9
- onUpdate?: (position: Position) => void | false;
10
- onUpdateEnd?: (position: Position) => void;
9
+ onUpdate?: (position: Position, edge: Edge) => void | false;
10
+ onUpdateEnd?: (position: Position, edge: Edge) => void;
11
11
  }): {
12
- update: (animation?: boolean) => void;
12
+ update: (animation?: boolean | undefined) => void;
13
13
  };
@@ -8,13 +8,6 @@ const util_1 = require("../util");
8
8
  * 吸边逻辑
9
9
  */
10
10
  function useNestleEdge(dom, { enable, safeAreaMargin, interactiveEdges, onUpdate, onUpdateEnd, }) {
11
- const eventTimerRef = (0, react_1.useRef)();
12
- const triggerUpdateEnd = (position) => {
13
- clearTimeout(eventTimerRef.current);
14
- eventTimerRef.current = setTimeout(() => {
15
- onUpdateEnd === null || onUpdateEnd === void 0 ? void 0 : onUpdateEnd(position);
16
- }, 300);
17
- };
18
11
  const update = (animation = true) => {
19
12
  if (!enable || !dom) {
20
13
  return;
@@ -23,7 +16,7 @@ function useNestleEdge(dom, { enable, safeAreaMargin, interactiveEdges, onUpdate
23
16
  const { left, top, width, height } = rect;
24
17
  const { width: screenWidth, height: screenHeight } = (0, util_1.getScreenSize)();
25
18
  const [edge] = (0, util_1.getNearlyEdge)(dom, interactiveEdges, rect);
26
- const position = { left, top };
19
+ let position = { left, top };
27
20
  switch (edge) {
28
21
  case 'top': {
29
22
  position.top = 0;
@@ -41,30 +34,45 @@ function useNestleEdge(dom, { enable, safeAreaMargin, interactiveEdges, onUpdate
41
34
  position.left = 0;
42
35
  break;
43
36
  }
44
- // no default
37
+ default: {
38
+ return;
39
+ }
45
40
  }
46
- if (position.left !== left || position.left !== top) {
47
- const { left, top } = (0, util_1.adjustBySafeAreaMargin)(position, dom.getBoundingClientRect(), safeAreaMargin) ||
48
- position;
49
- const shouldContinue = onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({ left, top });
50
- if (shouldContinue === false) {
41
+ position = (0, util_1.adjustBySafeAreaMargin)(position, { width, height }, safeAreaMargin) || position;
42
+ if (position.left !== left || position.top !== top) {
43
+ if ((onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(position, edge)) === false) {
51
44
  return;
52
45
  }
53
46
  dom.style.left = '0px';
54
47
  dom.style.top = '0px';
55
48
  if (animation) {
56
49
  dom.style.transition = 'transform 0.25s';
50
+ let isRemoved = false;
51
+ const onEnd = (0, utils_1.throttle)(() => {
52
+ onUpdateEnd === null || onUpdateEnd === void 0 ? void 0 : onUpdateEnd(position, edge);
53
+ dom.removeEventListener('transitionend', onEnd);
54
+ isRemoved = true;
55
+ }, 10);
56
+ dom.addEventListener('transitionend', onEnd);
57
+ // 超时 500ms 移除监听
58
+ setTimeout(() => {
59
+ if (!isRemoved) {
60
+ dom.removeEventListener('transitionend', onEnd);
61
+ }
62
+ }, 500);
63
+ dom.style.transform = `translate(${position.left}px, ${position.top}px)`;
57
64
  }
58
65
  else {
59
66
  dom.style.transition = '';
67
+ dom.style.transform = `translate(${position.left}px, ${position.top}px)`;
68
+ onUpdateEnd === null || onUpdateEnd === void 0 ? void 0 : onUpdateEnd(position, edge);
60
69
  }
61
- dom.style.transform = `translate(${left}px, ${top}px)`;
62
- triggerUpdateEnd({ left, top });
63
70
  }
64
71
  };
65
72
  const debounceUpdate = (0, utils_1.useDebounce)(() => update(), 100);
73
+ const throttleUpdate = (0, utils_1.useThrottle)(update, 16);
66
74
  (0, react_1.useLayoutEffect)(() => {
67
- update(false);
75
+ throttleUpdate(false);
68
76
  });
69
77
  (0, react_1.useEffect)(() => {
70
78
  window.addEventListener('resize', debounceUpdate);
@@ -73,7 +81,7 @@ function useNestleEdge(dom, { enable, safeAreaMargin, interactiveEdges, onUpdate
73
81
  };
74
82
  }, []);
75
83
  return {
76
- update,
84
+ update: throttleUpdate,
77
85
  };
78
86
  }
79
87
  exports.useNestleEdge = useNestleEdge;
@@ -164,6 +164,19 @@ export interface FloatButtonProps extends CommonProps {
164
164
  * @defaultValue true
165
165
  */
166
166
  autoNestleEdge?: boolean;
167
+ /**
168
+ * 当触发边缘吸附前的回调
169
+ * @param position - 吸附位置
170
+ * @param edge - 吸附的边
171
+ * @returns false 则不允许吸附
172
+ */
173
+ beforeNestleEdge?: (position: Position, edge: Edge) => false | void;
174
+ /**
175
+ * 吸附到边缘后的回调
176
+ * @param position - 吸附位置
177
+ * @param edge - 吸附的边
178
+ */
179
+ afterNestleEdge?: (position: Position, edge: Edge) => void;
167
180
  /**
168
181
  * 在屏幕边缘时是否自动隐藏
169
182
  */
@@ -181,10 +194,6 @@ export interface FloatButtonProps extends CommonProps {
181
194
  * 触发节点
182
195
  */
183
196
  trigger: NonNullable<BalloonProps['trigger']>;
184
- /**
185
- * 隐藏时的触发节点,默认为 trigger
186
- */
187
- hiddenTrigger?: BalloonProps['trigger'];
188
197
  /**
189
198
  * 触发方式
190
199
  * @defaultValue 'click'
@@ -5,4 +5,7 @@ export declare function getScreenSize(): {
5
5
  };
6
6
  export declare function getNearlyEdge(el: HTMLElement, interactiveEdges?: Edge[], rect?: DOMRect): [] | [nearestEdge: Edge, subNearestEdge?: Edge];
7
7
  export declare function isInScreen(dom: HTMLElement): boolean;
8
- export declare function adjustBySafeAreaMargin(position: Position | undefined, rect: DOMRect | null, safeAreaMargin?: Margin): Position | undefined;
8
+ export declare function adjustBySafeAreaMargin(position: Position | undefined, rect: {
9
+ width: number;
10
+ height: number;
11
+ } | null, safeAreaMargin?: Margin): Position | undefined;
@@ -29,7 +29,7 @@ const useDragable_1 = require("../hooks/useDragable");
29
29
  * | SPACE | Trigger the onClick event |
30
30
  */
31
31
  const FloatButton = (0, react_1.forwardRef)((props, ref) => {
32
- const { className, style, addonAfter, addonBefore, dragable, safeAreaMargin = [10, 10, 10, 10], autoNestleEdge = true, interactiveEdges = ['left', 'right'], autoHide, autoAlign, leftSizeOfHidden, defaultPosition, trigger, triggerType = 'click', showClose = false, align, balloonProps, children, _renderView } = props, others = tslib_1.__rest(props, ["className", "style", "addonAfter", "addonBefore", "dragable", "safeAreaMargin", "autoNestleEdge", "interactiveEdges", "autoHide", "autoAlign", "leftSizeOfHidden", "defaultPosition", "trigger", "triggerType", "showClose", "align", "balloonProps", "children", "_renderView"]);
32
+ const { className, style, addonAfter, addonBefore, dragable, safeAreaMargin = [10, 10, 10, 10], autoNestleEdge = true, interactiveEdges = ['left', 'right'], autoHide, autoAlign, leftSizeOfHidden, defaultPosition, trigger, triggerType = 'click', showClose = false, align, balloonProps, children, _renderView, beforeNestleEdge, afterNestleEdge } = props, others = tslib_1.__rest(props, ["className", "style", "addonAfter", "addonBefore", "dragable", "safeAreaMargin", "autoNestleEdge", "interactiveEdges", "autoHide", "autoAlign", "leftSizeOfHidden", "defaultPosition", "trigger", "triggerType", "showClose", "align", "balloonProps", "children", "_renderView", "beforeNestleEdge", "afterNestleEdge"]);
33
33
  const elRef = (0, react_1.useRef)(null);
34
34
  const triggerRef = (0, react_1.useRef)(null);
35
35
  const [el, setEl] = (0, react_1.useState)(null);
@@ -65,14 +65,18 @@ const FloatButton = (0, react_1.forwardRef)((props, ref) => {
65
65
  enable: !!autoNestleEdge,
66
66
  safeAreaMargin,
67
67
  interactiveEdges,
68
- onUpdate(position) {
68
+ onUpdate(position, edge) {
69
69
  if (isHideRef.current) {
70
70
  saveRestorePosition(position);
71
71
  return false;
72
72
  }
73
+ if ((beforeNestleEdge === null || beforeNestleEdge === void 0 ? void 0 : beforeNestleEdge(position, edge)) === false) {
74
+ return false;
75
+ }
73
76
  },
74
- onUpdateEnd() {
77
+ onUpdateEnd(position, edge) {
75
78
  updateAlign();
79
+ afterNestleEdge === null || afterNestleEdge === void 0 ? void 0 : afterNestleEdge(position, edge);
76
80
  },
77
81
  });
78
82
  (0, react_1.useImperativeHandle)(ref, () => ({
@@ -102,7 +106,7 @@ const FloatButton = (0, react_1.forwardRef)((props, ref) => {
102
106
  },
103
107
  onDragend() {
104
108
  if (!isHideRef.current) {
105
- Promise.resolve().then(() => updateNestleEdge());
109
+ updateNestleEdge();
106
110
  }
107
111
  },
108
112
  });
@@ -25,7 +25,7 @@ function getMountTarget(prefix, target) {
25
25
  parent = document.createElement('div');
26
26
  parent.classList.add(`${prefix}outer`);
27
27
  parent.dataset.chat = 'drawer';
28
- oldParent.appendChild(parent);
28
+ oldParent.insertBefore(parent, dom);
29
29
  parent.appendChild(dom);
30
30
  }
31
31
  let mountTarget = parent.childNodes[1];
@@ -38,11 +38,27 @@ function getMountTarget(prefix, target) {
38
38
  parent.appendChild(mountTarget);
39
39
  }
40
40
  dom.classList.add(`${prefix}inner-start`);
41
- return mountTarget;
41
+ return {
42
+ mountTarget,
43
+ unmount() {
44
+ // 这里直接根据 mountTarget 的 dom 结构来卸载,防止 dom 结构发生变化导致报错
45
+ if (!document.body.contains(mountTarget)) {
46
+ return;
47
+ }
48
+ const outer = mountTarget.parentElement;
49
+ if (!outer.dataset.chat) {
50
+ return;
51
+ }
52
+ const dom = outer.childNodes[0];
53
+ dom.classList.remove(`${prefix}inner-start`);
54
+ outer.parentElement.replaceChild(dom, outer);
55
+ },
56
+ };
42
57
  }
43
58
  function InnerDrawer({ className, prefix, target, children, trigger, triggerType, visible, onVisibleChange, closable = true, title, cache = false, width = 400, beforeOpen, beforeClose, afterOpen, afterClose, }) {
44
59
  const cls = `${prefix}inner-drawer`;
45
60
  const [mountTarget, setMountTarget] = (0, react_1.useState)(null);
61
+ const effectsRef = (0, react_1.useRef)(new Map());
46
62
  const [showPane, setShowPane] = (0, react_1.useState)(!!visible);
47
63
  const propsRef = (0, react_1.useRef)({ visible, afterOpen, afterClose });
48
64
  propsRef.current = {
@@ -57,10 +73,29 @@ function InnerDrawer({ className, prefix, target, children, trigger, triggerType
57
73
  };
58
74
  (0, react_1.useEffect)(() => {
59
75
  if (visible) {
60
- setMountTarget(getMountTarget(`${cls}-`, target));
76
+ const prefix = `${cls}-`;
77
+ const results = getMountTarget(prefix, target);
78
+ if (!results) {
79
+ return;
80
+ }
81
+ setMountTarget(pre => {
82
+ var _a;
83
+ if (pre !== results.mountTarget) {
84
+ // 当挂载节点发生变化时,清除上一次的副作用,保存本次的副作用清除函数
85
+ (_a = effectsRef.current.get('mount')) === null || _a === void 0 ? void 0 : _a();
86
+ effectsRef.current.set('mount', results.unmount);
87
+ }
88
+ return results.mountTarget;
89
+ });
61
90
  setShowPane(true);
62
91
  }
63
92
  }, [visible]);
93
+ (0, react_1.useEffect)(() => {
94
+ return () => {
95
+ // 组件卸载,清除副作用
96
+ effectsRef.current.forEach(f => f());
97
+ };
98
+ }, []);
64
99
  const handleVisibleChange = (nextVisible, type) => {
65
100
  onVisibleChange === null || onVisibleChange === void 0 ? void 0 : onVisibleChange(nextVisible, type);
66
101
  };
package/lib/index.js CHANGED
@@ -18,4 +18,4 @@ var tag_1 = require("./tag");
18
18
  Object.defineProperty(exports, "Tag", { enumerable: true, get: function () { return __importDefault(tag_1).default; } });
19
19
  var tab_1 = require("./tab");
20
20
  Object.defineProperty(exports, "Tab", { enumerable: true, get: function () { return __importDefault(tab_1).default; } });
21
- exports.version = '0.1.6';
21
+ exports.version = '0.1.8';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alifd/chat",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "A configurable component library for chat built on React.",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",