Skip to content

Commit

Permalink
feat: PresenceController - Added an optional name value which if prov…
Browse files Browse the repository at this point in the history
…ided will be included in the presence event name so that other controllers can react to specific presence events if there are multiple in a given scope.
  • Loading branch information
Sub-Xaero committed Oct 21, 2021
1 parent 2ea9501 commit a069d7b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
9 changes: 5 additions & 4 deletions docs/docs/controllers/presence_controller.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ A utility controller to wire up other stimulus actions when an element comes int

## [Values](https://stimulus.hotwire.dev/reference/values)


<NoValues/>
| Value | Type | Description | Default |
| --- | --- | --- | --- |
| `name` (Optional) | String | If provided, the `presence` events will be prefixed with the given name i.e. `foo:presence:added`, to allow you to wire up other controllers based on specifically named presence events | - |

## Events

| Event | When | Dispatched on | `event.detail` |
| --- | --- | --- |--- |
|`presence:added` | When the element enters the DOM | the controller root element | - |
|`presence:removed` | When the element leaves the DOM | the controller root element | - |
|`presence:added` / `${nameValue}:presence:added` | When the element enters the DOM | the controller root element | - |
|`presence:removed` / `${nameValue}:presence:removed` | When the element leaves the DOM | the controller root element | - |

## Side Effects

Expand Down
13 changes: 11 additions & 2 deletions src/controllers/utility/presence_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ import {BaseController} from "../../utilities/base_controller";

export class PresenceController extends BaseController {

static values = {name: String};

declare nameValue: string;
declare readonly hasNameValue: boolean;

get name(): string {
return this.hasNameValue ? `${this.nameValue}:` : ``;
}

connect() {
this.dispatch(this.el, "presence:added");
this.dispatch(this.el, `${this.nameValue}presence:added`);
}

disconnect() {
this.dispatch(this.el, "presence:removed");
this.dispatch(this.el, `${this.nameValue}presence:removed`);
}

}

0 comments on commit a069d7b

Please sign in to comment.