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

Function getDOMPosition

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

Source from the content-addressed store, hash-verified

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