MCPcopy Create free account
hub / github.com/adobe/react-spectrum / getDOMPosition

Function getDOMPosition

packages/react-aria/src/tokenfield/useTokenField.ts:762–779  ·  view source on GitHub ↗
(root: Element, pos: Position)

Source from the content-addressed store, hash-verified

760}
761
762function getDOMPosition(root: Element, pos: Position): [Node, number] {
763 let index = Math.max(0, Math.min(root.childNodes.length, pos.index));
764 let child = root.childNodes[index];
765 if (!child) {
766 return [root, index];
767 } else if (child.nodeType === Node.ELEMENT_NODE) {
768 // Place the cursor outside the token wrapper element.
769 // This is necessary for composition events.
770 if (pos.offset > 0) {
771 return [root, index + 1];
772 } else {
773 return [root, index];
774 }
775 } else {
776 let offset = Math.max(0, Math.min(child.textContent?.length ?? 0, pos.offset));
777 return [child, offset];
778 }
779}
780
781function isSamePosition(a: Position, b: Position): boolean {
782 return a.index === b.index && a.offset === b.offset;

Callers 3

setTokenFieldSelectionFunction · 0.85
getDOMRectPositionFunction · 0.85
createDOMRangeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected