Skip to content

Commit

Permalink
refactor(frontend): replace stimulus with web component for textarea
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Nov 4, 2023
1 parent b9bd1e1 commit 1a08876
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 21 deletions.
6 changes: 0 additions & 6 deletions package-lock.json

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

20 changes: 11 additions & 9 deletions packages/frontend/components/textarea/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { Controller } from "@hotwired/stimulus";
import { debounce } from "../../lib/utils/debounce.js";

export const TextareaController = class extends Controller {
initialize() {
export const TextareaComponent = class extends HTMLElement {
constructor() {
super();

this.adjustHeight = this.adjustHeight.bind(this);
this.textarea = this.querySelector("textarea");
}

connect() {
connectedCallback() {
const delay = 100;

this.element.style.overflow = "hidden";
this.textarea.style.overflow = "hidden";
this.onResize =
delay > 0 ? debounce(this.adjustHeight, delay) : this.adjustHeight;
this.adjustHeight();

this.element.addEventListener("input", this.adjustHeight);
this.textarea.addEventListener("input", this.adjustHeight);
window.addEventListener("resize", this.onResize);
}

disconnect() {
disconnectedCallback() {
window.removeEventListener("resize", this.onResize);
}

adjustHeight() {
this.element.style.height = "auto";
this.element.style.height = `${this.element.scrollHeight + 4}px`;
this.textarea.style.height = "auto";
this.textarea.style.height = `${this.textarea.scrollHeight + 4}px`;
}
};
4 changes: 4 additions & 0 deletions packages/frontend/components/textarea/styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
textarea-component {
display: block;
}

.textarea {
appearance: none;
background-color: var(--color-background);
Expand Down
1 change: 1 addition & 0 deletions packages/frontend/components/textarea/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{% set id = opts.id or opts.name | slugify({ decamelize: true }) %}
{% set describedBy = opts.describedBy if opts.describedBy else "" %}
{% call field({
element: "textarea-component",
classes: opts.field.classes,
attributes: opts.field.attributes,
errorMessage: opts.errorMessage
Expand Down
1 change: 0 additions & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
},
"dependencies": {
"@accessible-components/tag-input": "^0.2.0",
"@hotwired/stimulus": "^3.0.1",
"@indiekit/util": "^1.0.0-beta.5",
"@rollup/plugin-commonjs": "^25.0.3",
"@rollup/plugin-node-resolve": "^15.0.0",
Expand Down
7 changes: 2 additions & 5 deletions packages/frontend/scripts/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* global Stimulus */
import { Application } from "@hotwired/stimulus";
import { AddAnotherComponent } from "../components/add-another/index.js";
import { CheckboxesComponent } from "../components/checkboxes/index.js";
import { ErrorSummaryComponent } from "../components/error-summary/index.js";
Expand All @@ -8,9 +6,8 @@ import { PreviewComponent } from "../components/preview/index.js";
import { NotificationComponent } from "../components/notification/index.js";
import { RadiosComponent } from "../components/radios/index.js";
import { TagInputComponent } from "../components/tag-input/index.js";
import { TextareaController } from "../components/textarea/index.js";
import { TextareaComponent } from "../components/textarea/index.js";

window.Stimulus = Application.start();
customElements.define("add-another-component", AddAnotherComponent);
customElements.define("checkboxes-component", CheckboxesComponent);
customElements.define("error-summary-component", ErrorSummaryComponent);
Expand All @@ -19,4 +16,4 @@ customElements.define("notification-component", NotificationComponent);
customElements.define("preview-component", PreviewComponent);
customElements.define("radios-component", RadiosComponent);
customElements.define("tag-input-component", TagInputComponent);
Stimulus.register("textarea", TextareaController);
customElements.define("textarea-component", TextareaComponent);

0 comments on commit 1a08876

Please sign in to comment.