{"version":3,"sources":["../src/components/Dropdown/useListboxPopupState.ts"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { getPartitionedNativeProps, slot, useMergedRefs } from '@fluentui/react-utilities';\nimport type { ExtractSlotProps, Slot, SlotComponentType } from '@fluentui/react-utilities';\nimport { type FieldControlPropsOptions, useFieldControlProps_unstable } from '@fluentui/react-field';\nimport { useActiveDescendant, type ActiveDescendantImperativeRef } from '@fluentui/react-aria';\nimport {\n  isComboboxOptionElement,\n  useComboboxBaseState,\n  type ComboboxBaseProps,\n  type ComboboxBaseState,\n} from '@fluentui/react-combobox';\n\nimport { resolvePositioningShorthand, usePositioning } from '../../positioning';\nimport type { Listbox } from './Listbox';\nimport { useListboxSlot } from './useListboxSlot';\n\ntype TriggerElement = HTMLButtonElement | HTMLInputElement;\n\ntype RootSlot = SlotComponentType<ExtractSlotProps<Slot<'div'>>>;\n\ntype ListboxSlot = ReturnType<typeof useListboxSlot>;\n\n/**\n * Minimum prop shape this hook needs from the consumer (Dropdown / Combobox).\n *\n * The consumer's full props type extends this — for example `DropdownProps` adds `button` and\n * `clearButton`, while `ComboboxProps` adds `input` and `clearIcon`.\n */\nexport type ListboxPopupBaseProps = ComboboxBaseProps &\n  Pick<\n    React.HTMLAttributes<HTMLElement>,\n    'id' | 'aria-labelledby' | 'aria-describedby' | 'aria-invalid' | 'aria-required' | 'style' | 'className'\n  > & {\n    root?: Slot<'div'>;\n    listbox?: Slot<typeof Listbox>;\n    children?: React.ReactNode;\n  };\n\n/** Extras accepted by `useComboboxBaseState` beyond `ComboboxBaseProps`. */\ntype ComboboxBaseStateExtras = Partial<ComboboxBaseProps> & {\n  freeform?: boolean;\n  editable?: boolean;\n};\n\nexport type UseListboxPopupStateOptions<TProps extends ListboxPopupBaseProps> = {\n  /** Tag name of the primary slot — used by `getPartitionedNativeProps` to split native attributes. */\n  primarySlotTagName: 'button' | 'input';\n  /** Options forwarded to `useFieldControlProps_unstable`. */\n  fieldControlOptions: FieldControlPropsOptions;\n  /**\n   * Extra props merged into the `useComboboxBaseState` call (after field control merging).\n   * For example, Dropdown forces `freeform: false`; Combobox derives `editable` from `multiselect`.\n   */\n  baseStateExtras?: (mergedProps: TProps) => ComboboxBaseStateExtras;\n  /**\n   * Extra default props for the root slot. For example, Dropdown places its children on the root\n   * (the option list); Combobox does not.\n   */\n  rootDefaultProps?: (mergedProps: TProps) => Record<string, unknown>;\n};\n\nexport type UseListboxPopupStateReturn<TProps extends ListboxPopupBaseProps, TTrigger extends TriggerElement> = {\n  /** Props after merging with the surrounding `<Field>`, if any. */\n  props: TProps;\n  triggerRef: React.RefObject<TTrigger | null>;\n  activeParentRef: React.Ref<TTrigger>;\n  activeDescendantController: ActiveDescendantImperativeRef;\n  triggerNativeProps: Omit<TProps, 'children'>;\n  internalState: ComboboxBaseState;\n  listbox: ListboxSlot;\n  rootSlot: RootSlot;\n};\n\n/**\n * Shared core for `useDropdown` and `useCombobox`. Owns positioning, active-descendant wiring,\n * the listbox slot, the root slot, and the dev-only warning about unsupported\n * `clearable + multiselect` combinations.\n *\n * Consumers layer their own trigger and clear slots on top of the returned pieces.\n *\n * @internal\n */\nexport function useListboxPopupState<TProps extends ListboxPopupBaseProps, TTrigger extends TriggerElement>(\n  initialProps: TProps,\n  options: UseListboxPopupStateOptions<TProps>,\n): UseListboxPopupStateReturn<TProps, TTrigger> {\n  const { primarySlotTagName, fieldControlOptions, baseStateExtras, rootDefaultProps } = options;\n\n  const positioningOptions = resolvePositioningShorthand(initialProps.positioning);\n  const { targetRef: comboboxTargetRef, containerRef: comboboxPopupRef } = usePositioning(positioningOptions);\n\n  const props = useFieldControlProps_unstable(initialProps, fieldControlOptions) as TProps;\n\n  const {\n    listboxRef: activeDescendantListboxRef,\n    activeParentRef,\n    controller: activeDescendantController,\n  } = useActiveDescendant<TTrigger, HTMLDivElement>({\n    matchOption: isComboboxOptionElement,\n  });\n\n  const internalState = useComboboxBaseState({\n    ...props,\n    ...baseStateExtras?.(props),\n    activeDescendantController,\n  });\n  const { clearable, multiselect, open } = internalState;\n\n  const { primary: triggerNativeProps, root: rootNativeProps } = getPartitionedNativeProps({\n    props,\n    primarySlotTagName,\n    // The cast is needed because `excludedPropNames` is constrained to `Extract<keyof TProps, string>[]`,\n    // and TypeScript can't prove `'children'` is a key of the generic `TProps` despite the bound.\n    excludedPropNames: ['children'] as Extract<keyof TProps, string>[],\n  });\n\n  const triggerRef = React.useRef<TTrigger>(null);\n\n  const listbox = useListboxSlot(props.listbox, useMergedRefs(comboboxPopupRef, activeDescendantListboxRef), {\n    state: internalState,\n    triggerRef,\n    defaultProps: {\n      children: props.children,\n    },\n  });\n\n  const rootSlot = slot.always(props.root ?? undefined, {\n    defaultProps: {\n      'aria-owns': open ? listbox?.id : undefined,\n      ...rootDefaultProps?.(props),\n      ...rootNativeProps,\n    },\n    elementType: 'div',\n  }) as RootSlot;\n  rootSlot.ref = useMergedRefs(rootSlot.ref, comboboxTargetRef);\n\n  if (process.env.NODE_ENV !== 'production') {\n    // eslint-disable-next-line react-hooks/rules-of-hooks -- \"process.env\" does not change in runtime\n    React.useEffect(() => {\n      if (clearable && multiselect) {\n        // eslint-disable-next-line no-console\n        console.error(\n          `[@fluentui/react-headless-components-preview] \"clearable\" prop is not supported in multiselect mode.`,\n        );\n      }\n    }, [clearable, multiselect]);\n  }\n\n  return {\n    props,\n    triggerRef,\n    activeParentRef,\n    activeDescendantController,\n    // `getPartitionedNativeProps` returns `Omit<Props, ExcludedPropKeys>` where the excluded keys\n    // are inferred from a generic constraint. TS can't prove that simplifies to `Omit<TProps, 'children'>`.\n    triggerNativeProps: triggerNativeProps as unknown as Omit<TProps, 'children'>,\n    internalState,\n    listbox,\n    rootSlot,\n  };\n}\n"],"names":["useListboxPopupState","initialProps","options","primarySlotTagName","fieldControlOptions","baseStateExtras","rootDefaultProps","positioningOptions","resolvePositioningShorthand","positioning","targetRef","comboboxTargetRef","containerRef","comboboxPopupRef","usePositioning","props","useFieldControlProps_unstable","listboxRef","activeDescendantListboxRef","activeParentRef","controller","activeDescendantController","useActiveDescendant","matchOption","isComboboxOptionElement","internalState","useComboboxBaseState","clearable","multiselect","open","primary","triggerNativeProps","root","rootNativeProps","getPartitionedNativeProps","excludedPropNames","triggerRef","React","useRef","listbox","useListboxSlot","useMergedRefs","state","defaultProps","children","rootSlot","slot","always","undefined","id","elementType","ref","process","env","NODE_ENV","useEffect","console","error"],"mappings":"AAAA;;;;;+BAoFgBA;;;eAAAA;;;;iEAlFO;gCACwC;4BAEc;2BACL;+BAMjE;6BAEqD;gCAE7B;AAoExB,SAASA,qBACdC,YAAoB,EACpBC,OAA4C;IAE5C,MAAM,EAAEC,kBAAkB,EAAEC,mBAAmB,EAAEC,eAAe,EAAEC,gBAAgB,EAAE,GAAGJ;IAEvF,MAAMK,qBAAqBC,IAAAA,wCAA2B,EAACP,aAAaQ,WAAW;IAC/E,MAAM,EAAEC,WAAWC,iBAAiB,EAAEC,cAAcC,gBAAgB,EAAE,GAAGC,IAAAA,2BAAc,EAACP;IAExF,MAAMQ,QAAQC,IAAAA,yCAA6B,EAACf,cAAcG;IAE1D,MAAM,EACJa,YAAYC,0BAA0B,EACtCC,eAAe,EACfC,YAAYC,0BAA0B,EACvC,GAAGC,IAAAA,8BAAmB,EAA2B;QAChDC,aAAaC,sCAAuB;IACtC;IAEA,MAAMC,gBAAgBC,IAAAA,mCAAoB,EAAC;QACzC,GAAGX,KAAK;QACR,GAAGV,kBAAkBU,MAAM;QAC3BM;IACF;IACA,MAAM,EAAEM,SAAS,EAAEC,WAAW,EAAEC,IAAI,EAAE,GAAGJ;IAEzC,MAAM,EAAEK,SAASC,kBAAkB,EAAEC,MAAMC,eAAe,EAAE,GAAGC,IAAAA,yCAAyB,EAAC;QACvFnB;QACAZ;QACA,sGAAsG;QACtG,8FAA8F;QAC9FgC,mBAAmB;YAAC;SAAW;IACjC;IAEA,MAAMC,aAAaC,OAAMC,MAAM,CAAW;IAE1C,MAAMC,UAAUC,IAAAA,8BAAc,EAAC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