UNPKG

@atlaskit/textarea

Version:

A text area lets users enter long form text which spans over multiple lines.

167 lines (164 loc) 8.35 kB
/* text-area.tsx generated by @compiled/babel-plugin v3.0.2 */ import _extends from "@babel/runtime/helpers/extends"; import _typeof from "@babel/runtime/helpers/typeof"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["resize", "appearance", "isCompact", "isRequired", "isReadOnly", "isDisabled", "isInvalid", "isMonospaced", "minimumRows", "testId", "maxHeight", "onBlur", "onFocus", "onChange", "value", "style"]; import "./text-area.compiled.css"; import { ax, ix } from "@compiled/react/runtime"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React, { forwardRef, memo, useCallback, useEffect, useMemo, useRef } from 'react'; import { usePlatformLeafEventHandler } from '@atlaskit/analytics-next/usePlatformLeafEventHandler'; import { fg } from '@atlaskit/platform-feature-flags/fg'; var packageName = "@atlaskit/textarea"; var packageVersion = "10.2.3"; var analyticsParams = { componentName: 'textArea', packageName: packageName, packageVersion: packageVersion }; var lineHeightBase = 20; var lineHeightCompact = 16; var compactVerticalPadding = 2; var verticalPadding = 6; var transitionDuration = '0.2s'; var borderWidth = 2; var baseStyles = null; var finessePlaceholderStyles = null; var appearanceStyles = { standard: "_1dqonqa1 _kp82rsbi _1ltx1j9a _1bqsbk0g _z1pv187o _19wtrsbi _12yml4ek _1l02v77o _11zpv77o _141f1v6z _daj71v6z _olc612x7 _14ji12x7 _7ba01v6z _1kod1v6z _858u1v1w _jomrr01l _7ehivyy1 _186j1v1w _qu18187o", subtle: "_1dqonqa1 _kp821j28 _1ltx1j28 _19wtrsbi _12yml4ek _1l02v77o _11zpv77o _141f1v6z _daj71v6z _olc612x7 _14ji12x7 _1kod1v6z _7ba01v6z _858u1v1w _jomrr01l _7ehivyy1", none: "_1dqoglyw _kp821j28 _1ltx1j28 _19wt1j28 _12ym1j28 _1l02v77o _daj712x7 _11zpv77o _141f12x7 _olc612x7 _1kod12x7 _14ji12x7 _7ba012x7 _858u1j28 _jomr1j28 _7ehi3sij" }; // iOS Safari automatically zooms into form inputs on focus when the font size is less than 16px. // To prevent this zoom behaviour on mobile devices, the textarea uses font.body.large (16px) by default, // then switches to the smaller font.body on screens wider than 30rem (desktop). // @see: https://medium.com/@rares.popescu/2-ways-to-avoid-the-automatic-zoom-in-on-input-fields-8a71479e542e var fontStyles = { default: "_11c81d4k _1tn2fhey", monospace: "_11c81d4k _ect4zzfg _1tn2fhey _14md1vhz" }; var resizeStyles = { horizontal: "_14zg1e08", vertical: "_14zg1e54", auto: "_14zg1hrf", smart: "_14zgglyw", none: "_14zgglyw" }; var setSmartHeight = function setSmartHeight(el) { // Always reset height to auto before calculating new height el.style.height = 'auto'; var borderHeight = borderWidth; var paddingBoxHeight = el.scrollHeight; var borderBoxHeight = paddingBoxHeight + borderHeight * 2; el.style.height = "".concat(borderBoxHeight, "px"); }; var InnerTextArea = /*#__PURE__*/forwardRef(function (props, ref) { var ourRef = useRef(null); var _props$resize = props.resize, resize = _props$resize === void 0 ? 'smart' : _props$resize, _props$appearance = props.appearance, appearance = _props$appearance === void 0 ? 'standard' : _props$appearance, _props$isCompact = props.isCompact, isCompact = _props$isCompact === void 0 ? false : _props$isCompact, _props$isRequired = props.isRequired, isRequired = _props$isRequired === void 0 ? false : _props$isRequired, _props$isReadOnly = props.isReadOnly, isReadOnly = _props$isReadOnly === void 0 ? false : _props$isReadOnly, _props$isDisabled = props.isDisabled, isDisabled = _props$isDisabled === void 0 ? false : _props$isDisabled, _props$isInvalid = props.isInvalid, isInvalid = _props$isInvalid === void 0 ? false : _props$isInvalid, _props$isMonospaced = props.isMonospaced, isMonospaced = _props$isMonospaced === void 0 ? false : _props$isMonospaced, _props$minimumRows = props.minimumRows, minimumRows = _props$minimumRows === void 0 ? 2 : _props$minimumRows, testId = props.testId, _props$maxHeight = props.maxHeight, maxHeight = _props$maxHeight === void 0 ? '50vh' : _props$maxHeight, onBlur = props.onBlur, onFocus = props.onFocus, onChange = props.onChange, value = props.value, style = props.style, rest = _objectWithoutProperties(props, _excluded); var borderHeight = useMemo(function () { return appearance === 'none' ? 2 : 1; }, [appearance]); useEffect(function () { var el = ourRef.current; if (resize === 'smart' && el) { setSmartHeight(el); } }, [resize, value]); var onBlurWithAnalytics = usePlatformLeafEventHandler(_objectSpread({ fn: function fn(event) { onBlur && onBlur(event); }, action: 'blurred' }, analyticsParams)); var onFocusWithAnalytics = usePlatformLeafEventHandler(_objectSpread({ fn: function fn(event) { onFocus && onFocus(event); }, action: 'focused' }, analyticsParams)); var getTextAreaRef = function getTextAreaRef(elementRef) { ourRef.current = elementRef; if (ref && _typeof(ref) === 'object') { ref.current = elementRef; } if (ref && typeof ref === 'function') { ref(elementRef); } }; var handleOnChange = useCallback(function (e) { var el = ourRef.current; if (resize === 'smart' && el) { setSmartHeight(el); } onChange && onChange(e); }, [onChange, resize]); var controlProps = { 'data-invalid': isInvalid ? isInvalid : undefined, 'data-compact': isCompact ? isCompact : undefined, 'data-testid': testId ? testId : undefined }; var compactMinHeightStyles = null; return /*#__PURE__*/React.createElement("textarea", _extends({}, controlProps, { value: value, disabled: isDisabled, readOnly: isReadOnly, required: isRequired, ref: getTextAreaRef, onChange: handleOnChange, onBlur: onBlurWithAnalytics, onFocus: onFocusWithAnalytics, rows: minimumRows }, rest, { className: ax(["_19pkidpf _2hwxidpf _otyridpf _18u0idpf _16jlkb7n _1o9zkb7n _i0dl1osq _2rko1qi0 _11c8fhey _12ji1r31 _1qu2glyw _12y31o36 _1reo1wug _18m91wug _v5641gs0 _189ee4h9 _1e0c1ule _vchhusvi _1bsb1osq _1ul9idpf _p12f1osq _kqswh2mm _syazi7uo _slp31hna _153g1bqt _usy41j9a _1a2sizbr _vbqb1gmx _j5dh13gf _cfjqkb7n _k3o7n7od _1goxglyw _13xeglyw _1fra1bqt _ibjdl4ek _6zpvizbr _19k0187o _1idr1rpy _1lep1gmx _nmhq1fiz _1n388sxn _1luh1v1w _ndzbr01l _1vj0vyy1", fg('platform-dst-tokens-finesse') && "_1idrazsu", appearanceStyles[appearance], fontStyles[isMonospaced ? 'monospace' : 'default'], resizeStyles[resize], "_uzq71d1b _16lw1cn1"]), style: _objectSpread(_objectSpread({}, style), {}, { maxHeight: maxHeight, "--_19uzta9": ix("background-color ".concat(transitionDuration, " ease-in-out,\n border-color ").concat(transitionDuration, " ease-in-out")), "--_dqsusn": ix("".concat(lineHeightCompact * minimumRows + compactVerticalPadding * 2 + borderHeight * 2, "px")), "--_n9nqvh": ix("".concat(lineHeightBase * minimumRows + verticalPadding * 2 + borderHeight * 2, "px")) }) })); }); /** * __Text area__ * * A text area lets users enter long form text which spans over multiple lines. * * - [Examples](https://atlassian.design/components/textarea/examples) * - [Code](https://atlassian.design/components/textarea/code) * - [Usage](https://atlassian.design/components/textarea/usage) */ var TextArea = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(function (props, ref) { return /*#__PURE__*/React.createElement(InnerTextArea, _extends({ ref: ref }, props)); })); TextArea.displayName = 'TextArea'; export default TextArea;