Tabs.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import { ref, watch, computed, reactive, nextTick, onActivated, defineComponent, createVNode as _createVNode } from "vue";
  2. import { isDef, addUnit, isHidden, unitToPx, truthProp, numericProp, windowWidth, getElementTop, makeStringProp, callInterceptor, createNamespace, makeNumericProp, setRootScrollTop, BORDER_TOP_BOTTOM } from "../utils/index.mjs";
  3. import { scrollLeftTo, scrollTopTo } from "./utils.mjs";
  4. import { useRect, useChildren, useScrollParent, useEventListener, onMountedOrActivated } from "@vant/use";
  5. import { useId } from "../composables/use-id.mjs";
  6. import { route } from "../composables/use-route.mjs";
  7. import { useRefs } from "../composables/use-refs.mjs";
  8. import { useExpose } from "../composables/use-expose.mjs";
  9. import { onPopupReopen } from "../composables/on-popup-reopen.mjs";
  10. import { useVisibilityChange } from "../composables/use-visibility-change.mjs";
  11. import { Sticky } from "../sticky/index.mjs";
  12. import TabsContent from "./TabsContent.mjs";
  13. const [name, bem] = createNamespace("tabs");
  14. const tabsProps = {
  15. type: makeStringProp("line"),
  16. color: String,
  17. border: Boolean,
  18. sticky: Boolean,
  19. shrink: Boolean,
  20. active: makeNumericProp(0),
  21. duration: makeNumericProp(0.3),
  22. animated: Boolean,
  23. ellipsis: truthProp,
  24. swipeable: Boolean,
  25. scrollspy: Boolean,
  26. offsetTop: makeNumericProp(0),
  27. background: String,
  28. lazyRender: truthProp,
  29. showHeader: truthProp,
  30. lineWidth: numericProp,
  31. lineHeight: numericProp,
  32. beforeChange: Function,
  33. swipeThreshold: makeNumericProp(5),
  34. titleActiveColor: String,
  35. titleInactiveColor: String
  36. };
  37. const TABS_KEY = Symbol(name);
  38. var stdin_default = defineComponent({
  39. name,
  40. props: tabsProps,
  41. emits: ["change", "scroll", "rendered", "clickTab", "update:active"],
  42. setup(props, {
  43. emit,
  44. slots
  45. }) {
  46. let tabHeight;
  47. let lockScroll;
  48. let stickyFixed;
  49. let cancelScrollLeftToRaf;
  50. let cancelScrollTopToRaf;
  51. const root = ref();
  52. const navRef = ref();
  53. const wrapRef = ref();
  54. const contentRef = ref();
  55. const id = useId();
  56. const scroller = useScrollParent(root);
  57. const [titleRefs, setTitleRefs] = useRefs();
  58. const {
  59. children,
  60. linkChildren
  61. } = useChildren(TABS_KEY);
  62. const state = reactive({
  63. inited: false,
  64. position: "",
  65. lineStyle: {},
  66. currentIndex: -1
  67. });
  68. const scrollable = computed(() => children.length > +props.swipeThreshold || !props.ellipsis || props.shrink);
  69. const navStyle = computed(() => ({
  70. borderColor: props.color,
  71. background: props.background
  72. }));
  73. const getTabName = (tab, index) => {
  74. var _a;
  75. return (_a = tab.name) != null ? _a : index;
  76. };
  77. const currentName = computed(() => {
  78. const activeTab = children[state.currentIndex];
  79. if (activeTab) {
  80. return getTabName(activeTab, state.currentIndex);
  81. }
  82. });
  83. const offsetTopPx = computed(() => unitToPx(props.offsetTop));
  84. const scrollOffset = computed(() => {
  85. if (props.sticky) {
  86. return offsetTopPx.value + tabHeight;
  87. }
  88. return 0;
  89. });
  90. const scrollIntoView = (immediate) => {
  91. const nav = navRef.value;
  92. const titles = titleRefs.value;
  93. if (!scrollable.value || !nav || !titles || !titles[state.currentIndex]) {
  94. return;
  95. }
  96. const title = titles[state.currentIndex].$el;
  97. const to = title.offsetLeft - (nav.offsetWidth - title.offsetWidth) / 2;
  98. if (cancelScrollLeftToRaf) cancelScrollLeftToRaf();
  99. cancelScrollLeftToRaf = scrollLeftTo(nav, to, immediate ? 0 : +props.duration);
  100. };
  101. const setLine = () => {
  102. const shouldAnimate = state.inited;
  103. nextTick(() => {
  104. const titles = titleRefs.value;
  105. if (!titles || !titles[state.currentIndex] || props.type !== "line" || isHidden(root.value)) {
  106. return;
  107. }
  108. const title = titles[state.currentIndex].$el;
  109. const {
  110. lineWidth,
  111. lineHeight
  112. } = props;
  113. const left = title.offsetLeft + title.offsetWidth / 2;
  114. const lineStyle = {
  115. width: addUnit(lineWidth),
  116. backgroundColor: props.color,
  117. transform: `translateX(${left}px) translateX(-50%)`
  118. };
  119. if (shouldAnimate) {
  120. lineStyle.transitionDuration = `${props.duration}s`;
  121. }
  122. if (isDef(lineHeight)) {
  123. const height = addUnit(lineHeight);
  124. lineStyle.height = height;
  125. lineStyle.borderRadius = height;
  126. }
  127. state.lineStyle = lineStyle;
  128. });
  129. };
  130. const findAvailableTab = (index) => {
  131. const diff = index < state.currentIndex ? -1 : 1;
  132. while (index >= 0 && index < children.length) {
  133. if (!children[index].disabled) {
  134. return index;
  135. }
  136. index += diff;
  137. }
  138. };
  139. const setCurrentIndex = (currentIndex, skipScrollIntoView) => {
  140. const newIndex = findAvailableTab(currentIndex);
  141. if (!isDef(newIndex)) {
  142. return;
  143. }
  144. const newTab = children[newIndex];
  145. const newName = getTabName(newTab, newIndex);
  146. const shouldEmitChange = state.currentIndex !== null;
  147. if (state.currentIndex !== newIndex) {
  148. state.currentIndex = newIndex;
  149. if (!skipScrollIntoView) {
  150. scrollIntoView();
  151. }
  152. setLine();
  153. }
  154. if (newName !== props.active) {
  155. emit("update:active", newName);
  156. if (shouldEmitChange) {
  157. emit("change", newName, newTab.title);
  158. }
  159. }
  160. if (stickyFixed && !props.scrollspy) {
  161. setRootScrollTop(Math.ceil(getElementTop(root.value) - offsetTopPx.value));
  162. }
  163. };
  164. const setCurrentIndexByName = (name2, skipScrollIntoView) => {
  165. const matched = children.find((tab, index2) => getTabName(tab, index2) === name2);
  166. const index = matched ? children.indexOf(matched) : 0;
  167. setCurrentIndex(index, skipScrollIntoView);
  168. };
  169. const scrollToCurrentContent = (immediate = false) => {
  170. if (props.scrollspy) {
  171. const target = children[state.currentIndex].$el;
  172. if (target && scroller.value) {
  173. const to = getElementTop(target, scroller.value) - scrollOffset.value;
  174. lockScroll = true;
  175. if (cancelScrollTopToRaf) cancelScrollTopToRaf();
  176. cancelScrollTopToRaf = scrollTopTo(scroller.value, to, immediate ? 0 : +props.duration, () => {
  177. lockScroll = false;
  178. });
  179. }
  180. }
  181. };
  182. const onClickTab = (item, index, event) => {
  183. const {
  184. title,
  185. disabled
  186. } = children[index];
  187. const name2 = getTabName(children[index], index);
  188. if (!disabled) {
  189. callInterceptor(props.beforeChange, {
  190. args: [name2],
  191. done: () => {
  192. setCurrentIndex(index);
  193. scrollToCurrentContent();
  194. }
  195. });
  196. route(item);
  197. }
  198. emit("clickTab", {
  199. name: name2,
  200. title,
  201. event,
  202. disabled
  203. });
  204. };
  205. const onStickyScroll = (params) => {
  206. stickyFixed = params.isFixed;
  207. emit("scroll", params);
  208. };
  209. const scrollTo = (name2) => {
  210. nextTick(() => {
  211. setCurrentIndexByName(name2);
  212. scrollToCurrentContent(true);
  213. });
  214. };
  215. const getCurrentIndexOnScroll = () => {
  216. for (let index = 0; index < children.length; index++) {
  217. const {
  218. top
  219. } = useRect(children[index].$el);
  220. if (top > scrollOffset.value) {
  221. return index === 0 ? 0 : index - 1;
  222. }
  223. }
  224. return children.length - 1;
  225. };
  226. const onScroll = () => {
  227. if (props.scrollspy && !lockScroll) {
  228. const index = getCurrentIndexOnScroll();
  229. setCurrentIndex(index);
  230. }
  231. };
  232. const renderLine = () => {
  233. if (props.type === "line" && children.length) {
  234. return _createVNode("div", {
  235. "class": bem("line"),
  236. "style": state.lineStyle
  237. }, null);
  238. }
  239. };
  240. const renderHeader = () => {
  241. var _a, _b, _c;
  242. const {
  243. type,
  244. border,
  245. sticky
  246. } = props;
  247. const Header = [_createVNode("div", {
  248. "ref": sticky ? void 0 : wrapRef,
  249. "class": [bem("wrap"), {
  250. [BORDER_TOP_BOTTOM]: type === "line" && border
  251. }]
  252. }, [_createVNode("div", {
  253. "ref": navRef,
  254. "role": "tablist",
  255. "class": bem("nav", [type, {
  256. shrink: props.shrink,
  257. complete: scrollable.value
  258. }]),
  259. "style": navStyle.value,
  260. "aria-orientation": "horizontal"
  261. }, [(_a = slots["nav-left"]) == null ? void 0 : _a.call(slots), children.map((item) => item.renderTitle(onClickTab)), renderLine(), (_b = slots["nav-right"]) == null ? void 0 : _b.call(slots)])]), (_c = slots["nav-bottom"]) == null ? void 0 : _c.call(slots)];
  262. if (sticky) {
  263. return _createVNode("div", {
  264. "ref": wrapRef
  265. }, [Header]);
  266. }
  267. return Header;
  268. };
  269. const resize = () => {
  270. setLine();
  271. nextTick(() => {
  272. var _a, _b;
  273. scrollIntoView(true);
  274. (_b = (_a = contentRef.value) == null ? void 0 : _a.swipeRef.value) == null ? void 0 : _b.resize();
  275. });
  276. };
  277. watch(() => [props.color, props.duration, props.lineWidth, props.lineHeight], setLine);
  278. watch(windowWidth, resize);
  279. watch(() => props.active, (value) => {
  280. if (value !== currentName.value) {
  281. setCurrentIndexByName(value);
  282. }
  283. });
  284. watch(() => children.length, () => {
  285. if (state.inited) {
  286. setCurrentIndexByName(props.active);
  287. setLine();
  288. nextTick(() => {
  289. scrollIntoView(true);
  290. });
  291. }
  292. });
  293. const init = () => {
  294. setCurrentIndexByName(props.active, true);
  295. nextTick(() => {
  296. state.inited = true;
  297. if (wrapRef.value) {
  298. tabHeight = useRect(wrapRef.value).height;
  299. }
  300. scrollIntoView(true);
  301. });
  302. };
  303. const onRendered = (name2, title) => emit("rendered", name2, title);
  304. useExpose({
  305. resize,
  306. scrollTo
  307. });
  308. onActivated(setLine);
  309. onPopupReopen(setLine);
  310. onMountedOrActivated(init);
  311. useVisibilityChange(root, setLine);
  312. useEventListener("scroll", onScroll, {
  313. target: scroller,
  314. passive: true
  315. });
  316. linkChildren({
  317. id,
  318. props,
  319. setLine,
  320. scrollable,
  321. onRendered,
  322. currentName,
  323. setTitleRefs,
  324. scrollIntoView
  325. });
  326. return () => _createVNode("div", {
  327. "ref": root,
  328. "class": bem([props.type])
  329. }, [props.showHeader ? props.sticky ? _createVNode(Sticky, {
  330. "container": root.value,
  331. "offsetTop": offsetTopPx.value,
  332. "onScroll": onStickyScroll
  333. }, {
  334. default: () => [renderHeader()]
  335. }) : renderHeader() : null, _createVNode(TabsContent, {
  336. "ref": contentRef,
  337. "count": children.length,
  338. "inited": state.inited,
  339. "animated": props.animated,
  340. "duration": props.duration,
  341. "swipeable": props.swipeable,
  342. "lazyRender": props.lazyRender,
  343. "currentIndex": state.currentIndex,
  344. "onChange": setCurrentIndex
  345. }, {
  346. default: () => {
  347. var _a;
  348. return [(_a = slots.default) == null ? void 0 : _a.call(slots)];
  349. }
  350. })]);
  351. }
  352. });
  353. export {
  354. TABS_KEY,
  355. stdin_default as default,
  356. tabsProps
  357. };