@awsui/components-react
Version:
AWS UI is a collection of [React](https://reactjs.org/) components that help create intuitive, responsive, and accessible user experiences for web applications. It is developed by Amazon Web Services (AWS). This work is available under the terms of the [A
74 lines (73 loc) • 2.83 kB
JavaScript
import balanced from 'balanced-match';
export function findUpUntil(node, callback) {
var current = node;
while (current && !callback(current)) {
current = current.parentElement;
}
return current;
}
export function supportsStickyPosition() {
var _a, _b, _c;
if (typeof window === 'undefined') {
return false;
}
return (_c = (_b = (_a = window.CSS) === null || _a === void 0 ? void 0 : _a.supports) === null || _b === void 0 ? void 0 : _b.call(_a, 'position', 'sticky')) !== null && _c !== void 0 ? _c : false;
}
var _supportsContainingBlockPositioning = undefined;
function supportsContainingBlockPositioning() {
if (_supportsContainingBlockPositioning !== undefined) {
return _supportsContainingBlockPositioning;
}
var parent = document.createElement('div');
parent.style.transform = 'translateY(5px)';
document.body.appendChild(parent);
var child = document.createElement('div');
child.style.position = 'fixed';
child.style.top = '0';
parent.appendChild(child);
_supportsContainingBlockPositioning = parent.getBoundingClientRect().top === child.getBoundingClientRect().top;
document.body.removeChild(parent);
return _supportsContainingBlockPositioning;
}
export function getContainingBlock(startElement) {
if (!startElement.parentElement) {
return null;
}
return supportsContainingBlockPositioning()
? findUpUntil(startElement.parentElement, function (element) {
var computedStyle = getComputedStyle(element);
return ((!!computedStyle.transform && computedStyle.transform !== 'none') ||
(!!computedStyle.perspective && computedStyle.perspective !== 'none'));
})
: null;
}
var cssVariableExpression = /--.+?\s*,\s*(.+)/;
export function parseCssVariable(value) {
var _a, _b, _c;
if ((_c = (_b = (_a = window.CSS) === null || _a === void 0 ? void 0 : _a.supports) === null || _b === void 0 ? void 0 : _b.call(_a, 'color', 'var(--dummy, #000)')) !== null && _c !== void 0 ? _c : false) {
return value;
}
var varIndex = value.lastIndexOf('var(');
if (varIndex === -1) {
return value;
}
var expr = balanced('(', ')', value.substr(varIndex));
if (!expr) {
return value;
}
var match = expr.body.match(cssVariableExpression);
return match ? match[1] : value;
}
export function nodeContains(parent, descendant) {
if (!parent || !descendant) {
return false;
}
if (parent.contains && descendant.nodeType === Node.ELEMENT_NODE) {
return parent === descendant || parent.contains(descendant);
}
var upperNode = descendant;
while (upperNode && parent !== upperNode) {
upperNode = upperNode.parentNode;
}
return upperNode === parent;
}