vue-router
Version:
118 lines (117 loc) • 4.25 kB
JavaScript
/*!
* vue-router v5.5.3
* (c) 2026 Eduardo San Martin Morote
* @license MIT
*/
import { i as routerViewLocationKey, r as routerKey, s as diagnostics, y as noop } from "./injectionSymbols-CGHHjbrE.js";
import { O as isBrowser, d as isSameRouteLocationParams, i as shouldHandleLinkClick, n as includesParams, o as isRouteLocation, r as preferSingleVNode } from "./RouterLink-TyasWdjU.js";
import { computed, defineComponent, getCurrentInstance, h, inject, toValue, watchEffect } from "vue";
// src/experimental/RouterLink.ts
const orig = (r) => r && (r.aliasOf || r);
const indexOfRecord = (matched, record) => matched.findIndex((r) => orig(r) === orig(record));
/**
* Returns the behavior of a {@link RouterLink} without the rendering part.
* Active states compare against the route of the closest `RouterView`.
*/
function useLink(options) {
const router = inject(routerKey);
const currentRoute = inject(routerViewLocationKey);
const route = computed(() => {
const to = toValue(options.to);
if (process.env.NODE_ENV !== "production" && !isRouteLocation(to)) diagnostics.VUE_ROUTER_R0050({ to });
return router.resolve(to);
});
const state = computed(() => {
const target = route.value;
const matched = target.matched;
const current = currentRoute.value;
const currentMatched = current.matched;
const record = matched.at(-1);
if (!record) return 0;
let i = indexOfRecord(currentMatched, record);
const parent = matched.at(-2);
if (i < 0 && parent && orig(record).path === orig(parent).path && orig(currentMatched.at(-1))?.path !== orig(parent).path) i = indexOfRecord(currentMatched, parent);
return i < 0 || !includesParams(current.params, target.params) ? 0 : i === currentMatched.length - 1 && isSameRouteLocationParams(current.params, target.params) ? 2 : 1;
});
const isActive = computed(() => state.value > 0);
const isExactActive = computed(() => state.value > 1);
function navigate(e = {}) {
if (shouldHandleLinkClick(e)) {
e.preventDefault?.();
const p = router[toValue(options.replace) ? "replace" : "push"](toValue(options.to)).catch(noop);
if (toValue(options.viewTransition) && typeof document !== "undefined" && "startViewTransition" in document) document.startViewTransition(() => p);
return p;
}
return Promise.resolve();
}
if ((process.env.NODE_ENV !== "production" || __VUE_PROD_DEVTOOLS__) && isBrowser) {
const instance = getCurrentInstance();
if (instance) {
const ctx = {};
(instance.__vrl_devtools ||= []).push(ctx);
watchEffect(() => {
ctx.route = route.value;
ctx.isActive = isActive.value;
ctx.isExactActive = isExactActive.value;
ctx.error = isRouteLocation(toValue(options.to)) ? null : "Invalid \"to\" value";
}, { flush: "post" });
}
}
return {
route,
href: computed(() => route.value.href),
isActive,
isExactActive,
navigate
};
}
/**
* Component to render a link that triggers a navigation on click. Made for
* `experimental_createRouter()`.
*/
const RouterLink = /* @__PURE__ */ defineComponent({
name: "RouterLink",
props: {
to: {
type: [String, Object],
required: true
},
replace: Boolean,
activeClass: String,
exactActiveClass: String,
custom: Boolean,
ariaCurrentValue: {
type: String,
default: "page"
},
viewTransition: Boolean
},
useLink,
setup(props, { slots }) {
if (process.env.NODE_ENV !== "production" && props.custom) diagnostics.VUE_ROUTER_D0003();
const { route, isActive, isExactActive, navigate } = useLink(props);
const { options } = inject(routerKey);
return () => {
const r = route.value;
const active = isActive.value;
const exact = isExactActive.value;
const children = slots.default && preferSingleVNode(slots.default({
route: r,
href: r.href,
isActive: active,
isExactActive: exact,
navigate
}));
return props.custom ? children : h("a", {
"aria-current": exact ? props.ariaCurrentValue : null,
href: r.href,
onClick: navigate,
class: {
[props.activeClass ?? options.linkActiveClass ?? "router-link-active"]: active,
[props.exactActiveClass ?? options.linkExactActiveClass ?? "router-link-exact-active"]: exact
}
}, children);
};
}
});
export { useLink as n, RouterLink as t };