react-super-switch 1.0.3 → 1.0.6

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/README.md CHANGED
@@ -4,7 +4,22 @@ A small React utility component for **deterministic, readable, and safe conditio
4
4
 
5
5
  `react-super-switch` guarantees that **exactly one** view is rendered from a set of mutually exclusive options, even when conditions are complex, unrelated, or derived from business logic.
6
6
 
7
- ---
7
+ ## Bundle size
8
+
9
+ ![npm version](https://img.shields.io/npm/v/react-super-switch)
10
+ ![gzip size](https://img.shields.io/bundlephobia/minzip/react-super-switch)
11
+ ![dependencies](https://img.shields.io/badge/dependencies-none-brightgreen)
12
+ ![react](https://img.shields.io/badge/react-17%2B-blue)
13
+ ![typescript](https://img.shields.io/badge/typescript-fully%20typed-blue)
14
+ ![tree-shakeable](https://img.shields.io/badge/tree--shakeable-yes-success)
15
+
16
+ `react-super-switch` is designed to be extremely lightweight.
17
+
18
+ - **ESM bundle:** ~10 KB raw / **<= 1KB minified + gzipped**
19
+ - **CJS bundle:** ~7.2 KB raw / **<= 1KB minified + gzipped**
20
+ - **Dependencies:** none
21
+
22
+ Only the selected bundle format is included in your production build; TypeScript types and other metadata are not shipped to users.
8
23
 
9
24
  ## Why React Super Switch exists
10
25
 
@@ -1,10 +1,6 @@
1
1
  import type { PropsWithChildren } from "react";
2
- export type OptionProps = {
3
- condition?: boolean;
4
- "data-testid"?: string;
5
- default?: boolean;
6
- priority?: number;
7
- };
2
+ import type { ConditionalOptionProps, DefaultOptionProps } from "./types";
3
+ export type OptionProps = DefaultOptionProps | ConditionalOptionProps;
8
4
  declare const Option: ({ children }: PropsWithChildren<OptionProps>) => import("react/jsx-runtime").JSX.Element;
9
5
  export default Option;
10
6
  //# sourceMappingURL=Option.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Option.d.ts","sourceRoot":"","sources":["../../../src/components/Option/Option.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAE/C,MAAM,MAAM,WAAW,GAAG;IACxB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,QAAA,MAAM,MAAM,GAAI,cAAc,iBAAiB,CAAC,WAAW,CAAC,4CAAoB,CAAC;AAEjF,eAAe,MAAM,CAAC"}
1
+ {"version":3,"file":"Option.d.ts","sourceRoot":"","sources":["../../../src/components/Option/Option.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAE/C,OAAO,KAAK,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAE1E,MAAM,MAAM,WAAW,GAAG,kBAAkB,GAAG,sBAAsB,CAAC;AAEtE,QAAA,MAAM,MAAM,GAAI,cAAc,iBAAiB,CAAC,WAAW,CAAC,4CAAoB,CAAC;AAEjF,eAAe,MAAM,CAAC"}
@@ -0,0 +1,14 @@
1
+ interface BaseOptionProps {
2
+ "data-testid"?: string;
3
+ priority?: number;
4
+ }
5
+ export interface DefaultOptionProps extends BaseOptionProps {
6
+ default: true;
7
+ condition?: never;
8
+ }
9
+ export interface ConditionalOptionProps extends BaseOptionProps {
10
+ default?: never;
11
+ condition: boolean;
12
+ }
13
+ export {};
14
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/components/Option/types.ts"],"names":[],"mappings":"AAAA,UAAU,eAAe;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,kBAAmB,SAAQ,eAAe;IACzD,OAAO,EAAE,IAAI,CAAC;IACd,SAAS,CAAC,EAAE,KAAK,CAAC;CACnB;AAED,MAAM,WAAW,sBAAuB,SAAQ,eAAe;IAC7D,OAAO,CAAC,EAAE,KAAK,CAAC;IAChB,SAAS,EAAE,OAAO,CAAC;CACpB"}
@@ -1 +1 @@
1
- {"version":3,"file":"SuperSwitch.d.ts","sourceRoot":"","sources":["../../../src/components/SuperSwitch/SuperSwitch.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAI1C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAG3C,MAAM,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,EAAE,YAAY,CAAC,WAAW,CAAC,GAAG,YAAY,CAAC,WAAW,CAAC,EAAE,CAAC;IAClE,IAAI,CAAC,EAAE,UAAU,GAAG,MAAM,CAAC;CAC5B,CAAC;AAEF,QAAA,MAAM,WAAW,GAAI,oBAA6B,gBAAgB,uBA2CjE,CAAC;AAEF,eAAe,WAAW,CAAC"}
1
+ {"version":3,"file":"SuperSwitch.d.ts","sourceRoot":"","sources":["../../../src/components/SuperSwitch/SuperSwitch.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,OAAO,CAAC;AAI1C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAG3C,MAAM,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,EAAE,YAAY,CAAC,WAAW,CAAC,GAAG,YAAY,CAAC,WAAW,CAAC,EAAE,CAAC;IAClE,IAAI,CAAC,EAAE,UAAU,GAAG,MAAM,CAAC;CAC5B,CAAC;AAEF,QAAA,MAAM,WAAW,GAAI,oBAA6B,gBAAgB,uBA8CjE,CAAC;AAEF,eAAe,WAAW,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/components/SuperSwitch/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAIjD,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/components/SuperSwitch/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAKjD,CAAC"}
package/dist/index.cjs.js CHANGED
@@ -1,6 +1,6 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("react");var b={exports:{}},_={};var L;function ne(){if(L)return _;L=1;var i=Symbol.for("react.transitional.element"),l=Symbol.for("react.fragment");function f(u,n,s){var d=null;if(s!==void 0&&(d=""+s),n.key!==void 0&&(d=""+n.key),"key"in n){s={};for(var p in n)p!=="key"&&(s[p]=n[p])}else s=n;return n=s.ref,{$$typeof:i,type:u,key:d,ref:n!==void 0?n:null,props:s}}return _.Fragment=l,_.jsx=f,_.jsxs=f,_}var R={};var M;function oe(){return M||(M=1,process.env.NODE_ENV!=="production"&&(function(){function i(e){if(e==null)return null;if(typeof e=="function")return e.$$typeof===ee?null:e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case h:return"Fragment";case J:return"Profiler";case G:return"StrictMode";case X:return"Suspense";case Z:return"SuspenseList";case K:return"Activity"}if(typeof e=="object")switch(typeof e.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),e.$$typeof){case q:return"Portal";case H:return e.displayName||"Context";case z:return(e._context.displayName||"Context")+".Consumer";case B:var r=e.render;return e=e.displayName,e||(e=r.displayName||r.name||"",e=e!==""?"ForwardRef("+e+")":"ForwardRef"),e;case Q:return r=e.displayName||null,r!==null?r:i(e.type)||"Memo";case P:r=e._payload,e=e._init;try{return i(e(r))}catch{}}return null}function l(e){return""+e}function f(e){try{l(e);var r=!1}catch{r=!0}if(r){r=console;var t=r.error,o=typeof Symbol=="function"&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object";return t.call(r,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",o),l(e)}}function u(e){if(e===h)return"<>";if(typeof e=="object"&&e!==null&&e.$$typeof===P)return"<...>";try{var r=i(e);return r?"<"+r+">":"<...>"}catch{return"<...>"}}function n(){var e=w.A;return e===null?null:e.getOwner()}function s(){return Error("react-stack-top-frame")}function d(e){if(I.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return e.key!==void 0}function p(e,r){function t(){C||(C=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",r))}t.isReactWarning=!0,Object.defineProperty(e,"key",{get:t,configurable:!0})}function m(){var e=i(this.type);return Y[e]||(Y[e]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),e=this.props.ref,e!==void 0?e:null}function c(e,r,t,o,T,A){var a=t.ref;return e={$$typeof:x,type:e,key:r,props:t,_owner:o},(a!==void 0?a:null)!==null?Object.defineProperty(e,"ref",{enumerable:!1,get:m}):Object.defineProperty(e,"ref",{enumerable:!1,value:null}),e._store={},Object.defineProperty(e._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(e,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(e,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:T}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:A}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}function S(e,r,t,o,T,A){var a=r.children;if(a!==void 0)if(o)if(re(a)){for(o=0;o<a.length;o++)y(a[o]);Object.freeze&&Object.freeze(a)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else y(a);if(I.call(r,"key")){a=i(e);var E=Object.keys(r).filter(function(te){return te!=="key"});o=0<E.length?"{key: someKey, "+E.join(": ..., ")+": ...}":"{key: someKey}",F[a+o]||(E=0<E.length?"{"+E.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX:
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("react");var T={exports:{}},_={};var F;function ne(){if(F)return _;F=1;var i=Symbol.for("react.transitional.element"),c=Symbol.for("react.fragment");function f(u,n,s){var d=null;if(s!==void 0&&(d=""+s),n.key!==void 0&&(d=""+n.key),"key"in n){s={};for(var p in n)p!=="key"&&(s[p]=n[p])}else s=n;return n=s.ref,{$$typeof:i,type:u,key:d,ref:n!==void 0?n:null,props:s}}return _.Fragment=c,_.jsx=f,_.jsxs=f,_}var R={};var M;function oe(){return M||(M=1,process.env.NODE_ENV!=="production"&&(function(){function i(e){if(e==null)return null;if(typeof e=="function")return e.$$typeof===ee?null:e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case S:return"Fragment";case J:return"Profiler";case G:return"StrictMode";case X:return"Suspense";case Z:return"SuspenseList";case K:return"Activity"}if(typeof e=="object")switch(typeof e.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),e.$$typeof){case q:return"Portal";case B:return e.displayName||"Context";case z:return(e._context.displayName||"Context")+".Consumer";case H:var r=e.render;return e=e.displayName,e||(e=r.displayName||r.name||"",e=e!==""?"ForwardRef("+e+")":"ForwardRef"),e;case Q:return r=e.displayName||null,r!==null?r:i(e.type)||"Memo";case y:r=e._payload,e=e._init;try{return i(e(r))}catch{}}return null}function c(e){return""+e}function f(e){try{c(e);var r=!1}catch{r=!0}if(r){r=console;var t=r.error,o=typeof Symbol=="function"&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object";return t.call(r,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",o),c(e)}}function u(e){if(e===S)return"<>";if(typeof e=="object"&&e!==null&&e.$$typeof===y)return"<...>";try{var r=i(e);return r?"<"+r+">":"<...>"}catch{return"<...>"}}function n(){var e=P.A;return e===null?null:e.getOwner()}function s(){return Error("react-stack-top-frame")}function d(e){if(x.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return e.key!==void 0}function p(e,r){function t(){C||(C=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",r))}t.isReactWarning=!0,Object.defineProperty(e,"key",{get:t,configurable:!0})}function l(){var e=i(this.type);return Y[e]||(Y[e]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),e=this.props.ref,e!==void 0?e:null}function N(e,r,t,o,v,w){var a=t.ref;return e={$$typeof:j,type:e,key:r,props:t,_owner:o},(a!==void 0?a:null)!==null?Object.defineProperty(e,"ref",{enumerable:!1,get:l}):Object.defineProperty(e,"ref",{enumerable:!1,value:null}),e._store={},Object.defineProperty(e._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(e,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(e,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:v}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:w}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}function A(e,r,t,o,v,w){var a=r.children;if(a!==void 0)if(o)if(re(a)){for(o=0;o<a.length;o++)I(a[o]);Object.freeze&&Object.freeze(a)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else I(a);if(x.call(r,"key")){a=i(e);var E=Object.keys(r).filter(function(te){return te!=="key"});o=0<E.length?"{key: someKey, "+E.join(": ..., ")+": ...}":"{key: someKey}",$[a+o]||(E=0<E.length?"{"+E.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX:
2
2
  let props = %s;
3
3
  <%s {...props} />
4
4
  React keys must be passed directly to JSX without using spread:
5
5
  let props = %s;
6
- <%s key={someKey} {...props} />`,o,a,E,a),F[a+o]=!0)}if(a=null,t!==void 0&&(f(t),a=""+t),d(r)&&(f(r.key),a=""+r.key),"key"in r){t={};for(var N in r)N!=="key"&&(t[N]=r[N])}else t=r;return a&&p(t,typeof e=="function"?e.displayName||e.name||"Unknown":e),c(e,a,t,n(),T,A)}function y(e){j(e)?e._store&&(e._store.validated=1):typeof e=="object"&&e!==null&&e.$$typeof===P&&(e._payload.status==="fulfilled"?j(e._payload.value)&&e._payload.value._store&&(e._payload.value._store.validated=1):e._store&&(e._store.validated=1))}function j(e){return typeof e=="object"&&e!==null&&e.$$typeof===x}var v=O,x=Symbol.for("react.transitional.element"),q=Symbol.for("react.portal"),h=Symbol.for("react.fragment"),G=Symbol.for("react.strict_mode"),J=Symbol.for("react.profiler"),z=Symbol.for("react.consumer"),H=Symbol.for("react.context"),B=Symbol.for("react.forward_ref"),X=Symbol.for("react.suspense"),Z=Symbol.for("react.suspense_list"),Q=Symbol.for("react.memo"),P=Symbol.for("react.lazy"),K=Symbol.for("react.activity"),ee=Symbol.for("react.client.reference"),w=v.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,I=Object.prototype.hasOwnProperty,re=Array.isArray,k=console.createTask?console.createTask:function(){return null};v={react_stack_bottom_frame:function(e){return e()}};var C,Y={},D=v.react_stack_bottom_frame.bind(v,s)(),$=k(u(s)),F={};R.Fragment=h,R.jsx=function(e,r,t){var o=1e4>w.recentlyCreatedOwnerStacks++;return S(e,r,t,!1,o?Error("react-stack-top-frame"):D,o?k(u(e)):$)},R.jsxs=function(e,r,t){var o=1e4>w.recentlyCreatedOwnerStacks++;return S(e,r,t,!0,o?Error("react-stack-top-frame"):D,o?k(u(e)):$)}})()),R}var V;function ae(){return V||(V=1,process.env.NODE_ENV==="production"?b.exports=ne():b.exports=oe()),b.exports}var W=ae();const U=({children:i})=>W.jsx(W.Fragment,{children:i}),g={INVALID_CHILDREN_TYPE:"SuperSwitch only accepts <Option /> as children. Received an invalid child instead.",MISSING_PRIORITIES:'SuperSwitch is running in "priority" mode, but not all <Option /> elements define a priority. When using priority mode, every <Option /> must specify a numeric "priority" prop.',NO_OPTION_TO_RENDER:"SuperSwitch could not determine which option to render. No <Option /> had a truthy condition, and no default option was provided."},ie=i=>i.sort((l,f)=>{const u=l.props?.priority,n=f.props?.priority;return u===void 0&&n===void 0?0:u===void 0?1:n===void 0||u<n?-1:u>n?1:0}),se=({children:i,mode:l="fcfs"})=>{const[f,u]=O.useState(null);return O.useEffect(()=>{const n=[];let s=!1,d=!0;if(O.Children.forEach(i,c=>{if(!(c?.type===U))throw new Error(g.INVALID_CHILDREN_TYPE);n.push(c),c.props.priority!==void 0?s=!0:d=!1}),l==="priority"&&s&&!d)throw new Error(g.MISSING_PRIORITIES);const p=l==="priority"?ie(n):n,m=p.find(c=>!!c.props.condition&&!c.props.default)??p.find(c=>!!c.props.default);if(!m)throw new Error(g.NO_OPTION_TO_RENDER);u(m)},[i,l]),f};exports.Option=U;exports.SuperSwitch=se;
6
+ <%s key={someKey} {...props} />`,o,a,E,a),$[a+o]=!0)}if(a=null,t!==void 0&&(f(t),a=""+t),d(r)&&(f(r.key),a=""+r.key),"key"in r){t={};for(var k in r)k!=="key"&&(t[k]=r[k])}else t=r;return a&&p(t,typeof e=="function"?e.displayName||e.name||"Unknown":e),N(e,a,t,n(),v,w)}function I(e){g(e)?e._store&&(e._store.validated=1):typeof e=="object"&&e!==null&&e.$$typeof===y&&(e._payload.status==="fulfilled"?g(e._payload.value)&&e._payload.value._store&&(e._payload.value._store.validated=1):e._store&&(e._store.validated=1))}function g(e){return typeof e=="object"&&e!==null&&e.$$typeof===j}var m=O,j=Symbol.for("react.transitional.element"),q=Symbol.for("react.portal"),S=Symbol.for("react.fragment"),G=Symbol.for("react.strict_mode"),J=Symbol.for("react.profiler"),z=Symbol.for("react.consumer"),B=Symbol.for("react.context"),H=Symbol.for("react.forward_ref"),X=Symbol.for("react.suspense"),Z=Symbol.for("react.suspense_list"),Q=Symbol.for("react.memo"),y=Symbol.for("react.lazy"),K=Symbol.for("react.activity"),ee=Symbol.for("react.client.reference"),P=m.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,x=Object.prototype.hasOwnProperty,re=Array.isArray,h=console.createTask?console.createTask:function(){return null};m={react_stack_bottom_frame:function(e){return e()}};var C,Y={},D=m.react_stack_bottom_frame.bind(m,s)(),L=h(u(s)),$={};R.Fragment=S,R.jsx=function(e,r,t){var o=1e4>P.recentlyCreatedOwnerStacks++;return A(e,r,t,!1,o?Error("react-stack-top-frame"):D,o?h(u(e)):L)},R.jsxs=function(e,r,t){var o=1e4>P.recentlyCreatedOwnerStacks++;return A(e,r,t,!0,o?Error("react-stack-top-frame"):D,o?h(u(e)):L)}})()),R}var V;function ae(){return V||(V=1,process.env.NODE_ENV==="production"?T.exports=ne():T.exports=oe()),T.exports}var W=ae();const U=({children:i})=>W.jsx(W.Fragment,{children:i}),b={INVALID_CHILDREN_TYPE:"SuperSwitch only accepts <Option /> as children. Received an invalid child instead.",MISSING_PRIORITIES:'SuperSwitch is running in "priority" mode, but not all <Option /> elements define a priority. When using priority mode, every <Option /> must specify a numeric "priority" prop.',NO_OPTION_TO_RENDER:"SuperSwitch could not determine which option to render. No <Option /> had a truthy condition, and no default option was provided.",INVALID_OPTION_PROPS:'Invalid <Option />: "default" and "condition" cannot be used together. Use "default" for the fallback option, or "condition" for conditional options.'},ie=i=>i.sort((c,f)=>{const u=c.props?.priority,n=f.props?.priority;return u===void 0&&n===void 0?0:u===void 0?1:n===void 0||u<n?-1:u>n?1:0}),se=({children:i,mode:c="fcfs"})=>{const[f,u]=O.useState(null);return O.useEffect(()=>{const n=[];let s=!0;O.Children.forEach(i,l=>{if(!(l?.type===U))throw new Error(b.INVALID_CHILDREN_TYPE);if(l.props.default&&"condition"in l.props)throw new Error(b.INVALID_OPTION_PROPS);n.push(l),l.props.priority===void 0&&(s=!1)});let d=n;if(c==="priority"){if(!s)throw new Error(b.MISSING_PRIORITIES);d=ie(n)}const p=d.find(l=>!!l.props.condition&&!l.props.default)??d.find(l=>!!l.props.default);if(!p)throw new Error(b.NO_OPTION_TO_RENDER);u(p)},[i,c]),f};exports.Option=U;exports.SuperSwitch=se;
package/dist/index.esm.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import re, { useState as te, useEffect as ne, Children as oe } from "react";
2
- var b = { exports: {} }, _ = {};
3
- var F;
2
+ var T = { exports: {} }, _ = {};
3
+ var $;
4
4
  function ae() {
5
- if (F) return _;
6
- F = 1;
7
- var i = /* @__PURE__ */ Symbol.for("react.transitional.element"), u = /* @__PURE__ */ Symbol.for("react.fragment");
5
+ if ($) return _;
6
+ $ = 1;
7
+ var i = /* @__PURE__ */ Symbol.for("react.transitional.element"), c = /* @__PURE__ */ Symbol.for("react.fragment");
8
8
  function f(l, n, s) {
9
9
  var d = null;
10
10
  if (s !== void 0 && (d = "" + s), n.key !== void 0 && (d = "" + n.key), "key" in n) {
@@ -20,27 +20,27 @@ function ae() {
20
20
  props: s
21
21
  };
22
22
  }
23
- return _.Fragment = u, _.jsx = f, _.jsxs = f, _;
23
+ return _.Fragment = c, _.jsx = f, _.jsxs = f, _;
24
24
  }
25
- var m = {};
26
- var L;
25
+ var R = {};
26
+ var F;
27
27
  function ie() {
28
- return L || (L = 1, process.env.NODE_ENV !== "production" && (function() {
28
+ return F || (F = 1, process.env.NODE_ENV !== "production" && (function() {
29
29
  function i(e) {
30
30
  if (e == null) return null;
31
31
  if (typeof e == "function")
32
32
  return e.$$typeof === Q ? null : e.displayName || e.name || null;
33
33
  if (typeof e == "string") return e;
34
34
  switch (e) {
35
- case S:
35
+ case O:
36
36
  return "Fragment";
37
37
  case G:
38
38
  return "Profiler";
39
39
  case U:
40
40
  return "StrictMode";
41
- case H:
42
- return "Suspense";
43
41
  case B:
42
+ return "Suspense";
43
+ case H:
44
44
  return "SuspenseList";
45
45
  case Z:
46
46
  return "Activity";
@@ -60,7 +60,7 @@ function ie() {
60
60
  return e = e.displayName, e || (e = r.displayName || r.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
61
61
  case X:
62
62
  return r = e.displayName || null, r !== null ? r : i(e.type) || "Memo";
63
- case h:
63
+ case S:
64
64
  r = e._payload, e = e._init;
65
65
  try {
66
66
  return i(e(r));
@@ -69,12 +69,12 @@ function ie() {
69
69
  }
70
70
  return null;
71
71
  }
72
- function u(e) {
72
+ function c(e) {
73
73
  return "" + e;
74
74
  }
75
75
  function f(e) {
76
76
  try {
77
- u(e);
77
+ c(e);
78
78
  var r = !1;
79
79
  } catch {
80
80
  r = !0;
@@ -86,12 +86,12 @@ function ie() {
86
86
  r,
87
87
  "The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",
88
88
  o
89
- ), u(e);
89
+ ), c(e);
90
90
  }
91
91
  }
92
92
  function l(e) {
93
- if (e === S) return "<>";
94
- if (typeof e == "object" && e !== null && e.$$typeof === h)
93
+ if (e === O) return "<>";
94
+ if (typeof e == "object" && e !== null && e.$$typeof === S)
95
95
  return "<...>";
96
96
  try {
97
97
  var r = i(e);
@@ -101,14 +101,14 @@ function ie() {
101
101
  }
102
102
  }
103
103
  function n() {
104
- var e = P.A;
104
+ var e = y.A;
105
105
  return e === null ? null : e.getOwner();
106
106
  }
107
107
  function s() {
108
108
  return Error("react-stack-top-frame");
109
109
  }
110
110
  function d(e) {
111
- if (j.call(e, "key")) {
111
+ if (x.call(e, "key")) {
112
112
  var r = Object.getOwnPropertyDescriptor(e, "key").get;
113
113
  if (r && r.isReactWarning) return !1;
114
114
  }
@@ -116,7 +116,7 @@ function ie() {
116
116
  }
117
117
  function p(e, r) {
118
118
  function t() {
119
- I || (I = !0, console.error(
119
+ j || (j = !0, console.error(
120
120
  "%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",
121
121
  r
122
122
  ));
@@ -126,23 +126,23 @@ function ie() {
126
126
  configurable: !0
127
127
  });
128
128
  }
129
- function R() {
129
+ function u() {
130
130
  var e = i(this.type);
131
131
  return C[e] || (C[e] = !0, console.error(
132
132
  "Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release."
133
133
  )), e = this.props.ref, e !== void 0 ? e : null;
134
134
  }
135
- function c(e, r, t, o, T, k) {
135
+ function k(e, r, t, o, v, h) {
136
136
  var a = t.ref;
137
137
  return e = {
138
- $$typeof: x,
138
+ $$typeof: g,
139
139
  type: e,
140
140
  key: r,
141
141
  props: t,
142
142
  _owner: o
143
143
  }, (a !== void 0 ? a : null) !== null ? Object.defineProperty(e, "ref", {
144
144
  enumerable: !1,
145
- get: R
145
+ get: u
146
146
  }) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", {
147
147
  configurable: !1,
148
148
  enumerable: !1,
@@ -157,33 +157,33 @@ function ie() {
157
157
  configurable: !1,
158
158
  enumerable: !1,
159
159
  writable: !0,
160
- value: T
160
+ value: v
161
161
  }), Object.defineProperty(e, "_debugTask", {
162
162
  configurable: !1,
163
163
  enumerable: !1,
164
164
  writable: !0,
165
- value: k
165
+ value: h
166
166
  }), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
167
167
  }
168
- function O(e, r, t, o, T, k) {
168
+ function N(e, r, t, o, v, h) {
169
169
  var a = r.children;
170
170
  if (a !== void 0)
171
171
  if (o)
172
172
  if (K(a)) {
173
173
  for (o = 0; o < a.length; o++)
174
- y(a[o]);
174
+ A(a[o]);
175
175
  Object.freeze && Object.freeze(a);
176
176
  } else
177
177
  console.error(
178
178
  "React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."
179
179
  );
180
- else y(a);
181
- if (j.call(r, "key")) {
180
+ else A(a);
181
+ if (x.call(r, "key")) {
182
182
  a = i(e);
183
183
  var E = Object.keys(r).filter(function(ee) {
184
184
  return ee !== "key";
185
185
  });
186
- o = 0 < E.length ? "{key: someKey, " + E.join(": ..., ") + ": ...}" : "{key: someKey}", $[a + o] || (E = 0 < E.length ? "{" + E.join(": ..., ") + ": ...}" : "{}", console.error(
186
+ o = 0 < E.length ? "{key: someKey, " + E.join(": ..., ") + ": ...}" : "{key: someKey}", L[a + o] || (E = 0 < E.length ? "{" + E.join(": ..., ") + ": ...}" : "{}", console.error(
187
187
  `A props object containing a "key" prop is being spread into JSX:
188
188
  let props = %s;
189
189
  <%s {...props} />
@@ -194,94 +194,102 @@ React keys must be passed directly to JSX without using spread:
194
194
  a,
195
195
  E,
196
196
  a
197
- ), $[a + o] = !0);
197
+ ), L[a + o] = !0);
198
198
  }
199
199
  if (a = null, t !== void 0 && (f(t), a = "" + t), d(r) && (f(r.key), a = "" + r.key), "key" in r) {
200
200
  t = {};
201
- for (var A in r)
202
- A !== "key" && (t[A] = r[A]);
201
+ for (var w in r)
202
+ w !== "key" && (t[w] = r[w]);
203
203
  } else t = r;
204
204
  return a && p(
205
205
  t,
206
206
  typeof e == "function" ? e.displayName || e.name || "Unknown" : e
207
- ), c(
207
+ ), k(
208
208
  e,
209
209
  a,
210
210
  t,
211
211
  n(),
212
- T,
213
- k
212
+ v,
213
+ h
214
214
  );
215
215
  }
216
- function y(e) {
217
- g(e) ? e._store && (e._store.validated = 1) : typeof e == "object" && e !== null && e.$$typeof === h && (e._payload.status === "fulfilled" ? g(e._payload.value) && e._payload.value._store && (e._payload.value._store.validated = 1) : e._store && (e._store.validated = 1));
216
+ function A(e) {
217
+ I(e) ? e._store && (e._store.validated = 1) : typeof e == "object" && e !== null && e.$$typeof === S && (e._payload.status === "fulfilled" ? I(e._payload.value) && e._payload.value._store && (e._payload.value._store.validated = 1) : e._store && (e._store.validated = 1));
218
218
  }
219
- function g(e) {
220
- return typeof e == "object" && e !== null && e.$$typeof === x;
219
+ function I(e) {
220
+ return typeof e == "object" && e !== null && e.$$typeof === g;
221
221
  }
222
- var v = re, x = /* @__PURE__ */ Symbol.for("react.transitional.element"), W = /* @__PURE__ */ Symbol.for("react.portal"), S = /* @__PURE__ */ Symbol.for("react.fragment"), U = /* @__PURE__ */ Symbol.for("react.strict_mode"), G = /* @__PURE__ */ Symbol.for("react.profiler"), q = /* @__PURE__ */ Symbol.for("react.consumer"), J = /* @__PURE__ */ Symbol.for("react.context"), z = /* @__PURE__ */ Symbol.for("react.forward_ref"), H = /* @__PURE__ */ Symbol.for("react.suspense"), B = /* @__PURE__ */ Symbol.for("react.suspense_list"), X = /* @__PURE__ */ Symbol.for("react.memo"), h = /* @__PURE__ */ Symbol.for("react.lazy"), Z = /* @__PURE__ */ Symbol.for("react.activity"), Q = /* @__PURE__ */ Symbol.for("react.client.reference"), P = v.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, j = Object.prototype.hasOwnProperty, K = Array.isArray, w = console.createTask ? console.createTask : function() {
222
+ var m = re, g = /* @__PURE__ */ Symbol.for("react.transitional.element"), W = /* @__PURE__ */ Symbol.for("react.portal"), O = /* @__PURE__ */ Symbol.for("react.fragment"), U = /* @__PURE__ */ Symbol.for("react.strict_mode"), G = /* @__PURE__ */ Symbol.for("react.profiler"), q = /* @__PURE__ */ Symbol.for("react.consumer"), J = /* @__PURE__ */ Symbol.for("react.context"), z = /* @__PURE__ */ Symbol.for("react.forward_ref"), B = /* @__PURE__ */ Symbol.for("react.suspense"), H = /* @__PURE__ */ Symbol.for("react.suspense_list"), X = /* @__PURE__ */ Symbol.for("react.memo"), S = /* @__PURE__ */ Symbol.for("react.lazy"), Z = /* @__PURE__ */ Symbol.for("react.activity"), Q = /* @__PURE__ */ Symbol.for("react.client.reference"), y = m.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, x = Object.prototype.hasOwnProperty, K = Array.isArray, P = console.createTask ? console.createTask : function() {
223
223
  return null;
224
224
  };
225
- v = {
225
+ m = {
226
226
  react_stack_bottom_frame: function(e) {
227
227
  return e();
228
228
  }
229
229
  };
230
- var I, C = {}, Y = v.react_stack_bottom_frame.bind(
231
- v,
230
+ var j, C = {}, Y = m.react_stack_bottom_frame.bind(
231
+ m,
232
232
  s
233
- )(), D = w(l(s)), $ = {};
234
- m.Fragment = S, m.jsx = function(e, r, t) {
235
- var o = 1e4 > P.recentlyCreatedOwnerStacks++;
236
- return O(
233
+ )(), D = P(l(s)), L = {};
234
+ R.Fragment = O, R.jsx = function(e, r, t) {
235
+ var o = 1e4 > y.recentlyCreatedOwnerStacks++;
236
+ return N(
237
237
  e,
238
238
  r,
239
239
  t,
240
240
  !1,
241
241
  o ? Error("react-stack-top-frame") : Y,
242
- o ? w(l(e)) : D
242
+ o ? P(l(e)) : D
243
243
  );
244
- }, m.jsxs = function(e, r, t) {
245
- var o = 1e4 > P.recentlyCreatedOwnerStacks++;
246
- return O(
244
+ }, R.jsxs = function(e, r, t) {
245
+ var o = 1e4 > y.recentlyCreatedOwnerStacks++;
246
+ return N(
247
247
  e,
248
248
  r,
249
249
  t,
250
250
  !0,
251
251
  o ? Error("react-stack-top-frame") : Y,
252
- o ? w(l(e)) : D
252
+ o ? P(l(e)) : D
253
253
  );
254
254
  };
255
- })()), m;
255
+ })()), R;
256
256
  }
257
257
  var M;
258
258
  function se() {
259
- return M || (M = 1, process.env.NODE_ENV === "production" ? b.exports = ae() : b.exports = ie()), b.exports;
259
+ return M || (M = 1, process.env.NODE_ENV === "production" ? T.exports = ae() : T.exports = ie()), T.exports;
260
260
  }
261
261
  var V = se();
262
- const le = ({ children: i }) => /* @__PURE__ */ V.jsx(V.Fragment, { children: i }), N = {
262
+ const le = ({ children: i }) => /* @__PURE__ */ V.jsx(V.Fragment, { children: i }), b = {
263
263
  INVALID_CHILDREN_TYPE: "SuperSwitch only accepts <Option /> as children. Received an invalid child instead.",
264
264
  MISSING_PRIORITIES: 'SuperSwitch is running in "priority" mode, but not all <Option /> elements define a priority. When using priority mode, every <Option /> must specify a numeric "priority" prop.',
265
- NO_OPTION_TO_RENDER: "SuperSwitch could not determine which option to render. No <Option /> had a truthy condition, and no default option was provided."
266
- }, ue = (i) => i.sort((u, f) => {
267
- const l = u.props?.priority, n = f.props?.priority;
265
+ NO_OPTION_TO_RENDER: "SuperSwitch could not determine which option to render. No <Option /> had a truthy condition, and no default option was provided.",
266
+ INVALID_OPTION_PROPS: 'Invalid <Option />: "default" and "condition" cannot be used together. Use "default" for the fallback option, or "condition" for conditional options.'
267
+ }, ue = (i) => i.sort((c, f) => {
268
+ const l = c.props?.priority, n = f.props?.priority;
268
269
  return l === void 0 && n === void 0 ? 0 : l === void 0 ? 1 : n === void 0 || l < n ? -1 : l > n ? 1 : 0;
269
- }), fe = ({ children: i, mode: u = "fcfs" }) => {
270
+ }), fe = ({ children: i, mode: c = "fcfs" }) => {
270
271
  const [f, l] = te(null);
271
272
  return ne(() => {
272
273
  const n = [];
273
- let s = !1, d = !0;
274
- if (oe.forEach(i, (c) => {
275
- if (!(c?.type === le))
276
- throw new Error(N.INVALID_CHILDREN_TYPE);
277
- n.push(c), c.props.priority !== void 0 ? s = !0 : d = !1;
278
- }), u === "priority" && s && !d)
279
- throw new Error(N.MISSING_PRIORITIES);
280
- const p = u === "priority" ? ue(n) : n, R = p.find((c) => !!c.props.condition && !c.props.default) ?? p.find((c) => !!c.props.default);
281
- if (!R)
282
- throw new Error(N.NO_OPTION_TO_RENDER);
283
- l(R);
284
- }, [i, u]), f;
274
+ let s = !0;
275
+ oe.forEach(i, (u) => {
276
+ if (!(u?.type === le))
277
+ throw new Error(b.INVALID_CHILDREN_TYPE);
278
+ if (u.props.default && "condition" in u.props)
279
+ throw new Error(b.INVALID_OPTION_PROPS);
280
+ n.push(u), u.props.priority === void 0 && (s = !1);
281
+ });
282
+ let d = n;
283
+ if (c === "priority") {
284
+ if (!s)
285
+ throw new Error(b.MISSING_PRIORITIES);
286
+ d = ue(n);
287
+ }
288
+ const p = d.find((u) => !!u.props.condition && !u.props.default) ?? d.find((u) => !!u.props.default);
289
+ if (!p)
290
+ throw new Error(b.NO_OPTION_TO_RENDER);
291
+ l(p);
292
+ }, [i, c]), f;
285
293
  };
286
294
  export {
287
295
  le as Option,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-super-switch",
3
- "version": "1.0.3",
3
+ "version": "1.0.6",
4
4
  "type": "module",
5
5
  "description": "Deterministic conditional rendering for React. Render exactly one option from multiple independent conditions, with explicit priority and fail-fast guarantees.",
6
6
  "author": {
@@ -43,14 +43,17 @@
43
43
  "types": "dist/index.d.ts",
44
44
  "sideEffects": false,
45
45
  "scripts": {
46
+ "all": "npm run typecheck && npm run lint && npm run test:coverage && npm run build",
46
47
  "build": "tsc -p tsconfig.build.json && vite build",
47
48
  "format": "prettier . --write",
48
49
  "lint": "eslint .",
49
50
  "lint:fix": "eslint . --fix",
50
51
  "typecheck": "tsc -p .",
52
+ "tag": "node -e \"const v=require('./package.json').version; const { execSync } = require('child_process'); execSync('git tag ' + v + ' && git push origin ' + v, { stdio: 'inherit' });\"",
51
53
  "test": "vitest",
52
54
  "test:watch": "vitest --watch",
53
- "test:coverage": "vitest run --coverage"
55
+ "test:coverage": "vitest run --coverage",
56
+ "npmv": "npm version patch --no-git-tag-version"
54
57
  },
55
58
  "peerDependencies": {
56
59
  "react": ">=17.0.0",