|
1 | 1 | (function (global, doc) { |
2 | 2 | const INPUT_PADDING = 12; |
| 3 | + const TEXTAREA_SCROLLBAR_PADDING = 6; |
3 | 4 | const togglePasswordVisibility = (event) => { |
4 | 5 | const passwordTogglerBtn = event.currentTarget; |
5 | 6 | const passwordShowIcon = passwordTogglerBtn.querySelector('.ibexa-input-text-wrapper__password-show'); |
|
40 | 41 | inputClearBtns.forEach((clearBtn) => clearBtn.addEventListener('click', clearText, false)); |
41 | 42 | passwordTogglerBtns.forEach((passwordTogglerBtn) => passwordTogglerBtn.addEventListener('click', togglePasswordVisibility, false)); |
42 | 43 | recalculateStyling(); |
| 44 | + attachListenersToMultilineInputs(); |
43 | 45 | }; |
44 | 46 | const recalculateInputStyling = (inputActionsContainer) => { |
45 | | - const input = inputActionsContainer.closest('.ibexa-input-text-wrapper').querySelector('input'); |
| 47 | + const textWrapper = inputActionsContainer.closest('.ibexa-input-text-wrapper'); |
| 48 | + const inputType = textWrapper.classList.contains('ibexa-input-text-wrapper--multiline') ? 'textarea' : 'input'; |
| 49 | + const input = textWrapper.querySelector(inputType); |
46 | 50 |
|
47 | 51 | if (!input || input.type === 'number') { |
48 | 52 | return; |
|
64 | 68 | recalculateInputStyling(inputActionsContainer); |
65 | 69 | }); |
66 | 70 | }; |
| 71 | + const attachListenersToMultilineInputs = () => { |
| 72 | + const multilineInputWrappers = doc.querySelectorAll('.ibexa-input-text-wrapper.ibexa-input-text-wrapper--multiline'); |
| 73 | + |
| 74 | + multilineInputWrappers.forEach((multilineInputWrapper) => { |
| 75 | + const textareaComponent = multilineInputWrapper.querySelector('.ibexa-input--textarea'); |
| 76 | + |
| 77 | + const textareaComponentObserver = new ResizeObserver(() => { |
| 78 | + toggleScrollbarStyles(multilineInputWrapper); |
| 79 | + }); |
| 80 | + |
| 81 | + textareaComponentObserver.observe(textareaComponent); |
| 82 | + toggleScrollbarStyles(multilineInputWrapper); |
| 83 | + }); |
| 84 | + }; |
| 85 | + const toggleScrollbarStyles = (multilineInputWrapper) => { |
| 86 | + const textareaComponent = multilineInputWrapper.querySelector('textarea'); |
| 87 | + const { scrollHeight, clientHeight, offsetWidth, clientWidth } = textareaComponent; |
| 88 | + const { overflowY } = global.getComputedStyle(textareaComponent); |
| 89 | + const hasScrollbar = overflowY !== 'hidden' && scrollHeight > clientHeight; |
| 90 | + const scrollbarWidth = offsetWidth - clientWidth; |
| 91 | + |
| 92 | + multilineInputWrapper.classList.toggle('ibexa-input-text-wrapper--scrollbar-visible', hasScrollbar); |
| 93 | + multilineInputWrapper.style.setProperty('--scrollbar-width', `${scrollbarWidth + TEXTAREA_SCROLLBAR_PADDING}px`); |
| 94 | + }; |
67 | 95 |
|
68 | 96 | doc.body.addEventListener('ibexa-inputs:added', attachListenersToAllInputs, false); |
69 | 97 | doc.body.addEventListener('ibexa-inputs:recalculate-styling', recalculateStyling, false); |
|
0 commit comments