Skip to content

Commit

Permalink
feat: PrintController and PrintButtonController to enable buttons and…
Browse files Browse the repository at this point in the history
… actions to trigger the browser print dialogue
  • Loading branch information
Sub-Xaero committed Nov 2, 2021
1 parent 2912eca commit ab95de7
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
48 changes: 48 additions & 0 deletions docs/docs/controllers/print_button_controller.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
id: PrintButtonController
title: PrintButtonController
---

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

## Purpose

Controller that adds print functionality to a link/button, to open a print dialogue when clicked.

| Action | Purpose |
| --- | --- |
| `print` | Trigger the Browser's print dialogue |


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

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

| Class | Purpose | Default |
| --- | --- | --- |
| `unsupported ` | Applied to the controller element when `window | `unsupported` |

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

## Events
<NoEvents/>

## Side Effects

Adds event listeners for `click` on the controller root element.


## How to Use

<iframe
src="https://codesandbox.io/embed/printbuttoncontroller-9y5c7?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"
/>

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

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

## Purpose

Utility controller to trigger the page print dialogue when wired up to an event in the DOM.


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

| Action | Purpose |
| --- | --- |
| `print` | Trigger the Browser's print dialogue |

## [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
<NoEvents/>

## Side Effects

None


## How to Use

<iframe
src="https://codesandbox.io/embed/printcontroller-tpp9c?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"
/>

4 changes: 3 additions & 1 deletion docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = {
"controllers/ElementSaveController",
"controllers/EmptyDomController",
"controllers/PrefetchController",
"controllers/PrintButtonController",
"controllers/ResponsiveIframeController",
"controllers/SelfDestructController",
"controllers/StickyController",
Expand Down Expand Up @@ -101,9 +102,10 @@ module.exports = {
},
{
"Utility Controllers": [
"controllers/IntervalController",
"controllers/IntersectionController",
"controllers/IntervalController",
"controllers/PresenceController",
"controllers/PrintController",
"controllers/TimeoutController",
"controllers/UserFocusController",
],
Expand Down
50 changes: 50 additions & 0 deletions src/controllers/print_button_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {PrintController} from "./utility/print_controller";

export class PrintButtonController extends PrintController {

static classes = [
'unsupported',
];
declare readonly unsupportedClass: string;
declare readonly hasUnsupportedClass: boolean;

get _unsupportedClasses(): string[] {
return this.unsupportedClass.split(' ');
}

get _defaultUnsupportedClasses(): string[] {
return ["unsupported"];
}

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

connect() {
if (!("print" in window)) {
this._addUnsupportedClasses(this.el);
}
this.el.addEventListener('click', this.print);
}

disconnect() {
this.el.removeEventListener('click', this.print);
}

private _addUnsupportedClasses(el: HTMLElement = this.el) {
if (this.hasUnsupportedClass) {
el.classList.add(...this._unsupportedClasses);
} else {
el.classList.add(...this._defaultUnsupportedClasses);
}
}

private _removeUnsupportedClasses(el: HTMLElement = this.el) {
if (this.hasUnsupportedClass) {
el.classList.remove(...this._unsupportedClasses);
} else {
el.classList.remove(...this._defaultUnsupportedClasses);
}
}

}
10 changes: 10 additions & 0 deletions src/controllers/utility/print_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {BaseController} from '../../utilities/base_controller';

export class PrintController extends BaseController {

print(event?: Event) {
event?.preventDefault();
window.print();
}

}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export {PasswordPeekController} from "./controllers/forms/password_peek_controll
export {PollBlockController} from "./controllers/ajax/poll_block_controller";
export {PrefetchController} from "./controllers/prefetch_controller";
export {PresenceController} from "./controllers/utility/presence_controller";
export {PrintButtonController} from './controllers/print_button_controller';
export {PrintController} from './controllers/utility/print_controller';
export {RemoteFormController} from "./controllers/forms/remote_form_controller";
export {RemoveController} from "./controllers/dismissable_controller";
export {ResponsiveIframeBodyController, ResponsiveIframeWrapperController} from "./controllers/responsive_iframe_controller";
Expand Down

0 comments on commit ab95de7

Please sign in to comment.