@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
8 lines (7 loc) • 2.08 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/dashboard-policy/dashboard-policy.tsx"],
"sourcesContent": ["import { createContext, useContext, useMemo } from '@wordpress/element';\nimport type { ReactNode } from 'react';\nimport type { CanPerformDashboardOperation } from '../../types';\n\nconst PolicyContext = createContext< CanPerformDashboardOperation | null >(\n\tnull\n);\n\ninterface WidgetDashboardPolicyProps {\n\t/**\n\t * Answers whether the dashboards below may perform an operation.\n\t */\n\tcanPerform: CanPerformDashboardOperation;\n\n\t/**\n\t * Subtree the policy governs.\n\t */\n\tchildren: ReactNode;\n}\n\n/**\n * Governs the dashboards below it. Mount it around one dashboard, a group,\n * or the whole application. Nested policies compose restrictively: an\n * operation is allowed only when every enclosing policy allows it. Without\n * a policy, every operation is allowed.\n *\n * @param {WidgetDashboardPolicyProps} props Component props.\n */\nexport function WidgetDashboardPolicy( {\n\tcanPerform,\n\tchildren,\n}: WidgetDashboardPolicyProps ) {\n\tconst parent = useContext( PolicyContext );\n\n\tconst value = useMemo< CanPerformDashboardOperation >(\n\t\t() =>\n\t\t\tparent\n\t\t\t\t? ( request ) => parent( request ) && canPerform( request )\n\t\t\t\t: canPerform,\n\t\t[ parent, canPerform ]\n\t);\n\n\treturn (\n\t\t<PolicyContext.Provider value={ value }>\n\t\t\t{ children }\n\t\t</PolicyContext.Provider>\n\t);\n}\n\n/**\n * Reads the policy in effect; `null` when none is mounted.\n */\nexport function useDashboardPolicy(): CanPerformDashboardOperation | null {\n\treturn useContext( PolicyContext );\n}\n"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAAmD;AA2CjD;AAvCF,IAAM,oBAAgB;AAAA,EACrB;AACD;AAsBO,SAAS,sBAAuB;AAAA,EACtC;AAAA,EACA;AACD,GAAgC;AAC/B,QAAM,aAAS,2BAAY,aAAc;AAEzC,QAAM,YAAQ;AAAA,IACb,MACC,SACG,CAAE,YAAa,OAAQ,OAAQ,KAAK,WAAY,OAAQ,IACxD;AAAA,IACJ,CAAE,QAAQ,UAAW;AAAA,EACtB;AAEA,SACC,4CAAC,cAAc,UAAd,EAAuB,OACrB,UACH;AAEF;AAKO,SAAS,qBAA0D;AACzE,aAAO,2BAAY,aAAc;AAClC;",
"names": []
}