您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

KeyboardAvoider.tsx 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import React, { useEffect, useState } from 'react';
  2. import { isIosMobileBrowser } from '../../../base/environment/utils';
  3. /**
  4. * Component that renders an element to lift the chat input above the Safari keyboard,
  5. * computing the appropriate height comparisons based on the {@code visualViewport}.
  6. *
  7. * @returns {ReactElement}
  8. */
  9. function KeyboardAvoider() {
  10. if (!isIosMobileBrowser()) {
  11. return null;
  12. }
  13. const [ elementHeight, setElementHeight ] = useState(0);
  14. const [ storedHeight, setStoredHeight ] = useState(window.innerHeight);
  15. /**
  16. * Handles the resizing of the visual viewport in order to compute
  17. * the {@code KeyboardAvoider}'s height.
  18. *
  19. * @returns {void}
  20. */
  21. function handleViewportResize() {
  22. const { innerWidth, visualViewport: { width, height } } = window;
  23. // Compare the widths to make sure the {@code visualViewport} didn't resize due to zooming.
  24. if (width === innerWidth) {
  25. if (height < storedHeight) {
  26. setElementHeight(storedHeight - height);
  27. } else {
  28. setElementHeight(0);
  29. }
  30. setStoredHeight(height);
  31. }
  32. }
  33. useEffect(() => {
  34. // Call the handler in case the keyboard is open when the {@code KeyboardAvoider} is mounted.
  35. handleViewportResize();
  36. window.visualViewport.addEventListener('resize', handleViewportResize);
  37. return () => {
  38. window.visualViewport.removeEventListener('resize', handleViewportResize);
  39. };
  40. }, []);
  41. return <div style = {{ height: `${elementHeight}px` }} />;
  42. }
  43. export default KeyboardAvoider;