{"version":3,"file":"chunk-DragHandle.mjs","names":[],"sources":["../../src/components/DragHandle/drag.ts","../../src/components/DragHandle/DragHandle.tsx"],"sourcesContent":["//\n// Copyright 2026 DXOS.org\n//\n\nimport { type Size, SIZES } from '../../sizes.ts';\n\nconst ANNOUNCER_ID = 'dx-drag-announcer';\n\n/**\n * Speaks `message` through one polite live region shared by every drag handle, created on first use at the end of the\n * body, so announcing adds no element (and no grid track) beside the handle.\n */\nexport const announce = (doc: Document, message: string) => {\n  let region = doc.getElementById(ANNOUNCER_ID);\n  if (!region) {\n    region = doc.createElement('div');\n    region.id = ANNOUNCER_ID;\n    region.setAttribute('role', 'status');\n    region.setAttribute('aria-live', 'polite');\n    region.className = 'sr-only';\n    doc.body.append(region);\n  }\n  region.textContent = message;\n};\n\n/**\n * The size and level a drag preview needs to look like its source row once portalled out of the row's scope (AUDIT\n * 2.6): the `data-size` and `data-surface` of the source's nearest ancestors that set them (attribute reads, no layout).\n */\nexport const dragScope = (source: HTMLElement | null | undefined) => {\n  const size = source?.closest('[data-size]')?.getAttribute('data-size');\n  return {\n    size: SIZES.find((candidate): candidate is Size => candidate === size),\n    surface: source?.closest('[data-surface]:not([data-surface=\"bar\"])')?.getAttribute('data-surface') ?? undefined,\n  };\n};\n","//\n// Copyright 2026 DXOS.org\n//\n\n// @import-as-namespace\n\nimport React, { type KeyboardEvent, forwardRef, useRef, useState } from 'react';\nimport { useTranslation } from 'react-i18next';\n\nimport { useComposedRefs } from '@dxos/react-hooks';\nimport { mx } from '@dxos/ui-theme';\nimport { type ThemedClassName } from '@dxos/ui-types';\n\nimport { translationKey } from '#translations';\n\nimport { recipes } from '../../recipes.ts';\nimport { type Size } from '../../sizes.ts';\nimport { Button } from '../Button/Button.tsx';\nimport { ToolbarContext } from '../Toolbar/toolbar-context.ts';\nimport { announce, dragScope } from './drag.ts';\n\nexport type DragMoveDirection = 'up' | 'down';\n\n//\n// DragHandle\n//\n\ntype DragHandleProps = {\n  /** Names the handle for assistive tech; the translated \"Drag to rearrange\" by default. */\n  'label'?: string;\n  /**\n   * Moves the item one place; makes the handle a tab stop with a keyboard contract: Alt+ArrowUp/Down move at once,\n   * Space or Enter grabs so ArrowUp/Down move, and Space, Enter, Escape or leaving the handle drops. Without it the\n   * handle is a pointer-only grip, outside the tab order.\n   */\n  'onMove'?: (direction: DragMoveDirection) => void;\n  'disabled'?: boolean;\n  'data-testid'?: string;\n};\n\n/**\n * A ghost icon-only Button with the six-dot grip for a drag-and-drop source to bind (in a toolbar, a card or a list\n * row). It never joins a toolbar's roving focus and shows no Tooltip; pointer dragging belongs\n * to the caller's drag-and-drop binding (pragmatic-drag-and-drop in react-ui-list), keyboard moves to `onMove`. Moves\n * and grabs are announced through one shared live region.\n */\nexport const DragHandle = forwardRef<HTMLButtonElement, DragHandleProps>(\n  ({ label, onMove, disabled, 'data-testid': testId }, forwardedRef) => {\n    const { t } = useTranslation(translationKey);\n    const [grabbed, setGrabbed] = useState(false);\n    const localRef = useRef<HTMLButtonElement>(null);\n    const ref = useComposedRefs(forwardedRef, localRef);\n    // Reordering moves the handle's element, which blurs it; the move must not count as leaving the handle.\n    const moving = useRef(false);\n\n    const move = (direction: DragMoveDirection, element: HTMLButtonElement) => {\n      moving.current = true;\n      onMove?.(direction);\n      announce(element.ownerDocument, t(`drag-handle.moved-${direction}.message`));\n      requestAnimationFrame(() => {\n        moving.current = false;\n        if (localRef.current?.isConnected && localRef.current !== localRef.current.ownerDocument.activeElement) {\n          localRef.current.focus();\n        }\n      });\n    };\n\n    const drop = (element: HTMLButtonElement) => {\n      setGrabbed(false);\n      announce(element.ownerDocument, t('drag-handle.dropped.message'));\n    };\n\n    const handleKeyDown = (event: KeyboardEvent<HTMLButtonElement>) => {\n      if (!onMove || disabled) {\n        return;\n      }\n      const direction = event.key === 'ArrowUp' ? 'up' : event.key === 'ArrowDown' ? 'down' : undefined;\n      if (direction && (event.altKey || grabbed)) {\n        event.preventDefault();\n        move(direction, event.currentTarget);\n      } else if (event.key === ' ' || event.key === 'Enter') {\n        event.preventDefault();\n        if (grabbed) {\n          drop(event.currentTarget);\n        } else {\n          setGrabbed(true);\n          announce(event.currentTarget.ownerDocument, t('drag-handle.grabbed.message'));\n        }\n      } else if (event.key === 'Escape' && grabbed) {\n        event.preventDefault();\n        drop(event.currentTarget);\n      }\n    };\n\n    return (\n      <ToolbarContext.Provider value={undefined}>\n        <Button\n          icon='ph--dots-six-vertical--regular'\n          label={label ?? t('drag-handle.label')}\n          iconOnly\n          showTooltip={false}\n          variant='ghost'\n          disabled={disabled}\n          tabIndex={onMove ? 0 : -1}\n          aria-roledescription={onMove ? t('drag-handle.role.label') : undefined}\n          aria-pressed={onMove ? grabbed : undefined}\n          data-drag-handle=''\n          data-grabbed={grabbed ? '' : undefined}\n          data-testid={testId}\n          onKeyDown={handleKeyDown}\n          onBlur={(event) => {\n            if (grabbed && !moving.current) {\n              drop(event.currentTarget);\n            }\n          }}\n          ref={ref}\n        />\n      </ToolbarContext.Provider>\n    );\n  },\n);\n\nDragHandle.displayName = 'DragHandle';\n\n//\n// DropIndicator\n//\n\ntype DropIndicatorProps = ThemedClassName<{\n  /** The side of the target row the dragged item will land on. */\n  edge: 'top' | 'bottom';\n}>;\n\n/**\n * A line on one edge of a drop target, drawn in the focus-ring colour (`--dx-drop-indicator-color`), absolutely placed\n * so it takes no grid track in a row Container; the row must be positioned (Listbox rows are).\n */\nexport const DropIndicator = ({ classNames, edge }: DropIndicatorProps) => (\n  // A span, so it may sit inside a row's text as well as beside it.\n  <span\n    aria-hidden='true'\n    data-scope='drop-indicator'\n    data-part='root'\n    data-edge={edge}\n    className={mx(recipes.dropIndicator(), classNames)}\n  />\n);\n\nDropIndicator.displayName = 'DropIndicator';\n\n//\n// DragPreview\n//\n\ntype DragPreviewProps = ThemedClassName<{\n  /** The dragged row, whose scope sets the preview's size and level (`dragScope`). */\n  source?: HTMLElement | null;\n  /** Overrides the size read from the source. */\n  size?: Size;\n  children?: React.ReactNode;\n}>;\n\n/**\n * A block-tall chip for pragmatic-drag-and-drop's custom native preview, which is portalled out of the row's sized\n * scope: it copies the source's `data-size` and level so it reads at the row's size on the row's surface.\n */\nexport const DragPreview = forwardRef<HTMLDivElement, DragPreviewProps>(\n  ({ classNames, source, size, children }, forwardedRef) => {\n    const scope = dragScope(source);\n    return (\n      <div\n        data-scope='drag-preview'\n        data-part='root'\n        data-size={size ?? scope.size}\n        data-surface={scope.surface ?? 'raised'}\n        className={mx(recipes.dragPreview(), classNames)}\n        ref={forwardedRef}\n      >\n        {children}\n      </div>\n    );\n  },\n);\n\nDragPreview.displayName = 'DragPreview';\n\nexport type { DragHandleProps, DragPreviewProps, DropIndicatorProps };\n\nexport { dragScope } from './drag.ts';\n"],"mappings":";;;;;;;;;;;;AAMA,IAAM,eAAe;;;;;AAMrB,IAAa,YAAY,KAAe,YAAoB;CAC1D,IAAI,SAAS,IAAI,eAAe,YAAY;CAC5C,IAAI,CAAC,QAAQ;EACX,SAAS,IAAI,cAAc,KAAK;EAChC,OAAO,KAAK;EACZ,OAAO,aAAa,QAAQ,QAAQ;EACpC,OAAO,aAAa,aAAa,QAAQ;EACzC,OAAO,YAAY;EACnB,IAAI,KAAK,OAAO,MAAM;CACxB;CACA,OAAO,cAAc;AACvB;;;;;AAMA,IAAa,aAAa,WAA2C;CACnE,MAAM,OAAO,QAAQ,QAAQ,aAAa,CAAC,EAAE,aAAa,WAAW;CACrE,OAAO;EACL,MAAM,MAAM,MAAM,cAAiC,cAAc,IAAI;EACrE,SAAS,QAAQ,QAAQ,4CAA0C,CAAC,EAAE,aAAa,cAAc,KAAK,KAAA;CACxG;AACF;;;;;;;;;;;;;;;ACWA,IAAa,aAAa,YACvB,EAAE,OAAO,QAAQ,UAAU,eAAe,UAAU,iBAAiB;CACpE,MAAM,EAAE,MAAM,eAAe,cAAc;CAC3C,MAAM,CAAC,SAAS,cAAc,SAAS,KAAK;CAC5C,MAAM,WAAW,OAA0B,IAAI;CAC/C,MAAM,MAAM,gBAAgB,cAAc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