@react-stately/utils
Advanced tools
Comparing version 3.0.0-nightly.858 to 3.0.0-nightly.861
@@ -27,3 +27,3 @@ var { | ||
if (onChange) { | ||
if (stateRef.current !== value) { | ||
if (!Object.is(stateRef.current, value)) { | ||
for (var _len2 = arguments.length, onChangeArgs = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
@@ -83,2 +83,74 @@ onChangeArgs[_key2 - 1] = arguments[_key2]; | ||
exports.useControlledState = useControlledState; | ||
/* | ||
* Copyright 2020 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
/** | ||
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step. | ||
*/ | ||
function clamp(value, min, max) { | ||
if (min === void 0) { | ||
min = -Infinity; | ||
} | ||
if (max === void 0) { | ||
max = Infinity; | ||
} | ||
let newValue = Math.min(Math.max(value, min), max); | ||
return newValue; | ||
} | ||
exports.clamp = clamp; | ||
function roundToStep(value, step) { | ||
if (!isNaN(step)) { | ||
// have to avoid Math.round(num / multiple) * multiple; because it can give results like "0.3000000000000004" for Round(.2 + .1, .1) | ||
let diff = Math.abs(value % step); | ||
if (value >= 0) { | ||
return diff > step / 2 ? value - diff + step : value - diff; | ||
} else { | ||
return diff > step / 2 ? value + diff - step : value + diff; | ||
} | ||
} | ||
return value; | ||
} | ||
exports.roundToStep = roundToStep; | ||
function snapValueToStep(value, min, max, step) { | ||
let remainder = (value - min) % step; | ||
let snappedValue = Math.abs(remainder) * 2 >= step ? value - Math.abs(remainder) + step : value - remainder; | ||
if (snappedValue < min) { | ||
snappedValue = min; | ||
} else if (snappedValue > max) { | ||
snappedValue = min + Math.floor((max - min) / step) * step; | ||
} // correct floating point behavior by rounding to step precision | ||
let string = step.toString(); | ||
let index = string.indexOf('.'); | ||
let precision = index >= 0 ? string.length - index : 0; | ||
if (precision > 0) { | ||
let pow = Math.pow(10, precision); | ||
snappedValue = Math.round(snappedValue * pow) / pow; | ||
} | ||
return snappedValue; | ||
} | ||
exports.snapValueToStep = snapValueToStep; | ||
//# sourceMappingURL=main.js.map |
@@ -22,3 +22,3 @@ import { useCallback, useRef, useState } from "react"; | ||
if (onChange) { | ||
if (stateRef.current !== value) { | ||
if (!Object.is(stateRef.current, value)) { | ||
for (var _len2 = arguments.length, onChangeArgs = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
@@ -76,2 +76,66 @@ onChangeArgs[_key2 - 1] = arguments[_key2]; | ||
} | ||
/* | ||
* Copyright 2020 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
/** | ||
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step. | ||
*/ | ||
export function clamp(value, min, max) { | ||
if (min === void 0) { | ||
min = -Infinity; | ||
} | ||
if (max === void 0) { | ||
max = Infinity; | ||
} | ||
let newValue = Math.min(Math.max(value, min), max); | ||
return newValue; | ||
} | ||
export function roundToStep(value, step) { | ||
if (!isNaN(step)) { | ||
// have to avoid Math.round(num / multiple) * multiple; because it can give results like "0.3000000000000004" for Round(.2 + .1, .1) | ||
let diff = Math.abs(value % step); | ||
if (value >= 0) { | ||
return diff > step / 2 ? value - diff + step : value - diff; | ||
} else { | ||
return diff > step / 2 ? value + diff - step : value + diff; | ||
} | ||
} | ||
return value; | ||
} | ||
export function snapValueToStep(value, min, max, step) { | ||
let remainder = (value - min) % step; | ||
let snappedValue = Math.abs(remainder) * 2 >= step ? value - Math.abs(remainder) + step : value - remainder; | ||
if (snappedValue < min) { | ||
snappedValue = min; | ||
} else if (snappedValue > max) { | ||
snappedValue = min + Math.floor((max - min) / step) * step; | ||
} // correct floating point behavior by rounding to step precision | ||
let string = step.toString(); | ||
let index = string.indexOf('.'); | ||
let precision = index >= 0 ? string.length - index : 0; | ||
if (precision > 0) { | ||
let pow = Math.pow(10, precision); | ||
snappedValue = Math.round(snappedValue * pow) / pow; | ||
} | ||
return snappedValue; | ||
} | ||
//# sourceMappingURL=module.js.map |
export function useControlledState<T>(value: T, defaultValue: T, onChange: (value: T, ...args: any[]) => void): [T, (value: T | ((prevState: T) => T), ...args: any[]) => void]; | ||
/** | ||
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step. | ||
*/ | ||
export function clamp(value: number, min?: number, max?: number): number; | ||
export function roundToStep(value: number, step: number): number; | ||
export function snapValueToStep(value: number, min: number, max: number, step: number): number; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "@react-stately/utils", | ||
"version": "3.0.0-nightly.858+7326ad2a", | ||
"version": "3.0.0-nightly.861+bd1afa1d", | ||
"description": "Spectrum UI components in React", | ||
@@ -28,3 +28,3 @@ "license": "Apache-2.0", | ||
}, | ||
"gitHead": "7326ad2ab299a05c6786c1fc8e23df652b3d630d" | ||
"gitHead": "bd1afa1d8f11893a1b3de13eaa3e99c19300d6bd" | ||
} |
@@ -14,1 +14,2 @@ /* | ||
export * from './useControlledState'; | ||
export * from './number'; |
@@ -35,3 +35,3 @@ /* | ||
if (onChange) { | ||
if (stateRef.current !== value) { | ||
if (!Object.is(stateRef.current, value)) { | ||
onChange(value, ...onChangeArgs); | ||
@@ -38,0 +38,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the 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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
48794
12
375