{"version":3,"file":"PopoverTarget.mjs","sources":["../../../../src/components/Popover/PopoverTarget/PopoverTarget.tsx"],"sourcesContent":["import { cloneElement } from 'react';\nimport cx from 'clsx';\nimport { useMergedRef } from '@raikou/hooks';\nimport { factory, Factory, getRefProp, isElement, useProps } from '../../../core';\nimport { usePopoverContext } from '../Popover.context';\n\nexport interface PopoverTargetProps {\n  /** Target element */\n  children: React.ReactNode;\n\n  /** Key of the prop that should be used to access element ref */\n  refProp?: string;\n\n  /** Popup accessible type, `'dialog'` by default */\n  popupType?: string;\n}\n\nconst defaultProps: Partial<PopoverTargetProps> = {\n  refProp: 'ref',\n  popupType: 'dialog',\n};\n\nexport type PopoverTargetFactory = Factory<{\n  props: PopoverTargetProps;\n  ref: HTMLElement;\n  compound: true;\n}>;\n\nexport const PopoverTarget = factory<PopoverTargetFactory>((props, ref) => {\n  const { children, refProp, popupType, ...others } = useProps(\n    'PopoverTarget',\n    defaultProps,\n    props\n  );\n\n  if (!isElement(children)) {\n    throw new Error(\n      'Popover.Target component children should be an element or a component that accepts ref. Fragments, strings, numbers and other primitive values are not supported'\n    );\n  }\n\n  const forwardedProps: any = others;\n  const ctx = usePopoverContext();\n  const targetRef = useMergedRef(ctx.reference, getRefProp(children), ref);\n\n  const accessibleProps = ctx.withRoles\n    ? {\n        'aria-haspopup': popupType,\n        'aria-expanded': ctx.opened,\n        'aria-controls': ctx.getDropdownId(),\n        id: ctx.getTargetId(),\n      }\n    : {};\n\n  return cloneElement(children, {\n    ...forwardedProps,\n    ...accessibleProps,\n    ...ctx.targetProps,\n    className: cx(ctx.targetProps.className, forwardedProps.className, children.props.className),\n    [refProp!]: targetRef,\n    ...(!ctx.controlled ? { onClick: ctx.onToggle } : null),\n  });\n});\n\nPopoverTarget.displayName = '@raikou/core/PopoverTarget';\n"],"names":[],"mappings":";;;;;;;;;;;;;AAiBA,MAAM,YAA4C,GAAA;AAAA,EAChD,OAAS,EAAA,KAAA;AAAA,EACT,SAAW,EAAA;AACb,CAA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