@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
52 lines • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cast = exports.isElement = void 0;
exports.sortNodes = sortNodes;
exports.getNextIndex = getNextIndex;
exports.getPrevIndex = getPrevIndex;
/**
* Sort an array of DOM nodes according to the HTML tree order
* @see http://www.w3.org/TR/html5/infrastructure.html#tree-order
* Inspired by
* - https://github.com/floating-ui/floating-ui/blob/8e449abb0bfda143c6a6eb01d3e6943c095b744f/packages/react/src/components/FloatingList.tsx#L8
* - https://github.com/chakra-ui/chakra-ui/tree/5ec0be610b5a69afba01a9c22365155c1b519136/packages/components/descendant
*/
function sortNodes(nodes) {
return nodes.sort((a, b) => {
const compare = a.compareDocumentPosition(b);
if (compare & Node.DOCUMENT_POSITION_FOLLOWING ||
compare & Node.DOCUMENT_POSITION_CONTAINED_BY) {
// a < b
return -1;
}
if (compare & Node.DOCUMENT_POSITION_PRECEDING ||
compare & Node.DOCUMENT_POSITION_CONTAINS) {
// a > b
return 1;
}
if (compare & Node.DOCUMENT_POSITION_DISCONNECTED ||
compare & Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC) {
throw Error("Cannot sort the given nodes.");
}
return 0;
});
}
const isElement = (el) => typeof el === "object" &&
"nodeType" in el &&
el.nodeType === Node.ELEMENT_NODE;
exports.isElement = isElement;
function getNextIndex(current, max, loop) {
let next = current + 1;
if (loop && next >= max)
next = 0;
return next;
}
function getPrevIndex(current, max, loop) {
let next = current - 1;
if (loop && next < 0)
next = max;
return next;
}
const cast = (value) => value;
exports.cast = cast;
//# sourceMappingURL=utils.js.map