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

Function useToken

packages/react-aria/src/tokenfield/useToken.ts:29–62  ·  view source on GitHub ↗
(
  // Unused but matches the normal signature.
  _props: TokenProps,
  _state: any,
  ref: RefObject<HTMLSpanElement | null>
)

Source from the content-addressed store, hash-verified

27 * A token field allows users to enter text with inline tokens.
28 */
29export function useToken(
30 // Unused but matches the normal signature.
31 _props: TokenProps,
32 _state: any,
33 ref: RefObject<HTMLSpanElement | null>
34): TokenAria {
35 let [isSelected, setSelected] = useState(false);
36
37 useEvent(useRef(typeof document !== 'undefined' ? document : null), 'selectionchange', () => {
38 let selection = window.getSelection();
39 if (!selection || selection.rangeCount === 0 || !ref.current) {
40 return;
41 }
42
43 let range = selection.getRangeAt(0);
44 if (!range.collapsed && range.intersectsNode(ref.current)) {
45 setSelected(true);
46 } else {
47 setSelected(false);
48 }
49 });
50
51 return {
52 tokenProps: {
53 contentEditable: false,
54 suppressContentEditableWarning: true,
55 style: {
56 userSelect: 'all',
57 WebkitUserSelect: 'all'
58 }
59 },
60 isSelected
61 };
62}

Callers 1

TokenField.tsxFile · 0.85

Calls 2

useEventFunction · 0.90
setSelectedFunction · 0.50

Tested by

no test coverage detected