Source: autosize/autosize.es.js

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. const DEFAULT_APPEND_CONTENT = '  ';
  15. class AutoSize {
  16. constructor(inputElement) {
  17. this.inputElement = inputElement;
  18. this.computedStyle = getComputedStyle(this.inputElement);
  19. this.minHeight = parseInt(
  20. this.computedStyle.height.replace('px', ''),
  21. 10
  22. );
  23. this.paddingHeight =
  24. parseInt(this.computedStyle.paddingTop.replace('px', ''), 10) +
  25. parseInt(this.computedStyle.paddingBottom.replace('px', ''), 10);
  26. this.template = this.createTemplate(this.computedStyle);
  27. document.body.appendChild(this.template);
  28. this.inputElement.addEventListener('input', this.handleInput);
  29. this._resizeInput(this.inputElement);
  30. }
  31. createTemplate(computedStyle) {
  32. const template = document.createElement('pre');
  33. template.style.clip = 'rect(0, 0, 0, 0) !important';
  34. template.style.left = '0';
  35. template.style.overflowWrap = 'break-word';
  36. template.style.position = 'absolute';
  37. template.style.top = '0';
  38. template.style.transform = 'scale(0)';
  39. template.style.whiteSpace = 'pre-wrap';
  40. template.style.wordBreak = 'break-word';
  41. template.style.fontFamily = computedStyle.fontFamily;
  42. template.style.fontSize = computedStyle.fontSize;
  43. template.style.fontStyle = computedStyle.fontStyle;
  44. template.style.fontWeight = computedStyle.fontWeight;
  45. template.style.lineHeight = computedStyle.lineHeight;
  46. template.style.letterSpacing = computedStyle.letterSpacing;
  47. template.style.textTransform = computedStyle.textTransform;
  48. template.style.width = computedStyle.width;
  49. template.textContent = DEFAULT_APPEND_CONTENT;
  50. return template;
  51. }
  52. handleInput = (event) => {
  53. requestAnimationFrame(() => {
  54. this._resizeInput(event.target);
  55. });
  56. };
  57. _resizeInput(inputElement) {
  58. if (this.template.style.width !== this.computedStyle.width) {
  59. this.template.style.width = this.computedStyle.width;
  60. }
  61. this.template.innerHTML =
  62. Liferay.Util.escapeHTML(inputElement.value) +
  63. DEFAULT_APPEND_CONTENT;
  64. inputElement.style.height = `${
  65. this.template.scrollHeight + this.paddingHeight < this.minHeight
  66. ? this.minHeight
  67. : this.template.scrollHeight + this.paddingHeight
  68. }px`;
  69. }
  70. }
  71. export default AutoSize;