{"version":3,"sources":["../src/use-select-list.ts"],"sourcesContent":["import type { PropGetter } from \"@yamada-ui/core\"\nimport type { MotionProps } from \"@yamada-ui/motion\"\nimport { ariaAttr, handlerAll, isArray, mergeRefs } from \"@yamada-ui/utils\"\nimport { useCallback, useEffect, useRef } from \"react\"\nimport { useSelectContext, useSelectDescendantsContext } from \"./use-select\"\n\nexport const useSelectList = () => {\n  const { focusedIndex, open, value } = useSelectContext()\n  const descendants = useSelectDescendantsContext()\n  const listRef = useRef<HTMLDivElement>(null)\n  const beforeFocusedIndex = useRef<number>(-1)\n  const selectedValue = descendants.value(focusedIndex)\n  const multi = isArray(value)\n\n  const onAnimationComplete = useCallback(() => {\n    if (!open) listRef.current?.scrollTo({ top: 0 })\n  }, [open])\n\n  useEffect(() => {\n    if (!listRef.current || !selectedValue) return\n\n    if (beforeFocusedIndex.current === selectedValue.index) return\n\n    const parent = listRef.current\n    const child = selectedValue.node\n\n    const parentHeight = parent.clientHeight\n    const viewTop = parent.scrollTop\n    const viewBottom = viewTop + parentHeight\n\n    const childHeight = child.clientHeight\n    const childTop = child.offsetTop\n    const childBottom = childTop + childHeight\n\n    const inView = viewTop <= childTop && childBottom <= viewBottom\n\n    const scrollBottom = beforeFocusedIndex.current < selectedValue.index\n\n    if (!inView) {\n      if (childBottom <= parentHeight) {\n        listRef.current.scrollTo({ top: 0 })\n      } else {\n        if (!scrollBottom) {\n          listRef.current.scrollTo({ top: childTop + 1 })\n        } else {\n          listRef.current.scrollTo({ top: childBottom - parentHeight })\n        }\n      }\n    }\n\n    beforeFocusedIndex.current = selectedValue.index\n  }, [listRef, selectedValue])\n\n  const getContainerProps: PropGetter<MotionProps, MotionProps> = useCallback(\n    (props = {}, ref = null) => ({\n      ref,\n      \"aria-multiselectable\": ariaAttr(multi),\n      role: \"listbox\",\n      ...props,\n      onAnimationComplete: handlerAll(\n        props.onAnimationComplete,\n        onAnimationComplete,\n      ),\n    }),\n    [multi, onAnimationComplete],\n  )\n\n  const getListProps: PropGetter = useCallback(\n    (props = {}, ref = null) => ({\n      ref: mergeRefs(listRef, ref),\n      position: \"relative\",\n      tabIndex: -1,\n      ...props,\n    }),\n    [listRef],\n  )\n\n  return {\n    getContainerProps,\n    getListProps,\n  }\n}\n\nexport type UseSelectListReturn = ReturnType<typeof useSelectList>\n"],"mappings":";;;;;;;AAEA,SAAS,UAAU,YAAY,SAAS,iBAAiB;AACzD,SAAS,aAAa,WAAW,cAAc;AAGxC,IAAM,gBAAgB,MAAM;AACjC,QAAM,EAAE,cAAc,MAAM,MAAM,IAAI,iBAAiB;AACvD,QAAM,cAAc,4BAA4B;AAChD,QAAM,UAAU,OAAuB,IAAI;AAC3C,QAAM,qBAAqB,OAAe,EAAE;AAC5C,QAAM,gBAAgB,YAAY,MAAM,YAAY;AACpD,QAAM,QAAQ,QAAQ,KAAK;AAE3B,QAAM,sBAAsB,YAAY,MAAM;AAdhD;AAeI,QAAI,CAAC,KAAM,eAAQ,YAAR,mBAAiB,SAAS,EAAE,KAAK,EAAE;AAAA,EAChD,GAAG,CAAC,IAAI,CAAC;AAET,YAAU,MAAM;AACd,QAAI,CAAC,QAAQ,WAAW,CAAC,cAAe;AAExC,QAAI,mBAAmB,YAAY,cAAc,MAAO;AAExD,UAAM,SAAS,QAAQ;AACvB,UAAM,QAAQ,cAAc;AAE5B,UAAM,eAAe,OAAO;AAC5B,UAAM,UAAU,OAAO;AACvB,UAAM,aAAa,UAAU;AAE7B,UAAM,cAAc,MAAM;AAC1B,UAAM,WAAW,MAAM;AACvB,UAAM,cAAc,WAAW;AAE/B,UAAM,SAAS,WAAW,YAAY,eAAe;AAErD,UAAM,eAAe,mBAAmB,UAAU,cAAc;AAEhE,QAAI,CAAC,QAAQ;AACX,UAAI,eAAe,cAAc;AAC/B,gBAAQ,QAAQ,SAAS,EAAE,KAAK,EAAE,CAAC;AAAA,MACrC,OAAO;AACL,YAAI,CAAC,cAAc;AACjB,kBAAQ,QAAQ,SAAS,EAAE,KAAK,WAAW,EAA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