UNPKG

react-native-sortables

Version:

Powerful Sortable Components for Flexible Content Reordering in React Native

48 lines (47 loc) 1.63 kB
"use strict"; import React from "react"; import { createContext, useContext, useMemo } from 'react'; import { error, getContextProvider } from '../../utils'; import { jsx as _jsx } from "react/jsx-runtime"; export default function createProvider(name, options) { return function (factory) { const { guarded = true } = options ?? {}; const Context = /*#__PURE__*/createContext(null); Context.displayName = name; const Provider = props => { const { children = props.children, enabled = true, value } = factory(props); if (!value) { throw error(`${name}Context value must be provided. You likely forgot to return it from the factory function.`); } const memoValue = useMemo(() => enabled ? value : null, // eslint-disable-next-line react-hooks/exhaustive-deps typeof value === 'object' ? // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment [enabled, ...Object.values(value)] : [enabled, value]); const ContextProvider = getContextProvider(Context); return /*#__PURE__*/_jsx(ContextProvider, { value: memoValue, children: children }); }; const useEnhancedContext = () => { const context = useContext(Context); if (guarded && context === null) { throw error(`${name} context must be used within a ${name}Provider`); } return context; }; return { [`${name}Context`]: Context, [`${name}Provider`]: Provider, [`use${name}Context`]: useEnhancedContext }; }; } //# sourceMappingURL=createProvider.js.map