@julo-ui/context
Advanced tools
Comparing version 0.0.5 to 0.0.6
import { CreateContextOptions, CreateContextReturn } from './types.js'; | ||
import 'react'; | ||
declare function createContext<T>(options?: CreateContextOptions<T>): CreateContextReturn<T | undefined> | CreateContextReturn<T>; | ||
declare function createContext<T, Strict extends boolean = true>(options: CreateContextOptions<T, Strict>): CreateContextReturn<T, Strict>; | ||
export { createContext }; |
@@ -27,6 +27,10 @@ "use strict"; | ||
var import_react = require("react"); | ||
// src/utils.ts | ||
function getDefaultErrorMessage(hook, provider) { | ||
return `${hook} must be used within ${provider}`; | ||
} | ||
function createContext(options = {}) { | ||
// src/context.ts | ||
function createContext(options) { | ||
const { | ||
@@ -38,11 +42,12 @@ name, | ||
providerName = "Provider", | ||
errorMessage | ||
errorMessage: rootErrorMessage | ||
} = options; | ||
const Context = (0, import_react.createContext)(defaultValue); | ||
Context.displayName = name; | ||
function useContext() { | ||
function useContext(errorMessage) { | ||
var _a; | ||
const context = (0, import_react.useContext)(Context); | ||
if (!context && strict) { | ||
const error = new Error( | ||
errorMessage != null ? errorMessage : getDefaultErrorMessage(hookName, providerName) | ||
(_a = errorMessage != null ? errorMessage : rootErrorMessage) != null ? _a : getDefaultErrorMessage(hookName, providerName) | ||
); | ||
@@ -54,4 +59,2 @@ error.name = "ContextError"; | ||
} | ||
if (!strict) | ||
return [Context.Provider, useContext, Context]; | ||
return [Context.Provider, useContext, Context]; | ||
@@ -58,0 +61,0 @@ } |
@@ -29,6 +29,10 @@ "use strict"; | ||
var import_react = require("react"); | ||
// src/utils.ts | ||
function getDefaultErrorMessage(hook, provider) { | ||
return `${hook} must be used within ${provider}`; | ||
} | ||
function createContext(options = {}) { | ||
// src/context.ts | ||
function createContext(options) { | ||
const { | ||
@@ -40,11 +44,12 @@ name, | ||
providerName = "Provider", | ||
errorMessage | ||
errorMessage: rootErrorMessage | ||
} = options; | ||
const Context = (0, import_react.createContext)(defaultValue); | ||
Context.displayName = name; | ||
function useContext() { | ||
function useContext(errorMessage) { | ||
var _a; | ||
const context = (0, import_react.useContext)(Context); | ||
if (!context && strict) { | ||
const error = new Error( | ||
errorMessage != null ? errorMessage : getDefaultErrorMessage(hookName, providerName) | ||
(_a = errorMessage != null ? errorMessage : rootErrorMessage) != null ? _a : getDefaultErrorMessage(hookName, providerName) | ||
); | ||
@@ -56,4 +61,2 @@ error.name = "ContextError"; | ||
} | ||
if (!strict) | ||
return [Context.Provider, useContext, Context]; | ||
return [Context.Provider, useContext, Context]; | ||
@@ -60,0 +63,0 @@ } |
import { Provider, Context } from 'react'; | ||
interface CreateContextOptions<T> { | ||
/** | ||
* @default true | ||
*/ | ||
strict?: boolean; | ||
interface BaseCreateContextOptions<T> { | ||
hookName?: string; | ||
@@ -17,4 +13,14 @@ providerName?: string; | ||
} | ||
type CreateContextReturn<T> = [Provider<T>, () => T, Context<T>]; | ||
type CreateContextOptions<T, Strict extends boolean = true> = BaseCreateContextOptions<T> & (Strict extends true ? { | ||
strict?: true; | ||
} : { | ||
strict: false; | ||
}); | ||
type ContextValue<T, Strict extends boolean = true> = Strict extends true ? T : T | undefined; | ||
type CreateContextReturn<T, Strict extends boolean> = [ | ||
Provider<ContextValue<T, Strict>>, | ||
(errorMessage?: string) => ContextValue<T, Strict>, | ||
Context<ContextValue<T, Strict>> | ||
]; | ||
export { CreateContextOptions, CreateContextReturn }; |
{ | ||
"name": "@julo-ui/context", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Custom context hooks extended from React", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
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
11121
17
264