{"version":3,"sources":["../../../../src/components/Tree/TreeItem/useTreeItem.ts"],"sourcesContent":["'use client';\n\nimport type * as React from 'react';\nimport { ArrowLeft } from '@fluentui/keyboard-keys';\nimport {\n  useTreeContext_unstable,\n  useTreeItem_unstable,\n  useTreeItemContext_unstable,\n  useTreeItemContextValues_unstable,\n} from '@fluentui/react-tree';\nimport { useEventCallback, useTimeout } from '@fluentui/react-utilities';\n\nimport type { TreeItemContextValues, TreeItemProps, TreeItemState } from './TreeItem.types';\nimport { toDataAttributeValue } from '../../../utils';\n\n/**\n * Returns the state for a TreeItem component, given its props and ref.\n * The returned state can be modified with hooks before being passed to `renderTreeItem`.\n */\nexport const useTreeItem = (props: TreeItemProps, ref: React.Ref<HTMLDivElement>): TreeItemState => {\n  const state: TreeItemState = useTreeItem_unstable(props, ref);\n  const navigationMode = useTreeContext_unstable(ctx => ctx.navigationMode ?? 'tree');\n  const requestTreeResponse = useTreeContext_unstable(ctx => ctx.requestTreeResponse);\n  const parentValue = useTreeItemContext_unstable(ctx => props.parentValue ?? ctx.value);\n  const [setActionsNavigationTimeout] = useTimeout();\n\n  // Set data attributes for the item type, open and checked states to simplify styling of these states.\n  // eslint-disable-next-line react-hooks/immutability\n  state.root['data-item-type'] = state.itemType;\n  // eslint-disable-next-line react-hooks/immutability\n  state.root['data-open'] = toDataAttributeValue(state.itemType === 'branch' && state.open);\n  // eslint-disable-next-line react-hooks/immutability\n  state.root['data-checked'] = toDataAttributeValue(state.checked);\n\n  // In a treegrid, ArrowLeft on an action first lets the actions focusgroup move focus to the preceding action,\n  // in the reading direction. Focus only returns to the tree item when the focusgroup did not move it.\n  const { onKeyDown: handleTreeItemKeyDown } = state.root;\n  // eslint-disable-next-line react-hooks/immutability\n  state.root.onKeyDown = useEventCallback((event: React.KeyboardEvent<HTMLDivElement>) => {\n    const { actionsRef } = state;\n    const actions = actionsRef && 'current' in actionsRef ? actionsRef.current : null;\n    if (\n      navigationMode !== 'treegrid' ||\n      event.key !== ArrowLeft ||\n      event.altKey ||\n      !actions?.contains(event.target as Node)\n    ) {\n      handleTreeItemKeyDown?.(event);\n      return;\n    }\n\n    props.onKeyDown?.(event);\n    if (event.isDefaultPrevented()) {\n      return;\n    }\n    const target = event.currentTarget;\n    // The document or the shadow root containing the tree, whose active element is the focused action\n    const root = (event.target as Node).getRootNode() as Document | ShadowRoot;\n    const { activeElement } = root;\n    // The focusgroup moves focus as the default action of the event, after it is dispatched\n    setActionsNavigationTimeout(() => {\n      if (root.activeElement !== activeElement || !actions.contains(activeElement)) {\n        return;\n      }\n      if (root !== target.ownerDocument) {\n        // Tree navigation only sees the shadow host as the active element of the document,\n        // so focus is returned to the tree item directly.\n        target.focus();\n        return;\n      }\n      requestTreeResponse({\n        requestType: 'navigate',\n        event,\n        value: state.value,\n        itemType: state.itemType,\n        parentValue,\n        type: ArrowLeft,\n        target,\n      });\n    }, 0);\n  });\n\n  return state;\n};\n\n/**\n * Returns a value from the context of the closest tree item, such as its value, open or checked state.\n */\nexport { useTreeItemContext_unstable as useTreeItemContext } from '@fluentui/react-tree';\n\n/**\n * Maps the state of the tree item to the values that are passed through context to its layout and subtree.\n */\nexport const useTreeItemContextValues: (state: TreeItemState) => TreeItemContextValues =\n  useTreeItemContextValues_unstable;\n"],"names":["useTreeItem","useTreeItemContext","useTreeItemContext_unstable","useTreeItemContextValues","props","ref","state","useTreeItem_unstable","navigationMode","useTreeContext_unstable","ctx","requestTreeResponse","parentValue","value","setActionsNavigationTimeout","useTimeout","root","itemType","toDataAttributeValue","open","checked","onKeyDown","handleTreeItemKeyDown","useEventCallback","event","actionsRef","actions","current","key","ArrowLeft","altKey","contains","target","isDefaultPrevented","currentTarget","getRootNode","activeElement","ownerDocument","focus","requestType","type","useTreeItemContextValues_unstable"],"mappings":"AAAA;;;;;;;;;;;;QAmBaA;eAAAA;;QAqE2BC;eAA/BC,sCAA2B;;QAKvBC;eAAAA;;;8BA1Fa;2BAMnB;gCACsC;uBAGR;AAM9B,MAAMH,cAAc,C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