UNPKG

@mui/material

Version:

Material UI is an open-source React component library that implements Google's Material Design. It's comprehensive and can be used in production out of the box.

250 lines (243 loc) 8.79 kB
"use strict"; 'use client'; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var React = _interopRequireWildcard(require("react")); var _propTypes = _interopRequireDefault(require("prop-types")); var _utils = require("@mui/utils"); var _jsxRuntime = require("react/jsx-runtime"); function getStyleValue(value) { return parseInt(value, 10) || 0; } const styles = { shadow: { // Visibility needed to hide the extra text area on iPads visibility: 'hidden', // Remove from the content flow position: 'absolute', // Ignore the scrollbar width overflow: 'hidden', height: 0, top: 0, left: 0, // Create a new layer, increase the isolation of the computed values transform: 'translateZ(0)' } }; function isObjectEmpty(object) { // eslint-disable-next-line for (const _ in object) { return false; } return true; } function isEmpty(obj) { return isObjectEmpty(obj) || obj.outerHeightStyle === 0 && !obj.overflowing; } /** * * Demos: * * - [Textarea Autosize](https://mui.com/material-ui/react-textarea-autosize/) * * API: * * - [TextareaAutosize API](https://mui.com/material-ui/api/textarea-autosize/) */ const TextareaAutosize = /*#__PURE__*/React.forwardRef(function TextareaAutosize(props, forwardedRef) { const { onChange, maxRows, minRows = 1, style, value, ...other } = props; const { current: isControlled } = React.useRef(value != null); const textareaRef = React.useRef(null); const handleRef = (0, _utils.unstable_useForkRef)(forwardedRef, textareaRef); const heightRef = React.useRef(null); const hiddenTextareaRef = React.useRef(null); const calculateTextareaStyles = React.useCallback(() => { const textarea = textareaRef.current; const hiddenTextarea = hiddenTextareaRef.current; if (!textarea || !hiddenTextarea) { return undefined; } const containerWindow = (0, _utils.unstable_ownerWindow)(textarea); const computedStyle = containerWindow.getComputedStyle(textarea); // If input's width is shrunk and it's not visible, don't sync height. if (computedStyle.width === '0px') { return { outerHeightStyle: 0, overflowing: false }; } hiddenTextarea.style.width = computedStyle.width; hiddenTextarea.value = textarea.value || props.placeholder || 'x'; if (hiddenTextarea.value.slice(-1) === '\n') { // Certain fonts which overflow the line height will cause the textarea // to report a different scrollHeight depending on whether the last line // is empty. Make it non-empty to avoid this issue. hiddenTextarea.value += ' '; } const boxSizing = computedStyle.boxSizing; const padding = getStyleValue(computedStyle.paddingBottom) + getStyleValue(computedStyle.paddingTop); const border = getStyleValue(computedStyle.borderBottomWidth) + getStyleValue(computedStyle.borderTopWidth); // The height of the inner content const innerHeight = hiddenTextarea.scrollHeight; // Measure height of a textarea with a single row hiddenTextarea.value = 'x'; const singleRowHeight = hiddenTextarea.scrollHeight; // The height of the outer content let outerHeight = innerHeight; if (minRows) { outerHeight = Math.max(Number(minRows) * singleRowHeight, outerHeight); } if (maxRows) { outerHeight = Math.min(Number(maxRows) * singleRowHeight, outerHeight); } outerHeight = Math.max(outerHeight, singleRowHeight); // Take the box sizing into account for applying this value as a style. const outerHeightStyle = outerHeight + (boxSizing === 'border-box' ? padding + border : 0); const overflowing = Math.abs(outerHeight - innerHeight) <= 1; return { outerHeightStyle, overflowing }; }, [maxRows, minRows, props.placeholder]); const didHeightChange = (0, _utils.unstable_useEventCallback)(() => { const textarea = textareaRef.current; const textareaStyles = calculateTextareaStyles(); if (!textarea || !textareaStyles || isEmpty(textareaStyles)) { return false; } const outerHeightStyle = textareaStyles.outerHeightStyle; return heightRef.current != null && heightRef.current !== outerHeightStyle; }); const syncHeight = React.useCallback(() => { const textarea = textareaRef.current; const textareaStyles = calculateTextareaStyles(); if (!textarea || !textareaStyles || isEmpty(textareaStyles)) { return; } const outerHeightStyle = textareaStyles.outerHeightStyle; if (heightRef.current !== outerHeightStyle) { heightRef.current = outerHeightStyle; textarea.style.height = `${outerHeightStyle}px`; } textarea.style.overflow = textareaStyles.overflowing ? 'hidden' : ''; }, [calculateTextareaStyles]); const frameRef = React.useRef(-1); (0, _utils.unstable_useEnhancedEffect)(() => { const debouncedHandleResize = (0, _utils.unstable_debounce)(syncHeight); const textarea = textareaRef?.current; if (!textarea) { return undefined; } const containerWindow = (0, _utils.unstable_ownerWindow)(textarea); containerWindow.addEventListener('resize', debouncedHandleResize); let resizeObserver; if (typeof ResizeObserver !== 'undefined') { resizeObserver = new ResizeObserver(() => { if (didHeightChange()) { // avoid "ResizeObserver loop completed with undelivered notifications" error // by temporarily unobserving the textarea element while manipulating the height // and reobserving one frame later resizeObserver.unobserve(textarea); cancelAnimationFrame(frameRef.current); syncHeight(); frameRef.current = requestAnimationFrame(() => { resizeObserver.observe(textarea); }); } }); resizeObserver.observe(textarea); } return () => { debouncedHandleResize.clear(); cancelAnimationFrame(frameRef.current); containerWindow.removeEventListener('resize', debouncedHandleResize); if (resizeObserver) { resizeObserver.disconnect(); } }; }, [calculateTextareaStyles, syncHeight, didHeightChange]); (0, _utils.unstable_useEnhancedEffect)(() => { syncHeight(); }); const handleChange = event => { if (!isControlled) { syncHeight(); } if (onChange) { onChange(event); } }; return /*#__PURE__*/(0, _jsxRuntime.jsxs)(React.Fragment, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("textarea", { value: value, onChange: handleChange, ref: handleRef // Apply the rows prop to get a "correct" first SSR paint , rows: minRows, style: style, ...other }), /*#__PURE__*/(0, _jsxRuntime.jsx)("textarea", { "aria-hidden": true, className: props.className, readOnly: true, ref: hiddenTextareaRef, tabIndex: -1, style: { ...styles.shadow, ...style, paddingTop: 0, paddingBottom: 0 } })] }); }); process.env.NODE_ENV !== "production" ? TextareaAutosize.propTypes /* remove-proptypes */ = { // ┌────────────────────────────── Warning ──────────────────────────────┐ // │ These PropTypes are generated from the TypeScript type definitions. │ // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │ // └─────────────────────────────────────────────────────────────────────┘ /** * @ignore */ className: _propTypes.default.string, /** * Maximum number of rows to display. */ maxRows: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]), /** * Minimum number of rows to display. * @default 1 */ minRows: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string]), /** * @ignore */ onChange: _propTypes.default.func, /** * @ignore */ placeholder: _propTypes.default.string, /** * @ignore */ style: _propTypes.default.object, /** * @ignore */ value: _propTypes.default.oneOfType([_propTypes.default.arrayOf(_propTypes.default.string), _propTypes.default.number, _propTypes.default.string]) } : void 0; var _default = exports.default = TextareaAutosize;