Skip to content

Commit

Permalink
feat: TimeoutController - a utility controller that fires an event af…
Browse files Browse the repository at this point in the history
…ter x seconds to wire up to other controllers
  • Loading branch information
Sub-Xaero committed Apr 24, 2021
1 parent 425d338 commit 9f0e0da
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 9 deletions.
13 changes: 13 additions & 0 deletions dist/controllers/utility/timeout_controller.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { BaseController } from "../../utilities/base_controller";
export declare class TimeoutController extends BaseController {
static values: {
seconds: NumberConstructor;
};
readonly secondsValue: number;
_timeoutHandle: null | number;
initialize(): void;
connect(): void;
disconnect(): void;
_timeout(): void;
}
//# sourceMappingURL=timeout_controller.d.ts.map
1 change: 1 addition & 0 deletions dist/controllers/utility/timeout_controller.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export { StickyController } from "./controllers/sticky_controller";
export { TableSortController } from "./controllers/tables/table_sort_controller";
export { TableTruncateController } from "./controllers/tables/table_truncate_controller";
export { TeleportController } from "./controllers/teleport_controller";
export { TimeoutController } from "./controllers/utility/timeout_controller";
export { ToggleClassController } from "./controllers/toggle_class_controller";
export { TurboFrameRCController } from "./controllers/turbo_frame_rc_controller";
export { TurboFrameRefreshController } from "./controllers/turbo_frame_refresh_controller";
Expand Down
2 changes: 1 addition & 1 deletion dist/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/stimulus-library.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.modern.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.modern.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.module.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/stimulus-library.umd.js.map

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions docs/docs/controllers/timeout_controller.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
id: TimeoutController
title: TimeoutController
---





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

## Purpose

Utility controller to wire up other controller actions that need to fire after an elapsed time.

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

| Value | Type | Description | Default |
| --- | --- | --- | --- |
| `seconds` | Number | The number of seconds until the action fires | - |

## Events

| Event | When | Dispatched on | `event.detail` |
| --- | --- |--- | --- |
|`timeout:action` | Fired after `secondsValue` seconds, used to wire your desired controller actions to | the controller root element | `element`: the controller root element |

## Side Effects

Calls `setTimeout` with the desired timeout. Cleans up on disconnect if that happens before timeout occurs.

# How to Use

An example: Show a marketing popup after 30 seconds.

```html
<div data-controller="popup dismissable" data-action="timeout:action->popup#open">
<meta data-controller="timeout" data-timeout-seconds-value="30" />
<p>Sign up to get great deals!</p>

<a data-action="click->dismissable#dismiss">No thanks</a>
</div>
```
1 change: 1 addition & 0 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ module.exports = {
"Utility Controllers": [
"controllers/AppearanceController",
"controllers/IntersectionController",
"controllers/TimeoutController",
],
},
{
Expand Down
30 changes: 30 additions & 0 deletions src/controllers/utility/timeout_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {BaseController} from "../../utilities/base_controller";

export class TimeoutController extends BaseController {

static values = {seconds: Number};

declare readonly secondsValue: number;
_timeoutHandle: null | number = null;

initialize() {
this._timeout = this._timeout.bind(this);
}

connect() {
requestAnimationFrame(() => {
this._timeoutHandle = window.setTimeout(this._timeout, this.secondsValue * 1000);
});
}

disconnect() {
if (this._timeoutHandle) {
window.clearTimeout(this._timeoutHandle);
}
}

_timeout() {
this.dispatch(this.el, "timeout");
}

}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export {StickyController} from "./controllers/sticky_controller";
export {TableSortController} from "./controllers/tables/table_sort_controller";
export {TableTruncateController} from "./controllers/tables/table_truncate_controller";
export {TeleportController} from "./controllers/teleport_controller";
export {TimeoutController} from "./controllers/utility/timeout_controller";
export {ToggleClassController} from "./controllers/toggle_class_controller";
export {TurboFrameRCController} from "./controllers/turbo_frame_rc_controller";
export {TurboFrameRefreshController} from "./controllers/turbo_frame_refresh_controller";
Expand Down

0 comments on commit 9f0e0da

Please sign in to comment.