45 lines
2.2 KiB
JavaScript
45 lines
2.2 KiB
JavaScript
|
import * as React from 'react';
|
||
|
import { FocusRectsContext } from './useFocusRects';
|
||
|
export var FocusRectsProvider = function (props) {
|
||
|
var providerRef = props.providerRef, layerRoot = props.layerRoot;
|
||
|
var registeredProviders = React.useState([])[0];
|
||
|
var parentContext = React.useContext(FocusRectsContext);
|
||
|
// Inherit the parent context if it exists, unless this is a layer root.
|
||
|
// This allows the topmost provider element in the DOM tree to handle the focus events.
|
||
|
// Since layers are in a separate HTML tree from their parent, they shouldn't use the parent's providerRef.
|
||
|
var inheritParentContext = parentContext !== undefined && !layerRoot;
|
||
|
var context = React.useMemo(function () {
|
||
|
return inheritParentContext
|
||
|
? undefined
|
||
|
: {
|
||
|
providerRef: providerRef,
|
||
|
registeredProviders: registeredProviders,
|
||
|
registerProvider: function (ref) {
|
||
|
// Register this child provider with the current context, and any parent contexts
|
||
|
registeredProviders.push(ref);
|
||
|
parentContext === null || parentContext === void 0 ? void 0 : parentContext.registerProvider(ref);
|
||
|
},
|
||
|
unregisterProvider: function (ref) {
|
||
|
parentContext === null || parentContext === void 0 ? void 0 : parentContext.unregisterProvider(ref);
|
||
|
var i = registeredProviders.indexOf(ref);
|
||
|
if (i >= 0) {
|
||
|
registeredProviders.splice(i, 1);
|
||
|
}
|
||
|
},
|
||
|
};
|
||
|
}, [providerRef, registeredProviders, parentContext, inheritParentContext]);
|
||
|
React.useEffect(function () {
|
||
|
if (context) {
|
||
|
context.registerProvider(context.providerRef);
|
||
|
return function () { return context.unregisterProvider(context.providerRef); };
|
||
|
}
|
||
|
}, [context]);
|
||
|
// Create a new context provider if this is not inheriting from the parent.
|
||
|
if (context) {
|
||
|
return React.createElement(FocusRectsContext.Provider, { value: context }, props.children);
|
||
|
}
|
||
|
else {
|
||
|
return React.createElement(React.Fragment, null, props.children);
|
||
|
}
|
||
|
};
|
||
|
//# sourceMappingURL=FocusRectsProvider.js.map
|