IndexAnchor.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. indexAnchorProps: () => indexAnchorProps
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_vue2 = require("vue");
  26. var import_utils = require("../utils");
  27. var import_IndexBar = require("../index-bar/IndexBar");
  28. var import_dom = require("../utils/dom");
  29. var import_use = require("@vant/use");
  30. var import_use_expose = require("../composables/use-expose");
  31. const [name, bem] = (0, import_utils.createNamespace)("index-anchor");
  32. const indexAnchorProps = {
  33. index: import_utils.numericProp
  34. };
  35. var stdin_default = (0, import_vue2.defineComponent)({
  36. name,
  37. props: indexAnchorProps,
  38. setup(props, {
  39. slots
  40. }) {
  41. const state = (0, import_vue2.reactive)({
  42. top: 0,
  43. left: null,
  44. rect: {
  45. top: 0,
  46. height: 0
  47. },
  48. width: null,
  49. active: false
  50. });
  51. const root = (0, import_vue2.ref)();
  52. const {
  53. parent
  54. } = (0, import_use.useParent)(import_IndexBar.INDEX_BAR_KEY);
  55. if (!parent) {
  56. if (process.env.NODE_ENV !== "production") {
  57. console.error("[Vant] <IndexAnchor> must be a child component of <IndexBar>.");
  58. }
  59. return;
  60. }
  61. const isSticky = () => state.active && parent.props.sticky;
  62. const anchorStyle = (0, import_vue2.computed)(() => {
  63. const {
  64. zIndex,
  65. highlightColor
  66. } = parent.props;
  67. if (isSticky()) {
  68. return (0, import_utils.extend)((0, import_utils.getZIndexStyle)(zIndex), {
  69. left: state.left ? `${state.left}px` : void 0,
  70. width: state.width ? `${state.width}px` : void 0,
  71. transform: state.top ? `translate3d(0, ${state.top}px, 0)` : void 0,
  72. color: highlightColor
  73. });
  74. }
  75. });
  76. const getRect = (scrollParent, scrollParentRect) => {
  77. const rootRect = (0, import_use.useRect)(root);
  78. state.rect.height = rootRect.height;
  79. if (scrollParent === window || scrollParent === document.body) {
  80. state.rect.top = rootRect.top + (0, import_dom.getRootScrollTop)();
  81. } else {
  82. state.rect.top = rootRect.top + (0, import_dom.getScrollTop)(scrollParent) - scrollParentRect.top;
  83. }
  84. return state.rect;
  85. };
  86. (0, import_use_expose.useExpose)({
  87. state,
  88. getRect
  89. });
  90. return () => {
  91. const sticky = isSticky();
  92. return (0, import_vue.createVNode)("div", {
  93. "ref": root,
  94. "style": {
  95. height: sticky ? `${state.rect.height}px` : void 0
  96. }
  97. }, [(0, import_vue.createVNode)("div", {
  98. "style": anchorStyle.value,
  99. "class": [bem({
  100. sticky
  101. }), {
  102. [import_utils.BORDER_BOTTOM]: sticky
  103. }]
  104. }, [slots.default ? slots.default() : props.index])]);
  105. };
  106. }
  107. });