@casl/react 2.2.1 → 3.0.0

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.
@@ -1,2 +1,2 @@
1
- import t,{Fragment as n,createElement as r,Children as i,PureComponent as u}from"react";function e(t,n){for(var r=0;r<n.length;r++){var i=n[r];i.enumerable=i.enumerable||false;i.configurable=true;if("value"in i)i.writable=true;Object.defineProperty(t,i.key,i)}}function o(t,n,r){if(n)e(t.prototype,n);if(r)e(t,r);return t}function f(){f=Object.assign||function(t){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var i in r)if(Object.prototype.hasOwnProperty.call(r,i))t[i]=r[i]}return t};return f.apply(this,arguments)}function c(t,n){t.prototype=Object.create(n.prototype);t.prototype.constructor=t;t.__proto__=n}var a=function t(){};var s=n?function(t){if(!t)return null;return t.length>1?r.apply(void 0,[n,null].concat(t)):i.only(t)}:i.only;var h=function(t){c(n,t);function n(){var n;for(var r=arguments.length,i=new Array(r),u=0;u<r;u++)i[u]=arguments[u];n=t.call.apply(t,[this].concat(i))||this;n.t=false;n.i=null;n.u=a;return n}var r=n.prototype;r.componentWillUnmount=function t(){this.u()};r.o=function _connectToAbility(t){var n=this;if(t===this.i)return;this.u();this.i=null;if(t){this.i=t;this.u=t.on("updated",(function(){return n.forceUpdate()}))}};r.h=function t(){var n=this.props;var r=n.of||n.a||n.an||n.this||n.on;var i=n.not?"cannot":"can";return n.ability[i](n.I||n.do,r,n.field)};r.render=function t(){this.o(this.props.ability);this.t=this.h();return this.props.passThrough||this.t?this.l():null};r.l=function t(){var n=this.props,r=n.children,i=n.ability;var u="function"===typeof r?r(this.t,i):r;return s(u)};o(n,[{key:"allowed",get:function t(){return this.t}}]);return n}(u);function l(t){var n,r;return r=n=function(t){c(n,t);function n(){return t.apply(this,arguments)||this}return n}(h),n.defaultProps={ability:t},r}function v(t){return function(n){return r(t,null,(function(t){return r(h,f({ability:t},n))}))}}function useAbility(n){if("production"!==process.env.NODE_ENV&&"function"!==typeof t.useContext)throw new Error("You must use React >= 16.8 in order to use useAbility()");var r=t.useContext(n);var i=t.useState(),u=i[0],e=i[1];t.useEffect((function(){return r.on("updated",(function(t){if(t.rules!==u)e(t.rules)}))}),[]);return r}export{h as Can,l as createCanBoundTo,v as createContextualCan,useAbility};
1
+ import t,{PureComponent as n,createElement as r}from"react";function i(t,n){for(var r=0;r<n.length;r++){var i=n[r];i.enumerable=i.enumerable||false;i.configurable=true;if("value"in i)i.writable=true;Object.defineProperty(t,i.key,i)}}function u(t,n,r){if(n)i(t.prototype,n);if(r)i(t,r);return t}function e(){e=Object.assign||function(t){for(var n=1;n<arguments.length;n++){var r=arguments[n];for(var i in r)if(Object.prototype.hasOwnProperty.call(r,i))t[i]=r[i]}return t};return e.apply(this,arguments)}function o(t,n){t.prototype=Object.create(n.prototype);t.prototype.constructor=t;f(t,n)}function f(t,n){f=Object.setPrototypeOf||function t(n,r){n.__proto__=r;return n};return f(t,n)}var c=function t(){};var s=function(t){o(n,t);function n(){var n;for(var r=arguments.length,i=new Array(r),u=0;u<r;u++)i[u]=arguments[u];n=t.call.apply(t,[this].concat(i))||this;n.t=false;n.i=null;n.u=c;return n}var r=n.prototype;r.componentWillUnmount=function t(){this.u()};r.o=function _connectToAbility(t){var n=this;if(t===this.i)return;this.u();this.i=null;if(t){this.i=t;this.u=t.on("updated",(function(){return n.forceUpdate()}))}};r.h=function t(){var n=this.props;var r=n.of||n.a||n.an||n.this||n.on;var i=n.not?"cannot":"can";return n.ability[i](n.I||n.do,r,n.field)};r.render=function t(){this.o(this.props.ability);this.t=this.h();return this.props.passThrough||this.t?this.v():null};r.v=function t(){var n=this.props,r=n.children,i=n.ability;var u="function"===typeof r?r(this.t,i):r;return u};u(n,[{key:"allowed",get:function t(){return this.t}}]);return n}(n);function a(t){var n,r;return r=n=function(t){o(n,t);function n(){return t.apply(this,arguments)||this}return n}(s),n.defaultProps={ability:t},r}function h(t){return function(n){return r(t,{children:function t(i){return r(s,e({ability:i},n))}})}}function useAbility(n){if("production"!==process.env.NODE_ENV&&"function"!==typeof t.useContext)throw new Error("You must use React >= 16.8 in order to use useAbility()");var r=t.useContext(n);var i=t.useState(),u=i[0],e=i[1];t.useEffect((function(){return r.on("updated",(function(t){if(t.rules!==u)e(t.rules)}))}),[]);return r}export{s as Can,a as createCanBoundTo,h as createContextualCan,useAbility};
2
2
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/Can.ts","../../src/factory.ts","../../src/hooks/useAbility.ts"],"sourcesContent":["import { Children, ReactNodeArray, PureComponent, Fragment, createElement } from 'react';\nimport {\n Unsubscribe,\n AbilityTuple,\n SubjectType,\n AnyAbility,\n Generics,\n Abilities,\n IfString,\n} from '@casl/ability';\n\nconst noop = () => {};\nconst renderChildren = Fragment\n ? (children?: ReactNodeArray) => {\n if (!children) {\n return null;\n }\n\n return children.length > 1\n ? createElement(Fragment, null, ...children)\n : Children.only(children);\n }\n : Children.only;\n\ntype AbilityCanProps<\n T extends Abilities,\n Else = IfString<T, { do: T } | { I: T }>\n> = T extends AbilityTuple\n ? { do: T[0], on: T[1], field?: string } |\n { I: T[0], a: Extract<T[1], SubjectType>, field?: string } |\n { I: T[0], an: Extract<T[1], SubjectType>, field?: string } |\n { I: T[0], this: Exclude<T[1], SubjectType>, field?: string }\n : Else;\n\ninterface ExtraProps {\n not?: boolean\n passThrough?: boolean\n}\n\ninterface CanExtraProps<T extends AnyAbility> extends ExtraProps {\n ability: T\n}\n\ninterface BoundCanExtraProps<T extends AnyAbility> extends ExtraProps {\n ability?: T\n}\n\nexport type CanProps<T extends AnyAbility> =\n AbilityCanProps<Generics<T>['abilities']> & CanExtraProps<T>;\nexport type BoundCanProps<T extends AnyAbility> =\n AbilityCanProps<Generics<T>['abilities']> & BoundCanExtraProps<T>;\n\nexport class Can<\n T extends AnyAbility,\n IsBound extends boolean = false\n> extends PureComponent<IsBound extends true ? BoundCanProps<T> : CanProps<T>> {\n private _isAllowed: boolean = false;\n private _ability: T | null = null;\n private _unsubscribeFromAbility: Unsubscribe = noop;\n\n componentWillUnmount() {\n this._unsubscribeFromAbility();\n }\n\n private _connectToAbility(ability?: T) {\n if (ability === this._ability) {\n return;\n }\n\n this._unsubscribeFromAbility();\n this._ability = null;\n\n if (ability) {\n this._ability = ability;\n this._unsubscribeFromAbility = ability.on('updated', () => this.forceUpdate());\n }\n }\n\n get allowed() {\n return this._isAllowed;\n }\n\n private _canRender(): boolean {\n const props: any = this.props;\n const subject = props.of || props.a || props.an || props.this || props.on;\n const can = props.not ? 'cannot' : 'can';\n\n return props.ability[can](props.I || props.do, subject, props.field);\n }\n\n render() {\n this._connectToAbility(this.props.ability);\n this._isAllowed = this._canRender();\n return this.props.passThrough || this._isAllowed ? this._renderChildren() : null;\n }\n\n private _renderChildren() {\n const { children, ability } = this.props;\n const elements = typeof children === 'function'\n ? children(this._isAllowed, ability)\n : children;\n\n return renderChildren(elements);\n }\n}\n","import { createElement as h, ComponentClass, Consumer, StatelessComponent } from 'react';\nimport { AnyAbility } from '@casl/ability';\nimport { Can, BoundCanProps } from './Can';\n\ninterface BoundCanClass<T extends AnyAbility> extends ComponentClass<BoundCanProps<T>> {\n new (props: BoundCanProps<T>, context?: any): Can<T, true>\n}\n\nexport function createCanBoundTo<T extends AnyAbility>(ability: T): BoundCanClass<T> {\n return class extends Can<T, true> {\n static defaultProps = { ability } as BoundCanClass<T>['defaultProps'];\n };\n}\n\nexport function createContextualCan<T extends AnyAbility>(\n Getter: Consumer<T>\n): StatelessComponent<BoundCanProps<T>> {\n return (props: BoundCanProps<T>) => h(Getter, null, (ability: T) => h(Can, {\n ability,\n ...props,\n } as any));\n}\n","import React from 'react';\nimport { AnyAbility } from '@casl/ability';\n\nexport function useAbility<T extends AnyAbility>(context: React.Context<T>): T {\n if (process.env.NODE_ENV !== 'production' && typeof React.useContext !== 'function') {\n throw new Error('You must use React >= 16.8 in order to use useAbility()');\n }\n\n const ability = React.useContext<T>(context);\n const [rules, setRules] = React.useState<T['rules']>();\n\n React.useEffect(() => ability.on('updated', (event) => {\n if (event.rules !== rules) {\n setRules(event.rules);\n }\n }), []);\n\n return ability;\n}\n"],"names":["noop","renderChildren","Fragment","children","length","createElement","Children","only","Can","_isAllowed","_ability","_unsubscribeFromAbility","componentWillUnmount","_connectToAbility","ability","this","on","_this2","forceUpdate","_canRender","props","subject","of","a","an","can","not","I","do","field","render","passThrough","_renderChildren","elements","PureComponent","createCanBoundTo","defaultProps","createContextualCan","Getter","h","useAbility","context","process","env","NODE_ENV","React","useContext","Error","useState","rules","setRules","useEffect","event"],"mappings":"inBAWA,IAAMA,EAAO,SAAPA,MACN,IAAMC,EAAiBC,EACnB,SAACC,OACIA,SACI,YAGFA,EAASC,OAAS,EACrBC,gBAAcH,EAAU,aAASC,IACjCG,EAASC,KAAKJ,IAElBG,EAASC,SA8BAC,2JAIHC,EAAsB,QACtBC,EAAqB,OACrBC,EAAuCX,+BAE/CY,qBAAA,kBACOD,OAGCE,EAAR,2BAA0BC,iBACpBA,IAAYC,KAAKL,cAIhBC,SACAD,EAAW,QAEZI,EAAS,MACNJ,EAAWI,OACXH,EAA0BG,EAAQE,GAAG,WAAW,kBAAMC,EAAKC,qBAQ5DC,EAAR,iBACQC,EAAaL,KAAKK,UAClBC,EAAUD,EAAME,IAAMF,EAAMG,GAAKH,EAAMI,IAAMJ,EAAML,MAAQK,EAAMJ,OACjES,EAAML,EAAMM,IAAM,SAAW,aAE5BN,EAAMN,QAAQW,GAAKL,EAAMO,GAAKP,EAAMQ,GAAIP,EAASD,EAAMS,UAGhEC,OAAA,kBACOjB,EAAkBE,KAAKK,MAAMN,cAC7BL,EAAaM,KAAKI,WAChBJ,KAAKK,MAAMW,aAAehB,KAAKN,EAAaM,KAAKiB,IAAoB,QAGtEA,EAAR,mBACgCjB,KAAKK,MAA3BjB,IAAAA,SAAUW,IAAAA,YACZmB,EAA+B,oBAAb9B,EACpBA,EAASY,KAAKN,EAAYK,GAC1BX,SAEGF,EAAegC,gDAvBflB,KAAKN,gBAxBNyB,GC/CH,SAASC,EAAuCrB,qGAChCN,KACZ4B,aAAe,CAAEtB,QAAAA,KAIrB,SAASuB,EACdC,UAEO,SAAClB,UAA4BmB,EAAED,EAAQ,MAAM,SAACxB,UAAeyB,EAAE/B,KACpEM,QAAAA,GACGM,QChBA,SAASoB,WAAiCC,MAClB,eAAzBC,QAAQC,IAAIC,UAAyD,oBAArBC,EAAMC,iBAClD,IAAIC,MAAM,+DAGZjC,EAAU+B,EAAMC,WAAcL,SACVI,EAAMG,WAAzBC,OAAOC,OAEdL,EAAMM,WAAU,kBAAMrC,EAAQE,GAAG,WAAW,SAACoC,MACvCA,EAAMH,QAAUA,EAClBC,EAASE,EAAMH,YAEf,WAEGnC"}
1
+ {"version":3,"file":"index.js","sources":["../../src/Can.ts","../../src/factory.ts","../../src/hooks/useAbility.ts"],"sourcesContent":["import { PureComponent, ReactNode } from 'react';\nimport {\n Unsubscribe,\n AbilityTuple,\n SubjectType,\n AnyAbility,\n Generics,\n Abilities,\n IfString,\n} from '@casl/ability';\n\nconst noop = () => {};\n\ntype AbilityCanProps<\n T extends Abilities,\n Else = IfString<T, { do: T } | { I: T }>\n> = T extends AbilityTuple\n ? { do: T[0], on: T[1], field?: string } |\n { I: T[0], a: Extract<T[1], SubjectType>, field?: string } |\n { I: T[0], an: Extract<T[1], SubjectType>, field?: string } |\n { I: T[0], this: Exclude<T[1], SubjectType>, field?: string }\n : Else;\n\ninterface ExtraProps {\n not?: boolean\n passThrough?: boolean\n}\n\ninterface CanExtraProps<T extends AnyAbility> extends ExtraProps {\n ability: T\n children: ReactNode | ((isAllowed: boolean, ability: T) => ReactNode)\n}\n\ninterface BoundCanExtraProps<T extends AnyAbility> extends ExtraProps {\n ability?: T\n children: ReactNode | ((isAllowed: boolean, ability: T) => ReactNode)\n}\n\nexport type CanProps<T extends AnyAbility> =\n AbilityCanProps<Generics<T>['abilities']> & CanExtraProps<T>;\nexport type BoundCanProps<T extends AnyAbility> =\n AbilityCanProps<Generics<T>['abilities']> & BoundCanExtraProps<T>;\n\nexport class Can<\n T extends AnyAbility,\n IsBound extends boolean = false\n> extends PureComponent<IsBound extends true ? BoundCanProps<T> : CanProps<T>> {\n private _isAllowed: boolean = false;\n private _ability: T | null = null;\n private _unsubscribeFromAbility: Unsubscribe = noop;\n\n componentWillUnmount() {\n this._unsubscribeFromAbility();\n }\n\n private _connectToAbility(ability?: T) {\n if (ability === this._ability) {\n return;\n }\n\n this._unsubscribeFromAbility();\n this._ability = null;\n\n if (ability) {\n this._ability = ability;\n this._unsubscribeFromAbility = ability.on('updated', () => this.forceUpdate());\n }\n }\n\n get allowed() {\n return this._isAllowed;\n }\n\n private _canRender(): boolean {\n const props: any = this.props;\n const subject = props.of || props.a || props.an || props.this || props.on;\n const can = props.not ? 'cannot' : 'can';\n\n return props.ability[can](props.I || props.do, subject, props.field);\n }\n\n render() {\n this._connectToAbility(this.props.ability);\n this._isAllowed = this._canRender();\n return this.props.passThrough || this._isAllowed ? this._renderChildren() : null;\n }\n\n private _renderChildren() {\n const { children, ability } = this.props;\n const elements = typeof children === 'function'\n ? children(this._isAllowed, ability as any)\n : children;\n\n return elements as ReactNode;\n }\n}\n","import { createElement as h, ComponentClass, Consumer, FunctionComponent } from 'react';\nimport { AnyAbility } from '@casl/ability';\nimport { Can, BoundCanProps } from './Can';\n\ninterface BoundCanClass<T extends AnyAbility> extends ComponentClass<BoundCanProps<T>> {\n new (props: BoundCanProps<T>, context?: any): Can<T, true>\n}\n\nexport function createCanBoundTo<T extends AnyAbility>(ability: T): BoundCanClass<T> {\n return class extends Can<T, true> {\n static defaultProps = { ability } as BoundCanClass<T>['defaultProps'];\n };\n}\n\nexport function createContextualCan<T extends AnyAbility>(\n Getter: Consumer<T>\n): FunctionComponent<BoundCanProps<T>> {\n return (props: BoundCanProps<T>) => h(Getter, {\n children: (ability: T) => h(Can, {\n ability,\n ...props,\n } as any)\n });\n}\n","import React from 'react';\nimport { AnyAbility } from '@casl/ability';\n\nexport function useAbility<T extends AnyAbility>(context: React.Context<T>): T {\n if (process.env.NODE_ENV !== 'production' && typeof React.useContext !== 'function') {\n /* istanbul ignore next */\n throw new Error('You must use React >= 16.8 in order to use useAbility()');\n }\n\n const ability = React.useContext<T>(context);\n const [rules, setRules] = React.useState<T['rules']>();\n\n React.useEffect(() => ability.on('updated', (event) => {\n if (event.rules !== rules) {\n setRules(event.rules);\n }\n }), []);\n\n return ability;\n}\n"],"names":["noop","Can","_isAllowed","_ability","_unsubscribeFromAbility","componentWillUnmount","_connectToAbility","ability","this","on","_this2","forceUpdate","_canRender","props","subject","of","a","an","can","not","I","do","field","render","passThrough","_renderChildren","children","elements","PureComponent","createCanBoundTo","defaultProps","createContextualCan","Getter","h","useAbility","context","process","env","NODE_ENV","React","useContext","Error","useState","rules","setRules","useEffect","event"],"mappings":"6qBAWA,IAAMA,EAAO,SAAPA,UAgCOC,2JAIHC,EAAsB,QACtBC,EAAqB,OACrBC,EAAuCJ,+BAE/CK,qBAAA,kBACOD,OAGCE,EAAR,2BAA0BC,iBACpBA,IAAYC,KAAKL,cAIhBC,SACAD,EAAW,QAEZI,EAAS,MACNJ,EAAWI,OACXH,EAA0BG,EAAQE,GAAG,WAAW,kBAAMC,EAAKC,qBAQ5DC,EAAR,iBACQC,EAAaL,KAAKK,UAClBC,EAAUD,EAAME,IAAMF,EAAMG,GAAKH,EAAMI,IAAMJ,EAAML,MAAQK,EAAMJ,OACjES,EAAML,EAAMM,IAAM,SAAW,aAE5BN,EAAMN,QAAQW,GAAKL,EAAMO,GAAKP,EAAMQ,GAAIP,EAASD,EAAMS,UAGhEC,OAAA,kBACOjB,EAAkBE,KAAKK,MAAMN,cAC7BL,EAAaM,KAAKI,WAChBJ,KAAKK,MAAMW,aAAehB,KAAKN,EAAaM,KAAKiB,IAAoB,QAGtEA,EAAR,mBACgCjB,KAAKK,MAA3Ba,IAAAA,SAAUnB,IAAAA,YACZoB,EAA+B,oBAAbD,EACpBA,EAASlB,KAAKN,EAAYK,GAC1BmB,SAEGC,2BAxBT,oBACSnB,KAAKN,gBAxBN0B,GCtCH,SAASC,EAAuCtB,qGAChCN,KACZ6B,aAAe,CAAEvB,QAAAA,KAIrB,SAASwB,EACdC,UAEO,SAACnB,UAA4BoB,EAAED,EAAQ,CAC5CN,SAAU,WAACnB,UAAe0B,EAAEhC,KAC1BM,QAAAA,GACGM,QCjBF,SAASqB,WAAiCC,MAClB,eAAzBC,QAAQC,IAAIC,UAAyD,oBAArBC,EAAMC,iBAElD,IAAIC,MAAM,+DAGZlC,EAAUgC,EAAMC,WAAcL,SACVI,EAAMG,WAAzBC,OAAOC,OAEdL,EAAMM,WAAU,kBAAMtC,EAAQE,GAAG,WAAW,SAACqC,MACvCA,EAAMH,QAAUA,EAClBC,EAASE,EAAMH,YAEf,WAEGpC"}
@@ -1,2 +1,2 @@
1
- import t,{Fragment as n,createElement as i,Children as s,PureComponent as e}from"react";const r=()=>{};const o=n?t=>{if(!t)return null;return t.length>1?i(n,null,...t):s.only(t)}:s.only;class u extends e{constructor(...t){super(...t);this.t=false;this.i=null;this.o=r}componentWillUnmount(){this.o()}u(t){if(t===this.i)return;this.o();this.i=null;if(t){this.i=t;this.o=t.on("updated",(()=>this.forceUpdate()))}}get allowed(){return this.t}l(){const t=this.props;const n=t.of||t.a||t.an||t.this||t.on;const i=t.not?"cannot":"can";return t.ability[i](t.I||t.do,n,t.field)}render(){this.u(this.props.ability);this.t=this.l();return this.props.passThrough||this.t?this.h():null}h(){const{children:t,ability:n}=this.props;const i="function"===typeof t?t(this.t,n):t;return o(i)}}function c(t){var n,i;return i=n=class extends u{},n.defaultProps={ability:t},i}function l(t){return n=>i(t,null,(t=>i(u,Object.assign({ability:t},n))))}function useAbility(n){if("production"!==process.env.NODE_ENV&&"function"!==typeof t.useContext)throw new Error("You must use React >= 16.8 in order to use useAbility()");const i=t.useContext(n);const[s,e]=t.useState();t.useEffect((()=>i.on("updated",(t=>{if(t.rules!==s)e(t.rules)}))),[]);return i}export{u as Can,c as createCanBoundTo,l as createContextualCan,useAbility};
1
+ import t,{PureComponent as i,createElement as n}from"react";const s=()=>{};class e extends i{constructor(...t){super(...t);this.t=false;this.i=null;this.o=s}componentWillUnmount(){this.o()}u(t){if(t===this.i)return;this.o();this.i=null;if(t){this.i=t;this.o=t.on("updated",(()=>this.forceUpdate()))}}get allowed(){return this.t}h(){const t=this.props;const i=t.of||t.a||t.an||t.this||t.on;const n=t.not?"cannot":"can";return t.ability[n](t.I||t.do,i,t.field)}render(){this.u(this.props.ability);this.t=this.h();return this.props.passThrough||this.t?this.l():null}l(){const{children:t,ability:i}=this.props;const n="function"===typeof t?t(this.t,i):t;return n}}function r(t){var i,n;return n=i=class extends e{},i.defaultProps={ability:t},n}function o(t){return i=>n(t,{children:t=>n(e,Object.assign({ability:t},i))})}function useAbility(i){if("production"!==process.env.NODE_ENV&&"function"!==typeof t.useContext)throw new Error("You must use React >= 16.8 in order to use useAbility()");const n=t.useContext(i);const[s,e]=t.useState();t.useEffect((()=>n.on("updated",(t=>{if(t.rules!==s)e(t.rules)}))),[]);return n}export{e as Can,r as createCanBoundTo,o as createContextualCan,useAbility};
2
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../../src/Can.ts","../../src/factory.ts","../../src/hooks/useAbility.ts"],"sourcesContent":["import { Children, ReactNodeArray, PureComponent, Fragment, createElement } from 'react';\nimport {\n Unsubscribe,\n AbilityTuple,\n SubjectType,\n AnyAbility,\n Generics,\n Abilities,\n IfString,\n} from '@casl/ability';\n\nconst noop = () => {};\nconst renderChildren = Fragment\n ? (children?: ReactNodeArray) => {\n if (!children) {\n return null;\n }\n\n return children.length > 1\n ? createElement(Fragment, null, ...children)\n : Children.only(children);\n }\n : Children.only;\n\ntype AbilityCanProps<\n T extends Abilities,\n Else = IfString<T, { do: T } | { I: T }>\n> = T extends AbilityTuple\n ? { do: T[0], on: T[1], field?: string } |\n { I: T[0], a: Extract<T[1], SubjectType>, field?: string } |\n { I: T[0], an: Extract<T[1], SubjectType>, field?: string } |\n { I: T[0], this: Exclude<T[1], SubjectType>, field?: string }\n : Else;\n\ninterface ExtraProps {\n not?: boolean\n passThrough?: boolean\n}\n\ninterface CanExtraProps<T extends AnyAbility> extends ExtraProps {\n ability: T\n}\n\ninterface BoundCanExtraProps<T extends AnyAbility> extends ExtraProps {\n ability?: T\n}\n\nexport type CanProps<T extends AnyAbility> =\n AbilityCanProps<Generics<T>['abilities']> & CanExtraProps<T>;\nexport type BoundCanProps<T extends AnyAbility> =\n AbilityCanProps<Generics<T>['abilities']> & BoundCanExtraProps<T>;\n\nexport class Can<\n T extends AnyAbility,\n IsBound extends boolean = false\n> extends PureComponent<IsBound extends true ? BoundCanProps<T> : CanProps<T>> {\n private _isAllowed: boolean = false;\n private _ability: T | null = null;\n private _unsubscribeFromAbility: Unsubscribe = noop;\n\n componentWillUnmount() {\n this._unsubscribeFromAbility();\n }\n\n private _connectToAbility(ability?: T) {\n if (ability === this._ability) {\n return;\n }\n\n this._unsubscribeFromAbility();\n this._ability = null;\n\n if (ability) {\n this._ability = ability;\n this._unsubscribeFromAbility = ability.on('updated', () => this.forceUpdate());\n }\n }\n\n get allowed() {\n return this._isAllowed;\n }\n\n private _canRender(): boolean {\n const props: any = this.props;\n const subject = props.of || props.a || props.an || props.this || props.on;\n const can = props.not ? 'cannot' : 'can';\n\n return props.ability[can](props.I || props.do, subject, props.field);\n }\n\n render() {\n this._connectToAbility(this.props.ability);\n this._isAllowed = this._canRender();\n return this.props.passThrough || this._isAllowed ? this._renderChildren() : null;\n }\n\n private _renderChildren() {\n const { children, ability } = this.props;\n const elements = typeof children === 'function'\n ? children(this._isAllowed, ability)\n : children;\n\n return renderChildren(elements);\n }\n}\n","import { createElement as h, ComponentClass, Consumer, StatelessComponent } from 'react';\nimport { AnyAbility } from '@casl/ability';\nimport { Can, BoundCanProps } from './Can';\n\ninterface BoundCanClass<T extends AnyAbility> extends ComponentClass<BoundCanProps<T>> {\n new (props: BoundCanProps<T>, context?: any): Can<T, true>\n}\n\nexport function createCanBoundTo<T extends AnyAbility>(ability: T): BoundCanClass<T> {\n return class extends Can<T, true> {\n static defaultProps = { ability } as BoundCanClass<T>['defaultProps'];\n };\n}\n\nexport function createContextualCan<T extends AnyAbility>(\n Getter: Consumer<T>\n): StatelessComponent<BoundCanProps<T>> {\n return (props: BoundCanProps<T>) => h(Getter, null, (ability: T) => h(Can, {\n ability,\n ...props,\n } as any));\n}\n","import React from 'react';\nimport { AnyAbility } from '@casl/ability';\n\nexport function useAbility<T extends AnyAbility>(context: React.Context<T>): T {\n if (process.env.NODE_ENV !== 'production' && typeof React.useContext !== 'function') {\n throw new Error('You must use React >= 16.8 in order to use useAbility()');\n }\n\n const ability = React.useContext<T>(context);\n const [rules, setRules] = React.useState<T['rules']>();\n\n React.useEffect(() => ability.on('updated', (event) => {\n if (event.rules !== rules) {\n setRules(event.rules);\n }\n }), []);\n\n return ability;\n}\n"],"names":["noop","renderChildren","Fragment","children","length","createElement","Children","only","Can","PureComponent","_isAllowed","_ability","_unsubscribeFromAbility","componentWillUnmount","_connectToAbility","ability","this","on","forceUpdate","_canRender","props","subject","of","a","an","can","not","I","do","field","render","passThrough","_renderChildren","elements","createCanBoundTo","defaultProps","createContextualCan","Getter","h","useAbility","context","process","env","NODE_ENV","React","useContext","Error","rules","setRules","useState","useEffect","event"],"mappings":"wFAWA,MAAMA,EAAO,OACb,MAAMC,EAAiBC,EAClBC,QACIA,SACI,YAGFA,EAASC,OAAS,EACrBC,EAAcH,EAAU,QAASC,GACjCG,EAASC,KAAKJ,IAElBG,EAASC,KA8BN,MAAMC,UAGHC,qCACAC,EAAsB,WACtBC,EAAqB,UACrBC,EAAuCZ,EAE/Ca,4BACOD,IAGCE,EAAkBC,MACpBA,IAAYC,KAAKL,cAIhBC,SACAD,EAAW,QAEZI,EAAS,MACNJ,EAAWI,OACXH,EAA0BG,EAAQE,GAAG,WAAW,IAAMD,KAAKE,sCAK3DF,KAAKN,EAGNS,UACAC,EAAaJ,KAAKI,YAClBC,EAAUD,EAAME,IAAMF,EAAMG,GAAKH,EAAMI,IAAMJ,EAAMJ,MAAQI,EAAMH,SACjEQ,EAAML,EAAMM,IAAM,SAAW,aAE5BN,EAAML,QAAQU,GAAKL,EAAMO,GAAKP,EAAMQ,GAAIP,EAASD,EAAMS,OAGhEC,cACOhB,EAAkBE,KAAKI,MAAML,cAC7BL,EAAaM,KAAKG,WAChBH,KAAKI,MAAMW,aAAef,KAAKN,EAAaM,KAAKgB,IAAoB,KAGtEA,UACA7B,SAAEA,EAAFY,QAAYA,GAAYC,KAAKI,YAC7Ba,EAA+B,oBAAb9B,EACpBA,EAASa,KAAKN,EAAYK,GAC1BZ,SAEGF,EAAegC,IC9FnB,SAASC,EAAuCnB,sBAC9C,cAAcP,MACZ2B,aAAe,CAAEpB,QAAAA,KAIrB,SAASqB,EACdC,UAEQjB,GAA4BkB,EAAED,EAAQ,MAAOtB,GAAeuB,EAAE9B,iBACpEO,QAAAA,GACGK,MChBA,SAASmB,WAAiCC,MAClB,eAAzBC,QAAQC,IAAIC,UAAyD,oBAArBC,EAAMC,iBAClD,IAAIC,MAAM,iEAGZ/B,EAAU6B,EAAMC,WAAcL,SAC7BO,EAAOC,GAAYJ,EAAMK,WAEhCL,EAAMM,WAAU,IAAMnC,EAAQE,GAAG,WAAYkC,OACvCA,EAAMJ,QAAUA,EAClBC,EAASG,EAAMJ,WAEf,WAEGhC"}
1
+ {"version":3,"file":"index.mjs","sources":["../../src/Can.ts","../../src/factory.ts","../../src/hooks/useAbility.ts"],"sourcesContent":["import { PureComponent, ReactNode } from 'react';\nimport {\n Unsubscribe,\n AbilityTuple,\n SubjectType,\n AnyAbility,\n Generics,\n Abilities,\n IfString,\n} from '@casl/ability';\n\nconst noop = () => {};\n\ntype AbilityCanProps<\n T extends Abilities,\n Else = IfString<T, { do: T } | { I: T }>\n> = T extends AbilityTuple\n ? { do: T[0], on: T[1], field?: string } |\n { I: T[0], a: Extract<T[1], SubjectType>, field?: string } |\n { I: T[0], an: Extract<T[1], SubjectType>, field?: string } |\n { I: T[0], this: Exclude<T[1], SubjectType>, field?: string }\n : Else;\n\ninterface ExtraProps {\n not?: boolean\n passThrough?: boolean\n}\n\ninterface CanExtraProps<T extends AnyAbility> extends ExtraProps {\n ability: T\n children: ReactNode | ((isAllowed: boolean, ability: T) => ReactNode)\n}\n\ninterface BoundCanExtraProps<T extends AnyAbility> extends ExtraProps {\n ability?: T\n children: ReactNode | ((isAllowed: boolean, ability: T) => ReactNode)\n}\n\nexport type CanProps<T extends AnyAbility> =\n AbilityCanProps<Generics<T>['abilities']> & CanExtraProps<T>;\nexport type BoundCanProps<T extends AnyAbility> =\n AbilityCanProps<Generics<T>['abilities']> & BoundCanExtraProps<T>;\n\nexport class Can<\n T extends AnyAbility,\n IsBound extends boolean = false\n> extends PureComponent<IsBound extends true ? BoundCanProps<T> : CanProps<T>> {\n private _isAllowed: boolean = false;\n private _ability: T | null = null;\n private _unsubscribeFromAbility: Unsubscribe = noop;\n\n componentWillUnmount() {\n this._unsubscribeFromAbility();\n }\n\n private _connectToAbility(ability?: T) {\n if (ability === this._ability) {\n return;\n }\n\n this._unsubscribeFromAbility();\n this._ability = null;\n\n if (ability) {\n this._ability = ability;\n this._unsubscribeFromAbility = ability.on('updated', () => this.forceUpdate());\n }\n }\n\n get allowed() {\n return this._isAllowed;\n }\n\n private _canRender(): boolean {\n const props: any = this.props;\n const subject = props.of || props.a || props.an || props.this || props.on;\n const can = props.not ? 'cannot' : 'can';\n\n return props.ability[can](props.I || props.do, subject, props.field);\n }\n\n render() {\n this._connectToAbility(this.props.ability);\n this._isAllowed = this._canRender();\n return this.props.passThrough || this._isAllowed ? this._renderChildren() : null;\n }\n\n private _renderChildren() {\n const { children, ability } = this.props;\n const elements = typeof children === 'function'\n ? children(this._isAllowed, ability as any)\n : children;\n\n return elements as ReactNode;\n }\n}\n","import { createElement as h, ComponentClass, Consumer, FunctionComponent } from 'react';\nimport { AnyAbility } from '@casl/ability';\nimport { Can, BoundCanProps } from './Can';\n\ninterface BoundCanClass<T extends AnyAbility> extends ComponentClass<BoundCanProps<T>> {\n new (props: BoundCanProps<T>, context?: any): Can<T, true>\n}\n\nexport function createCanBoundTo<T extends AnyAbility>(ability: T): BoundCanClass<T> {\n return class extends Can<T, true> {\n static defaultProps = { ability } as BoundCanClass<T>['defaultProps'];\n };\n}\n\nexport function createContextualCan<T extends AnyAbility>(\n Getter: Consumer<T>\n): FunctionComponent<BoundCanProps<T>> {\n return (props: BoundCanProps<T>) => h(Getter, {\n children: (ability: T) => h(Can, {\n ability,\n ...props,\n } as any)\n });\n}\n","import React from 'react';\nimport { AnyAbility } from '@casl/ability';\n\nexport function useAbility<T extends AnyAbility>(context: React.Context<T>): T {\n if (process.env.NODE_ENV !== 'production' && typeof React.useContext !== 'function') {\n /* istanbul ignore next */\n throw new Error('You must use React >= 16.8 in order to use useAbility()');\n }\n\n const ability = React.useContext<T>(context);\n const [rules, setRules] = React.useState<T['rules']>();\n\n React.useEffect(() => ability.on('updated', (event) => {\n if (event.rules !== rules) {\n setRules(event.rules);\n }\n }), []);\n\n return ability;\n}\n"],"names":["noop","Can","PureComponent","_isAllowed","_ability","_unsubscribeFromAbility","componentWillUnmount","_connectToAbility","ability","this","on","forceUpdate","_canRender","props","subject","of","a","an","can","not","I","do","field","render","passThrough","_renderChildren","children","elements","createCanBoundTo","defaultProps","createContextualCan","Getter","h","useAbility","context","process","env","NODE_ENV","React","useContext","Error","rules","setRules","useState","useEffect","event"],"mappings":"4DAWA,MAAMA,EAAO,OAgCN,MAAMC,UAGHC,qCACAC,EAAsB,WACtBC,EAAqB,UACrBC,EAAuCL,EAE/CM,4BACOD,IAGCE,EAAkBC,MACpBA,IAAYC,KAAKL,cAIhBC,SACAD,EAAW,QAEZI,EAAS,MACNJ,EAAWI,OACXH,EAA0BG,EAAQE,GAAG,WAAW,IAAMD,KAAKE,sCAK3DF,KAAKN,EAGNS,UACAC,EAAaJ,KAAKI,YAClBC,EAAUD,EAAME,IAAMF,EAAMG,GAAKH,EAAMI,IAAMJ,EAAMJ,MAAQI,EAAMH,SACjEQ,EAAML,EAAMM,IAAM,SAAW,aAE5BN,EAAML,QAAQU,GAAKL,EAAMO,GAAKP,EAAMQ,GAAIP,EAASD,EAAMS,OAGhEC,cACOhB,EAAkBE,KAAKI,MAAML,cAC7BL,EAAaM,KAAKG,WAChBH,KAAKI,MAAMW,aAAef,KAAKN,EAAaM,KAAKgB,IAAoB,KAGtEA,UACAC,SAAEA,EAAFlB,QAAYA,GAAYC,KAAKI,YAC7Bc,EAA+B,oBAAbD,EACpBA,EAASjB,KAAKN,EAAYK,GAC1BkB,SAEGC,GCrFJ,SAASC,EAAuCpB,sBAC9C,cAAcP,MACZ4B,aAAe,CAAErB,QAAAA,KAIrB,SAASsB,EACdC,UAEQlB,GAA4BmB,EAAED,EAAQ,CAC5CL,SAAWlB,GAAewB,EAAE/B,iBAC1BO,QAAAA,GACGK,MCjBF,SAASoB,WAAiCC,MAClB,eAAzBC,QAAQC,IAAIC,UAAyD,oBAArBC,EAAMC,iBAElD,IAAIC,MAAM,iEAGZhC,EAAU8B,EAAMC,WAAcL,SAC7BO,EAAOC,GAAYJ,EAAMK,WAEhCL,EAAMM,WAAU,IAAMpC,EAAQE,GAAG,WAAYmC,OACvCA,EAAMJ,QAAUA,EAClBC,EAASG,EAAMJ,WAEf,WAEGjC"}
@@ -1,4 +1,4 @@
1
- import { PureComponent } from 'react';
1
+ import { PureComponent, ReactNode } from 'react';
2
2
  import { AbilityTuple, SubjectType, AnyAbility, Generics, Abilities, IfString } from '@casl/ability';
3
3
  declare type AbilityCanProps<T extends Abilities, Else = IfString<T, {
4
4
  do: T;
@@ -27,9 +27,11 @@ interface ExtraProps {
27
27
  }
28
28
  interface CanExtraProps<T extends AnyAbility> extends ExtraProps {
29
29
  ability: T;
30
+ children: ReactNode | ((isAllowed: boolean, ability: T) => ReactNode);
30
31
  }
31
32
  interface BoundCanExtraProps<T extends AnyAbility> extends ExtraProps {
32
33
  ability?: T;
34
+ children: ReactNode | ((isAllowed: boolean, ability: T) => ReactNode);
33
35
  }
34
36
  export declare type CanProps<T extends AnyAbility> = AbilityCanProps<Generics<T>['abilities']> & CanExtraProps<T>;
35
37
  export declare type BoundCanProps<T extends AnyAbility> = AbilityCanProps<Generics<T>['abilities']> & BoundCanExtraProps<T>;
@@ -41,7 +43,7 @@ export declare class Can<T extends AnyAbility, IsBound extends boolean = false>
41
43
  private _connectToAbility;
42
44
  get allowed(): boolean;
43
45
  private _canRender;
44
- render(): any;
46
+ render(): number | boolean | import("react").ReactElement<any, string | ((props: any) => import("react").ReactElement<any, any> | null) | (new (props: any) => import("react").Component<any, any, any>)> | import("react").ReactFragment | null | undefined;
45
47
  private _renderChildren;
46
48
  }
47
49
  export {};
@@ -1,9 +1,9 @@
1
- import { ComponentClass, Consumer, StatelessComponent } from 'react';
1
+ import { ComponentClass, Consumer, FunctionComponent } from 'react';
2
2
  import { AnyAbility } from '@casl/ability';
3
3
  import { Can, BoundCanProps } from './Can';
4
4
  interface BoundCanClass<T extends AnyAbility> extends ComponentClass<BoundCanProps<T>> {
5
5
  new (props: BoundCanProps<T>, context?: any): Can<T, true>;
6
6
  }
7
7
  export declare function createCanBoundTo<T extends AnyAbility>(ability: T): BoundCanClass<T>;
8
- export declare function createContextualCan<T extends AnyAbility>(Getter: Consumer<T>): StatelessComponent<BoundCanProps<T>>;
8
+ export declare function createContextualCan<T extends AnyAbility>(Getter: Consumer<T>): FunctionComponent<BoundCanProps<T>>;
9
9
  export {};
package/dist/umd/index.js CHANGED
@@ -1,2 +1,2 @@
1
- (function(t,n){"object"===typeof exports&&"undefined"!==typeof module?n(exports,require("react")):"function"===typeof define&&define.amd?define(["exports","react"],n):(t="undefined"!==typeof globalThis?globalThis:t||self,n((t.casl=t.casl||{},t.casl.react={}),t.React))})(this,(function(t,n){"use strict";function i(t){return t&&"object"===typeof t&&"default"in t?t:{default:t}}var r=i(n);function e(t,n){for(var i=0;i<n.length;i++){var r=n[i];r.enumerable=r.enumerable||false;r.configurable=true;if("value"in r)r.writable=true;Object.defineProperty(t,r.key,r)}}function u(t,n,i){if(n)e(t.prototype,n);if(i)e(t,i);return t}function o(){o=Object.assign||function(t){for(var n=1;n<arguments.length;n++){var i=arguments[n];for(var r in i)if(Object.prototype.hasOwnProperty.call(i,r))t[r]=i[r]}return t};return o.apply(this,arguments)}function f(t,n){t.prototype=Object.create(n.prototype);t.prototype.constructor=t;t.__proto__=n}var c=function t(){};var s=n.Fragment?function(t){if(!t)return null;return t.length>1?n.createElement.apply(void 0,[n.Fragment,null].concat(t)):n.Children.only(t)}:n.Children.only;var a=function(t){f(n,t);function n(){var n;for(var i=arguments.length,r=new Array(i),e=0;e<i;e++)r[e]=arguments[e];n=t.call.apply(t,[this].concat(r))||this;n.t=false;n.i=null;n.u=c;return n}var i=n.prototype;i.componentWillUnmount=function t(){this.u()};i.o=function _connectToAbility(t){var n=this;if(t===this.i)return;this.u();this.i=null;if(t){this.i=t;this.u=t.on("updated",(function(){return n.forceUpdate()}))}};i.l=function t(){var n=this.props;var i=n.of||n.a||n.an||n.this||n.on;var r=n.not?"cannot":"can";return n.ability[r](n.I||n.do,i,n.field)};i.render=function t(){this.o(this.props.ability);this.t=this.l();return this.props.passThrough||this.t?this.h():null};i.h=function t(){var n=this.props,i=n.children,r=n.ability;var e="function"===typeof i?i(this.t,r):i;return s(e)};u(n,[{key:"allowed",get:function t(){return this.t}}]);return n}(n.PureComponent);function l(t){var n,i;return i=n=function(t){f(n,t);function n(){return t.apply(this,arguments)||this}return n}(a),n.defaultProps={ability:t},i}function h(t){return function(i){return n.createElement(t,null,(function(t){return n.createElement(a,o({ability:t},i))}))}}function useAbility(t){if("production"!==process.env.NODE_ENV&&"function"!==typeof r["default"].useContext)throw new Error("You must use React >= 16.8 in order to use useAbility()");var n=r["default"].useContext(t);var i=r["default"].useState(),e=i[0],u=i[1];r["default"].useEffect((function(){return n.on("updated",(function(t){if(t.rules!==e)u(t.rules)}))}),[]);return n}t.Can=a;t.createCanBoundTo=l;t.createContextualCan=h;t.useAbility=useAbility;Object.defineProperty(t,"__esModule",{value:true})}));
1
+ (function(t,n){"object"===typeof exports&&"undefined"!==typeof module?n(exports,require("react")):"function"===typeof define&&define.amd?define(["exports","react"],n):(t="undefined"!==typeof globalThis?globalThis:t||self,n((t.casl=t.casl||{},t.casl.react={}),t.React))})(this,(function(t,n){"use strict";function i(t){return t&&"object"===typeof t&&"default"in t?t:{default:t}}var r=i(n);function e(t,n){for(var i=0;i<n.length;i++){var r=n[i];r.enumerable=r.enumerable||false;r.configurable=true;if("value"in r)r.writable=true;Object.defineProperty(t,r.key,r)}}function u(t,n,i){if(n)e(t.prototype,n);if(i)e(t,i);return t}function o(){o=Object.assign||function(t){for(var n=1;n<arguments.length;n++){var i=arguments[n];for(var r in i)if(Object.prototype.hasOwnProperty.call(i,r))t[r]=i[r]}return t};return o.apply(this,arguments)}function f(t,n){t.prototype=Object.create(n.prototype);t.prototype.constructor=t;c(t,n)}function c(t,n){c=Object.setPrototypeOf||function t(n,i){n.__proto__=i;return n};return c(t,n)}var s=function t(){};var a=function(t){f(n,t);function n(){var n;for(var i=arguments.length,r=new Array(i),e=0;e<i;e++)r[e]=arguments[e];n=t.call.apply(t,[this].concat(r))||this;n.t=false;n.i=null;n.u=s;return n}var i=n.prototype;i.componentWillUnmount=function t(){this.u()};i.o=function _connectToAbility(t){var n=this;if(t===this.i)return;this.u();this.i=null;if(t){this.i=t;this.u=t.on("updated",(function(){return n.forceUpdate()}))}};i.h=function t(){var n=this.props;var i=n.of||n.a||n.an||n.this||n.on;var r=n.not?"cannot":"can";return n.ability[r](n.I||n.do,i,n.field)};i.render=function t(){this.o(this.props.ability);this.t=this.h();return this.props.passThrough||this.t?this.l():null};i.l=function t(){var n=this.props,i=n.children,r=n.ability;var e="function"===typeof i?i(this.t,r):i;return e};u(n,[{key:"allowed",get:function t(){return this.t}}]);return n}(n.PureComponent);function h(t){var n,i;return i=n=function(t){f(n,t);function n(){return t.apply(this,arguments)||this}return n}(a),n.defaultProps={ability:t},i}function l(t){return function(i){return n.createElement(t,{children:function t(r){return n.createElement(a,o({ability:r},i))}})}}function useAbility(t){if("production"!==process.env.NODE_ENV&&"function"!==typeof r["default"].useContext)throw new Error("You must use React >= 16.8 in order to use useAbility()");var n=r["default"].useContext(t);var i=r["default"].useState(),e=i[0],u=i[1];r["default"].useEffect((function(){return n.on("updated",(function(t){if(t.rules!==e)u(t.rules)}))}),[]);return n}t.Can=a;t.createCanBoundTo=h;t.createContextualCan=l;t.useAbility=useAbility;Object.defineProperty(t,"__esModule",{value:true})}));
2
2
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/Can.ts","../../src/factory.ts","../../src/hooks/useAbility.ts"],"sourcesContent":["import { Children, ReactNodeArray, PureComponent, Fragment, createElement } from 'react';\nimport {\n Unsubscribe,\n AbilityTuple,\n SubjectType,\n AnyAbility,\n Generics,\n Abilities,\n IfString,\n} from '@casl/ability';\n\nconst noop = () => {};\nconst renderChildren = Fragment\n ? (children?: ReactNodeArray) => {\n if (!children) {\n return null;\n }\n\n return children.length > 1\n ? createElement(Fragment, null, ...children)\n : Children.only(children);\n }\n : Children.only;\n\ntype AbilityCanProps<\n T extends Abilities,\n Else = IfString<T, { do: T } | { I: T }>\n> = T extends AbilityTuple\n ? { do: T[0], on: T[1], field?: string } |\n { I: T[0], a: Extract<T[1], SubjectType>, field?: string } |\n { I: T[0], an: Extract<T[1], SubjectType>, field?: string } |\n { I: T[0], this: Exclude<T[1], SubjectType>, field?: string }\n : Else;\n\ninterface ExtraProps {\n not?: boolean\n passThrough?: boolean\n}\n\ninterface CanExtraProps<T extends AnyAbility> extends ExtraProps {\n ability: T\n}\n\ninterface BoundCanExtraProps<T extends AnyAbility> extends ExtraProps {\n ability?: T\n}\n\nexport type CanProps<T extends AnyAbility> =\n AbilityCanProps<Generics<T>['abilities']> & CanExtraProps<T>;\nexport type BoundCanProps<T extends AnyAbility> =\n AbilityCanProps<Generics<T>['abilities']> & BoundCanExtraProps<T>;\n\nexport class Can<\n T extends AnyAbility,\n IsBound extends boolean = false\n> extends PureComponent<IsBound extends true ? BoundCanProps<T> : CanProps<T>> {\n private _isAllowed: boolean = false;\n private _ability: T | null = null;\n private _unsubscribeFromAbility: Unsubscribe = noop;\n\n componentWillUnmount() {\n this._unsubscribeFromAbility();\n }\n\n private _connectToAbility(ability?: T) {\n if (ability === this._ability) {\n return;\n }\n\n this._unsubscribeFromAbility();\n this._ability = null;\n\n if (ability) {\n this._ability = ability;\n this._unsubscribeFromAbility = ability.on('updated', () => this.forceUpdate());\n }\n }\n\n get allowed() {\n return this._isAllowed;\n }\n\n private _canRender(): boolean {\n const props: any = this.props;\n const subject = props.of || props.a || props.an || props.this || props.on;\n const can = props.not ? 'cannot' : 'can';\n\n return props.ability[can](props.I || props.do, subject, props.field);\n }\n\n render() {\n this._connectToAbility(this.props.ability);\n this._isAllowed = this._canRender();\n return this.props.passThrough || this._isAllowed ? this._renderChildren() : null;\n }\n\n private _renderChildren() {\n const { children, ability } = this.props;\n const elements = typeof children === 'function'\n ? children(this._isAllowed, ability)\n : children;\n\n return renderChildren(elements);\n }\n}\n","import { createElement as h, ComponentClass, Consumer, StatelessComponent } from 'react';\nimport { AnyAbility } from '@casl/ability';\nimport { Can, BoundCanProps } from './Can';\n\ninterface BoundCanClass<T extends AnyAbility> extends ComponentClass<BoundCanProps<T>> {\n new (props: BoundCanProps<T>, context?: any): Can<T, true>\n}\n\nexport function createCanBoundTo<T extends AnyAbility>(ability: T): BoundCanClass<T> {\n return class extends Can<T, true> {\n static defaultProps = { ability } as BoundCanClass<T>['defaultProps'];\n };\n}\n\nexport function createContextualCan<T extends AnyAbility>(\n Getter: Consumer<T>\n): StatelessComponent<BoundCanProps<T>> {\n return (props: BoundCanProps<T>) => h(Getter, null, (ability: T) => h(Can, {\n ability,\n ...props,\n } as any));\n}\n","import React from 'react';\nimport { AnyAbility } from '@casl/ability';\n\nexport function useAbility<T extends AnyAbility>(context: React.Context<T>): T {\n if (process.env.NODE_ENV !== 'production' && typeof React.useContext !== 'function') {\n throw new Error('You must use React >= 16.8 in order to use useAbility()');\n }\n\n const ability = React.useContext<T>(context);\n const [rules, setRules] = React.useState<T['rules']>();\n\n React.useEffect(() => ability.on('updated', (event) => {\n if (event.rules !== rules) {\n setRules(event.rules);\n }\n }), []);\n\n return ability;\n}\n"],"names":["noop","renderChildren","Fragment","children","length","createElement","Children","only","Can","_isAllowed","_ability","_unsubscribeFromAbility","componentWillUnmount","_connectToAbility","ability","this","on","_this2","forceUpdate","_canRender","props","subject","of","a","an","can","not","I","do","field","render","passThrough","_renderChildren","elements","PureComponent","createCanBoundTo","defaultProps","createContextualCan","Getter","h","useAbility","context","process","env","NODE_ENV","React","useContext","Error","useState","rules","setRules","useEffect","event"],"mappings":"65BAWA,IAAMA,EAAO,SAAPA,MACN,IAAMC,EAAiBC,WACnB,SAACC,OACIA,SACI,YAGFA,EAASC,OAAS,EACrBC,8BAAcH,WAAU,aAASC,IACjCG,WAASC,KAAKJ,IAElBG,WAASC,SA8BAC,2JAIHC,EAAsB,QACtBC,EAAqB,OACrBC,EAAuCX,+BAE/CY,qBAAA,kBACOD,OAGCE,EAAR,2BAA0BC,iBACpBA,IAAYC,KAAKL,cAIhBC,SACAD,EAAW,QAEZI,EAAS,MACNJ,EAAWI,OACXH,EAA0BG,EAAQE,GAAG,WAAW,kBAAMC,EAAKC,qBAQ5DC,EAAR,iBACQC,EAAaL,KAAKK,UAClBC,EAAUD,EAAME,IAAMF,EAAMG,GAAKH,EAAMI,IAAMJ,EAAML,MAAQK,EAAMJ,OACjES,EAAML,EAAMM,IAAM,SAAW,aAE5BN,EAAMN,QAAQW,GAAKL,EAAMO,GAAKP,EAAMQ,GAAIP,EAASD,EAAMS,UAGhEC,OAAA,kBACOjB,EAAkBE,KAAKK,MAAMN,cAC7BL,EAAaM,KAAKI,WAChBJ,KAAKK,MAAMW,aAAehB,KAAKN,EAAaM,KAAKiB,IAAoB,QAGtEA,EAAR,mBACgCjB,KAAKK,MAA3BjB,IAAAA,SAAUW,IAAAA,YACZmB,EAA+B,oBAAb9B,EACpBA,EAASY,KAAKN,EAAYK,GAC1BX,SAEGF,EAAegC,gDAvBflB,KAAKN,gBAxBNyB,iBC/CH,SAASC,EAAuCrB,qGAChCN,KACZ4B,aAAe,CAAEtB,QAAAA,KAIrB,SAASuB,EACdC,UAEO,SAAClB,UAA4BmB,gBAAED,EAAQ,MAAM,SAACxB,UAAeyB,gBAAE/B,KACpEM,QAAAA,GACGM,QChBA,SAASoB,WAAiCC,MAClB,eAAzBC,QAAQC,IAAIC,UAAyD,oBAArBC,aAAMC,iBAClD,IAAIC,MAAM,+DAGZjC,EAAU+B,aAAMC,WAAcL,SACVI,aAAMG,WAAzBC,OAAOC,OAEdL,aAAMM,WAAU,kBAAMrC,EAAQE,GAAG,WAAW,SAACoC,MACvCA,EAAMH,QAAUA,EAClBC,EAASE,EAAMH,YAEf,WAEGnC"}
1
+ {"version":3,"file":"index.js","sources":["../../src/Can.ts","../../src/factory.ts","../../src/hooks/useAbility.ts"],"sourcesContent":["import { PureComponent, ReactNode } from 'react';\nimport {\n Unsubscribe,\n AbilityTuple,\n SubjectType,\n AnyAbility,\n Generics,\n Abilities,\n IfString,\n} from '@casl/ability';\n\nconst noop = () => {};\n\ntype AbilityCanProps<\n T extends Abilities,\n Else = IfString<T, { do: T } | { I: T }>\n> = T extends AbilityTuple\n ? { do: T[0], on: T[1], field?: string } |\n { I: T[0], a: Extract<T[1], SubjectType>, field?: string } |\n { I: T[0], an: Extract<T[1], SubjectType>, field?: string } |\n { I: T[0], this: Exclude<T[1], SubjectType>, field?: string }\n : Else;\n\ninterface ExtraProps {\n not?: boolean\n passThrough?: boolean\n}\n\ninterface CanExtraProps<T extends AnyAbility> extends ExtraProps {\n ability: T\n children: ReactNode | ((isAllowed: boolean, ability: T) => ReactNode)\n}\n\ninterface BoundCanExtraProps<T extends AnyAbility> extends ExtraProps {\n ability?: T\n children: ReactNode | ((isAllowed: boolean, ability: T) => ReactNode)\n}\n\nexport type CanProps<T extends AnyAbility> =\n AbilityCanProps<Generics<T>['abilities']> & CanExtraProps<T>;\nexport type BoundCanProps<T extends AnyAbility> =\n AbilityCanProps<Generics<T>['abilities']> & BoundCanExtraProps<T>;\n\nexport class Can<\n T extends AnyAbility,\n IsBound extends boolean = false\n> extends PureComponent<IsBound extends true ? BoundCanProps<T> : CanProps<T>> {\n private _isAllowed: boolean = false;\n private _ability: T | null = null;\n private _unsubscribeFromAbility: Unsubscribe = noop;\n\n componentWillUnmount() {\n this._unsubscribeFromAbility();\n }\n\n private _connectToAbility(ability?: T) {\n if (ability === this._ability) {\n return;\n }\n\n this._unsubscribeFromAbility();\n this._ability = null;\n\n if (ability) {\n this._ability = ability;\n this._unsubscribeFromAbility = ability.on('updated', () => this.forceUpdate());\n }\n }\n\n get allowed() {\n return this._isAllowed;\n }\n\n private _canRender(): boolean {\n const props: any = this.props;\n const subject = props.of || props.a || props.an || props.this || props.on;\n const can = props.not ? 'cannot' : 'can';\n\n return props.ability[can](props.I || props.do, subject, props.field);\n }\n\n render() {\n this._connectToAbility(this.props.ability);\n this._isAllowed = this._canRender();\n return this.props.passThrough || this._isAllowed ? this._renderChildren() : null;\n }\n\n private _renderChildren() {\n const { children, ability } = this.props;\n const elements = typeof children === 'function'\n ? children(this._isAllowed, ability as any)\n : children;\n\n return elements as ReactNode;\n }\n}\n","import { createElement as h, ComponentClass, Consumer, FunctionComponent } from 'react';\nimport { AnyAbility } from '@casl/ability';\nimport { Can, BoundCanProps } from './Can';\n\ninterface BoundCanClass<T extends AnyAbility> extends ComponentClass<BoundCanProps<T>> {\n new (props: BoundCanProps<T>, context?: any): Can<T, true>\n}\n\nexport function createCanBoundTo<T extends AnyAbility>(ability: T): BoundCanClass<T> {\n return class extends Can<T, true> {\n static defaultProps = { ability } as BoundCanClass<T>['defaultProps'];\n };\n}\n\nexport function createContextualCan<T extends AnyAbility>(\n Getter: Consumer<T>\n): FunctionComponent<BoundCanProps<T>> {\n return (props: BoundCanProps<T>) => h(Getter, {\n children: (ability: T) => h(Can, {\n ability,\n ...props,\n } as any)\n });\n}\n","import React from 'react';\nimport { AnyAbility } from '@casl/ability';\n\nexport function useAbility<T extends AnyAbility>(context: React.Context<T>): T {\n if (process.env.NODE_ENV !== 'production' && typeof React.useContext !== 'function') {\n /* istanbul ignore next */\n throw new Error('You must use React >= 16.8 in order to use useAbility()');\n }\n\n const ability = React.useContext<T>(context);\n const [rules, setRules] = React.useState<T['rules']>();\n\n React.useEffect(() => ability.on('updated', (event) => {\n if (event.rules !== rules) {\n setRules(event.rules);\n }\n }), []);\n\n return ability;\n}\n"],"names":["noop","Can","_isAllowed","_ability","_unsubscribeFromAbility","componentWillUnmount","_connectToAbility","ability","this","on","_this2","forceUpdate","_canRender","props","subject","of","a","an","can","not","I","do","field","render","passThrough","_renderChildren","children","elements","PureComponent","createCanBoundTo","defaultProps","createContextualCan","Getter","h","useAbility","context","process","env","NODE_ENV","React","useContext","Error","useState","rules","setRules","useEffect","event"],"mappings":"q/BAWA,IAAMA,EAAO,SAAPA,UAgCOC,2JAIHC,EAAsB,QACtBC,EAAqB,OACrBC,EAAuCJ,+BAE/CK,qBAAA,kBACOD,OAGCE,EAAR,2BAA0BC,iBACpBA,IAAYC,KAAKL,cAIhBC,SACAD,EAAW,QAEZI,EAAS,MACNJ,EAAWI,OACXH,EAA0BG,EAAQE,GAAG,WAAW,kBAAMC,EAAKC,qBAQ5DC,EAAR,iBACQC,EAAaL,KAAKK,UAClBC,EAAUD,EAAME,IAAMF,EAAMG,GAAKH,EAAMI,IAAMJ,EAAML,MAAQK,EAAMJ,OACjES,EAAML,EAAMM,IAAM,SAAW,aAE5BN,EAAMN,QAAQW,GAAKL,EAAMO,GAAKP,EAAMQ,GAAIP,EAASD,EAAMS,UAGhEC,OAAA,kBACOjB,EAAkBE,KAAKK,MAAMN,cAC7BL,EAAaM,KAAKI,WAChBJ,KAAKK,MAAMW,aAAehB,KAAKN,EAAaM,KAAKiB,IAAoB,QAGtEA,EAAR,mBACgCjB,KAAKK,MAA3Ba,IAAAA,SAAUnB,IAAAA,YACZoB,EAA+B,oBAAbD,EACpBA,EAASlB,KAAKN,EAAYK,GAC1BmB,SAEGC,2BAxBT,oBACSnB,KAAKN,gBAxBN0B,iBCtCH,SAASC,EAAuCtB,qGAChCN,KACZ6B,aAAe,CAAEvB,QAAAA,KAIrB,SAASwB,EACdC,UAEO,SAACnB,UAA4BoB,gBAAED,EAAQ,CAC5CN,SAAU,WAACnB,UAAe0B,gBAAEhC,KAC1BM,QAAAA,GACGM,QCjBF,SAASqB,WAAiCC,MAClB,eAAzBC,QAAQC,IAAIC,UAAyD,oBAArBC,aAAMC,iBAElD,IAAIC,MAAM,+DAGZlC,EAAUgC,aAAMC,WAAcL,SACVI,aAAMG,WAAzBC,OAAOC,OAEdL,aAAMM,WAAU,kBAAMtC,EAAQE,GAAG,WAAW,SAACqC,MACvCA,EAAMH,QAAUA,EAClBC,EAASE,EAAMH,YAEf,WAEGpC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@casl/react",
3
- "version": "2.2.1",
3
+ "version": "3.0.0",
4
4
  "description": "React component for CASL which makes it easy to add permissions in any React application",
5
5
  "main": "dist/umd/index.js",
6
6
  "module": "dist/es5m/index.js",
@@ -14,7 +14,8 @@
14
14
  },
15
15
  "repository": {
16
16
  "type": "git",
17
- "url": "https://github.com/stalniy/casl.git"
17
+ "url": "https://github.com/stalniy/casl.git",
18
+ "directory": "packages/casl-react"
18
19
  },
19
20
  "homepage": "https://casl.js.org",
20
21
  "publishConfig": {
@@ -22,61 +23,36 @@
22
23
  },
23
24
  "scripts": {
24
25
  "prebuild": "rm -rf dist/* && npm run build.types",
25
- "build": "BUILD_TYPES=es5m,es6m,umd rollup -c ../../rollup.config.js -n casl.react -g react:React,prop-types:React.PropTypes,@casl/ability:casl",
26
- "build.types": "tsc -p tsconfig.build.json",
27
- "lint": "eslint --ext .ts,.js src/ spec/",
28
- "test": "NODE_ENV=test jest --config ../../tools/jest.config.js",
26
+ "build": "BUILD_TYPES=es5m,es6m,umd dx rollup -n casl.react -g react:React,prop-types:React.PropTypes,@casl/ability:casl",
27
+ "build.types": "dx tsc",
28
+ "lint": "dx eslint src/ spec/",
29
+ "test": "dx jest --env jsdom --config ../dx/config/jest.chai.config.js",
29
30
  "prerelease": "npm run lint && npm test && NODE_ENV=production npm run build",
30
- "release": "semantic-release -e ../../tools/semantic-release"
31
+ "release": "dx semantic-release"
31
32
  },
32
33
  "keywords": [
33
34
  "casl",
34
35
  "react",
35
- "access control",
36
36
  "authorization",
37
37
  "acl",
38
- "cancan",
39
- "security",
40
38
  "permissions"
41
39
  ],
42
40
  "author": "Sergii Stotskyi <sergiy.stotskiy@gmail.com>",
43
41
  "license": "MIT",
44
42
  "peerDependencies": {
45
43
  "@casl/ability": "^3.0.0 || ^4.0.0 || ^5.1.0",
46
- "react": "^15.0.0 || ^16.0.0"
44
+ "react": "^16.0.0 || ^17.0.0 || ^18.0.0"
47
45
  },
48
46
  "devDependencies": {
49
- "@babel/core": "^7.8.4",
50
- "@babel/plugin-proposal-class-properties": "^7.8.3",
51
- "@babel/plugin-proposal-object-rest-spread": "^7.10.0",
52
- "@babel/plugin-transform-typescript": "^7.10.0",
53
- "@babel/preset-env": "^7.8.4",
54
47
  "@casl/ability": "^5.1.0",
55
- "@rollup/plugin-babel": "^5.0.2",
56
- "@rollup/plugin-node-resolve": "^9.0.0",
57
- "@semantic-release/changelog": "^5.0.1",
58
- "@semantic-release/git": "^9.0.0",
59
- "@semantic-release/github": "^7.0.7",
60
- "@semantic-release/npm": "^7.0.5",
61
- "@testing-library/react-hooks": "^3.2.1",
48
+ "@casl/dx": "workspace:^1.0.0",
49
+ "@testing-library/react-hooks": "^4.0.1",
62
50
  "@types/node": "^14.0.5",
63
- "@types/react": "^16.9.19",
64
- "@typescript-eslint/eslint-plugin": "4.11.0",
51
+ "@types/react": "^18.0.0",
65
52
  "chai": "^4.1.0",
66
53
  "chai-spies": "^1.0.0",
67
- "eslint": "^7.1.0",
68
- "eslint-config-airbnb-base": "^14.1.0",
69
- "eslint-config-airbnb-typescript": "^12.0.0",
70
- "eslint-plugin-import": "^2.20.2",
71
- "jest": "^26.0.0",
72
- "react": "^16.3.0",
73
- "react-test-renderer": "^16.3.0",
74
- "rollup": "^2.10.9",
75
- "rollup-plugin-sourcemaps": "^0.6.2",
76
- "rollup-plugin-terser": "^7.0.0",
77
- "semantic-release": "17.1.1",
78
- "ts-jest": "^26.4.4",
79
- "typescript": "~4.0.3"
54
+ "react": "^18.0.0",
55
+ "react-test-renderer": "^18.0.0"
80
56
  },
81
57
  "files": [
82
58
  "dist",
package/CHANGELOG.md DELETED
@@ -1,266 +0,0 @@
1
- # Change Log
2
-
3
- All notable changes to this project will be documented in this file.
4
-
5
- ## [2.2.1](https://github.com/stalniy/casl/compare/@casl/react@2.2.0...@casl/react@2.2.1) (2020-12-28)
6
-
7
-
8
- ### Bug Fixes
9
-
10
- * **dist:** adds separate `tsconfig.build.json` to every completementary project ([87742ce](https://github.com/stalniy/casl/commit/87742cec518a8a68d5fc29be2bbc9561cbc7da6c)), closes [#419](https://github.com/stalniy/casl/issues/419)
11
-
12
- # [2.2.0](https://github.com/stalniy/casl/compare/@casl/react@2.1.1...@casl/react@2.2.0) (2020-12-26)
13
-
14
-
15
- ### Bug Fixes
16
-
17
- * **angular:** fixes sourcemap generation for the code built by ngc ([7715263](https://github.com/stalniy/casl/commit/771526379ff8203170a433d71b68644a48ff44eb)), closes [#387](https://github.com/stalniy/casl/issues/387) [#382](https://github.com/stalniy/casl/issues/382)
18
- * **package:** removes `engine` section that points to npm@6 ([eecd12a](https://github.com/stalniy/casl/commit/eecd12ac49f56d6a0f57d1a57fb37487335b5f03)), closes [#417](https://github.com/stalniy/casl/issues/417)
19
-
20
-
21
- ### Features
22
-
23
- * **builder:** improves typings for AbilityBuilder [skip release] ([ebd4d17](https://github.com/stalniy/casl/commit/ebd4d17a355a2646467033118a3d6efee4321d27)), closes [#379](https://github.com/stalniy/casl/issues/379)
24
- * **esm:** adds ESM support for latest Node.js through `exports` prop in package.json ([cac2506](https://github.com/stalniy/casl/commit/cac2506a80c18f194210c2d89108d1d094751fa4)), closes [#331](https://github.com/stalniy/casl/issues/331)
25
-
26
-
27
- ### BREAKING CHANGES
28
-
29
- * **builder:** changes main generic parameter to be a class instead of instance and makes `defineAbility` to accept options as the 2nd argument.
30
-
31
- **Before**
32
-
33
- ```ts
34
- import { AbilityBuilder, defineAbility, Ability } from '@casl/ability';
35
-
36
- const resolveAction = (action: string) => {/* custom implementation */ };
37
- const ability = defineAbility({ resolveAction }, (can) => can('read', 'Item'));
38
- const builder = new AbilityBuilder<Ability>(Ability);
39
- ```
40
-
41
- **After**
42
-
43
- ```ts
44
- import { AbilityBuilder, defineAbility, Ability } from '@casl/ability';
45
-
46
- const resolveAction = (action: string) => {/* custom implementation */ };
47
- const ability = defineAbility((can) => can('read', 'Item'), { resolveAction });
48
- const builder = new AbilityBuilder(Ability); // first argument is now mandatory!
49
- ```
50
-
51
- The 1st parameter to `AbilityBuilder` is now madatory. This allows to infer generic parameters from it and makes AbilityType that is built to be explicit.
52
-
53
- ## [2.1.1](https://github.com/stalniy/casl/compare/@casl/react@2.1.0...@casl/react@2.1.1) (2020-06-09)
54
-
55
-
56
- ### Bug Fixes
57
-
58
- * **docs:** ensure README and docs for all packages are in sync ([8df3684](https://github.com/stalniy/casl/commit/8df3684b139de0af60c9c37f284a5028ffbf2224)), closes [#338](https://github.com/stalniy/casl/issues/338)
59
-
60
- # [2.1.0](https://github.com/stalniy/casl/compare/@casl/react@2.0.2...@casl/react@2.1.0) (2020-04-18)
61
-
62
-
63
- ### Features
64
-
65
- * **react:** adds `useAbility` hook ([#292](https://github.com/stalniy/casl/issues/292)) ([cd81a22](https://github.com/stalniy/casl/commit/cd81a224b4d44b036ecf5e53601e34d7a51e7be3)), closes [#291](https://github.com/stalniy/casl/issues/291)
66
-
67
- ## [2.0.2](https://github.com/stalniy/casl/compare/@casl/react@2.0.1...@casl/react@2.0.2) (2020-04-10)
68
-
69
-
70
- ### Bug Fixes
71
-
72
- * **react:** ensure that terser doesn't mangle reserved required props ([08eb4f4](https://github.com/stalniy/casl/commit/08eb4f4f730f1b453b3a026d5e0e0ca4ff24be10))
73
-
74
- ## [2.0.1](https://github.com/stalniy/casl/compare/@casl/react@2.0.0...@casl/react@2.0.1) (2020-04-09)
75
-
76
-
77
- ### Bug Fixes
78
-
79
- * **react:** adds support for casl/ability@4 in package.json ([4367b43](https://github.com/stalniy/casl/commit/4367b430fcfd2076292607581df4aa0d247e011b))
80
-
81
- # [2.0.0](https://github.com/stalniy/casl/compare/@casl/react@1.0.4...@casl/react@2.0.0) (2020-04-09)
82
-
83
-
84
- ### Bug Fixes
85
-
86
- * **react:** makes sure `Can` infers types for props from provided `Ability` ([5813b25](https://github.com/stalniy/casl/commit/5813b25d286af6ff76bec6c266fe21af817fe45b)), closes [#248](https://github.com/stalniy/casl/issues/248)
87
-
88
-
89
- ### Features
90
-
91
- * **react:** adds generics for Ability and related components [skip ci] ([3102b6e](https://github.com/stalniy/casl/commit/3102b6e639213553570cf97661b7b7f4c3640687)), closes [#256](https://github.com/stalniy/casl/issues/256)
92
- * **react:** adds support for action only components ([a2db577](https://github.com/stalniy/casl/commit/a2db577910763ecc0b5eca3196bdc22fc8ff3dba)), closes [#107](https://github.com/stalniy/casl/issues/107)
93
- * **vue:** adds better generics typying for Vue ([5cc7b60](https://github.com/stalniy/casl/commit/5cc7b60d8a2a53db217f8ad1a4673a28f67aefce)), closes [#107](https://github.com/stalniy/casl/issues/107)
94
-
95
-
96
- ### BREAKING CHANGES
97
-
98
- * **react:** support for react v15 was loosen. If you use react v15 you will need to add `@casl/react/contextApiPatch.d.ts` file into your typings
99
- * **typescript:** weak hand written declaration files are removed as `@casl/react` has been completely rewritten to TypeScript. TypeScript now checks that you correctly use property aliases
100
- * **Can:** `of` alias is removed and field was extracted to a separate prop
101
-
102
- **Before**
103
-
104
- ```jsx
105
- export default <Can I="read title" of="Post">...</Can>
106
- ```
107
-
108
- **After**
109
-
110
- ```jsx
111
- export default <Can I="read" a="Post" field="title">...</Can>
112
- ```
113
-
114
-
115
- # [@casl/react-v1.0.4](https://github.com/stalniy/casl/compare/@casl/react@1.0.3...@casl/react@1.0.4) (2019-07-28)
116
-
117
-
118
- ### Performance Improvements
119
-
120
- * **react:** adds check if children array has length of 1 then render it without React.Fragment ([655d08f](https://github.com/stalniy/casl/commit/655d08f)), closes [#211](https://github.com/stalniy/casl/issues/211)
121
-
122
- # [@casl/react-v1.0.3](https://github.com/stalniy/casl/compare/@casl/react@1.0.2...@casl/react@1.0.3) (2019-03-25)
123
-
124
-
125
- ### Bug Fixes
126
-
127
- * **react:** handles `an` prop as subject name ([608c99b](https://github.com/stalniy/casl/commit/608c99b)), closes [#169](https://github.com/stalniy/casl/issues/169)
128
-
129
- # [@casl/react-v1.0.2](https://github.com/stalniy/casl/compare/@casl/react@1.0.1...@casl/react@1.0.2) (2019-02-16)
130
-
131
-
132
- ### Performance Improvements
133
-
134
- * **react:** checks whether ability changed before unsubsribing ([d865fa8](https://github.com/stalniy/casl/commit/d865fa8)), closes [#159](https://github.com/stalniy/casl/issues/159)
135
-
136
- # [@casl/react-v1.0.1](https://github.com/stalniy/casl/compare/@casl/react@1.0.0...@casl/react@1.0.1) (2019-02-10)
137
-
138
-
139
- ### Bug Fixes
140
-
141
- * **packages:** increases peerDependency of [@casl](https://github.com/casl)/ability ([9f6a7b8](https://github.com/stalniy/casl/commit/9f6a7b8)), closes [#119](https://github.com/stalniy/casl/issues/119)
142
-
143
- # [@casl/react-v1.0.0](https://github.com/stalniy/casl/compare/@casl/react@0.8.1...@casl/react@1.0.0) (2019-02-03)
144
-
145
- ### Breaking Changes
146
-
147
- * **react:** removes internal state in <Can> component ([#122](https://github.com/stalniy/casl/issues/122)) ([df9dd51](https://github.com/stalniy/casl/commit/df9dd51)), closes [#122](https://github.com/stalniy/casl/issues/122)
148
-
149
- # [@casl/react-v0.8.1](https://github.com/stalniy/casl/compare/@casl/react@0.8.0...@casl/react@0.8.1) (2018-11-08)
150
-
151
-
152
- ### Bug Fixes
153
-
154
- * **react:** moves this.connectToAbility to the setState callback ([#129](https://github.com/stalniy/casl/issues/129)) ([9d0c839](https://github.com/stalniy/casl/commit/9d0c839)), closes [#128](https://github.com/stalniy/casl/issues/128)
155
-
156
- # [@casl/react-v0.8.0](https://github.com/stalniy/casl/compare/@casl/react@0.7.2...@casl/react@0.8.0) (2018-09-03)
157
-
158
-
159
- ### Bug Fixes
160
-
161
- * **README:** changes links to [@casl](https://github.com/casl)/ability to point to npm package instead to git root [skip ci] ([a74086b](https://github.com/stalniy/casl/commit/a74086b)), closes [#102](https://github.com/stalniy/casl/issues/102)
162
-
163
-
164
- ### Features
165
-
166
- * **react:can:** adds `an` alias to `on` prop ([748ea64](https://github.com/stalniy/casl/commit/748ea64))
167
- * **react:can:** adds `passThrough` option ([045318c](https://github.com/stalniy/casl/commit/045318c)), closes [#105](https://github.com/stalniy/casl/issues/105)
168
- * **react:can:** adds support for multiple <Can> children ([c022b32](https://github.com/stalniy/casl/commit/c022b32))
169
- * **react:can:** updates typescript declarations ([70953ed](https://github.com/stalniy/casl/commit/70953ed))
170
- * **react:can:** updates typescript declarations ([213dcde](https://github.com/stalniy/casl/commit/213dcde))
171
-
172
-
173
- ### Performance Improvements
174
-
175
- * **react:can:** moves prop type checks undef `if`, so they can be removed for production builds ([4bebf0b](https://github.com/stalniy/casl/commit/4bebf0b))
176
-
177
- # [@casl/react-v0.7.2](https://github.com/stalniy/casl/compare/@casl/react@0.7.1...@casl/react@0.7.2) (2018-07-29)
178
-
179
-
180
- ### Bug Fixes
181
-
182
- * **react:** makes `not` prop to be optional ([8f841bf](https://github.com/stalniy/casl/commit/8f841bf)), closes [#95](https://github.com/stalniy/casl/issues/95)
183
-
184
- <a name="@casl/react-v0.7.1"></a>
185
- # [@casl/react-v0.7.1](https://github.com/stalniy/casl/compare/@casl/react@0.7.0...@casl/react@0.7.1) (2018-07-02)
186
-
187
-
188
- ### Bug Fixes
189
-
190
- * **package:** changes location of ES5M modules ([2b1ad4e](https://github.com/stalniy/casl/commit/2b1ad4e)), closes [#89](https://github.com/stalniy/casl/issues/89)
191
-
192
- <a name="@casl/react-v0.7.0"></a>
193
- # [@casl/react-v0.7.0](https://github.com/stalniy/casl/compare/@casl/react@0.6.0...@casl/react@0.7.0) (2018-06-15)
194
-
195
-
196
- ### Bug Fixes
197
-
198
- * **react:** adds ts defs for `not` attribute ([17f76a9](https://github.com/stalniy/casl/commit/17f76a9))
199
- * **react:** contextual can don't respect 'not' prop ([#74](https://github.com/stalniy/casl/issues/74)) ([d727230](https://github.com/stalniy/casl/commit/d727230))
200
-
201
-
202
- ### Features
203
-
204
- * **vue:** adds can component ([42ee540](https://github.com/stalniy/casl/commit/42ee540)), closes [#63](https://github.com/stalniy/casl/issues/63)
205
-
206
-
207
- <a name="@casl/react-v0.6.0"></a>
208
- # [@casl/react-v0.6.0](https://github.com/stalniy/casl/compare/@casl/react@0.5.0...@casl/react@0.6.0) (2018-05-28)
209
-
210
-
211
- ### Bug Fixes
212
-
213
- * **react:** adds `I`, `a`, `of`, `this` aliases in types ([a412868](https://github.com/stalniy/casl/commit/a412868)), closes [#65](https://github.com/stalniy/casl/issues/65)
214
-
215
-
216
- ### Features
217
-
218
- * **react:** adds `not` attribute ([94ef6d4](https://github.com/stalniy/casl/commit/94ef6d4)), by @emilbruckner in [#66](https://github.com/stalniy/casl/issues/66)
219
-
220
-
221
- <a name="0.5.0"></a>
222
- # 0.5.0 (2018-05-14)
223
-
224
- ### Features
225
-
226
- * **react:** support for more readable component ([1a8c1ec](https://github.com/stalniy/casl/commit/1a8c1ec))
227
-
228
-
229
-
230
- <a name="0.4.0"></a>
231
- # 0.4.0 (2018-04-23)
232
-
233
-
234
- ### Bug Fixes
235
-
236
- * **react:** rechecks abilities in `Can` on props update ([e08db1e](https://github.com/stalniy/casl/commit/e08db1e)), closes [#55](https://github.com/stalniy/casl/issues/55)
237
-
238
-
239
- <a name="0.3.0"></a>
240
- # 0.3.0 (2018-04-20)
241
-
242
-
243
- ### Bug Fixes
244
-
245
- * **react:** fixes support for React 15.x ([8e17738](https://github.com/stalniy/casl/commit/8e17738))
246
- * **react:** fixes typings for react 15.x & 16.x ([f147a8a](https://github.com/stalniy/casl/commit/f147a8a)), closes [#38](https://github.com/stalniy/casl/issues/38)
247
-
248
-
249
-
250
- <a name="0.2.0"></a>
251
- # 0.2.0 (2018-04-20)
252
-
253
-
254
- ### Features
255
-
256
- * **react:** adds typescript definition for package [@casl](https://github.com/casl)/react ([37718fe](https://github.com/stalniy/casl/commit/37718fe)), closes [#54](https://github.com/stalniy/casl/issues/54)
257
-
258
-
259
- <a name="0.1.0"></a>
260
- # 0.1.0 (2018-03-23)
261
-
262
- ### Features
263
-
264
- * **component:** adds `Can` component, relates to [#23](https://github.com/stalniy/casl/issues/23)
265
- * **integration:** adds `createContextualCan` function which allows to work with new React Context API, relates to [#23](https://github.com/stalniy/casl/issues/23)
266
- * **integration:** adds `createCanBoundTo` function which allows to bind ability to component, relates to [#23](https://github.com/stalniy/casl/issues/23)