From a069d7bf7f16e13db431dce1e98a96768238b26b Mon Sep 17 00:00:00 2001 From: Dylan Clarke Date: Thu, 21 Oct 2021 16:44:49 +0100 Subject: [PATCH] feat: PresenceController - Added an optional name value which if provided 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. --- docs/docs/controllers/presence_controller.mdx | 9 +++++---- src/controllers/utility/presence_controller.ts | 13 +++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/docs/docs/controllers/presence_controller.mdx b/docs/docs/controllers/presence_controller.mdx index 7ac76132..60296fa8 100644 --- a/docs/docs/controllers/presence_controller.mdx +++ b/docs/docs/controllers/presence_controller.mdx @@ -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) - - +| 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 diff --git a/src/controllers/utility/presence_controller.ts b/src/controllers/utility/presence_controller.ts index 1f1a4371..644408f9 100644 --- a/src/controllers/utility/presence_controller.ts +++ b/src/controllers/utility/presence_controller.ts @@ -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`); } }