UNPKG

react-native-web

Version:
127 lines (126 loc) 3.59 kB
/** * Copyright (c) Nicolas Gallagher. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * */ import getBoundingClientRect from '../../modules/getBoundingClientRect'; import setValueForStyles from '../../modules/setValueForStyles'; var getRect = node => { var height = node.offsetHeight; var width = node.offsetWidth; var left = node.offsetLeft; var top = node.offsetTop; node = node.offsetParent; while (node && node.nodeType === 1 /* Node.ELEMENT_NODE */) { left += node.offsetLeft + node.clientLeft - node.scrollLeft; top += node.offsetTop + node.clientTop - node.scrollTop; node = node.offsetParent; } top -= window.scrollY; left -= window.scrollX; return { width, height, top, left }; }; var measureLayout = (node, relativeToNativeNode, callback) => { var relativeNode = relativeToNativeNode || node && node.parentNode; if (node && relativeNode) { setTimeout(() => { if (node.isConnected && relativeNode.isConnected) { var relativeRect = getRect(relativeNode); var _getRect = getRect(node), height = _getRect.height, left = _getRect.left, top = _getRect.top, width = _getRect.width; var x = left - relativeRect.left; var y = top - relativeRect.top; callback(x, y, width, height, left, top); } }, 0); } }; var elementsToIgnore = { A: true, BODY: true, INPUT: true, SELECT: true, TEXTAREA: true }; var UIManager = { blur(node) { try { node.blur(); } catch (err) {} }, focus(node) { try { var name = node.nodeName; // A tabIndex of -1 allows element to be programmatically focused but // prevents keyboard focus. We don't want to set the tabindex value on // elements that should not prevent keyboard focus. if (node.getAttribute('tabIndex') == null && node.isContentEditable !== true && elementsToIgnore[name] == null) { node.setAttribute('tabIndex', '-1'); } node.focus(); } catch (err) {} }, measure(node, callback) { measureLayout(node, null, callback); }, measureInWindow(node, callback) { if (node) { setTimeout(() => { var _getBoundingClientRec = getBoundingClientRect(node), height = _getBoundingClientRec.height, left = _getBoundingClientRec.left, top = _getBoundingClientRec.top, width = _getBoundingClientRec.width; callback(left, top, width, height); }, 0); } }, measureLayout(node, relativeToNativeNode, onFail, onSuccess) { measureLayout(node, relativeToNativeNode, onSuccess); }, updateView(node, props) { for (var prop in props) { if (!Object.prototype.hasOwnProperty.call(props, prop)) { continue; } var value = props[prop]; switch (prop) { case 'style': { setValueForStyles(node, value); break; } case 'class': case 'className': { node.setAttribute('class', value); break; } case 'text': case 'value': // native platforms use `text` prop to replace text input value node.value = value; break; default: node.setAttribute(prop, value); } } }, configureNextLayoutAnimation(config, onAnimationDidEnd) { onAnimationDidEnd(); }, // mocks setLayoutAnimationEnabledExperimental() {} }; export default UIManager;