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

Function getDOMPosition

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

Source from the content-addressed store, hash-verified

845}
846
847function getDOMPosition(root: Element, pos: Position): [Node, number] {
848 let index = Math.max(0, Math.min(root.childNodes.length, pos.index));
849 let child = root.childNodes[index];
850 if (!child) {
851 return [root, index];
852 } else if (child.nodeType === Node.ELEMENT_NODE) {
853 // Place the cursor outside the token wrapper element.
854 // This is necessary for composition events.
855 if (pos.offset > 0) {
856 return [root, index + 1];
857 } else {
858 return [root, index];
859 }
860 } else {
861 let offset = Math.max(0, Math.min(child.textContent?.length ?? 0, pos.offset));
862 return [child, offset];
863 }
864}
865
866function isSamePosition(a: Position, b: Position): boolean {
867 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