@reach/rect
Advanced tools
Comparing version 0.2.1 to 0.3.0-alpha.0
@@ -1,2 +0,2 @@ | ||
var _extends = Object.assign || 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; }; | ||
function _extends() { _extends = Object.assign || 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); } | ||
@@ -12,5 +12,8 @@ import React, { useRef, useState, useLayoutEffect } from "react"; | ||
rect = _ref.state.rect; | ||
return children({ ref: function ref(node) { | ||
return children({ | ||
ref: function ref(node) { | ||
return refs.node = node; | ||
}, rect: rect }); | ||
}, | ||
rect: rect | ||
}); | ||
}; | ||
@@ -27,6 +30,10 @@ | ||
} | ||
refs.observer = observeRect(refs.node, function (rect) { | ||
props.onChange && props.onChange(rect); | ||
setState({ rect: rect }); | ||
setState({ | ||
rect: rect | ||
}); | ||
}); | ||
if (props.observe) { | ||
@@ -51,3 +58,2 @@ refs.observer.observe(); | ||
var refs = _ref4.refs; | ||
refs.observer.unobserve(); | ||
@@ -72,8 +78,2 @@ }; | ||
process.env.NODE_ENV !== "production" ? Rect.propTypes = { | ||
children: func, | ||
observe: bool, | ||
onChange: func | ||
} : void 0; | ||
Rect.defaultProps = { | ||
@@ -83,5 +83,15 @@ observe: true | ||
export function useRect(nodeRef) { | ||
var observe = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
if (process.env.NODE_ENV !== "production") { | ||
Rect.propTypes = { | ||
children: func, | ||
observe: bool, | ||
onChange: func | ||
}; | ||
} | ||
export function useRect(nodeRef, observe) { | ||
if (observe === void 0) { | ||
observe = true; | ||
} | ||
var _useState = useState(null), | ||
@@ -93,15 +103,16 @@ rect = _useState[0], | ||
useLayoutEffect(function () { | ||
if (!observerRef.current) { | ||
if (!observerRef.current && nodeRef.current) { | ||
observerRef.current = observeRect(nodeRef.current, setRect); | ||
} | ||
if (observe) { | ||
observerRef.current.observe(); | ||
} | ||
return function () { | ||
return observerRef.current.unobserve(); | ||
}; | ||
}; // eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [observe]); | ||
return rect; | ||
} | ||
export default Rect; |
69
index.js
"use strict"; | ||
exports.__esModule = true; | ||
var _extends = Object.assign || 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; }; | ||
exports.useRect = useRect; | ||
exports["default"] = void 0; | ||
var _react = require("react"); | ||
var _react = _interopRequireWildcard(require("react")); | ||
var _react2 = _interopRequireDefault(_react); | ||
var _componentComponent = _interopRequireDefault(require("@reach/component-component")); | ||
var _componentComponent = require("@reach/component-component"); | ||
var _observeRect = _interopRequireDefault(require("@reach/observe-rect")); | ||
var _componentComponent2 = _interopRequireDefault(_componentComponent); | ||
var _propTypes = require("prop-types"); | ||
var _observeRect = require("@reach/observe-rect"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
var _observeRect2 = _interopRequireDefault(_observeRect); | ||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } | ||
var _propTypes = require("prop-types"); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _extends() { _extends = Object.assign || 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); } | ||
@@ -29,5 +27,8 @@ var render = function render(_ref) { | ||
rect = _ref.state.rect; | ||
return children({ ref: function ref(node) { | ||
return children({ | ||
ref: function ref(node) { | ||
return refs.node = node; | ||
}, rect: rect }); | ||
}, | ||
rect: rect | ||
}); | ||
}; | ||
@@ -44,6 +45,10 @@ | ||
} | ||
refs.observer = (0, _observeRect2.default)(refs.node, function (rect) { | ||
refs.observer = (0, _observeRect["default"])(refs.node, function (rect) { | ||
props.onChange && props.onChange(rect); | ||
setState({ rect: rect }); | ||
setState({ | ||
rect: rect | ||
}); | ||
}); | ||
if (props.observe) { | ||
@@ -68,3 +73,2 @@ refs.observer.observe(); | ||
var refs = _ref4.refs; | ||
refs.observer.unobserve(); | ||
@@ -74,3 +78,3 @@ }; | ||
var Rect = function Rect(props) { | ||
return _react2.default.createElement(_componentComponent2.default, _extends({}, props, { | ||
return _react["default"].createElement(_componentComponent["default"], _extends({}, props, { | ||
refs: { | ||
@@ -90,8 +94,2 @@ node: undefined, | ||
process.env.NODE_ENV !== "production" ? Rect.propTypes = { | ||
children: _propTypes.func, | ||
observe: _propTypes.bool, | ||
onChange: _propTypes.func | ||
} : void 0; | ||
Rect.defaultProps = { | ||
@@ -101,5 +99,15 @@ observe: true | ||
function useRect(nodeRef) { | ||
var observe = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
if (process.env.NODE_ENV !== "production") { | ||
Rect.propTypes = { | ||
children: _propTypes.func, | ||
observe: _propTypes.bool, | ||
onChange: _propTypes.func | ||
}; | ||
} | ||
function useRect(nodeRef, observe) { | ||
if (observe === void 0) { | ||
observe = true; | ||
} | ||
var _useState = (0, _react.useState)(null), | ||
@@ -111,11 +119,13 @@ rect = _useState[0], | ||
(0, _react.useLayoutEffect)(function () { | ||
if (!observerRef.current) { | ||
observerRef.current = (0, _observeRect2.default)(nodeRef.current, setRect); | ||
if (!observerRef.current && nodeRef.current) { | ||
observerRef.current = (0, _observeRect["default"])(nodeRef.current, setRect); | ||
} | ||
if (observe) { | ||
observerRef.current.observe(); | ||
} | ||
return function () { | ||
return observerRef.current.unobserve(); | ||
}; | ||
}; // eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [observe]); | ||
@@ -125,2 +135,3 @@ return rect; | ||
exports.default = Rect; | ||
var _default = Rect; | ||
exports["default"] = _default; |
{ | ||
"name": "@reach/rect", | ||
"version": "0.2.1", | ||
"version": "0.3.0-alpha.0", | ||
"description": "Measure React elements position in the DOM", | ||
@@ -8,3 +8,2 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Write some tests you bum!\"", | ||
"build": "node ../../shared/build-package", | ||
@@ -14,11 +13,11 @@ "lint": "eslint . --max-warnings=0" | ||
"dependencies": { | ||
"@reach/component-component": "^0.1.3", | ||
"@reach/observe-rect": "^1.0.3" | ||
"@reach/component-component": "^0.3.0-alpha.0", | ||
"@reach/observe-rect": "^1.0.3", | ||
"prop-types": "^15.7.2" | ||
}, | ||
"peerDependencies": { | ||
"prop-types": "^15.6.2", | ||
"react": "^16.8.0", | ||
"react-dom": "^16.8.0" | ||
}, | ||
"author": "", | ||
"author": "Ryan Florence <@ryanflorence>", | ||
"license": "MIT", | ||
@@ -31,3 +30,4 @@ "files": [ | ||
"styles.css" | ||
] | ||
], | ||
"gitHead": "3234e9cf2a55f51c31ac5a7d883d34d6be6f033d" | ||
} |
@@ -52,8 +52,2 @@ import React, { useRef, useState, useLayoutEffect } from "react"; | ||
Rect.propTypes = { | ||
children: func, | ||
observe: bool, | ||
onChange: func | ||
}; | ||
Rect.defaultProps = { | ||
@@ -63,2 +57,10 @@ observe: true | ||
if (__DEV__) { | ||
Rect.propTypes = { | ||
children: func, | ||
observe: bool, | ||
onChange: func | ||
}; | ||
} | ||
export function useRect(nodeRef, observe = true) { | ||
@@ -68,3 +70,3 @@ let [rect, setRect] = useState(null); | ||
useLayoutEffect(() => { | ||
if (!observerRef.current) { | ||
if (!observerRef.current && nodeRef.current) { | ||
observerRef.current = observeRect(nodeRef.current, setRect); | ||
@@ -76,2 +78,3 @@ } | ||
return () => observerRef.current.unobserve(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [observe]); | ||
@@ -78,0 +81,0 @@ return rect; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
10379
5
271
1
1
+ Addedprop-types@^15.7.2
+ Added@reach/component-component@0.3.0(transitive)
- Removed@reach/component-component@0.1.3(transitive)