@reach/utils
Advanced tools
Comparing version 0.7.4 to 0.8.0
@@ -46,3 +46,3 @@ import React from "react"; | ||
*/ | ||
export declare function assignRef<T = any>(ref: AssignableRef<T>, value: any): void; | ||
export declare function assignRef<T = any>(ref: AssignableRef<T> | undefined, value: any): void; | ||
export declare function boolOrBoolString(value: any): boolean; | ||
@@ -68,2 +68,3 @@ export declare function canUseDOM(): boolean; | ||
export declare function noop(): void; | ||
export declare function useConstant<T>(fn: () => T): T; | ||
/** | ||
@@ -76,3 +77,3 @@ * Passes or assigns a value to multiple refs (typically a DOM node). Useful for | ||
*/ | ||
export declare function useForkedRef<T = any>(...refs: AssignableRef<T>[]): ((node: any) => void) | null; | ||
export declare function useForkedRef<T = any>(...refs: (AssignableRef<T> | undefined)[]): ((node: any) => void) | null; | ||
/** | ||
@@ -103,3 +104,3 @@ * Returns the previous value of a reference after a component update. | ||
* infer props based on a component or JSX string passed into an `as` prop is | ||
* kind of a huge pain. Getting it to work and satisfy the contraints of | ||
* kind of a huge pain. Getting it to work and satisfy the constraints of | ||
* `forwardRef` seems dang near impossible. To avoid needing to do this awkward | ||
@@ -119,2 +120,3 @@ * type song-and-dance every time we want to forward a ref into a component | ||
element: (T extends HTMLElement ? T : HTMLElement) | null; | ||
index: number; | ||
}; | ||
@@ -150,3 +152,3 @@ export interface IDescendantContext<T, P> { | ||
*/ | ||
export declare function useDescendant<T, P>({ context, element, ...rest }: Descendant<T, P> & { | ||
export declare function useDescendant<T, P>({ context, element, ...rest }: Omit<Descendant<T, P>, "index"> & { | ||
context: React.Context<IDescendantContext<T, P>>; | ||
@@ -153,0 +155,0 @@ }, indexProp?: number): number; |
@@ -5,2 +5,6 @@ import * as React from "react"; | ||
/** | ||
* Type can be either a single `T` or an array of `T` | ||
*/ | ||
export declare type SingleOrArray<T> = T[] | T; | ||
/** | ||
* The built-in utility type `Omit` does not distribute over unions. So if you | ||
@@ -7,0 +11,0 @@ * have: |
@@ -197,3 +197,16 @@ 'use strict'; | ||
function noop() {} | ||
function noop() {} // React hook for creating a value exactly once. | ||
// https://github.com/Andarist/use-constant | ||
function useConstant(fn) { | ||
var ref = React__default.useRef(); | ||
if (!ref.current) { | ||
ref.current = { | ||
v: fn() | ||
}; | ||
} | ||
return ref.current.v; | ||
} | ||
/** | ||
@@ -278,3 +291,3 @@ * Passes or assigns a value to multiple refs (typically a DOM node). Useful for | ||
* infer props based on a component or JSX string passed into an `as` prop is | ||
* kind of a huge pain. Getting it to work and satisfy the contraints of | ||
* kind of a huge pain. Getting it to work and satisfy the constraints of | ||
* `forwardRef` seems dang near impossible. To avoid needing to do this awkward | ||
@@ -411,3 +424,4 @@ * type song-and-dance every time we want to forward a ref into a component | ||
var newItem = _extends({ | ||
element: element | ||
element: element, | ||
index: index | ||
}, rest); // If an index is not found we will push the element to the end. | ||
@@ -483,2 +497,3 @@ | ||
exports.noop = noop; | ||
exports.useConstant = useConstant; | ||
exports.useDescendant = useDescendant; | ||
@@ -485,0 +500,0 @@ exports.useDescendants = useDescendants; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,n=require("react"),t=(e=n)&&"object"==typeof e&&"default"in e?e.default:e;function r(){return(r=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e}).apply(this,arguments)}function o(e,n){if(null==e)return{};var t,r,o={},u=Object.keys(e);for(r=0;r<u.length;r++)n.indexOf(t=u[r])>=0||(o[t]=e[t]);return o}var u=f()?t.useLayoutEffect:t.useEffect,c=u;function i(e,n){if(null!=e)if("function"==typeof e)e(n);else try{e.current=n}catch(t){throw new Error('Cannot assign value "'+n+'" to ref "'+e+'"')}}function f(){return"undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement}function s(e,t){var r=n.createContext(t);return r.displayName=e,r}function a(){}exports.DescendantProvider=function(e){var u=e.context,c=e.children,i=e.items,f=e.set,s=t.useCallback((function(e){var n=e.element,t=o(e,["element"]);n&&f((function(e){if(null==e.find((function(e){return e.element===n}))){var o=e.findIndex((function(e){var t=e.element;return!(!t||!n)&&Boolean(t.compareDocumentPosition(n)&Node.DOCUMENT_POSITION_PRECEDING)})),u=r({element:n},t);return-1===o?[].concat(e,[u]):[].concat(e.slice(0,o),[u],e.slice(o))}return e}))}),[]),a=n.useCallback((function(e){e&&f((function(n){return n.filter((function(n){return e!==n.element}))}))}),[]),l=n.useMemo((function(){return{descendants:i,registerDescendant:s,unregisterDescendant:a}}),[i,s,a]);return t.createElement(u.Provider,{value:l},c)},exports.assignRef=i,exports.boolOrBoolString=function(e){return"false"!==e&&Boolean(e)},exports.canUseDOM=f,exports.checkStyles=function(e){},exports.cloneValidElement=function(e,t){if(!n.isValidElement(e))return e;for(var r=arguments.length,o=new Array(r>2?r-2:0),u=2;u<r;u++)o[u-2]=arguments[u];return n.cloneElement.apply(void 0,[e,t].concat(o))},exports.createDescendantContext=function(e,n){return void 0===n&&(n={}),s(e,r({descendants:[],registerDescendant:a,unregisterDescendant:a},n))},exports.createNamedContext=s,exports.findLastIndex=function(e,n){var t=e.length>>>0;if(!t)return-1;for(var r=t-1;r>=0;){if(n(e[r],r,e))return r;--r}return-1},exports.forwardRefWithAs=function(e){return t.forwardRef(e)},exports.getScrollbarOffset=function(){try{if(window.innerWidth>document.documentElement.clientWidth)return window.innerWidth-document.documentElement.clientWidth}catch(e){}return 0},exports.isUndefined=function(e){return void 0===e},exports.makeId=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return n.filter((function(e){return null!=e})).join("--")},exports.noop=a,exports.useDescendant=function(e,t){var u=e.context,i=e.element,f=o(e,["context","element"]),s=n.useState()[1],a=n.useContext(u),l=a.registerDescendant,d=a.unregisterDescendant,p=a.descendants;return c((function(){return i||s({}),l(r({element:i},f)),function(){return d(i)}}),[i].concat(Object.values(f))),null!=t?t:p.findIndex((function(e){return e.element===i}))},exports.useDescendants=function(){return n.useState([])},exports.useForkedRef=function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return n.useMemo((function(){return t.every((function(e){return null==e}))?null:function(e){t.forEach((function(n){i(n,e)}))}}),t)},exports.useIsomorphicLayoutEffect=u,exports.usePrevious=function(e){var t=n.useRef(null);return n.useEffect((function(){t.current=e}),[e]),t.current},exports.useUpdateEffect=function(e,t){var r=n.useRef(!1);n.useEffect((function(){r.current?e():r.current=!0}),t)},exports.wrapEvent=function(e,n){return function(t){if(e&&e(t),!t.defaultPrevented)return n(t)}}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,n=require("react"),t=(e=n)&&"object"==typeof e&&"default"in e?e.default:e;function r(){return(r=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e}).apply(this,arguments)}function o(e,n){if(null==e)return{};var t,r,o={},u=Object.keys(e);for(r=0;r<u.length;r++)n.indexOf(t=u[r])>=0||(o[t]=e[t]);return o}var u=f()?t.useLayoutEffect:t.useEffect,c=u;function i(e,n){if(null!=e)if("function"==typeof e)e(n);else try{e.current=n}catch(t){throw new Error('Cannot assign value "'+n+'" to ref "'+e+'"')}}function f(){return"undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement}function s(e,t){var r=n.createContext(t);return r.displayName=e,r}function a(){}exports.DescendantProvider=function(e){var u=e.context,c=e.children,i=e.items,f=e.set,s=t.useCallback((function(e){var n=e.element,t=o(e,["element"]);n&&f((function(e){if(null==e.find((function(e){return e.element===n}))){var o=e.findIndex((function(e){var t=e.element;return!(!t||!n)&&Boolean(t.compareDocumentPosition(n)&Node.DOCUMENT_POSITION_PRECEDING)})),u=r({element:n,index:o},t);return-1===o?[].concat(e,[u]):[].concat(e.slice(0,o),[u],e.slice(o))}return e}))}),[]),a=n.useCallback((function(e){e&&f((function(n){return n.filter((function(n){return e!==n.element}))}))}),[]),l=n.useMemo((function(){return{descendants:i,registerDescendant:s,unregisterDescendant:a}}),[i,s,a]);return t.createElement(u.Provider,{value:l},c)},exports.assignRef=i,exports.boolOrBoolString=function(e){return"false"!==e&&Boolean(e)},exports.canUseDOM=f,exports.checkStyles=function(e){},exports.cloneValidElement=function(e,t){if(!n.isValidElement(e))return e;for(var r=arguments.length,o=new Array(r>2?r-2:0),u=2;u<r;u++)o[u-2]=arguments[u];return n.cloneElement.apply(void 0,[e,t].concat(o))},exports.createDescendantContext=function(e,n){return void 0===n&&(n={}),s(e,r({descendants:[],registerDescendant:a,unregisterDescendant:a},n))},exports.createNamedContext=s,exports.findLastIndex=function(e,n){var t=e.length>>>0;if(!t)return-1;for(var r=t-1;r>=0;){if(n(e[r],r,e))return r;--r}return-1},exports.forwardRefWithAs=function(e){return t.forwardRef(e)},exports.getScrollbarOffset=function(){try{if(window.innerWidth>document.documentElement.clientWidth)return window.innerWidth-document.documentElement.clientWidth}catch(e){}return 0},exports.isUndefined=function(e){return void 0===e},exports.makeId=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return n.filter((function(e){return null!=e})).join("--")},exports.noop=a,exports.useConstant=function(e){var n=t.useRef();return n.current||(n.current={v:e()}),n.current.v},exports.useDescendant=function(e,t){var u=e.context,i=e.element,f=o(e,["context","element"]),s=n.useState()[1],a=n.useContext(u),l=a.registerDescendant,d=a.unregisterDescendant,p=a.descendants;return c((function(){return i||s({}),l(r({element:i},f)),function(){return d(i)}}),[i].concat(Object.values(f))),null!=t?t:p.findIndex((function(e){return e.element===i}))},exports.useDescendants=function(){return n.useState([])},exports.useForkedRef=function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return n.useMemo((function(){return t.every((function(e){return null==e}))?null:function(e){t.forEach((function(n){i(n,e)}))}}),t)},exports.useIsomorphicLayoutEffect=u,exports.usePrevious=function(e){var t=n.useRef(null);return n.useEffect((function(){t.current=e}),[e]),t.current},exports.useUpdateEffect=function(e,t){var r=n.useRef(!1);n.useEffect((function(){r.current?e():r.current=!0}),t)},exports.wrapEvent=function(e,n){return function(t){if(e&&e(t),!t.defaultPrevented)return n(t)}}; | ||
//# sourceMappingURL=utils.cjs.production.min.js.map |
@@ -190,3 +190,16 @@ import React, { isValidElement, cloneElement, createContext, useMemo, useRef, useEffect, useState, useContext, useCallback } from 'react'; | ||
function noop() {} | ||
function noop() {} // React hook for creating a value exactly once. | ||
// https://github.com/Andarist/use-constant | ||
function useConstant(fn) { | ||
var ref = React.useRef(); | ||
if (!ref.current) { | ||
ref.current = { | ||
v: fn() | ||
}; | ||
} | ||
return ref.current.v; | ||
} | ||
/** | ||
@@ -271,3 +284,3 @@ * Passes or assigns a value to multiple refs (typically a DOM node). Useful for | ||
* infer props based on a component or JSX string passed into an `as` prop is | ||
* kind of a huge pain. Getting it to work and satisfy the contraints of | ||
* kind of a huge pain. Getting it to work and satisfy the constraints of | ||
* `forwardRef` seems dang near impossible. To avoid needing to do this awkward | ||
@@ -404,3 +417,4 @@ * type song-and-dance every time we want to forward a ref into a component | ||
var newItem = _extends({ | ||
element: element | ||
element: element, | ||
index: index | ||
}, rest); // If an index is not found we will push the element to the end. | ||
@@ -463,3 +477,3 @@ | ||
export { DescendantProvider, assignRef, boolOrBoolString, canUseDOM, checkStyles, cloneValidElement, createDescendantContext, createNamedContext, findLastIndex, forwardRefWithAs, getScrollbarOffset, isUndefined, makeId, noop, useDescendant, useDescendants, useForkedRef, useIsomorphicLayoutEffect, usePrevious, useUpdateEffect, wrapEvent }; | ||
export { DescendantProvider, assignRef, boolOrBoolString, canUseDOM, checkStyles, cloneValidElement, createDescendantContext, createNamedContext, findLastIndex, forwardRefWithAs, getScrollbarOffset, isUndefined, makeId, noop, useConstant, useDescendant, useDescendants, useForkedRef, useIsomorphicLayoutEffect, usePrevious, useUpdateEffect, wrapEvent }; | ||
//# sourceMappingURL=utils.esm.js.map |
{ | ||
"name": "@reach/utils", | ||
"version": "0.7.4", | ||
"version": "0.8.0", | ||
"description": "Internal, shared utilities for Reach UI.", | ||
@@ -28,3 +28,3 @@ "author": "React Training <hello@reacttraining.com>", | ||
], | ||
"gitHead": "f460709e283a60dd5ea62952b7feaf88546a50ff" | ||
"gitHead": "7702c46373c7f11adc21184e7def9977203e9326" | ||
} |
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
110598
1082