IndexAnchor.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => stdin_default
  21. });
  22. module.exports = __toCommonJS(stdin_exports);
  23. var import_vue = require("vue");
  24. var import_vue2 = require("vue");
  25. var import_utils = require("../utils");
  26. var import_IndexBar = require("../index-bar/IndexBar");
  27. var import_dom = require("../utils/dom");
  28. var import_use = require("@vant/use");
  29. var import_use_expose = require("../composables/use-expose");
  30. const [name, bem] = (0, import_utils.createNamespace)("index-anchor");
  31. const indexAnchorProps = {
  32. index: import_utils.numericProp
  33. };
  34. var stdin_default = (0, import_vue2.defineComponent)({
  35. name,
  36. props: indexAnchorProps,
  37. setup(props, {
  38. slots
  39. }) {
  40. const state = (0, import_vue2.reactive)({
  41. top: 0,
  42. left: null,
  43. rect: {
  44. top: 0,
  45. height: 0
  46. },
  47. width: null,
  48. active: false
  49. });
  50. const root = (0, import_vue2.ref)();
  51. const {
  52. parent
  53. } = (0, import_use.useParent)(import_IndexBar.INDEX_BAR_KEY);
  54. if (!parent) {
  55. if (process.env.NODE_ENV !== "production") {
  56. console.error("[Vant] <IndexAnchor> must be a child component of <IndexBar>.");
  57. }
  58. return;
  59. }
  60. const isSticky = () => state.active && parent.props.sticky;
  61. const anchorStyle = (0, import_vue2.computed)(() => {
  62. const {
  63. zIndex,
  64. highlightColor
  65. } = parent.props;
  66. if (isSticky()) {
  67. return (0, import_utils.extend)((0, import_utils.getZIndexStyle)(zIndex), {
  68. left: state.left ? `${state.left}px` : void 0,
  69. width: state.width ? `${state.width}px` : void 0,
  70. transform: state.top ? `translate3d(0, ${state.top}px, 0)` : void 0,
  71. color: highlightColor
  72. });
  73. }
  74. });
  75. const getRect = (scrollParent, scrollParentRect) => {
  76. const rootRect = (0, import_use.useRect)(root);
  77. state.rect.height = rootRect.height;
  78. if (scrollParent === window || scrollParent === document.body) {
  79. state.rect.top = rootRect.top + (0, import_dom.getRootScrollTop)();
  80. } else {
  81. state.rect.top = rootRect.top + (0, import_dom.getScrollTop)(scrollParent) - scrollParentRect.top;
  82. }
  83. return state.rect;
  84. };
  85. (0, import_use_expose.useExpose)({
  86. state,
  87. getRect
  88. });
  89. return () => {
  90. const sticky = isSticky();
  91. return (0, import_vue.createVNode)("div", {
  92. "ref": root,
  93. "style": {
  94. height: sticky ? `${state.rect.height}px` : void 0
  95. }
  96. }, [(0, import_vue.createVNode)("div", {
  97. "style": anchorStyle.value,
  98. "class": [bem({
  99. sticky
  100. }), {
  101. [import_utils.BORDER_BOTTOM]: sticky
  102. }]
  103. }, [slots.default ? slots.default() : props.index])]);
  104. };
  105. }
  106. });