Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rc-align

Package Overview
Dependencies
Maintainers
4
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rc-align - npm Package Compare versions

Comparing version 4.0.15 to 5.0.0-0

2

es/Align.d.ts

@@ -7,3 +7,3 @@ /**

import type { AlignResult, AlignType, TargetType } from './interface';
declare type OnAlign = (source: HTMLElement, result: AlignResult) => void;
type OnAlign = (source: HTMLElement, result: AlignResult) => void;
export interface AlignProps {

@@ -10,0 +10,0 @@ align: AlignType;

import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _typeof from "@babel/runtime/helpers/esm/typeof";
/**

@@ -8,3 +7,4 @@ * Removed props:

*/
import { alignElement, alignPoint } from 'dom-align';
import { alignElement, alignPoint } from '@rc-component/dom-align';
import isEqual from "rc-util/es/isEqual";

@@ -18,3 +18,2 @@ import addEventListener from "rc-util/es/Dom/addEventListener";

import { isSamePoint, monitorResize, restoreFocus } from "./util";
function getElement(func) {

@@ -24,3 +23,2 @@ if (typeof func !== 'function') return null;

}
function getPoint(point) {

@@ -30,19 +28,19 @@ if (_typeof(point) !== 'object' || !point) return null;

}
var Align = function Align(_ref, ref) {
var children = _ref.children,
disabled = _ref.disabled,
target = _ref.target,
align = _ref.align,
onAlign = _ref.onAlign,
monitorWindowResize = _ref.monitorWindowResize,
_ref$monitorBufferTim = _ref.monitorBufferTime,
monitorBufferTime = _ref$monitorBufferTim === void 0 ? 0 : _ref$monitorBufferTim;
disabled = _ref.disabled,
target = _ref.target,
align = _ref.align,
onAlign = _ref.onAlign,
monitorWindowResize = _ref.monitorWindowResize,
_ref$monitorBufferTim = _ref.monitorBufferTime,
monitorBufferTime = _ref$monitorBufferTim === void 0 ? 0 : _ref$monitorBufferTim;
var cacheRef = React.useRef({});
/** Popup node ref */
var nodeRef = React.useRef();
var childNode = React.Children.only(children);
var nodeRef = React.useRef();
var childNode = React.Children.only(children); // ===================== Align ======================
// ===================== Align ======================
// We save the props here to avoid closure makes props ood
var forceAlignPropsRef = React.useRef({});

@@ -53,5 +51,4 @@ forceAlignPropsRef.current.disabled = disabled;

forceAlignPropsRef.current.onAlign = onAlign;
var _useBuffer = useBuffer(function () {
var _forceAlignPropsRef$c = forceAlignPropsRef.current,
var _forceAlignPropsRef$c = forceAlignPropsRef.current,
latestDisabled = _forceAlignPropsRef$c.disabled,

@@ -61,52 +58,44 @@ latestTarget = _forceAlignPropsRef$c.target,

latestOnAlign = _forceAlignPropsRef$c.onAlign;
var source = nodeRef.current;
var source = nodeRef.current;
if (!latestDisabled && latestTarget && source) {
var _result;
var _element = getElement(latestTarget);
var _point = getPoint(latestTarget);
cacheRef.current.element = _element;
cacheRef.current.point = _point;
cacheRef.current.align = latestAlign;
if (!latestDisabled && latestTarget && source) {
var _result;
// IE lose focus after element realign
// We should record activeElement and restore later
var _document = document,
activeElement = _document.activeElement;
var _element = getElement(latestTarget);
var _point = getPoint(latestTarget);
cacheRef.current.element = _element;
cacheRef.current.point = _point;
cacheRef.current.align = latestAlign; // IE lose focus after element realign
// We should record activeElement and restore later
var _document = document,
activeElement = _document.activeElement; // We only align when element is visible
if (_element && isVisible(_element)) {
_result = alignElement(source, _element, latestAlign);
} else if (_point) {
_result = alignPoint(source, _point, latestAlign);
// We only align when element is visible
if (_element && isVisible(_element)) {
_result = alignElement(source, _element, latestAlign);
} else if (_point) {
_result = alignPoint(source, _point, latestAlign);
}
restoreFocus(activeElement, source);
if (latestOnAlign && _result) {
latestOnAlign(source, _result);
}
return true;
}
return false;
}, monitorBufferTime),
_useBuffer2 = _slicedToArray(_useBuffer, 2),
_forceAlign = _useBuffer2[0],
cancelForceAlign = _useBuffer2[1];
restoreFocus(activeElement, source);
if (latestOnAlign && _result) {
latestOnAlign(source, _result);
}
return true;
}
return false;
}, monitorBufferTime),
_useBuffer2 = _slicedToArray(_useBuffer, 2),
_forceAlign = _useBuffer2[0],
cancelForceAlign = _useBuffer2[1]; // ===================== Effect =====================
// ===================== Effect =====================
// Handle props change
var _React$useState = React.useState(),
_React$useState2 = _slicedToArray(_React$useState, 2),
element = _React$useState2[0],
setElement = _React$useState2[1];
_React$useState2 = _slicedToArray(_React$useState, 2),
element = _React$useState2[0],
setElement = _React$useState2[1];
var _React$useState3 = React.useState(),
_React$useState4 = _slicedToArray(_React$useState3, 2),
point = _React$useState4[0],
setPoint = _React$useState4[1];
_React$useState4 = _slicedToArray(_React$useState3, 2),
point = _React$useState4[0],
setPoint = _React$useState4[1];
useLayoutEffect(function () {

@@ -120,14 +109,17 @@ setElement(getElement(target));

}
}); // Watch popup element resize
});
// Watch popup element resize
React.useEffect(function () {
var cancelFn = monitorResize(nodeRef.current, _forceAlign);
return cancelFn;
}, [nodeRef.current]); // Watch target element resize
}, [nodeRef.current]);
// Watch target element resize
React.useEffect(function () {
var cancelFn = monitorResize(element, _forceAlign);
return cancelFn;
}, [element]); // Listen for disabled change
}, [element]);
// Listen for disabled change
React.useEffect(function () {

@@ -139,4 +131,5 @@ if (!disabled) {

}
}, [disabled]); // Listen for window resize
}, [disabled]);
// Listen for window resize
React.useEffect(function () {

@@ -147,4 +140,5 @@ if (monitorWindowResize) {

}
}, [monitorWindowResize]); // Clear all if unmount
}, [monitorWindowResize]);
// Clear all if unmount
React.useEffect(function () {

@@ -154,4 +148,5 @@ return function () {

};
}, []); // ====================== Ref =======================
}, []);
// ====================== Ref =======================
React.useImperativeHandle(ref, function () {

@@ -163,4 +158,5 @@ return {

};
}); // ===================== Render =====================
});
// ===================== Render =====================
if ( /*#__PURE__*/React.isValidElement(childNode)) {

@@ -171,8 +167,6 @@ childNode = /*#__PURE__*/React.cloneElement(childNode, {

}
return childNode;
};
var RcAlign = /*#__PURE__*/React.forwardRef(Align);
RcAlign.displayName = 'Align';
export default RcAlign;

@@ -5,10 +5,7 @@ import React from 'react';

var timeoutRef = React.useRef(null);
function cancelTrigger() {
window.clearTimeout(timeoutRef.current);
}
function trigger(force) {
cancelTrigger();
if (!calledRef.current || force === true) {

@@ -19,3 +16,2 @@ if (callback(force) === false) {

}
calledRef.current = true;

@@ -32,3 +28,2 @@ timeoutRef.current = window.setTimeout(function () {

}
return [trigger, function () {

@@ -35,0 +30,0 @@ calledRef.current = false;

/** Two char of 't' 'b' 'c' 'l' 'r'. Example: 'lt' */
export declare type AlignPoint = string;
export type AlignPoint = string;
export interface AlignType {

@@ -56,2 +56,2 @@ /**

}
export declare type TargetType = (() => HTMLElement) | TargetPoint;
export type TargetType = (() => HTMLElement) | TargetPoint;

@@ -7,11 +7,8 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";

if (!prev || !next) return false;
if ('pageX' in next && 'pageY' in next) {
return prev.pageX === next.pageX && prev.pageY === next.pageY;
}
if ('clientX' in next && 'clientY' in next) {
return prev.clientX === next.clientX && prev.clientY === next.clientY;
}
return false;

@@ -28,16 +25,11 @@ }

var prevHeight = null;
function onResize(_ref) {
var _ref2 = _slicedToArray(_ref, 1),
target = _ref2[0].target;
target = _ref2[0].target;
if (!document.documentElement.contains(target)) return;
var _target$getBoundingCl = target.getBoundingClientRect(),
width = _target$getBoundingCl.width,
height = _target$getBoundingCl.height;
width = _target$getBoundingCl.width,
height = _target$getBoundingCl.height;
var fixedWidth = Math.floor(width);
var fixedHeight = Math.floor(height);
if (prevWidth !== fixedWidth || prevHeight !== fixedHeight) {

@@ -52,13 +44,9 @@ // https://webkit.org/blog/9997/resizeobserver-in-webkit/

}
prevWidth = fixedWidth;
prevHeight = fixedHeight;
}
var resizeObserver = new ResizeObserver(onResize);
if (element) {
resizeObserver.observe(element);
}
return function () {

@@ -65,0 +53,0 @@ resizeObserver.disconnect();

@@ -7,3 +7,3 @@ /**

import type { AlignResult, AlignType, TargetType } from './interface';
declare type OnAlign = (source: HTMLElement, result: AlignResult) => void;
type OnAlign = (source: HTMLElement, result: AlignResult) => void;
export interface AlignProps {

@@ -10,0 +10,0 @@ align: AlignType;

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {

@@ -9,25 +8,13 @@ value: true

exports.default = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _domAlign = require("dom-align");
var _domAlign = require("@rc-component/dom-align");
var _isEqual = _interopRequireDefault(require("rc-util/lib/isEqual"));
var _addEventListener = _interopRequireDefault(require("rc-util/lib/Dom/addEventListener"));
var _isVisible = _interopRequireDefault(require("rc-util/lib/Dom/isVisible"));
var _useLayoutEffect = _interopRequireDefault(require("rc-util/lib/hooks/useLayoutEffect"));
var _ref2 = require("rc-util/lib/ref");
var _react = _interopRequireDefault(require("react"));
var _useBuffer3 = _interopRequireDefault(require("./hooks/useBuffer"));
var _util = require("./util");
/**

@@ -37,2 +24,3 @@ * Removed props:

*/
function getElement(func) {

@@ -42,3 +30,2 @@ if (typeof func !== 'function') return null;

}
function getPoint(point) {

@@ -48,25 +35,20 @@ if ((0, _typeof2.default)(point) !== 'object' || !point) return null;

}
var Align = function Align(_ref, ref) {
var children = _ref.children,
disabled = _ref.disabled,
target = _ref.target,
align = _ref.align,
onAlign = _ref.onAlign,
monitorWindowResize = _ref.monitorWindowResize,
_ref$monitorBufferTim = _ref.monitorBufferTime,
monitorBufferTime = _ref$monitorBufferTim === void 0 ? 0 : _ref$monitorBufferTim;
disabled = _ref.disabled,
target = _ref.target,
align = _ref.align,
onAlign = _ref.onAlign,
monitorWindowResize = _ref.monitorWindowResize,
_ref$monitorBufferTim = _ref.monitorBufferTime,
monitorBufferTime = _ref$monitorBufferTim === void 0 ? 0 : _ref$monitorBufferTim;
var cacheRef = _react.default.useRef({});
var cacheRef = _react.default.useRef({});
/** Popup node ref */
var nodeRef = _react.default.useRef();
var childNode = _react.default.Children.only(children);
var childNode = _react.default.Children.only(children); // ===================== Align ======================
// ===================== Align ======================
// We save the props here to avoid closure makes props ood
var forceAlignPropsRef = _react.default.useRef({});
forceAlignPropsRef.current.disabled = disabled;

@@ -76,5 +58,4 @@ forceAlignPropsRef.current.target = target;

forceAlignPropsRef.current.onAlign = onAlign;
var _useBuffer = (0, _useBuffer3.default)(function () {
var _forceAlignPropsRef$c = forceAlignPropsRef.current,
var _forceAlignPropsRef$c = forceAlignPropsRef.current,
latestDisabled = _forceAlignPropsRef$c.disabled,

@@ -84,52 +65,44 @@ latestTarget = _forceAlignPropsRef$c.target,

latestOnAlign = _forceAlignPropsRef$c.onAlign;
var source = nodeRef.current;
var source = nodeRef.current;
if (!latestDisabled && latestTarget && source) {
var _result;
var _element = getElement(latestTarget);
var _point = getPoint(latestTarget);
cacheRef.current.element = _element;
cacheRef.current.point = _point;
cacheRef.current.align = latestAlign;
if (!latestDisabled && latestTarget && source) {
var _result;
// IE lose focus after element realign
// We should record activeElement and restore later
var _document = document,
activeElement = _document.activeElement;
var _element = getElement(latestTarget);
var _point = getPoint(latestTarget);
cacheRef.current.element = _element;
cacheRef.current.point = _point;
cacheRef.current.align = latestAlign; // IE lose focus after element realign
// We should record activeElement and restore later
var _document = document,
activeElement = _document.activeElement; // We only align when element is visible
if (_element && (0, _isVisible.default)(_element)) {
_result = (0, _domAlign.alignElement)(source, _element, latestAlign);
} else if (_point) {
_result = (0, _domAlign.alignPoint)(source, _point, latestAlign);
// We only align when element is visible
if (_element && (0, _isVisible.default)(_element)) {
_result = (0, _domAlign.alignElement)(source, _element, latestAlign);
} else if (_point) {
_result = (0, _domAlign.alignPoint)(source, _point, latestAlign);
}
(0, _util.restoreFocus)(activeElement, source);
if (latestOnAlign && _result) {
latestOnAlign(source, _result);
}
return true;
}
return false;
}, monitorBufferTime),
_useBuffer2 = (0, _slicedToArray2.default)(_useBuffer, 2),
_forceAlign = _useBuffer2[0],
cancelForceAlign = _useBuffer2[1];
(0, _util.restoreFocus)(activeElement, source);
if (latestOnAlign && _result) {
latestOnAlign(source, _result);
}
return true;
}
return false;
}, monitorBufferTime),
_useBuffer2 = (0, _slicedToArray2.default)(_useBuffer, 2),
_forceAlign = _useBuffer2[0],
cancelForceAlign = _useBuffer2[1]; // ===================== Effect =====================
// ===================== Effect =====================
// Handle props change
var _React$useState = _react.default.useState(),
_React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
element = _React$useState2[0],
setElement = _React$useState2[1];
_React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
element = _React$useState2[0],
setElement = _React$useState2[1];
var _React$useState3 = _react.default.useState(),
_React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
point = _React$useState4[0],
setPoint = _React$useState4[1];
_React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
point = _React$useState4[0],
setPoint = _React$useState4[1];
(0, _useLayoutEffect.default)(function () {

@@ -139,3 +112,2 @@ setElement(getElement(target));

});
_react.default.useEffect(function () {

@@ -145,17 +117,17 @@ if (cacheRef.current.element !== element || !(0, _util.isSamePoint)(cacheRef.current.point, point) || !(0, _isEqual.default)(cacheRef.current.align, align)) {

}
}); // Watch popup element resize
});
// Watch popup element resize
_react.default.useEffect(function () {
var cancelFn = (0, _util.monitorResize)(nodeRef.current, _forceAlign);
return cancelFn;
}, [nodeRef.current]); // Watch target element resize
}, [nodeRef.current]);
// Watch target element resize
_react.default.useEffect(function () {
var cancelFn = (0, _util.monitorResize)(element, _forceAlign);
return cancelFn;
}, [element]); // Listen for disabled change
}, [element]);
// Listen for disabled change
_react.default.useEffect(function () {

@@ -167,5 +139,5 @@ if (!disabled) {

}
}, [disabled]); // Listen for window resize
}, [disabled]);
// Listen for window resize
_react.default.useEffect(function () {

@@ -176,5 +148,5 @@ if (monitorWindowResize) {

}
}, [monitorWindowResize]); // Clear all if unmount
}, [monitorWindowResize]);
// Clear all if unmount
_react.default.useEffect(function () {

@@ -184,5 +156,5 @@ return function () {

};
}, []); // ====================== Ref =======================
}, []);
// ====================== Ref =======================
_react.default.useImperativeHandle(ref, function () {

@@ -194,5 +166,5 @@ return {

};
}); // ===================== Render =====================
});
// ===================== Render =====================
if ( /*#__PURE__*/_react.default.isValidElement(childNode)) {

@@ -203,10 +175,7 @@ childNode = /*#__PURE__*/_react.default.cloneElement(childNode, {

}
return childNode;
};
var RcAlign = /*#__PURE__*/_react.default.forwardRef(Align);
RcAlign.displayName = 'Align';
var _default = RcAlign;
exports.default = _default;
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {

@@ -9,17 +8,11 @@ value: true

exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _default = function _default(callback, buffer) {
var calledRef = _react.default.useRef(false);
var timeoutRef = _react.default.useRef(null);
function cancelTrigger() {
window.clearTimeout(timeoutRef.current);
}
function trigger(force) {
cancelTrigger();
if (!calledRef.current || force === true) {

@@ -30,3 +23,2 @@ if (callback(force) === false) {

}
calledRef.current = true;

@@ -43,3 +35,2 @@ timeoutRef.current = window.setTimeout(function () {

}
return [trigger, function () {

@@ -50,3 +41,2 @@ calledRef.current = false;

};
exports.default = _default;
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {

@@ -9,7 +8,5 @@ value: true

exports.default = void 0;
var _Align = _interopRequireDefault(require("./Align"));
// export this package's api
var _default = _Align.default;
exports.default = _default;
/** Two char of 't' 'b' 'c' 'l' 'r'. Example: 'lt' */
export declare type AlignPoint = string;
export type AlignPoint = string;
export interface AlignType {

@@ -56,2 +56,2 @@ /**

}
export declare type TargetType = (() => HTMLElement) | TargetPoint;
export type TargetType = (() => HTMLElement) | TargetPoint;
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {

@@ -11,24 +10,16 @@ value: true

exports.restoreFocus = restoreFocus;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _resizeObserverPolyfill = _interopRequireDefault(require("resize-observer-polyfill"));
var _contains = _interopRequireDefault(require("rc-util/lib/Dom/contains"));
function isSamePoint(prev, next) {
if (prev === next) return true;
if (!prev || !next) return false;
if ('pageX' in next && 'pageY' in next) {
return prev.pageX === next.pageX && prev.pageY === next.pageY;
}
if ('clientX' in next && 'clientY' in next) {
return prev.clientX === next.clientX && prev.clientY === next.clientY;
}
return false;
}
function restoreFocus(activeElement, container) {

@@ -40,20 +31,14 @@ // Focus back if is in the container

}
function monitorResize(element, callback) {
var prevWidth = null;
var prevHeight = null;
function onResize(_ref) {
var _ref2 = (0, _slicedToArray2.default)(_ref, 1),
target = _ref2[0].target;
target = _ref2[0].target;
if (!document.documentElement.contains(target)) return;
var _target$getBoundingCl = target.getBoundingClientRect(),
width = _target$getBoundingCl.width,
height = _target$getBoundingCl.height;
width = _target$getBoundingCl.width,
height = _target$getBoundingCl.height;
var fixedWidth = Math.floor(width);
var fixedHeight = Math.floor(height);
if (prevWidth !== fixedWidth || prevHeight !== fixedHeight) {

@@ -68,13 +53,9 @@ // https://webkit.org/blog/9997/resizeobserver-in-webkit/

}
prevWidth = fixedWidth;
prevHeight = fixedHeight;
}
var resizeObserver = new _resizeObserverPolyfill.default(onResize);
if (element) {
resizeObserver.observe(element);
}
return function () {

@@ -81,0 +62,0 @@ resizeObserver.disconnect();

{
"name": "rc-align",
"version": "4.0.15",
"version": "5.0.0-0",
"description": "align ui component for react",

@@ -33,3 +33,3 @@ "keywords": [

"now-build": "npm run build",
"prepublishOnly": "npm run compile && np --yolo --no-publish",
"prepublishOnly": "npm run compile && np --yolo --no-publish --branch feat/@rc-component/dom-align",
"start": "dumi dev",

@@ -40,6 +40,6 @@ "test": "rc-test",

"dependencies": {
"@babel/runtime": "^7.10.1",
"@babel/runtime": "^7.23.2",
"classnames": "2.x",
"dom-align": "^1.7.0",
"rc-util": "^5.26.0",
"@rc-component/dom-align": "^2.0.1",
"rc-util": "^5.38.0",
"resize-observer-polyfill": "^1.5.1"

@@ -57,3 +57,3 @@ },

"father": "^4.0.0",
"np": "^5.0.3",
"np": "^8.0.4",
"rc-test": "^7.0.14",

@@ -60,0 +60,0 @@ "react": "^18.0.0",

# rc-align
---
React Align Component. Wrapper around https://github.com/yiminghe/dom-align.
React Align Component. Wrapper around https://github.com/react-component/dom-align.

@@ -81,3 +81,3 @@ [![NPM version][npm-image]][npm-url]

<td></td>
<td>same with alignConfig from https://github.com/yiminghe/dom-align</td>
<td>same with alignConfig from https://github.com/react-component/dom-align</td>
</tr>

@@ -99,3 +99,3 @@ <tr>

<td>
a function which returned value or point is used for target from https://github.com/yiminghe/dom-align
a function which returned value or point is used for target from https://github.com/react-component/dom-align
</td>

@@ -102,0 +102,0 @@ </tr>

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc