Tabs.js 13 KB

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