{"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\n> = T extends AbilityTuple\n ? { do: T[0], on: T[1], field?: string } |\n { I: T[0], a: Extract, field?: string } |\n { I: T[0], an: Extract, field?: string } |\n { I: T[0], this: Exclude, field?: string }\n : Else;\n\ninterface ExtraProps {\n not?: boolean\n passThrough?: boolean\n}\n\ninterface CanExtraProps extends ExtraProps {\n ability: T\n}\n\ninterface BoundCanExtraProps extends ExtraProps {\n ability?: T\n}\n\nexport type CanProps =\n AbilityCanProps['abilities']> & CanExtraProps;\nexport type BoundCanProps =\n AbilityCanProps['abilities']> & BoundCanExtraProps;\n\nexport class Can<\n T extends AnyAbility,\n IsBound extends boolean = false\n> extends PureComponent : CanProps> {\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 extends ComponentClass> {\n new (props: BoundCanProps, context?: any): Can\n}\n\nexport function createCanBoundTo(ability: T): BoundCanClass {\n return class extends Can {\n static defaultProps = { ability } as BoundCanClass['defaultProps'];\n };\n}\n\nexport function createContextualCan(\n Getter: Consumer\n): StatelessComponent> {\n return (props: BoundCanProps) => 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(context: React.Context): 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(context);\n const [rules, setRules] = React.useState();\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,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