Skip to main content
rule official · GitHub

pcf-events — GitHub Copilot rule: custom events in PCF components

A rule that tells Copilot how to declare and handle custom events in Power Apps Component Framework components.

Install into GitHub Copilot · Manually: put the files in place All methods and platforms ↓
.github/instructions/pcf-events.instructions.md
Open on GitHub

What pcf-events does

The rule covers the PCF event mechanism: a component declares events in the manifest with the event element, raises them in code, and the hosting app reacts. In Canvas Apps you wire the handler with Power Fx expressions in the properties pane. In Model-Driven Apps you attach handlers through addEventHandler on formContext.getControl(...).

Model-Driven Apps can pass a payload with the event, including a callback function so the script handler can call back into the component. Events fire separately for each instance of the component on a form — worth checking if your logic assumes a single control.

The rule applies to **/*.{ts,tsx,js,json,xml,pcfproj,csproj}. The topic is marked pre-release, so the documentation may change and behaviour is worth verifying against the current version.

What it helps with

  • declare a custom event in a PCF component manifest
  • subscribe to a control event in Model-Driven Apps with addEventHandler
  • handle a component event in Canvas Apps through Power Fx
  • pass a payload and callback from the component to the script handler
  • keep handlers straight when several instances of the control sit on one form

Example prompts for the agent

  • add a customEvent1 event to the component manifest and raise it in code
  • show me how to subscribe to customEvent1 with addEventHandler in Model-Driven Apps
  • how do I pass a callback function in the payload of a PCF component event

How to install pcf-events

Manually: put the files in place

.github/instructions/pcf-events.instructions.md

The applyTo field sets which files it applies to

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about pcf-events

How do I install pcf-events in GitHub Copilot?

Place the file at .github/instructions/pcf-events.instructions.md in your repository — Copilot picks it up for files matching applyTo.

What does pcf-events do?

It describes declaring and handling custom events in PCF components: the event element in the manifest, subscriptions in Canvas Apps and Model-Driven Apps, and payload passing.

Which files does the rule apply to?

Files matching **/*.{ts,tsx,js,json,xml,pcfproj,csproj}, as set by applyTo in its frontmatter.

Source text

pcf-events.instructions.md · MIT
---
description: 'Define and handle custom events in PCF components'
applyTo: '**/*.{ts,tsx,js,json,xml,pcfproj,csproj}'
---

# Define Events (Preview)

[This topic is pre-release documentation and is subject to change.]

A common requirement when building custom components with the Power Apps Component Framework is the ability to react to events generated within the control. These events can be invoked either due to user interaction or programmatically via code. For example, an application can have a code component that lets a user build a product bundle. This component can also raise an event which could show product information in another area of the application.

## Component Data Flow

The common data flow for a code component is data flowing from the hosting application into the control as inputs and updated data flowing out of the control to the hosting form or page. This diagram shows the standard pattern of data flow for a typical PCF component:

![Shows that data update from the code component to the binding field triggers the OnChange event](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/media/component-events-onchange-example.png)

The data update from the code component to the bound field triggers the `OnChange` event. For most component scenarios, this is enough and makers just add a handler to trigger subsequent actions. However, a more complicated control might require events to be raised that aren't field updates. The event mechanism allows code components to define events that have separate event handlers.

## Using Events

The event mechanism in PCF is based on the standard event model in JavaScript. The component can define events in the manifest file and raise these events in the code. The hosting application can listen to these events and react to them.

### Define Events in Manifest

The component defines events using the [event element](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/manifest-schema-reference/event) in the manifest file. This data allows the respective hosting application to react to events in different ways.

```xml
<property
  name="sampleProperty"
  display-name-key="Property_Display_Key"
  description-key="Property_Desc_Key"
  of-type="SingleLine.Text"
  usage="bound"
  required="true"
/>
<event
  name="customEvent1"
  display-name-key="customEvent1"
  description-key="customEvent1"
/>
<event
  name="customEvent2"
  display-name-key="customEvent2"
  description-key="customEvent2"
/>
```

### Canvas Apps Event Handling

Canvas apps react to the event using Power Fx expressions:

![Shows the custom events in the canvas apps designer](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/media/custom-events-in-canvas-designer.png)

### Model-Driven Apps Event Handling

Model Driven Apps use the [addEventHandler method](https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/controls/addeventhandler) to associate event handlers to custom events for a component.

```javascript
const controlName1 = "cr116_personid";

this.onLoad = function (executionContext) {
  const formContext = executionContext.getFormContext();

  const sampleControl1 = formContext.getControl(controlName1);
  sampleControl1.addEventHandler("customEvent1", this.onSampleControl1CustomEvent1);
  sampleControl1.addEventHandler("customEvent2", this.onSampleControl1CustomEvent2);
}
```

> **Note**: These events occur separately for each instance of the code component in the app.

## Defining an Event for Model-Driven Apps

For model-driven apps you can pass a payload with the event allowing for more complex scenarios. For example in the diagram below the component passes a callback function in the event allowing the script handling to call back to the component.

![In this example, the component passes a callback function in the event allowing the script handling to call back to the component](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/media/passing-payload-in-events.png)

```javascript
this.onSampleControl1CustomEvent1 = function (params) {
   //alert(`SampleControl1 Custom Event 1: ${params}`);
   alert(`SampleControl1 Custom Event 1`);
}.bind(this);

this.onSampleControl2CustomEvent2 = function (params) {
  alert(`SampleControl2 Custom Event 2: ${params.message}`);
  // prevent the default action for the event
  params.callBackFunction();
}
```

## Defining an Event for Canvas Apps

Makers configure an event using Power Fx on the PCF control in the properties pane.

## Calling an Event

See how to call an event in [Events](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/reference/events).

## Next Steps

[Tutorial: Define a custom event in a component](https://learn.microsoft.com/en-us/power-apps/developer/component-framework/tutorial-define-event)

© github · MIT · github/awesome-copilot

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set732

The whole set →
playwright-typescript This rule tells Copilot how to write Playwright tests: role-based locators, auto-retrying assertions, one file per feature. Useful when an agent writes the tests. ★ 40,418repo stars php-symfony This rule steers Copilot toward official Symfony practices: project layout, dependency injection, Doctrine, Twig and testing. Useful when you keep re-explaining conventions in chat. ★ 40,418repo stars pcf-tooling A rule that tells Copilot to use Microsoft Power Platform CLI for building, debugging and deploying Power Apps code components. ★ 40,418repo stars pcf-manifest-schema A rule that feeds Copilot the structure of ControlManifest.Input.xml: which elements and attributes exist and what they do. Useful when editing a PCF component manifest. ★ 40,418repo stars pcf-code-components A rule that teaches Copilot how Power Apps code components are built: manifest, TypeScript implementation, resources. Useful when editing ControlManifest.Input.xml or index.ts. ★ 40,418repo stars oop-design-patterns A GitHub Copilot rule that steers code generation and refactoring toward GoF design patterns and SOLID principles in object-oriented code. ★ 40,418repo stars no-heredoc A rule that stops the agent from writing files through terminal heredocs and redirections, because those commands corrupt content in VS Code. ★ 40,418repo stars nestjs This rule feeds GitHub Copilot a set of NestJS conventions: modules, DI, DTOs and validation. Useful when the agent writes TypeScript server code. ★ 40,418repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor