@fluentui/react-utilities 9.9.1 → 9.9.2
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.
- package/CHANGELOG.json +16 -1
- package/CHANGELOG.md +11 -2
- package/dist/index.d.ts +1 -1
- package/lib/compose/constants.js +1 -2
- package/lib/compose/constants.js.map +1 -1
- package/lib/compose/getSlots.js +40 -36
- package/lib/compose/getSlots.js.map +1 -1
- package/lib/compose/getSlotsNext.js +30 -28
- package/lib/compose/getSlotsNext.js.map +1 -1
- package/lib/compose/index.js +0 -1
- package/lib/compose/index.js.map +1 -1
- package/lib/compose/isResolvedShorthand.js +2 -4
- package/lib/compose/isResolvedShorthand.js.map +1 -1
- package/lib/compose/resolveShorthand.js +21 -26
- package/lib/compose/resolveShorthand.js.map +1 -1
- package/lib/compose/types.js +0 -1
- package/lib/compose/types.js.map +1 -1
- package/lib/events/index.js +0 -1
- package/lib/events/index.js.map +1 -1
- package/lib/events/mouseTouchHelpers.js +27 -27
- package/lib/events/mouseTouchHelpers.js.map +1 -1
- package/lib/hooks/index.js +0 -1
- package/lib/hooks/index.js.map +1 -1
- package/lib/hooks/useControllableState.js +55 -42
- package/lib/hooks/useControllableState.js.map +1 -1
- package/lib/hooks/useEventCallback.js +15 -13
- package/lib/hooks/useEventCallback.js.map +1 -1
- package/lib/hooks/useFirstMount.js +7 -9
- package/lib/hooks/useFirstMount.js.map +1 -1
- package/lib/hooks/useForceUpdate.js +2 -4
- package/lib/hooks/useForceUpdate.js.map +1 -1
- package/lib/hooks/useId.js +32 -29
- package/lib/hooks/useId.js.map +1 -1
- package/lib/hooks/useIsomorphicLayoutEffect.js +1 -2
- package/lib/hooks/useIsomorphicLayoutEffect.js.map +1 -1
- package/lib/hooks/useMergedRefs.js +17 -18
- package/lib/hooks/useMergedRefs.js.map +1 -1
- package/lib/hooks/useOnClickOutside.js +110 -114
- package/lib/hooks/useOnClickOutside.js.map +1 -1
- package/lib/hooks/useOnScrollOutside.js +25 -29
- package/lib/hooks/useOnScrollOutside.js.map +1 -1
- package/lib/hooks/usePrevious.js +8 -8
- package/lib/hooks/usePrevious.js.map +1 -1
- package/lib/hooks/useScrollbarWidth.js +24 -26
- package/lib/hooks/useScrollbarWidth.js.map +1 -1
- package/lib/hooks/useTimeout.js +22 -19
- package/lib/hooks/useTimeout.js.map +1 -1
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/lib/ssr/SSRContext.js +40 -39
- package/lib/ssr/SSRContext.js.map +1 -1
- package/lib/ssr/canUseDOM.js +3 -5
- package/lib/ssr/canUseDOM.js.map +1 -1
- package/lib/ssr/index.js +0 -1
- package/lib/ssr/index.js.map +1 -1
- package/lib/trigger/applyTriggerPropsToChildren.js +20 -23
- package/lib/trigger/applyTriggerPropsToChildren.js.map +1 -1
- package/lib/trigger/getTriggerChild.js +7 -10
- package/lib/trigger/getTriggerChild.js.map +1 -1
- package/lib/trigger/index.js +0 -1
- package/lib/trigger/index.js.map +1 -1
- package/lib/trigger/isFluentTrigger.js +2 -4
- package/lib/trigger/isFluentTrigger.js.map +1 -1
- package/lib/trigger/types.js +0 -1
- package/lib/trigger/types.js.map +1 -1
- package/lib/utils/clamp.js +1 -2
- package/lib/utils/clamp.js.map +1 -1
- package/lib/utils/getNativeElementProps.js +38 -40
- package/lib/utils/getNativeElementProps.js.map +1 -1
- package/lib/utils/getRTLSafeKey.js +12 -13
- package/lib/utils/getRTLSafeKey.js.map +1 -1
- package/lib/utils/getReactCallbackName.js +16 -15
- package/lib/utils/getReactCallbackName.js.map +1 -1
- package/lib/utils/index.js +0 -1
- package/lib/utils/index.js.map +1 -1
- package/lib/utils/isHTMLElement.js +5 -6
- package/lib/utils/isHTMLElement.js.map +1 -1
- package/lib/utils/isInteractiveHTMLElement.js +13 -17
- package/lib/utils/isInteractiveHTMLElement.js.map +1 -1
- package/lib/utils/mergeCallbacks.js +5 -6
- package/lib/utils/mergeCallbacks.js.map +1 -1
- package/lib/utils/omit.js +7 -8
- package/lib/utils/omit.js.map +1 -1
- package/lib/utils/priorityQueue.js +85 -86
- package/lib/utils/priorityQueue.js.map +1 -1
- package/lib/utils/properties.js +290 -80
- package/lib/utils/properties.js.map +1 -1
- package/lib-commonjs/compose/constants.js +1 -3
- package/lib-commonjs/compose/constants.js.map +1 -1
- package/lib-commonjs/compose/getSlots.js +1 -3
- package/lib-commonjs/compose/getSlots.js.map +1 -1
- package/lib-commonjs/compose/getSlotsNext.js +2 -3
- package/lib-commonjs/compose/getSlotsNext.js.map +1 -1
- package/lib-commonjs/compose/index.js +0 -3
- package/lib-commonjs/compose/index.js.map +1 -1
- package/lib-commonjs/compose/isResolvedShorthand.js +1 -3
- package/lib-commonjs/compose/isResolvedShorthand.js.map +1 -1
- package/lib-commonjs/compose/resolveShorthand.js +1 -3
- package/lib-commonjs/compose/resolveShorthand.js.map +1 -1
- package/lib-commonjs/compose/types.js +0 -3
- package/lib-commonjs/compose/types.js.map +1 -1
- package/lib-commonjs/events/index.js +0 -3
- package/lib-commonjs/events/index.js.map +1 -1
- package/lib-commonjs/events/mouseTouchHelpers.js +1 -3
- package/lib-commonjs/events/mouseTouchHelpers.js.map +1 -1
- package/lib-commonjs/hooks/index.js +0 -3
- package/lib-commonjs/hooks/index.js.map +1 -1
- package/lib-commonjs/hooks/useControllableState.js +1 -3
- package/lib-commonjs/hooks/useControllableState.js.map +1 -1
- package/lib-commonjs/hooks/useEventCallback.js +1 -3
- package/lib-commonjs/hooks/useEventCallback.js.map +1 -1
- package/lib-commonjs/hooks/useFirstMount.js +1 -3
- package/lib-commonjs/hooks/useFirstMount.js.map +1 -1
- package/lib-commonjs/hooks/useForceUpdate.js +1 -3
- package/lib-commonjs/hooks/useForceUpdate.js.map +1 -1
- package/lib-commonjs/hooks/useId.js +1 -3
- package/lib-commonjs/hooks/useId.js.map +1 -1
- package/lib-commonjs/hooks/useIsomorphicLayoutEffect.js +1 -3
- package/lib-commonjs/hooks/useIsomorphicLayoutEffect.js.map +1 -1
- package/lib-commonjs/hooks/useMergedRefs.js +2 -5
- package/lib-commonjs/hooks/useMergedRefs.js.map +1 -1
- package/lib-commonjs/hooks/useOnClickOutside.js +1 -3
- package/lib-commonjs/hooks/useOnClickOutside.js.map +1 -1
- package/lib-commonjs/hooks/useOnScrollOutside.js +1 -3
- package/lib-commonjs/hooks/useOnScrollOutside.js.map +1 -1
- package/lib-commonjs/hooks/usePrevious.js +1 -3
- package/lib-commonjs/hooks/usePrevious.js.map +1 -1
- package/lib-commonjs/hooks/useScrollbarWidth.js +2 -4
- package/lib-commonjs/hooks/useScrollbarWidth.js.map +1 -1
- package/lib-commonjs/hooks/useTimeout.js +1 -3
- package/lib-commonjs/hooks/useTimeout.js.map +1 -1
- package/lib-commonjs/index.js +0 -3
- package/lib-commonjs/index.js.map +1 -1
- package/lib-commonjs/ssr/SSRContext.js +1 -3
- package/lib-commonjs/ssr/SSRContext.js.map +1 -1
- package/lib-commonjs/ssr/canUseDOM.js +1 -3
- package/lib-commonjs/ssr/canUseDOM.js.map +1 -1
- package/lib-commonjs/ssr/index.js +0 -3
- package/lib-commonjs/ssr/index.js.map +1 -1
- package/lib-commonjs/trigger/applyTriggerPropsToChildren.js +1 -3
- package/lib-commonjs/trigger/applyTriggerPropsToChildren.js.map +1 -1
- package/lib-commonjs/trigger/getTriggerChild.js +2 -5
- package/lib-commonjs/trigger/getTriggerChild.js.map +1 -1
- package/lib-commonjs/trigger/index.js +0 -3
- package/lib-commonjs/trigger/index.js.map +1 -1
- package/lib-commonjs/trigger/isFluentTrigger.js +1 -3
- package/lib-commonjs/trigger/isFluentTrigger.js.map +1 -1
- package/lib-commonjs/trigger/types.js +0 -3
- package/lib-commonjs/trigger/types.js.map +1 -1
- package/lib-commonjs/utils/clamp.js +1 -3
- package/lib-commonjs/utils/clamp.js.map +1 -1
- package/lib-commonjs/utils/getNativeElementProps.js +1 -3
- package/lib-commonjs/utils/getNativeElementProps.js.map +1 -1
- package/lib-commonjs/utils/getRTLSafeKey.js +1 -3
- package/lib-commonjs/utils/getRTLSafeKey.js.map +1 -1
- package/lib-commonjs/utils/getReactCallbackName.js +1 -3
- package/lib-commonjs/utils/getReactCallbackName.js.map +1 -1
- package/lib-commonjs/utils/index.js +0 -3
- package/lib-commonjs/utils/index.js.map +1 -1
- package/lib-commonjs/utils/isHTMLElement.js +1 -3
- package/lib-commonjs/utils/isHTMLElement.js.map +1 -1
- package/lib-commonjs/utils/isInteractiveHTMLElement.js +1 -3
- package/lib-commonjs/utils/isInteractiveHTMLElement.js.map +1 -1
- package/lib-commonjs/utils/mergeCallbacks.js +1 -3
- package/lib-commonjs/utils/mergeCallbacks.js.map +1 -1
- package/lib-commonjs/utils/omit.js +1 -3
- package/lib-commonjs/utils/omit.js.map +1 -1
- package/lib-commonjs/utils/priorityQueue.js +5 -7
- package/lib-commonjs/utils/priorityQueue.js.map +1 -1
- package/lib-commonjs/utils/properties.js +28 -31
- package/lib-commonjs/utils/properties.js.map +1 -1
- package/package.json +1 -1
@@ -1,6 +1,6 @@
|
|
1
1
|
import * as React from 'react';
|
2
2
|
function isFactoryDispatch(newState) {
|
3
|
-
|
3
|
+
return typeof newState === 'function';
|
4
4
|
}
|
5
5
|
/**
|
6
6
|
* @internal
|
@@ -18,52 +18,65 @@ function isFactoryDispatch(newState) {
|
|
18
18
|
* ❗️❗️ Calls to the dispatcher will only modify the state if the state is `uncontrolled`.
|
19
19
|
* Meaning that if a state is `controlled`, calls to the dispatcher do not modify the state.
|
20
20
|
*
|
21
|
-
*/
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
21
|
+
*/ export const useControllableState = (options)=>{
|
22
|
+
const [internalState, setInternalState] = React.useState(()=>{
|
23
|
+
if (options.defaultState === undefined) {
|
24
|
+
return options.initialState;
|
25
|
+
}
|
26
|
+
return isInitializer(options.defaultState) ? options.defaultState() : options.defaultState;
|
27
|
+
});
|
28
|
+
// Heads up!
|
29
|
+
// This part is specific for controlled mode and mocks behavior of React dispatcher function.
|
30
|
+
const stateValueRef = React.useRef(options.state);
|
31
|
+
React.useEffect(()=>{
|
32
|
+
stateValueRef.current = options.state;
|
33
|
+
}, [
|
34
|
+
options.state
|
35
|
+
]);
|
36
|
+
const setControlledState = React.useCallback((newState)=>{
|
37
|
+
if (isFactoryDispatch(newState)) {
|
38
|
+
newState(stateValueRef.current);
|
39
|
+
}
|
40
|
+
}, []);
|
41
|
+
return useIsControlled(options.state) ? [
|
42
|
+
options.state,
|
43
|
+
setControlledState
|
44
|
+
] : [
|
45
|
+
internalState,
|
46
|
+
setInternalState
|
47
|
+
];
|
41
48
|
};
|
42
49
|
function isInitializer(value) {
|
43
|
-
|
50
|
+
return typeof value === 'function';
|
44
51
|
}
|
45
52
|
/**
|
46
53
|
* Helper hook to handle previous comparison of controlled/uncontrolled
|
47
54
|
* Prints an error when isControlled value switches between subsequent renders
|
48
55
|
* @returns - whether the value is controlled
|
49
|
-
*/
|
50
|
-
const
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
56
|
+
*/ const useIsControlled = (controlledValue)=>{
|
57
|
+
const [isControlled] = React.useState(()=>controlledValue !== undefined);
|
58
|
+
if (process.env.NODE_ENV !== 'production') {
|
59
|
+
// We don't want these warnings in production even though it is against native behaviour
|
60
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
61
|
+
React.useEffect(()=>{
|
62
|
+
if (isControlled !== (controlledValue !== undefined)) {
|
63
|
+
const error = new Error();
|
64
|
+
const controlWarning = isControlled ? 'a controlled value to be uncontrolled' : 'an uncontrolled value to be controlled';
|
65
|
+
const undefinedWarning = isControlled ? 'defined to an undefined' : 'undefined to a defined';
|
66
|
+
// eslint-disable-next-line no-console
|
67
|
+
console.error([
|
68
|
+
// Default react error
|
69
|
+
'A component is changing ' + controlWarning + '. This is likely caused by the value',
|
70
|
+
'changing from ' + undefinedWarning + ' value, which should not happen.',
|
71
|
+
'Decide between using a controlled or uncontrolled input element for the lifetime of the component.',
|
72
|
+
'More info: https://reactjs.org/link/controlled-components',
|
73
|
+
error.stack
|
74
|
+
].join(' '));
|
75
|
+
}
|
76
|
+
}, [
|
77
|
+
isControlled,
|
78
|
+
controlledValue
|
79
|
+
]);
|
80
|
+
}
|
81
|
+
return isControlled;
|
68
82
|
};
|
69
|
-
//# sourceMappingURL=useControllableState.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["useControllableState.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * @internal\n */\nexport type UseControllableStateOptions<State> = {\n /**\n * User-provided default state or initializer, for uncontrolled usage.\n */\n defaultState?: State | (() => State);\n /**\n * User-provided controlled state. `undefined` means internal state will be used.\n */\n state: State | undefined;\n /**\n * Used as the initial state if `state` and `defaultState` are both `undefined`.\n * If `undefined` is the correct initial state, pass that here.\n */\n initialState: State;\n};\n\nfunction isFactoryDispatch<State>(newState: React.SetStateAction<State>): newState is (prevState: State) => State {\n return typeof newState === 'function';\n}\n\n/**\n * @internal\n *\n * A [`useState`](https://reactjs.org/docs/hooks-reference.html#usestate)-like hook\n * to manage a value that could be either `controlled` or `uncontrolled`,\n * such as a checked state or text input string.\n *\n * @see https://react.dev/learn/sharing-state-between-components#controlled-and-uncontrolled-components for more details on `controlled`/`uncontrolled`\n *\n * @returns an array of the current value and an updater (dispatcher) function.\n * The updater function is referentially stable (won't change during the component's lifecycle).\n * It can take either a new value, or a function which is passed the previous value and returns the new value.\n *\n * ❗️❗️ Calls to the dispatcher will only modify the state if the state is `uncontrolled`.\n * Meaning that if a state is `controlled`, calls to the dispatcher do not modify the state.\n *\n */\nexport const useControllableState = <State>(\n options: UseControllableStateOptions<State>,\n): [State, React.Dispatch<React.SetStateAction<State>>] => {\n const [internalState, setInternalState] = React.useState<State>(() => {\n if (options.defaultState === undefined) {\n return options.initialState;\n }\n return isInitializer(options.defaultState) ? options.defaultState() : options.defaultState;\n });\n\n // Heads up!\n // This part is specific for controlled mode and mocks behavior of React dispatcher function.\n\n const stateValueRef = React.useRef<State | undefined>(options.state);\n\n React.useEffect(() => {\n stateValueRef.current = options.state;\n }, [options.state]);\n\n const setControlledState = React.useCallback((newState: React.SetStateAction<State>) => {\n if (isFactoryDispatch(newState)) {\n newState(stateValueRef.current as State);\n }\n }, []);\n\n return useIsControlled(options.state) ? [options.state, setControlledState] : [internalState, setInternalState];\n};\n\nfunction isInitializer<State>(value: State | (() => State)): value is () => State {\n return typeof value === 'function';\n}\n\n/**\n * Helper hook to handle previous comparison of controlled/uncontrolled\n * Prints an error when isControlled value switches between subsequent renders\n * @returns - whether the value is controlled\n */\nconst useIsControlled = <V>(controlledValue: V | undefined): controlledValue is V => {\n const [isControlled] = React.useState<boolean>(() => controlledValue !== undefined);\n\n if (process.env.NODE_ENV !== 'production') {\n // We don't want these warnings in production even though it is against native behaviour\n // eslint-disable-next-line react-hooks/rules-of-hooks\n React.useEffect(() => {\n if (isControlled !== (controlledValue !== undefined)) {\n const error = new Error();\n\n const controlWarning = isControlled\n ? 'a controlled value to be uncontrolled'\n : 'an uncontrolled value to be controlled';\n\n const undefinedWarning = isControlled ? 'defined to an undefined' : 'undefined to a defined';\n\n // eslint-disable-next-line no-console\n console.error(\n [\n // Default react error\n 'A component is changing ' + controlWarning + '. This is likely caused by the value',\n 'changing from ' + undefinedWarning + ' value, which should not happen.',\n 'Decide between using a controlled or uncontrolled input element for the lifetime of the component.',\n 'More info: https://reactjs.org/link/controlled-components',\n error.stack,\n ].join(' '),\n );\n }\n }, [isControlled, controlledValue]);\n }\n\n return isControlled;\n};\n"],"names":["React","isFactoryDispatch","newState","useControllableState","options","internalState","setInternalState","useState","defaultState","undefined","initialState","isInitializer","stateValueRef","useRef","state","useEffect","current","setControlledState","useCallback","useIsControlled","value","controlledValue","isControlled","process","env","NODE_ENV","error","Error","controlWarning","undefinedWarning","console","stack","join"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAqB/B,SAASC,kBAAyBC,QAAqC,EAA2C;IAChH,OAAO,OAAOA,aAAa;AAC7B;AAEA;;;;;;;;;;;;;;;;CAgBC,GACD,OAAO,MAAMC,uBAAuB,CAClCC,UACyD;IACzD,MAAM,CAACC,eAAeC,iBAAiB,GAAGN,MAAMO,QAAQ,CAAQ,IAAM;QACpE,IAAIH,QAAQI,YAAY,KAAKC,WAAW;YACtC,OAAOL,QAAQM,YAAY;QAC7B,CAAC;QACD,OAAOC,cAAcP,QAAQI,YAAY,IAAIJ,QAAQI,YAAY,KAAKJ,QAAQI,YAAY;IAC5F;IAEA,YAAY;IACZ,6FAA6F;IAE7F,MAAMI,gBAAgBZ,MAAMa,MAAM,CAAoBT,QAAQU,KAAK;IAEnEd,MAAMe,SAAS,CAAC,IAAM;QACpBH,cAAcI,OAAO,GAAGZ,QAAQU,KAAK;IACvC,GAAG;QAACV,QAAQU,KAAK;KAAC;IAElB,MAAMG,qBAAqBjB,MAAMkB,WAAW,CAAC,CAAChB,WAA0C;QACtF,IAAID,kBAAkBC,WAAW;YAC/BA,SAASU,cAAcI,OAAO;QAChC,CAAC;IACH,GAAG,EAAE;IAEL,OAAOG,gBAAgBf,QAAQU,KAAK,IAAI;QAACV,QAAQU,KAAK;QAAEG;KAAmB,GAAG;QAACZ;QAAeC;KAAiB;AACjH,EAAE;AAEF,SAASK,cAAqBS,KAA4B,EAAwB;IAChF,OAAO,OAAOA,UAAU;AAC1B;AAEA;;;;CAIC,GACD,MAAMD,kBAAkB,CAAIE,kBAAyD;IACnF,MAAM,CAACC,aAAa,GAAGtB,MAAMO,QAAQ,CAAU,IAAMc,oBAAoBZ;IAEzE,IAAIc,QAAQC,GAAG,CAACC,QAAQ,KAAK,cAAc;QACzC,wFAAwF;QACxF,sDAAsD;QACtDzB,MAAMe,SAAS,CAAC,IAAM;YACpB,IAAIO,iBAAkBD,CAAAA,oBAAoBZ,SAAQ,GAAI;gBACpD,MAAMiB,QAAQ,IAAIC;gBAElB,MAAMC,iBAAiBN,eACnB,0CACA,wCAAwC;gBAE5C,MAAMO,mBAAmBP,eAAe,4BAA4B,wBAAwB;gBAE5F,sCAAsC;gBACtCQ,QAAQJ,KAAK,CACX;oBACE,sBAAsB;oBACtB,6BAA6BE,iBAAiB;oBAC9C,mBAAmBC,mBAAmB;oBACtC;oBACA;oBACAH,MAAMK,KAAK;iBACZ,CAACC,IAAI,CAAC;YAEX,CAAC;QACH,GAAG;YAACV;YAAcD;SAAgB;IACpC,CAAC;IAED,OAAOC;AACT"}
|
@@ -12,17 +12,19 @@ import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
|
|
12
12
|
* will not be invalidated between re-renders
|
13
13
|
*
|
14
14
|
* @param fn - The callback function that will be used
|
15
|
-
*/
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
15
|
+
*/ export const useEventCallback = (fn)=>{
|
16
|
+
const callbackRef = React.useRef(()=>{
|
17
|
+
throw new Error('Cannot call an event handler while rendering');
|
18
|
+
});
|
19
|
+
useIsomorphicLayoutEffect(()=>{
|
20
|
+
callbackRef.current = fn;
|
21
|
+
}, [
|
22
|
+
fn
|
23
|
+
]);
|
24
|
+
return React.useCallback((...args)=>{
|
25
|
+
const callback = callbackRef.current;
|
26
|
+
return callback(...args);
|
27
|
+
}, [
|
28
|
+
callbackRef
|
29
|
+
]);
|
27
30
|
};
|
28
|
-
//# sourceMappingURL=useEventCallback.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["useEventCallback.ts"],"sourcesContent":["import * as React from 'react';\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';\n\n/**\n * @internal\n * https://reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback\n *\n * Modified `useCallback` that can be used when dependencies change too frequently. Can occur when\n * e.g. user props are dependencies which could change on every render\n * e.g. volatile values (i.e. useState/useDispatch) are dependencies which could change frequently\n *\n * This should not be used often, but can be a useful re-render optimization since the callback is a ref and\n * will not be invalidated between re-renders\n *\n * @param fn - The callback function that will be used\n */\nexport const useEventCallback = <Args extends unknown[], Return>(fn: (...args: Args) => Return) => {\n const callbackRef = React.useRef<typeof fn>(() => {\n throw new Error('Cannot call an event handler while rendering');\n });\n\n useIsomorphicLayoutEffect(() => {\n callbackRef.current = fn;\n }, [fn]);\n\n return React.useCallback(\n (...args: Args) => {\n const callback = callbackRef.current;\n return callback(...args);\n },\n [callbackRef],\n );\n};\n"],"names":["React","useIsomorphicLayoutEffect","useEventCallback","fn","callbackRef","useRef","Error","current","useCallback","args","callback"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,yBAAyB,QAAQ,8BAA8B;AAExE;;;;;;;;;;;;CAYC,GACD,OAAO,MAAMC,mBAAmB,CAAiCC,KAAkC;IACjG,MAAMC,cAAcJ,MAAMK,MAAM,CAAY,IAAM;QAChD,MAAM,IAAIC,MAAM,gDAAgD;IAClE;IAEAL,0BAA0B,IAAM;QAC9BG,YAAYG,OAAO,GAAGJ;IACxB,GAAG;QAACA;KAAG;IAEP,OAAOH,MAAMQ,WAAW,CACtB,CAAC,GAAGC,OAAe;QACjB,MAAMC,WAAWN,YAAYG,OAAO;QACpC,OAAOG,YAAYD;IACrB,GACA;QAACL;KAAY;AAEjB,EAAE"}
|
@@ -9,13 +9,11 @@ import * as React from 'react';
|
|
9
9
|
*
|
10
10
|
* @example
|
11
11
|
* const isFirstMount = useFirstMount();
|
12
|
-
*/
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
return isFirst.current;
|
12
|
+
*/ export function useFirstMount() {
|
13
|
+
const isFirst = React.useRef(true);
|
14
|
+
if (isFirst.current) {
|
15
|
+
isFirst.current = false;
|
16
|
+
return true;
|
17
|
+
}
|
18
|
+
return isFirst.current;
|
20
19
|
}
|
21
|
-
//# sourceMappingURL=useFirstMount.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["useFirstMount.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * @internal\n * Checks if components was mounted the first time.\n * Since concurrent mode will be released in the future this needs to be verified\n * Currently (React 17) will always render the initial mount once\n * https://codesandbox.io/s/heuristic-brook-s4w0q?file=/src/App.jsx\n * https://codesandbox.io/s/holy-grass-8nieu?file=/src/App.jsx\n *\n * @example\n * const isFirstMount = useFirstMount();\n */\nexport function useFirstMount(): boolean {\n const isFirst = React.useRef(true);\n\n if (isFirst.current) {\n isFirst.current = false;\n return true;\n }\n\n return isFirst.current;\n}\n"],"names":["React","useFirstMount","isFirst","useRef","current"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAE/B;;;;;;;;;;CAUC,GACD,OAAO,SAASC,gBAAyB;IACvC,MAAMC,UAAUF,MAAMG,MAAM,CAAC,IAAI;IAEjC,IAAID,QAAQE,OAAO,EAAE;QACnBF,QAAQE,OAAO,GAAG,KAAK;QACvB,OAAO,IAAI;IACb,CAAC;IAED,OAAOF,QAAQE,OAAO;AACxB,CAAC"}
|
@@ -2,8 +2,6 @@ import { useReducer } from 'react';
|
|
2
2
|
/**
|
3
3
|
* @internal
|
4
4
|
* Forces a re-render, similar to `forceUpdate` in class components.
|
5
|
-
*/
|
6
|
-
|
7
|
-
return useReducer(x => x + 1, 0)[1];
|
5
|
+
*/ export function useForceUpdate() {
|
6
|
+
return useReducer((x)=>x + 1, 0)[1];
|
8
7
|
}
|
9
|
-
//# sourceMappingURL=useForceUpdate.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["useForceUpdate.ts"],"sourcesContent":["import { useReducer } from 'react';\n\n/**\n * @internal\n * Forces a re-render, similar to `forceUpdate` in class components.\n */\nexport function useForceUpdate() {\n return useReducer(x => x + 1, 0)[1];\n}\n"],"names":["useReducer","useForceUpdate","x"],"mappings":"AAAA,SAASA,UAAU,QAAQ,QAAQ;AAEnC;;;CAGC,GACD,OAAO,SAASC,iBAAiB;IAC/B,OAAOD,WAAWE,CAAAA,IAAKA,IAAI,GAAG,EAAE,CAAC,EAAE;AACrC,CAAC"}
|
package/lib/hooks/useId.js
CHANGED
@@ -1,19 +1,17 @@
|
|
1
1
|
import * as React from 'react';
|
2
2
|
import { defaultSSRContextValue, useSSRContext } from '../ssr/index';
|
3
|
-
const IdPrefixContext =
|
3
|
+
const IdPrefixContext = React.createContext(undefined);
|
4
4
|
/**
|
5
5
|
* Allows to define a prefix that will be used for all IDs generated by useId() hook. It's useful to avoid collisions
|
6
6
|
* between different bundles.
|
7
|
-
*/
|
8
|
-
export const IdPrefixProvider = IdPrefixContext.Provider;
|
7
|
+
*/ export const IdPrefixProvider = IdPrefixContext.Provider;
|
9
8
|
function useIdPrefix() {
|
10
|
-
|
9
|
+
return React.useContext(IdPrefixContext) || '';
|
11
10
|
}
|
12
11
|
/**
|
13
12
|
* Resets generated IDs, should be used only in tests.
|
14
|
-
*/
|
15
|
-
|
16
|
-
defaultSSRContextValue.current = 0;
|
13
|
+
*/ export function resetIdsForTests() {
|
14
|
+
defaultSSRContextValue.current = 0;
|
17
15
|
}
|
18
16
|
/**
|
19
17
|
* Hook to generate a unique ID.
|
@@ -22,27 +20,32 @@ export function resetIdsForTests() {
|
|
22
20
|
* @param providedId - Optional id provided by a parent component. Defaults to the provided value if present,
|
23
21
|
* without conditioning the hook call
|
24
22
|
* @returns The ID
|
25
|
-
*/
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
// Hooks appear to be running conditionally, but they will always run in the same order since it's based on
|
39
|
-
// the version of React being used. This is safe to ignore.
|
40
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
41
|
-
return React.useMemo(() => {
|
42
|
-
if (providedId) {
|
43
|
-
return providedId;
|
23
|
+
*/ export function useId(prefix = 'fui-', providedId) {
|
24
|
+
const contextValue = useSSRContext();
|
25
|
+
const idPrefix = useIdPrefix();
|
26
|
+
// Checking if useId is available on React, if it is, we use it to generate the id. String concatenation is used to
|
27
|
+
// prevent bundlers from complaining with older versions of React.
|
28
|
+
const _useId = React['use' + 'Id'];
|
29
|
+
if (_useId) {
|
30
|
+
const generatedId = _useId();
|
31
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
32
|
+
const escapedId = React.useMemo(()=>generatedId.replace(/:/g, ''), [
|
33
|
+
generatedId
|
34
|
+
]);
|
35
|
+
return providedId || `${idPrefix}${prefix}${escapedId}`;
|
44
36
|
}
|
45
|
-
|
46
|
-
|
37
|
+
// Hooks appear to be running conditionally, but they will always run in the same order since it's based on
|
38
|
+
// the version of React being used. This is safe to ignore.
|
39
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
40
|
+
return React.useMemo(()=>{
|
41
|
+
if (providedId) {
|
42
|
+
return providedId;
|
43
|
+
}
|
44
|
+
return `${idPrefix}${prefix}${++contextValue.current}`;
|
45
|
+
}, [
|
46
|
+
idPrefix,
|
47
|
+
prefix,
|
48
|
+
providedId,
|
49
|
+
contextValue
|
50
|
+
]);
|
47
51
|
}
|
48
|
-
//# sourceMappingURL=useId.js.map
|
package/lib/hooks/useId.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["useId.ts"],"sourcesContent":["import * as React from 'react';\nimport { defaultSSRContextValue, useSSRContext } from '../ssr/index';\n\nconst IdPrefixContext = React.createContext<string | undefined>(undefined);\n\n/**\n * Allows to define a prefix that will be used for all IDs generated by useId() hook. It's useful to avoid collisions\n * between different bundles.\n */\nexport const IdPrefixProvider = IdPrefixContext.Provider;\n\nfunction useIdPrefix(): string {\n return React.useContext(IdPrefixContext) || '';\n}\n\n/**\n * Resets generated IDs, should be used only in tests.\n */\nexport function resetIdsForTests(): void {\n defaultSSRContextValue.current = 0;\n}\n\n/**\n * Hook to generate a unique ID.\n *\n * @param prefix - Optional prefix for the ID. Defaults to 'fui-'.\n * @param providedId - Optional id provided by a parent component. Defaults to the provided value if present,\n * without conditioning the hook call\n * @returns The ID\n */\nexport function useId(prefix: string = 'fui-', providedId?: string): string {\n const contextValue = useSSRContext();\n const idPrefix = useIdPrefix();\n\n // Checking if useId is available on React, if it is, we use it to generate the id. String concatenation is used to\n // prevent bundlers from complaining with older versions of React.\n const _useId = (React as never)['use' + 'Id'] as (() => string) | undefined;\n\n if (_useId) {\n const generatedId = _useId();\n\n // eslint-disable-next-line react-hooks/rules-of-hooks\n const escapedId = React.useMemo(() => generatedId.replace(/:/g, ''), [generatedId]);\n\n return providedId || `${idPrefix}${prefix}${escapedId}`;\n }\n\n // Hooks appear to be running conditionally, but they will always run in the same order since it's based on\n // the version of React being used. This is safe to ignore.\n // eslint-disable-next-line react-hooks/rules-of-hooks\n return React.useMemo(() => {\n if (providedId) {\n return providedId;\n }\n\n return `${idPrefix}${prefix}${++contextValue.current}`;\n }, [idPrefix, prefix, providedId, contextValue]);\n}\n"],"names":["React","defaultSSRContextValue","useSSRContext","IdPrefixContext","createContext","undefined","IdPrefixProvider","Provider","useIdPrefix","useContext","resetIdsForTests","current","useId","prefix","providedId","contextValue","idPrefix","_useId","generatedId","escapedId","useMemo","replace"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,sBAAsB,EAAEC,aAAa,QAAQ,eAAe;AAErE,MAAMC,kBAAkBH,MAAMI,aAAa,CAAqBC;AAEhE;;;CAGC,GACD,OAAO,MAAMC,mBAAmBH,gBAAgBI,QAAQ,CAAC;AAEzD,SAASC,cAAsB;IAC7B,OAAOR,MAAMS,UAAU,CAACN,oBAAoB;AAC9C;AAEA;;CAEC,GACD,OAAO,SAASO,mBAAyB;IACvCT,uBAAuBU,OAAO,GAAG;AACnC,CAAC;AAED;;;;;;;CAOC,GACD,OAAO,SAASC,MAAMC,SAAiB,MAAM,EAAEC,UAAmB,EAAU;IAC1E,MAAMC,eAAeb;IACrB,MAAMc,WAAWR;IAEjB,mHAAmH;IACnH,kEAAkE;IAClE,MAAMS,SAAS,AAACjB,KAAe,CAAC,QAAQ,KAAK;IAE7C,IAAIiB,QAAQ;QACV,MAAMC,cAAcD;QAEpB,sDAAsD;QACtD,MAAME,YAAYnB,MAAMoB,OAAO,CAAC,IAAMF,YAAYG,OAAO,CAAC,MAAM,KAAK;YAACH;SAAY;QAElF,OAAOJ,cAAc,CAAC,EAAEE,SAAS,EAAEH,OAAO,EAAEM,UAAU,CAAC;IACzD,CAAC;IAED,2GAA2G;IAC3G,2DAA2D;IAC3D,sDAAsD;IACtD,OAAOnB,MAAMoB,OAAO,CAAC,IAAM;QACzB,IAAIN,YAAY;YACd,OAAOA;QACT,CAAC;QAED,OAAO,CAAC,EAAEE,SAAS,EAAEH,OAAO,EAAE,EAAEE,aAAaJ,OAAO,CAAC,CAAC;IACxD,GAAG;QAACK;QAAUH;QAAQC;QAAYC;KAAa;AACjD,CAAC"}
|
@@ -9,5 +9,4 @@ import { canUseDOM } from '../ssr/index';
|
|
9
9
|
* https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85
|
10
10
|
* https://github.com/reduxjs/react-redux/blob/master/src/utils/useIsomorphicLayoutEffect.js
|
11
11
|
*/ // eslint-disable-next-line no-restricted-properties
|
12
|
-
export const useIsomorphicLayoutEffect =
|
13
|
-
//# sourceMappingURL=useIsomorphicLayoutEffect.js.map
|
12
|
+
export const useIsomorphicLayoutEffect = canUseDOM() ? React.useLayoutEffect : React.useEffect;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["useIsomorphicLayoutEffect.ts"],"sourcesContent":["import * as React from 'react';\nimport { canUseDOM } from '../ssr/index';\n\n/**\n * React currently throws a warning when using useLayoutEffect on the server. To get around it, we can conditionally\n * useEffect on the server (no-op) and useLayoutEffect in the browser. We occasionally need useLayoutEffect to\n * ensure we don't get a render flash for certain operations, but we may also need affected components to render on\n * the server.\n *\n * https://gist.github.com/gaearon/e7d97cdf38a2907924ea12e4ebdf3c85\n * https://github.com/reduxjs/react-redux/blob/master/src/utils/useIsomorphicLayoutEffect.js\n */\n// eslint-disable-next-line no-restricted-properties\nexport const useIsomorphicLayoutEffect: typeof React.useEffect = canUseDOM() ? React.useLayoutEffect : React.useEffect;\n"],"names":["React","canUseDOM","useIsomorphicLayoutEffect","useLayoutEffect","useEffect"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,SAAS,QAAQ,eAAe;AAEzC;;;;;;;;CAQC,GACD,oDAAoD;AACpD,OAAO,MAAMC,4BAAoDD,cAAcD,MAAMG,eAAe,GAAGH,MAAMI,SAAS,CAAC"}
|
@@ -4,22 +4,21 @@ import * as React from 'react';
|
|
4
4
|
* updates all provided refs
|
5
5
|
* @param refs - Refs to collectively update with one ref value.
|
6
6
|
* @returns A function with an attached "current" prop, so that it can be treated like a RefObject.
|
7
|
-
*/
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
}
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
7
|
+
*/ export function useMergedRefs(...refs) {
|
8
|
+
const mergedCallback = React.useCallback((value)=>{
|
9
|
+
// Update the "current" prop hanging on the function.
|
10
|
+
mergedCallback.current = value;
|
11
|
+
for (const ref of refs){
|
12
|
+
if (typeof ref === 'function') {
|
13
|
+
ref(value);
|
14
|
+
} else if (ref) {
|
15
|
+
// work around the immutability of the React.Ref type
|
16
|
+
ref.current = value;
|
17
|
+
}
|
18
|
+
}
|
19
|
+
}, // eslint-disable-next-line react-hooks/exhaustive-deps -- already exhaustive
|
20
|
+
[
|
21
|
+
...refs
|
22
|
+
]);
|
23
|
+
return mergedCallback;
|
24
24
|
}
|
25
|
-
//# sourceMappingURL=useMergedRefs.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"
|
1
|
+
{"version":3,"sources":["useMergedRefs.ts"],"sourcesContent":["import * as React from 'react';\n\n/**\n * A Ref function which can be treated like a ref object in that it has an attached\n * current property, which will be updated as the ref is evaluated.\n */\nexport type RefObjectFunction<T> = React.RefObject<T> & ((value: T) => void);\n\n/**\n * React hook to merge multiple React refs (either MutableRefObjects or ref callbacks) into a single ref callback that\n * updates all provided refs\n * @param refs - Refs to collectively update with one ref value.\n * @returns A function with an attached \"current\" prop, so that it can be treated like a RefObject.\n */\nexport function useMergedRefs<T>(...refs: (React.Ref<T> | undefined)[]): RefObjectFunction<T> {\n const mergedCallback: RefObjectFunction<T> = React.useCallback(\n (value: T) => {\n // Update the \"current\" prop hanging on the function.\n (mergedCallback as unknown as React.MutableRefObject<T>).current = value;\n\n for (const ref of refs) {\n if (typeof ref === 'function') {\n ref(value);\n } else if (ref) {\n // work around the immutability of the React.Ref type\n (ref as unknown as React.MutableRefObject<T>).current = value;\n }\n }\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps -- already exhaustive\n [...refs],\n ) as unknown as RefObjectFunction<T>;\n\n return mergedCallback;\n}\n"],"names":["React","useMergedRefs","refs","mergedCallback","useCallback","value","current","ref"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAQ/B;;;;;CAKC,GACD,OAAO,SAASC,cAAiB,GAAGC,IAAkC,EAAwB;IAC5F,MAAMC,iBAAuCH,MAAMI,WAAW,CAC5D,CAACC,QAAa;QACZ,qDAAqD;QACpDF,eAAwDG,OAAO,GAAGD;QAEnE,KAAK,MAAME,OAAOL,KAAM;YACtB,IAAI,OAAOK,QAAQ,YAAY;gBAC7BA,IAAIF;YACN,OAAO,IAAIE,KAAK;gBACd,qDAAqD;gBACpDA,IAA6CD,OAAO,GAAGD;YAC1D,CAAC;QACH;IACF,GACA,6EAA6E;IAC7E;WAAIH;KAAK;IAGX,OAAOC;AACT,CAAC"}
|