UNPKG

@responsive-ui/textarea

Version:
740 lines (703 loc) 24.3 kB
var Textarea = (function () { 'use strict'; function noop() { } function assign(tar, src) { // @ts-ignore for (const k in src) tar[k] = src[k]; return tar; } function run(fn) { return fn(); } function blank_object() { return Object.create(null); } function run_all(fns) { fns.forEach(run); } function is_function(thing) { return typeof thing === 'function'; } function safe_not_equal(a, b) { return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function'); } function is_empty(obj) { return Object.keys(obj).length === 0; } function create_slot(definition, ctx, $$scope, fn) { if (definition) { const slot_ctx = get_slot_context(definition, ctx, $$scope, fn); return definition[0](slot_ctx); } } function get_slot_context(definition, ctx, $$scope, fn) { return definition[1] && fn ? assign($$scope.ctx.slice(), definition[1](fn(ctx))) : $$scope.ctx; } function get_slot_changes(definition, $$scope, dirty, fn) { if (definition[2] && fn) { const lets = definition[2](fn(dirty)); if ($$scope.dirty === undefined) { return lets; } if (typeof lets === 'object') { const merged = []; const len = Math.max($$scope.dirty.length, lets.length); for (let i = 0; i < len; i += 1) { merged[i] = $$scope.dirty[i] | lets[i]; } return merged; } return $$scope.dirty | lets; } return $$scope.dirty; } function update_slot_base(slot, slot_definition, ctx, $$scope, slot_changes, get_slot_context_fn) { if (slot_changes) { const slot_context = get_slot_context(slot_definition, ctx, $$scope, get_slot_context_fn); slot.p(slot_context, slot_changes); } } function get_all_dirty_from_scope($$scope) { if ($$scope.ctx.length > 32) { const dirty = []; const length = $$scope.ctx.length / 32; for (let i = 0; i < length; i++) { dirty[i] = -1; } return dirty; } return -1; } function exclude_internal_props(props) { const result = {}; for (const k in props) if (k[0] !== '$') result[k] = props[k]; return result; } function compute_rest_props(props, keys) { const rest = {}; keys = new Set(keys); for (const k in props) if (!keys.has(k) && k[0] !== '$') rest[k] = props[k]; return rest; } function append(target, node) { target.appendChild(node); } function insert(target, node, anchor) { target.insertBefore(node, anchor || null); } function detach(node) { node.parentNode.removeChild(node); } function element(name) { return document.createElement(name); } function text(data) { return document.createTextNode(data); } function space() { return text(' '); } function listen(node, event, handler, options) { node.addEventListener(event, handler, options); return () => node.removeEventListener(event, handler, options); } function attr(node, attribute, value) { if (value == null) node.removeAttribute(attribute); else if (node.getAttribute(attribute) !== value) node.setAttribute(attribute, value); } function set_attributes(node, attributes) { // @ts-ignore const descriptors = Object.getOwnPropertyDescriptors(node.__proto__); for (const key in attributes) { if (attributes[key] == null) { node.removeAttribute(key); } else if (key === 'style') { node.style.cssText = attributes[key]; } else if (key === '__value') { node.value = node[key] = attributes[key]; } else if (descriptors[key] && descriptors[key].set) { node[key] = attributes[key]; } else { attr(node, key, attributes[key]); } } } function children(element) { return Array.from(element.childNodes); } function set_data(text, data) { data = '' + data; if (text.wholeText !== data) text.data = data; } function toggle_class(element, name, toggle) { element.classList[toggle ? 'add' : 'remove'](name); } let current_component; function set_current_component(component) { current_component = component; } // TODO figure out if we still want to support // shorthand events, or if we want to implement // a real bubbling mechanism function bubble(component, event) { const callbacks = component.$$.callbacks[event.type]; if (callbacks) { // @ts-ignore callbacks.slice().forEach(fn => fn.call(this, event)); } } const dirty_components = []; const binding_callbacks = []; const render_callbacks = []; const flush_callbacks = []; const resolved_promise = Promise.resolve(); let update_scheduled = false; function schedule_update() { if (!update_scheduled) { update_scheduled = true; resolved_promise.then(flush); } } function add_render_callback(fn) { render_callbacks.push(fn); } // flush() calls callbacks in this order: // 1. All beforeUpdate callbacks, in order: parents before children // 2. All bind:this callbacks, in reverse order: children before parents. // 3. All afterUpdate callbacks, in order: parents before children. EXCEPT // for afterUpdates called during the initial onMount, which are called in // reverse order: children before parents. // Since callbacks might update component values, which could trigger another // call to flush(), the following steps guard against this: // 1. During beforeUpdate, any updated components will be added to the // dirty_components array and will cause a reentrant call to flush(). Because // the flush index is kept outside the function, the reentrant call will pick // up where the earlier call left off and go through all dirty components. The // current_component value is saved and restored so that the reentrant call will // not interfere with the "parent" flush() call. // 2. bind:this callbacks cannot trigger new flush() calls. // 3. During afterUpdate, any updated components will NOT have their afterUpdate // callback called a second time; the seen_callbacks set, outside the flush() // function, guarantees this behavior. const seen_callbacks = new Set(); let flushidx = 0; // Do *not* move this inside the flush() function function flush() { const saved_component = current_component; do { // first, call beforeUpdate functions // and update components while (flushidx < dirty_components.length) { const component = dirty_components[flushidx]; flushidx++; set_current_component(component); update(component.$$); } set_current_component(null); dirty_components.length = 0; flushidx = 0; while (binding_callbacks.length) binding_callbacks.pop()(); // then, once components are updated, call // afterUpdate functions. This may cause // subsequent updates... for (let i = 0; i < render_callbacks.length; i += 1) { const callback = render_callbacks[i]; if (!seen_callbacks.has(callback)) { // ...so guard against infinite loops seen_callbacks.add(callback); callback(); } } render_callbacks.length = 0; } while (dirty_components.length); while (flush_callbacks.length) { flush_callbacks.pop()(); } update_scheduled = false; seen_callbacks.clear(); set_current_component(saved_component); } function update($$) { if ($$.fragment !== null) { $$.update(); run_all($$.before_update); const dirty = $$.dirty; $$.dirty = [-1]; $$.fragment && $$.fragment.p($$.ctx, dirty); $$.after_update.forEach(add_render_callback); } } const outroing = new Set(); let outros; function group_outros() { outros = { r: 0, c: [], p: outros // parent group }; } function check_outros() { if (!outros.r) { run_all(outros.c); } outros = outros.p; } function transition_in(block, local) { if (block && block.i) { outroing.delete(block); block.i(local); } } function transition_out(block, local, detach, callback) { if (block && block.o) { if (outroing.has(block)) return; outroing.add(block); outros.c.push(() => { outroing.delete(block); if (callback) { if (detach) block.d(1); callback(); } }); block.o(local); } } function get_spread_update(levels, updates) { const update = {}; const to_null_out = {}; const accounted_for = { $$scope: 1 }; let i = levels.length; while (i--) { const o = levels[i]; const n = updates[i]; if (n) { for (const key in o) { if (!(key in n)) to_null_out[key] = 1; } for (const key in n) { if (!accounted_for[key]) { update[key] = n[key]; accounted_for[key] = 1; } } levels[i] = n; } else { for (const key in o) { accounted_for[key] = 1; } } } for (const key in to_null_out) { if (!(key in update)) update[key] = undefined; } return update; } function mount_component(component, target, anchor, customElement) { const { fragment, on_mount, on_destroy, after_update } = component.$$; fragment && fragment.m(target, anchor); if (!customElement) { // onMount happens before the initial afterUpdate add_render_callback(() => { const new_on_destroy = on_mount.map(run).filter(is_function); if (on_destroy) { on_destroy.push(...new_on_destroy); } else { // Edge case - component was destroyed immediately, // most likely as a result of a binding initialising run_all(new_on_destroy); } component.$$.on_mount = []; }); } after_update.forEach(add_render_callback); } function destroy_component(component, detaching) { const $$ = component.$$; if ($$.fragment !== null) { run_all($$.on_destroy); $$.fragment && $$.fragment.d(detaching); // TODO null out other refs, including component.$$ (but need to // preserve final state?) $$.on_destroy = $$.fragment = null; $$.ctx = []; } } function make_dirty(component, i) { if (component.$$.dirty[0] === -1) { dirty_components.push(component); schedule_update(); component.$$.dirty.fill(0); } component.$$.dirty[(i / 31) | 0] |= (1 << (i % 31)); } function init(component, options, instance, create_fragment, not_equal, props, append_styles, dirty = [-1]) { const parent_component = current_component; set_current_component(component); const $$ = component.$$ = { fragment: null, ctx: null, // state props, update: noop, not_equal, bound: blank_object(), // lifecycle on_mount: [], on_destroy: [], on_disconnect: [], before_update: [], after_update: [], context: new Map(options.context || (parent_component ? parent_component.$$.context : [])), // everything else callbacks: blank_object(), dirty, skip_bound: false, root: options.target || parent_component.$$.root }; append_styles && append_styles($$.root); let ready = false; $$.ctx = instance ? instance(component, options.props || {}, (i, ret, ...rest) => { const value = rest.length ? rest[0] : ret; if ($$.ctx && not_equal($$.ctx[i], $$.ctx[i] = value)) { if (!$$.skip_bound && $$.bound[i]) $$.bound[i](value); if (ready) make_dirty(component, i); } return ret; }) : []; $$.update(); ready = true; run_all($$.before_update); // `false` as a special case of no DOM component $$.fragment = create_fragment ? create_fragment($$.ctx) : false; if (options.target) { if (options.hydrate) { const nodes = children(options.target); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion $$.fragment && $$.fragment.l(nodes); nodes.forEach(detach); } else { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion $$.fragment && $$.fragment.c(); } if (options.intro) transition_in(component.$$.fragment); mount_component(component, options.target, options.anchor, options.customElement); flush(); } set_current_component(parent_component); } /** * Base class for Svelte components. Used when dev=false. */ class SvelteComponent { $destroy() { destroy_component(this, 1); this.$destroy = noop; } $on(type, callback) { const callbacks = (this.$$.callbacks[type] || (this.$$.callbacks[type] = [])); callbacks.push(callback); return () => { const index = callbacks.indexOf(callback); if (index !== -1) callbacks.splice(index, 1); }; } $set($$props) { if (this.$$set && !is_empty($$props)) { this.$$.skip_bound = true; this.$$set($$props); this.$$.skip_bound = false; } } } /* components/textarea/src/Textarea.svelte generated by Svelte v3.46.4 */ function create_if_block(ctx) { let current; const default_slot_template = /*#slots*/ ctx[11].default; const default_slot = create_slot(default_slot_template, ctx, /*$$scope*/ ctx[10], null); const default_slot_or_fallback = default_slot || fallback_block(ctx); return { c() { if (default_slot_or_fallback) default_slot_or_fallback.c(); }, m(target, anchor) { if (default_slot_or_fallback) { default_slot_or_fallback.m(target, anchor); } current = true; }, p(ctx, dirty) { if (default_slot) { if (default_slot.p && (!current || dirty & /*$$scope*/ 1024)) { update_slot_base( default_slot, default_slot_template, ctx, /*$$scope*/ ctx[10], !current ? get_all_dirty_from_scope(/*$$scope*/ ctx[10]) : get_slot_changes(default_slot_template, /*$$scope*/ ctx[10], dirty, null), null ); } } else { if (default_slot_or_fallback && default_slot_or_fallback.p && (!current || dirty & /*maxlength, value*/ 34)) { default_slot_or_fallback.p(ctx, !current ? -1 : dirty); } } }, i(local) { if (current) return; transition_in(default_slot_or_fallback, local); current = true; }, o(local) { transition_out(default_slot_or_fallback, local); current = false; }, d(detaching) { if (default_slot_or_fallback) default_slot_or_fallback.d(detaching); } }; } // (35:10) function fallback_block(ctx) { let div; let t0_value = /*value*/ ctx[1].length + ""; let t0; let t1; let t2; let t3; return { c() { div = element("div"); t0 = text(t0_value); t1 = text("/"); t2 = text(/*maxlength*/ ctx[5]); t3 = text(" characters"); attr(div, "class", "resp-textarea__char"); }, m(target, anchor) { insert(target, div, anchor); append(div, t0); append(div, t1); append(div, t2); append(div, t3); }, p(ctx, dirty) { if (dirty & /*value*/ 2 && t0_value !== (t0_value = /*value*/ ctx[1].length + "")) set_data(t0, t0_value); if (dirty & /*maxlength*/ 32) set_data(t2, /*maxlength*/ ctx[5]); }, d(detaching) { if (detaching) detach(div); } }; } function create_fragment(ctx) { let div; let textarea; let textarea_class_value; let t; let current; let mounted; let dispose; let textarea_levels = [ { class: textarea_class_value = "resp-textarea " + /*className*/ ctx[2] }, { maxlength: /*maxlength*/ ctx[5] }, { rows: /*rows*/ ctx[4] }, { cols: /*cols*/ ctx[3] }, { value: /*value*/ ctx[1] }, /*$$restProps*/ ctx[8] ]; let textarea_data = {}; for (let i = 0; i < textarea_levels.length; i += 1) { textarea_data = assign(textarea_data, textarea_levels[i]); } let if_block = /*maxlength*/ ctx[5] > 0 && create_if_block(ctx); return { c() { div = element("div"); textarea = element("textarea"); t = space(); if (if_block) if_block.c(); set_attributes(textarea, textarea_data); toggle_class(textarea, "resp-textarea--bordered", /*bordered*/ ctx[6]); attr(div, "class", "resp-textarea__box"); }, m(target, anchor) { insert(target, div, anchor); append(div, textarea); if (textarea.autofocus) textarea.focus(); /*textarea_binding*/ ctx[15](textarea); append(div, t); if (if_block) if_block.m(div, null); current = true; if (!mounted) { dispose = [ listen(textarea, "input", /*handleChange*/ ctx[7]), listen(textarea, "focus", /*focus_handler*/ ctx[12]), listen(textarea, "blur", /*blur_handler*/ ctx[13]), listen(textarea, "input", /*input_handler*/ ctx[14]) ]; mounted = true; } }, p(ctx, [dirty]) { set_attributes(textarea, textarea_data = get_spread_update(textarea_levels, [ (!current || dirty & /*className*/ 4 && textarea_class_value !== (textarea_class_value = "resp-textarea " + /*className*/ ctx[2])) && { class: textarea_class_value }, (!current || dirty & /*maxlength*/ 32) && { maxlength: /*maxlength*/ ctx[5] }, (!current || dirty & /*rows*/ 16) && { rows: /*rows*/ ctx[4] }, (!current || dirty & /*cols*/ 8) && { cols: /*cols*/ ctx[3] }, (!current || dirty & /*value*/ 2) && { value: /*value*/ ctx[1] }, dirty & /*$$restProps*/ 256 && /*$$restProps*/ ctx[8] ])); toggle_class(textarea, "resp-textarea--bordered", /*bordered*/ ctx[6]); if (/*maxlength*/ ctx[5] > 0) { if (if_block) { if_block.p(ctx, dirty); if (dirty & /*maxlength*/ 32) { transition_in(if_block, 1); } } else { if_block = create_if_block(ctx); if_block.c(); transition_in(if_block, 1); if_block.m(div, null); } } else if (if_block) { group_outros(); transition_out(if_block, 1, 1, () => { if_block = null; }); check_outros(); } }, i(local) { if (current) return; transition_in(if_block); current = true; }, o(local) { transition_out(if_block); current = false; }, d(detaching) { if (detaching) detach(div); /*textarea_binding*/ ctx[15](null); if (if_block) if_block.d(); mounted = false; run_all(dispose); } }; } function instance($$self, $$props, $$invalidate) { const omit_props_names = ["class","ref","cols","rows","maxlength","value","bordered","autoResize"]; let $$restProps = compute_rest_props($$props, omit_props_names); let { $$slots: slots = {}, $$scope } = $$props; let { class: className = "" } = $$props; let { ref } = $$props; let { cols = 80 } = $$props; let { rows = 4 } = $$props; let { maxlength = 100 } = $$props; let { value = "" } = $$props; let { bordered = true } = $$props; let { autoResize = true } = $$props; const handleChange = e => { $$invalidate(1, value = e.target.value); if (autoResize && ref) { $$invalidate(0, ref.style.height = "auto", ref); $$invalidate(0, ref.style.height = `${ref.scrollHeight}px`, ref); } }; function focus_handler(event) { bubble.call(this, $$self, event); } function blur_handler(event) { bubble.call(this, $$self, event); } function input_handler(event) { bubble.call(this, $$self, event); } function textarea_binding($$value) { binding_callbacks[$$value ? 'unshift' : 'push'](() => { ref = $$value; $$invalidate(0, ref); }); } $$self.$$set = $$new_props => { $$props = assign(assign({}, $$props), exclude_internal_props($$new_props)); $$invalidate(8, $$restProps = compute_rest_props($$props, omit_props_names)); if ('class' in $$new_props) $$invalidate(2, className = $$new_props.class); if ('ref' in $$new_props) $$invalidate(0, ref = $$new_props.ref); if ('cols' in $$new_props) $$invalidate(3, cols = $$new_props.cols); if ('rows' in $$new_props) $$invalidate(4, rows = $$new_props.rows); if ('maxlength' in $$new_props) $$invalidate(5, maxlength = $$new_props.maxlength); if ('value' in $$new_props) $$invalidate(1, value = $$new_props.value); if ('bordered' in $$new_props) $$invalidate(6, bordered = $$new_props.bordered); if ('autoResize' in $$new_props) $$invalidate(9, autoResize = $$new_props.autoResize); if ('$$scope' in $$new_props) $$invalidate(10, $$scope = $$new_props.$$scope); }; return [ ref, value, className, cols, rows, maxlength, bordered, handleChange, $$restProps, autoResize, $$scope, slots, focus_handler, blur_handler, input_handler, textarea_binding ]; } class Textarea extends SvelteComponent { constructor(options) { super(); init(this, options, instance, create_fragment, safe_not_equal, { class: 2, ref: 0, cols: 3, rows: 4, maxlength: 5, value: 1, bordered: 6, autoResize: 9 }); } } return Textarea; })();