{"version":3,"sources":["../src/usePositioningSlideDirection.ts"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { useEventCallback, isHTMLElement } from '@fluentui/react-utilities';\nimport type { PositioningProps } from './types';\nimport { POSITIONING_SLIDE_DIRECTION_VAR_X, POSITIONING_SLIDE_DIRECTION_VAR_Y } from './constants';\n\nconst registeredProperties = new WeakMap<Document, Set<string>>();\nconst slideDirectionProperties = [POSITIONING_SLIDE_DIRECTION_VAR_X, POSITIONING_SLIDE_DIRECTION_VAR_Y];\n\n/**\n * Returns the slide direction unit vectors for a given Floating UI placement.\n * Values are -1, 0, or 1, representing the direction the element slides in from.\n */\nexport function getPlacementSlideDirections(placement: string): { x: number; y: number } {\n  const side = placement.split('-')[0];\n  // Default to sliding down from the top side\n  let x = 0;\n  let y = 1;\n\n  if (side === 'right') {\n    x = -1;\n    y = 0;\n  } else if (side === 'bottom') {\n    x = 0;\n    y = -1;\n  } else if (side === 'left') {\n    x = 1;\n    y = 0;\n  }\n\n  return { x, y };\n}\n\ntype UsePositioningSlideDirectionOptions = {\n  /** The target document for CSS.registerProperty. */\n  targetDocument: Document | undefined;\n  /** The user's original onPositioningEnd callback, if any. */\n  onPositioningEnd?: PositioningProps['onPositioningEnd'];\n};\n\n/**\n * A hook that manages CSS custom properties for slide direction based on positioning placement.\n *\n * It wraps the `onPositioningEnd` callback to set `--fui-positioning-slide-direction-x` and\n * `--fui-positioning-slide-direction-y` CSS custom properties on the positioned element,\n * and registers them via `CSS.registerProperty` to avoid properties propagation down to a DOM tree.\n *\n * @returns The wrapped `onPositioningEnd` handler to pass to the positioning config.\n */\nexport function usePositioningSlideDirection(\n  options: UsePositioningSlideDirectionOptions,\n): NonNullable<PositioningProps['onPositioningEnd']> {\n  const { targetDocument, onPositioningEnd } = options;\n\n  const handlePositionEnd: NonNullable<PositioningProps['onPositioningEnd']> = useEventCallback(e => {\n    onPositioningEnd?.(e);\n\n    const element = e.target;\n    const placement = e.detail.placement;\n\n    if (!isHTMLElement(element)) {\n      return;\n    }\n\n    const { x, y } = getPlacementSlideDirections(placement);\n\n    element.style.setProperty(POSITIONING_SLIDE_DIRECTION_VAR_X, `${x}px`);\n    element.style.setProperty(POSITIONING_SLIDE_DIRECTION_VAR_Y, `${y}px`);\n  });\n\n  React.useEffect(() => {\n    const css = targetDocument?.defaultView?.CSS;\n    if (!targetDocument || !css?.registerProperty) {\n      return;\n    }\n    let properties = registeredProperties.get(targetDocument);\n    if (!properties) {\n      properties = new Set<string>();\n      registeredProperties.set(targetDocument, properties);\n    }\n    for (const name of slideDirectionProperties) {\n      if (properties.has(name)) {\n        continue;\n      }\n      try {\n        css.registerProperty({ name, syntax: '<length>', inherits: false, initialValue: '0px' });\n        properties.add(name);\n      } catch (error) {\n        if (error && typeof error === 'object' && 'name' in error && error.name === 'InvalidModificationError') {\n          properties.add(name);\n        }\n      }\n    }\n  }, [targetDocument]);\n\n  return handlePositionEnd;\n}\n"],"names":["React","useEventCallback","isHTMLElement","POSITIONING_SLIDE_DIRECTION_VAR_X","POSITIONING_SLIDE_DIRECTION_VAR_Y","registeredProperties","WeakMap","slideDirectionProperties","getPlacementSlideDirections","placement","side","split","x","y","usePositioningSlideDirection","options","targetDocument","onPositioningEnd","handlePositionEnd","e","element","target","detail","style","setProperty","useEffect","css","defaultView","CSS","registerProperty","properties","get","Set","set","name","has","syntax","inherits","initialValue","add","error"],"mappings":"AAAA;;;;;;;;;;;;QAcgBQ;;;;eAoCAM;;;;iEAhDO,QAAQ;gCACiB,4BAA4B;2BAES,iBAAc;AAEnG,MAAMT,uBAAuB,IAAIC;AACjC,MAAMC,2BAA2B;IAACJ,4CAAAA;IAAmCC,4CAAAA;CAAkC;AAMhG,qCAAqCK,SAAiB;IAC3D,MAAMC,OAAOD,UAAUE,KAAK,CAAC,IAAI,CAAC,EAAE;IACpC,4CAA4C;IAC5C,IAAIC,IAAI;IACR,IAAIC,IAAI;IAER,IAAIH,SAAS,SAAS;QACpBE,IAAI,CAAC;QACLC,IAA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