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

Function useSelectionChange

packages/@react-spectrum/ai/src/TokenField.tsx:666–683  ·  view source on GitHub ↗
(ref: React.RefObject<Element | null>, handler: () => void)

Source from the content-addressed store, hash-verified

664}
665
666function useSelectionChange(ref: React.RefObject<Element | null>, handler: () => void) {
667 useEvent(useRef(document), 'selectionchange', () => {
668 if (isProgrammaticSelectionChange) {
669 isProgrammaticSelectionChange = false;
670 return;
671 }
672
673 let selection = window.getSelection();
674 if (!selection || selection.rangeCount === 0 || !ref.current) {
675 return;
676 }
677
678 let range = selection.getRangeAt(0);
679 if (range.intersectsNode(ref.current)) {
680 handler();
681 }
682 });
683}
684
685function isCollapsed(pos1: Position, pos2: Position) {
686 return pos1.index === pos2.index && pos1.offset === pos2.offset;

Callers 1

TokenField.tsxFile · 0.85

Calls 2

useEventFunction · 0.90
handlerFunction · 0.50

Tested by

no test coverage detected