Skip to content

Commit

Permalink
feat: PresenceController - A utility controller to wire up other cont…
Browse files Browse the repository at this point in the history
…rollers when an element comes into or leaves the DOM (#26)
  • Loading branch information
Sub-Xaero authored May 13, 2021
1 parent ecec9e8 commit d3537d9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/docs/controllers/presence_controller.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
id: PresenceController
title: PresenceController
---

import NoActions from "../_partials/no-actions.md";
import NoTargets from "../_partials/no-targets.md";
import NoClasses from "../_partials/no-classes.md";
import NoValues from "../_partials/no-values.md";

## Purpose

A utility controller to wire up other stimulus actions when an element comes into or leaves the document.


## [Actions](https://stimulus.hotwire.dev/reference/actions)

<NoActions/>

## [Targets](https://stimulus.hotwire.dev/reference/targets)


<NoTargets/>

## [Classes](https://stimulus.hotwire.dev/reference/classes)


<NoClasses/>

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


<NoValues/>

## 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 | - |

## Side Effects

None

## How to Use

<iframe
src="https://codesandbox.io/embed/presencecontroller-k7qgy?fontsize=14&hidenavigation=1&theme=dark"
style={{width: "100%", height: "500px", border: "0", borderRadius: "4px", overflow: "hidden"}}
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
/>


1 change: 1 addition & 0 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ module.exports = {
"controllers/AppearanceController",
"controllers/IntervalController",
"controllers/IntersectionController",
"controllers/PresenceController",
"controllers/TimeoutController",
],
},
Expand Down
13 changes: 13 additions & 0 deletions src/controllers/utility/presence_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {BaseController} from "../../utilities/base_controller";

export class PresenceController extends BaseController {

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

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

}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export {NavigateFormErrorsController} from "./controllers/forms/navigate_form_er
export {NestedFormController} from "./controllers/forms/nested_form_controller";
export {PasswordConfirmController} from "./controllers/forms/password_confirm_controller";
export {PasswordPeekController} from "./controllers/forms/password_peek_controller";
export {PresenceController} from "./controllers/utility/presence_controller";
export {RemoteFormController} from "./controllers/forms/remote_form_controller";
export {ResponsiveIframeBodyController, ResponsiveIframeWrapperController} from "./controllers/responsive_iframe_controller";
export {ScrollIntoFocusController} from "./controllers/scroll/scroll_into_focus_controller";
Expand Down

0 comments on commit d3537d9

Please sign in to comment.