@rc-component/trigger 3.7.1 → 3.8.0

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.
@@ -3,6 +3,7 @@ import { type ResizeObserverProps } from '@rc-component/resize-observer';
3
3
  import * as React from 'react';
4
4
  import type { TriggerProps } from '../';
5
5
  import type { AlignType, ArrowPos, ArrowTypeOuter } from '../interface';
6
+ import type { PortalProps } from '@rc-component/portal';
6
7
  export interface MobileConfig {
7
8
  mask?: boolean;
8
9
  /** Set popup motion. You can ref `rc-motion` for more info. */
@@ -11,6 +12,7 @@ export interface MobileConfig {
11
12
  maskMotion?: CSSMotionProps;
12
13
  }
13
14
  export interface PopupProps {
15
+ onEsc?: PortalProps['onEsc'];
14
16
  prefixCls: string;
15
17
  className?: string;
16
18
  style?: React.CSSProperties;
package/es/Popup/index.js CHANGED
@@ -12,6 +12,7 @@ import useOffsetStyle from "../hooks/useOffsetStyle";
12
12
  import { useEvent } from '@rc-component/util';
13
13
  const Popup = /*#__PURE__*/React.forwardRef((props, ref) => {
14
14
  const {
15
+ onEsc,
15
16
  popup,
16
17
  className,
17
18
  prefixCls,
@@ -120,7 +121,8 @@ const Popup = /*#__PURE__*/React.forwardRef((props, ref) => {
120
121
  return /*#__PURE__*/React.createElement(Portal, {
121
122
  open: forceRender || isNodeVisible,
122
123
  getContainer: getPopupContainer && (() => getPopupContainer(target)),
123
- autoDestroy: autoDestroy
124
+ autoDestroy: autoDestroy,
125
+ onEsc: onEsc
124
126
  }, /*#__PURE__*/React.createElement(Mask, {
125
127
  prefixCls: prefixCls,
126
128
  open: open,
@@ -119,6 +119,7 @@ const UniqueProvider = ({
119
119
  }, /*#__PURE__*/React.createElement(Popup, {
120
120
  ref: setPopupRef,
121
121
  portal: Portal,
122
+ onEsc: mergedOptions.onEsc,
122
123
  prefixCls: prefixCls,
123
124
  popup: mergedOptions.popup,
124
125
  className: clsx(mergedOptions.popupClassName, alignedClassName, `${prefixCls}-unique-controlled`),
package/es/context.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import type { CSSMotionProps } from '@rc-component/motion';
3
+ import type { PortalProps } from '@rc-component/portal';
3
4
  import type { TriggerProps } from './index';
4
5
  import type { AlignType, ArrowTypeOuter, BuildInPlacements } from './interface';
5
6
  export interface TriggerContextProps {
@@ -28,6 +29,7 @@ export interface UniqueShowOptions {
28
29
  arrow?: ArrowTypeOuter;
29
30
  getPopupContainer?: TriggerProps['getPopupContainer'];
30
31
  getPopupClassNameFromAlign?: (align: AlignType) => string;
32
+ onEsc?: PortalProps['onEsc'];
31
33
  }
32
34
  export interface UniqueContextProps {
33
35
  show: (options: UniqueShowOptions, isOpen: () => boolean) => void;
@@ -54,12 +54,12 @@ function reversePoints(points, index) {
54
54
  l: 'r',
55
55
  r: 'l'
56
56
  };
57
- return points.map((point, i) => {
58
- if (i === index) {
59
- return reverseMap[point] || 'c';
60
- }
61
- return point;
62
- }).join('');
57
+ const clone = [...points];
58
+ clone[index] = reverseMap[points[index]] || 'c';
59
+ return clone;
60
+ }
61
+ function flatPoints(points) {
62
+ return points.join('');
63
63
  }
64
64
  export default function useAlign(open, popupEle, target, placement, builtinPlacements, popupAlign, onPopupAlign, mobile) {
65
65
  const [offsetInfo, setOffsetInfo] = React.useState({
@@ -245,6 +245,7 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
245
245
  const nextAlignInfo = {
246
246
  ...placementInfo
247
247
  };
248
+ let nextPoints = [popupPoints, targetPoints];
248
249
 
249
250
  // Next Offset
250
251
  let nextOffsetX = targetAlignPoint.x - popupAlignPoint.x + popupOffsetX;
@@ -322,7 +323,7 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
322
323
  prevFlipRef.current.bt = true;
323
324
  nextOffsetY = tmpNextOffsetY;
324
325
  popupOffsetY = -popupOffsetY;
325
- nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
326
+ nextPoints = [reversePoints(nextPoints[0], 0), reversePoints(nextPoints[1], 0)];
326
327
  } else {
327
328
  prevFlipRef.current.bt = false;
328
329
  }
@@ -346,7 +347,7 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
346
347
  prevFlipRef.current.tb = true;
347
348
  nextOffsetY = tmpNextOffsetY;
348
349
  popupOffsetY = -popupOffsetY;
349
- nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
350
+ nextPoints = [reversePoints(nextPoints[0], 0), reversePoints(nextPoints[1], 0)];
350
351
  } else {
351
352
  prevFlipRef.current.tb = false;
352
353
  }
@@ -376,7 +377,7 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
376
377
  prevFlipRef.current.rl = true;
377
378
  nextOffsetX = tmpNextOffsetX;
378
379
  popupOffsetX = -popupOffsetX;
379
- nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
380
+ nextPoints = [reversePoints(nextPoints[0], 1), reversePoints(nextPoints[1], 1)];
380
381
  } else {
381
382
  prevFlipRef.current.rl = false;
382
383
  }
@@ -400,11 +401,12 @@ export default function useAlign(open, popupEle, target, placement, builtinPlace
400
401
  prevFlipRef.current.lr = true;
401
402
  nextOffsetX = tmpNextOffsetX;
402
403
  popupOffsetX = -popupOffsetX;
403
- nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
404
+ nextPoints = [reversePoints(nextPoints[0], 1), reversePoints(nextPoints[1], 1)];
404
405
  } else {
405
406
  prevFlipRef.current.lr = false;
406
407
  }
407
408
  }
409
+ nextAlignInfo.points = [flatPoints(nextPoints[0]), flatPoints(nextPoints[1])];
408
410
 
409
411
  // ============================ Shift ============================
410
412
  syncNextPopupPosition();
package/es/index.js CHANGED
@@ -175,7 +175,8 @@ export function generateTrigger(PortalComponent = Portal) {
175
175
  arrow: innerArrow,
176
176
  getPopupContainer,
177
177
  getPopupClassNameFromAlign,
178
- id
178
+ id,
179
+ onEsc
179
180
  }));
180
181
 
181
182
  // Handle controlled state changes for UniqueProvider
@@ -233,6 +234,13 @@ export function generateTrigger(PortalComponent = Portal) {
233
234
  internalTriggerOpen(nextOpen);
234
235
  }, delay);
235
236
  };
237
+ function onEsc({
238
+ top
239
+ }) {
240
+ if (top) {
241
+ triggerOpen(false);
242
+ }
243
+ }
236
244
 
237
245
  // ========================== Motion ============================
238
246
  const [inMotion, setInMotion] = React.useState(false);
@@ -518,7 +526,8 @@ export function generateTrigger(PortalComponent = Portal) {
518
526
  ,
519
527
  forceRender: forceRender,
520
528
  autoDestroy: mergedAutoDestroy,
521
- getPopupContainer: getPopupContainer
529
+ getPopupContainer: getPopupContainer,
530
+ onEsc: onEsc
522
531
  // Arrow
523
532
  ,
524
533
  align: alignInfo,
package/es/util.js CHANGED
@@ -1,8 +1,9 @@
1
1
  function isPointsEq(a1 = [], a2 = [], isAlignPoint) {
2
+ const getVal = (a, index) => a[index] || '';
2
3
  if (isAlignPoint) {
3
- return a1[0] === a2[0];
4
+ return getVal(a1, 0) === getVal(a2, 0);
4
5
  }
5
- return a1[0] === a2[0] && a1[1] === a2[1];
6
+ return getVal(a1, 0) === getVal(a2, 0) && getVal(a1, 1) === getVal(a2, 1);
6
7
  }
7
8
  export function getAlignPopupClassName(builtinPlacements, prefixCls, align, isAlignPoint) {
8
9
  const {
@@ -3,6 +3,7 @@ import { type ResizeObserverProps } from '@rc-component/resize-observer';
3
3
  import * as React from 'react';
4
4
  import type { TriggerProps } from '../';
5
5
  import type { AlignType, ArrowPos, ArrowTypeOuter } from '../interface';
6
+ import type { PortalProps } from '@rc-component/portal';
6
7
  export interface MobileConfig {
7
8
  mask?: boolean;
8
9
  /** Set popup motion. You can ref `rc-motion` for more info. */
@@ -11,6 +12,7 @@ export interface MobileConfig {
11
12
  maskMotion?: CSSMotionProps;
12
13
  }
13
14
  export interface PopupProps {
15
+ onEsc?: PortalProps['onEsc'];
14
16
  prefixCls: string;
15
17
  className?: string;
16
18
  style?: React.CSSProperties;
@@ -21,6 +21,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
21
21
  function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
22
22
  const Popup = /*#__PURE__*/React.forwardRef((props, ref) => {
23
23
  const {
24
+ onEsc,
24
25
  popup,
25
26
  className,
26
27
  prefixCls,
@@ -129,7 +130,8 @@ const Popup = /*#__PURE__*/React.forwardRef((props, ref) => {
129
130
  return /*#__PURE__*/React.createElement(Portal, {
130
131
  open: forceRender || isNodeVisible,
131
132
  getContainer: getPopupContainer && (() => getPopupContainer(target)),
132
- autoDestroy: autoDestroy
133
+ autoDestroy: autoDestroy,
134
+ onEsc: onEsc
133
135
  }, /*#__PURE__*/React.createElement(_Mask.default, {
134
136
  prefixCls: prefixCls,
135
137
  open: open,
@@ -128,6 +128,7 @@ const UniqueProvider = ({
128
128
  }, /*#__PURE__*/React.createElement(_Popup.default, {
129
129
  ref: setPopupRef,
130
130
  portal: _portal.default,
131
+ onEsc: mergedOptions.onEsc,
131
132
  prefixCls: prefixCls,
132
133
  popup: mergedOptions.popup,
133
134
  className: (0, _clsx.clsx)(mergedOptions.popupClassName, alignedClassName, `${prefixCls}-unique-controlled`),
package/lib/context.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import type { CSSMotionProps } from '@rc-component/motion';
3
+ import type { PortalProps } from '@rc-component/portal';
3
4
  import type { TriggerProps } from './index';
4
5
  import type { AlignType, ArrowTypeOuter, BuildInPlacements } from './interface';
5
6
  export interface TriggerContextProps {
@@ -28,6 +29,7 @@ export interface UniqueShowOptions {
28
29
  arrow?: ArrowTypeOuter;
29
30
  getPopupContainer?: TriggerProps['getPopupContainer'];
30
31
  getPopupClassNameFromAlign?: (align: AlignType) => string;
32
+ onEsc?: PortalProps['onEsc'];
31
33
  }
32
34
  export interface UniqueContextProps {
33
35
  show: (options: UniqueShowOptions, isOpen: () => boolean) => void;
@@ -63,12 +63,12 @@ function reversePoints(points, index) {
63
63
  l: 'r',
64
64
  r: 'l'
65
65
  };
66
- return points.map((point, i) => {
67
- if (i === index) {
68
- return reverseMap[point] || 'c';
69
- }
70
- return point;
71
- }).join('');
66
+ const clone = [...points];
67
+ clone[index] = reverseMap[points[index]] || 'c';
68
+ return clone;
69
+ }
70
+ function flatPoints(points) {
71
+ return points.join('');
72
72
  }
73
73
  function useAlign(open, popupEle, target, placement, builtinPlacements, popupAlign, onPopupAlign, mobile) {
74
74
  const [offsetInfo, setOffsetInfo] = React.useState({
@@ -254,6 +254,7 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
254
254
  const nextAlignInfo = {
255
255
  ...placementInfo
256
256
  };
257
+ let nextPoints = [popupPoints, targetPoints];
257
258
 
258
259
  // Next Offset
259
260
  let nextOffsetX = targetAlignPoint.x - popupAlignPoint.x + popupOffsetX;
@@ -331,7 +332,7 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
331
332
  prevFlipRef.current.bt = true;
332
333
  nextOffsetY = tmpNextOffsetY;
333
334
  popupOffsetY = -popupOffsetY;
334
- nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
335
+ nextPoints = [reversePoints(nextPoints[0], 0), reversePoints(nextPoints[1], 0)];
335
336
  } else {
336
337
  prevFlipRef.current.bt = false;
337
338
  }
@@ -355,7 +356,7 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
355
356
  prevFlipRef.current.tb = true;
356
357
  nextOffsetY = tmpNextOffsetY;
357
358
  popupOffsetY = -popupOffsetY;
358
- nextAlignInfo.points = [reversePoints(popupPoints, 0), reversePoints(targetPoints, 0)];
359
+ nextPoints = [reversePoints(nextPoints[0], 0), reversePoints(nextPoints[1], 0)];
359
360
  } else {
360
361
  prevFlipRef.current.tb = false;
361
362
  }
@@ -385,7 +386,7 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
385
386
  prevFlipRef.current.rl = true;
386
387
  nextOffsetX = tmpNextOffsetX;
387
388
  popupOffsetX = -popupOffsetX;
388
- nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
389
+ nextPoints = [reversePoints(nextPoints[0], 1), reversePoints(nextPoints[1], 1)];
389
390
  } else {
390
391
  prevFlipRef.current.rl = false;
391
392
  }
@@ -409,11 +410,12 @@ function useAlign(open, popupEle, target, placement, builtinPlacements, popupAli
409
410
  prevFlipRef.current.lr = true;
410
411
  nextOffsetX = tmpNextOffsetX;
411
412
  popupOffsetX = -popupOffsetX;
412
- nextAlignInfo.points = [reversePoints(popupPoints, 1), reversePoints(targetPoints, 1)];
413
+ nextPoints = [reversePoints(nextPoints[0], 1), reversePoints(nextPoints[1], 1)];
413
414
  } else {
414
415
  prevFlipRef.current.lr = false;
415
416
  }
416
417
  }
418
+ nextAlignInfo.points = [flatPoints(nextPoints[0]), flatPoints(nextPoints[1])];
417
419
 
418
420
  // ============================ Shift ============================
419
421
  syncNextPopupPosition();
package/lib/index.js CHANGED
@@ -189,7 +189,8 @@ function generateTrigger(PortalComponent = _portal.default) {
189
189
  arrow: innerArrow,
190
190
  getPopupContainer,
191
191
  getPopupClassNameFromAlign,
192
- id
192
+ id,
193
+ onEsc
193
194
  }));
194
195
 
195
196
  // Handle controlled state changes for UniqueProvider
@@ -247,6 +248,13 @@ function generateTrigger(PortalComponent = _portal.default) {
247
248
  internalTriggerOpen(nextOpen);
248
249
  }, delay);
249
250
  };
251
+ function onEsc({
252
+ top
253
+ }) {
254
+ if (top) {
255
+ triggerOpen(false);
256
+ }
257
+ }
250
258
 
251
259
  // ========================== Motion ============================
252
260
  const [inMotion, setInMotion] = React.useState(false);
@@ -532,7 +540,8 @@ function generateTrigger(PortalComponent = _portal.default) {
532
540
  ,
533
541
  forceRender: forceRender,
534
542
  autoDestroy: mergedAutoDestroy,
535
- getPopupContainer: getPopupContainer
543
+ getPopupContainer: getPopupContainer,
544
+ onEsc: onEsc
536
545
  // Arrow
537
546
  ,
538
547
  align: alignInfo,
package/lib/util.js CHANGED
@@ -9,10 +9,11 @@ exports.getVisibleArea = getVisibleArea;
9
9
  exports.getWin = getWin;
10
10
  exports.toNum = toNum;
11
11
  function isPointsEq(a1 = [], a2 = [], isAlignPoint) {
12
+ const getVal = (a, index) => a[index] || '';
12
13
  if (isAlignPoint) {
13
- return a1[0] === a2[0];
14
+ return getVal(a1, 0) === getVal(a2, 0);
14
15
  }
15
- return a1[0] === a2[0] && a1[1] === a2[1];
16
+ return getVal(a1, 0) === getVal(a2, 0) && getVal(a1, 1) === getVal(a2, 1);
16
17
  }
17
18
  function getAlignPopupClassName(builtinPlacements, prefixCls, align, isAlignPoint) {
18
19
  const {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rc-component/trigger",
3
- "version": "3.7.1",
3
+ "version": "3.8.0",
4
4
  "description": "base abstract trigger component for react",
5
5
  "engines": {
6
6
  "node": ">=8.x"
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@rc-component/motion": "^1.1.4",
45
- "@rc-component/portal": "^2.0.0",
45
+ "@rc-component/portal": "^2.2.0",
46
46
  "@rc-component/resize-observer": "^1.0.0",
47
47
  "@rc-component/util": "^1.2.1",
48
48
  "clsx": "^2.1.1"