UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

250 lines (243 loc) • 15.4 kB
/*! * All material copyright ESRI, All Rights Reserved, unless otherwise specified. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details. * v1.5.0-next.4 */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const index = require('./index-55f8a3b7.js'); const form = require('./form-f9d34433.js'); const label = require('./label-fb3080ea.js'); const dom = require('./dom-18ca68ff.js'); const locale = require('./locale-fc347462.js'); const observers = require('./observers-83b3999d.js'); const loadable = require('./loadable-53f729bb.js'); const t9n = require('./t9n-14d528c4.js'); const interactive = require('./interactive-26294f2c.js'); const throttle = require('./throttle-c651269b.js'); require('./guid-db20443e.js'); require('./resources-45d84c94.js'); require('./key-2ce02f02.js'); require('./browser-28ea2ce1.js'); require('./debounce-492319d0.js'); const CSS = { characterLimit: "character-limit", content: "content", container: "container", footer: "footer", resizeDisabled: "resize--disabled", resizeDisabledX: "resize--disabled-x", resizeDisabledY: "resize--disabled-y", characterOverLimit: "character--over-limit", readOnly: "readonly", textAreaInvalid: "text-area--invalid", footerSlotted: "footer--slotted", borderColor: "border--color", hide: "hide", blockSizeFull: "block-size--full", footerEndSlotOnly: "footer--end-only" }; const SLOTS = { footerStart: "footer-start", footerEnd: "footer-end" }; const RESIZE_TIMEOUT = 100; const textAreaCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-right{0%{opacity:0;transform:translate3D(-5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-left{0%{opacity:0;transform:translate3D(5px, 0, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-right{animation-name:in-right}.calcite-animate__in-left{animation-name:in-left}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing);--calcite-floating-ui-z-index:var(--calcite-app-z-index-dropdown)}:host([hidden]){display:none}:host([disabled]){cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) *,:host([disabled]) ::slotted(*){pointer-events:none}:host{position:relative;display:inline-block;block-size:100%;inline-size:100%}textarea{position:relative;margin:0px;box-sizing:border-box;display:block;inline-size:100%;border-width:1px;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-foreground-1);font-family:var(--calcite-sans-family);color:var(--calcite-ui-text-1);min-inline-size:12rem;border-block-end:1px solid var(--calcite-ui-border-3)}@media screen and (max-width: 480px){textarea{resize:none}}textarea:focus{outline:2px solid var(--calcite-ui-focus-color, var(--calcite-ui-brand));outline-offset:calc(\n -2px *\n calc(\n 1 -\n 2 * clamp(\n 0,\n var(--calcite-ui-focus-offset-invert),\n 1\n )\n )\n )}textarea.text-area--invalid{border-width:1px;border-block-end-width:1px;border-style:solid;border-color:var(--calcite-ui-danger)}textarea.text-area--invalid:focus{outline:2px solid var(--calcite-ui-danger);outline-offset:calc(\n -2px *\n calc(\n 1 -\n 2 * clamp(\n 0,\n var(--calcite-ui-focus-offset-invert),\n 1\n )\n )\n )}textarea.footer--slotted{min-inline-size:18rem}.footer{box-sizing:border-box;display:flex;align-items:center;border-width:1px;border-block-start-width:0px;border-style:solid;border-color:var(--calcite-ui-border-input);background-color:var(--calcite-ui-foreground-1)}.character-limit{display:flex;align-items:center;justify-content:flex-end;white-space:nowrap;font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-2)}.character--over-limit{font-weight:var(--calcite-font-weight-bold);color:var(--calcite-ui-danger)}:host([resize=none]) textarea{resize:none}:host([resize=horizontal]) textarea{resize:horizontal}:host([resize=vertical]) textarea{resize:vertical}:host([scale=s]){font-size:var(--calcite-font-size--2)}:host([scale=s]) .footer{padding-block:0.25rem;padding-inline:0.5rem;min-block-size:1.75rem}:host([scale=s]) textarea{padding-block:0.25rem;padding-inline:0.5rem}:host([scale=s]) textarea,:host([scale=s]) .footer,:host([scale=s]) .character-limit{padding-inline-start:0.5rem;font-size:var(--calcite-font-size--2)}:host([scale=m]) textarea{padding-block:0.5rem;padding-inline:0.75rem}:host([scale=m]) .footer{padding-block:0.5rem;padding-inline:0.75rem;min-block-size:2.25rem}:host([scale=m]) textarea,:host([scale=m]) .footer,:host([scale=m]) .character-limit{padding-inline-start:0.75rem;font-size:var(--calcite-font-size--1)}:host([scale=l]){font-size:var(--calcite-font-size-0)}:host([scale=l]) textarea{padding-block:0.75rem;padding-inline:1rem}:host([scale=l]) .footer{padding-block:0.75rem;padding-inline:1rem;min-block-size:2.75rem}:host([scale=l]) textarea,:host([scale=l]) .footer,:host([scale=l]) .character-limit{padding-inline-start:1rem;font-size:var(--calcite-font-size-0)}.readonly{background-color:var(--calcite-ui-background);font-weight:var(--calcite-font-weight-medium)}:host([disabled]) textarea,:host([disabled]) footer{opacity:0.5}.border--color{border-block-end-width:1px;border-color:var(--calcite-ui-border-input)}.border--color:focus{border-block-end-width:2px}textarea.block-size--full{block-size:100%}.content,.hide{display:none}.container{display:flex;inline-size:100%;justify-content:space-between}.footer--end-only{justify-content:flex-end}::slotted(input[slot=hidden-form-input]){margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;inset:0 !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}"; const TextArea = class { constructor(hostRef) { index.registerInstance(this, hostRef); this.calciteTextAreaInput = index.createEvent(this, "calciteTextAreaInput", 7); this.calciteTextAreaChange = index.createEvent(this, "calciteTextAreaChange", 7); this.handleInput = (event) => { this.value = event.target["value"]; this.calciteTextAreaInput.emit(); }; this.handleChange = () => { this.calciteTextAreaChange.emit(); }; this.contentSlotChangeHandler = () => { if (!this.value) { const nodes = this.el.childNodes; nodes.forEach((el) => { if (el.nodeName === "#text") { this.value = el.nodeValue.trim(); } }); } }; this.renderCharacterLimit = () => { return this.maxLength ? (index.h("span", { class: CSS.characterLimit }, index.h("span", { class: { [CSS.characterOverLimit]: this.value?.length > this.maxLength } }, this.getLocalizedCharacterLength()), "/", locale.numberStringFormatter.localize(this.maxLength.toString()))) : null; }; this.resizeObserver = observers.createObserver("resize", async () => { await loadable.componentLoaded(this); const { textAreaHeight, textAreaWidth, elHeight, elWidth, footerHeight, footerWidth } = this.getHeightandWidthOfElements(); if (footerWidth > 0 && footerWidth !== textAreaWidth) { this.footerEl.style.width = `${textAreaWidth}px`; } if (elWidth !== textAreaWidth || elHeight !== textAreaHeight + (footerHeight || 0)) { this.setHeightAndWidthToAuto(); } }); // height and width are set to auto here to avoid overlapping on to neighboring elements in the layout when user starts resizing. // throttle is used to avoid flashing of textarea when user resizes. this.setHeightAndWidthToAuto = throttle.throttle(() => { if (this.resize === "vertical" || this.resize === "both") { this.el.style.height = "auto"; } if (this.resize === "horizontal" || this.resize === "both") { this.el.style.width = "auto"; } }, RESIZE_TIMEOUT, { leading: false }); this.setTextAreaEl = (el) => { this.textAreaEl = el; this.resizeObserver.observe(el); }; this.autofocus = false; this.columns = undefined; this.disabled = false; this.form = undefined; this.groupSeparator = false; this.label = undefined; this.maxLength = undefined; this.messages = undefined; this.name = undefined; this.numberingSystem = undefined; this.placeholder = undefined; this.readOnly = false; this.required = false; this.resize = "both"; this.rows = undefined; this.scale = "m"; this.value = undefined; this.wrap = "soft"; this.messageOverrides = undefined; this.defaultMessages = undefined; this.endSlotHasElements = undefined; this.startSlotHasElements = undefined; this.effectiveLocale = ""; } onMessagesChange() { /* wired up by t9n util */ } //-------------------------------------------------------------------------- // // Lifecycle // //-------------------------------------------------------------------------- connectedCallback() { interactive.connectInteractive(this); label.connectLabel(this); form.connectForm(this); locale.connectLocalized(this); t9n.connectMessages(this); } async componentWillLoad() { loadable.setUpLoadableComponent(this); await t9n.setUpMessages(this); } componentDidLoad() { loadable.setComponentLoaded(this); } componentDidRender() { interactive.updateHostInteraction(this); this.setTextAreaHeight(); } disconnectedCallback() { interactive.disconnectInteractive(this); label.disconnectLabel(this); form.disconnectForm(this); locale.disconnectLocalized(this); t9n.disconnectMessages(this); this.resizeObserver?.disconnect(); } render() { const hasFooter = this.startSlotHasElements || this.endSlotHasElements || !!this.maxLength; return (index.h(index.Host, null, index.h("textarea", { "aria-invalid": dom.toAriaBoolean(this.value?.length > this.maxLength), "aria-label": label.getLabelText(this), autofocus: this.autofocus, class: { [CSS.readOnly]: this.readOnly, [CSS.textAreaInvalid]: this.value?.length > this.maxLength, [CSS.footerSlotted]: this.endSlotHasElements && this.startSlotHasElements, [CSS.blockSizeFull]: !hasFooter, [CSS.borderColor]: !hasFooter }, cols: this.columns, disabled: this.disabled, name: this.name, onChange: this.handleChange, onInput: this.handleInput, placeholder: this.placeholder, readonly: this.readOnly, required: this.required, rows: this.rows, value: this.value, wrap: this.wrap, // eslint-disable-next-line react/jsx-sort-props ref: this.setTextAreaEl }), index.h("span", { class: { [CSS.content]: true } }, index.h("slot", { onSlotchange: this.contentSlotChangeHandler })), index.h("footer", { class: { [CSS.footer]: true, [CSS.readOnly]: this.readOnly, [CSS.hide]: !hasFooter }, ref: (el) => (this.footerEl = el) }, index.h("div", { class: { [CSS.container]: true, [CSS.footerEndSlotOnly]: !this.startSlotHasElements && this.endSlotHasElements } }, index.h("slot", { name: SLOTS.footerStart, onSlotchange: (event) => (this.startSlotHasElements = dom.slotChangeHasAssignedElement(event)) }), index.h("slot", { name: SLOTS.footerEnd, onSlotchange: (event) => (this.endSlotHasElements = dom.slotChangeHasAssignedElement(event)) })), this.renderCharacterLimit()), index.h(form.HiddenFormInputSlot, { component: this }))); } //-------------------------------------------------------------------------- // // Public Methods // //-------------------------------------------------------------------------- /** Sets focus on the component. */ async setFocus() { await loadable.componentLoaded(this); this.textAreaEl.focus(); } /** Selects the text of the component's `value`. */ async selectText() { await loadable.componentLoaded(this); this.textAreaEl.select(); } effectiveLocaleChange() { t9n.updateMessages(this, this.effectiveLocale); } //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- onFormReset() { this.value = this.defaultValue; } onLabelClick() { this.setFocus(); } getLocalizedCharacterLength() { locale.numberStringFormatter.numberFormatOptions = { locale: this.effectiveLocale, numberingSystem: this.numberingSystem, signDisplay: "never", useGrouping: this.groupSeparator }; return locale.numberStringFormatter.localize(this.value ? this.value.length.toString() : "0"); } syncHiddenFormInput(input) { input.setCustomValidity(""); if (this.value?.length > this.maxLength) { input.setCustomValidity(this.messages.tooLong); } } setTextAreaHeight() { const { textAreaHeight, elHeight, footerHeight } = this.getHeightandWidthOfElements(); if (footerHeight > 0 && textAreaHeight + footerHeight != elHeight) { this.textAreaEl.style.height = `${elHeight - footerHeight}px`; } } getHeightandWidthOfElements() { const { height: textAreaHeight, width: textAreaWidth } = this.textAreaEl.getBoundingClientRect(); const { height: elHeight, width: elWidth } = this.el.getBoundingClientRect(); const { height: footerHeight, width: footerWidth } = this.footerEl?.getBoundingClientRect(); return { textAreaHeight, textAreaWidth, elHeight, elWidth, footerHeight, footerWidth }; } static get assetsDirs() { return ["assets"]; } get el() { return index.getElement(this); } static get watchers() { return { "messageOverrides": ["onMessagesChange"], "effectiveLocale": ["effectiveLocaleChange"] }; } }; TextArea.style = textAreaCss; exports.calcite_text_area = TextArea;