anchor-link2.mjs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { defineComponent, ref, inject, computed, watch, nextTick, onMounted, onBeforeUnmount, openBlock, createElementBlock, normalizeClass, unref, createElementVNode, renderSlot, createTextVNode, toDisplayString, createCommentVNode } from 'vue';
  2. import { anchorLinkProps } from './anchor-link.mjs';
  3. import { anchorKey } from './constants.mjs';
  4. import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
  5. const __default__ = defineComponent({
  6. name: "ElAnchorLink"
  7. });
  8. const _sfc_main = /* @__PURE__ */ defineComponent({
  9. ...__default__,
  10. props: anchorLinkProps,
  11. setup(__props) {
  12. const props = __props;
  13. const linkRef = ref(null);
  14. const {
  15. ns,
  16. direction,
  17. currentAnchor,
  18. addLink,
  19. removeLink,
  20. handleClick: contextHandleClick
  21. } = inject(anchorKey);
  22. const cls = computed(() => [
  23. ns.e("link"),
  24. ns.is("active", currentAnchor.value === props.href)
  25. ]);
  26. const handleClick = (e) => {
  27. contextHandleClick(e, props.href);
  28. };
  29. watch(() => props.href, (val, oldVal) => {
  30. nextTick(() => {
  31. if (oldVal)
  32. removeLink(oldVal);
  33. if (val) {
  34. addLink({
  35. href: val,
  36. el: linkRef.value
  37. });
  38. }
  39. });
  40. });
  41. onMounted(() => {
  42. const { href } = props;
  43. if (href) {
  44. addLink({
  45. href,
  46. el: linkRef.value
  47. });
  48. }
  49. });
  50. onBeforeUnmount(() => {
  51. const { href } = props;
  52. if (href) {
  53. removeLink(href);
  54. }
  55. });
  56. return (_ctx, _cache) => {
  57. return openBlock(), createElementBlock("div", {
  58. class: normalizeClass(unref(ns).e("item"))
  59. }, [
  60. createElementVNode("a", {
  61. ref_key: "linkRef",
  62. ref: linkRef,
  63. class: normalizeClass(unref(cls)),
  64. href: _ctx.href,
  65. onClick: handleClick
  66. }, [
  67. renderSlot(_ctx.$slots, "default", {}, () => [
  68. createTextVNode(toDisplayString(_ctx.title), 1)
  69. ])
  70. ], 10, ["href"]),
  71. _ctx.$slots["sub-link"] && unref(direction) === "vertical" ? (openBlock(), createElementBlock("div", {
  72. key: 0,
  73. class: normalizeClass(unref(ns).e("list"))
  74. }, [
  75. renderSlot(_ctx.$slots, "sub-link")
  76. ], 2)) : createCommentVNode("v-if", true)
  77. ], 2);
  78. };
  79. }
  80. });
  81. var AnchorLink = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "anchor-link.vue"]]);
  82. export { AnchorLink as default };
  83. //# sourceMappingURL=anchor-link2.mjs.map