vue-router
Version:
127 lines (126 loc) • 4.49 kB
JavaScript
/*!
* vue-router v5.5.3
* (c) 2026 Eduardo San Martin Morote
* @license MIT
*/
import { O as isBrowser } from "../RouterLink-TyasWdjU.js";
import { f as assign, r as routerKey, s as diagnostics } from "../injectionSymbols-CGHHjbrE.js";
import { n as useLink } from "../RouterLink-DWhhQwUD.js";
import { n as routerViewProps, r as useRouterViewState } from "../RouterView-CtWCmpOu.js";
import { computed, createComponent, createDynamicComponent, createSlot, createTemplateRefSetter, defineVaporComponent, inject, isVaporComponent, nextTick, onActivated, onMounted, onUnmounted, reactive, renderEffect, setDynamicProps, setInsertionState, template, watch } from "vue";
// src/experimental/vapor/RouterLink.ts
const t0 = /*#__PURE__*/ template("<a>", 1);
/**
* Vapor version of the experimental `RouterLink`. Renders a link that
* triggers a navigation on click.
*/
const RouterLink = /* @__PURE__ */ defineVaporComponent({
name: "RouterLink",
inheritAttrs: false,
props: {
to: {
type: [String, Object],
required: true
},
replace: Boolean,
activeClass: String,
exactActiveClass: String,
ariaCurrentValue: {
type: String,
default: "page"
},
viewTransition: Boolean
},
useLink,
setup(props, { attrs }) {
if (process.env.NODE_ENV !== "production" && "custom" in attrs) diagnostics.VUE_ROUTER_R0051();
const link = reactive(useLink(props));
const { options } = inject(routerKey);
const a = t0();
renderEffect(() => setDynamicProps(a, [{
"aria-current": link.isExactActive ? props.ariaCurrentValue : null,
href: link.href,
onClick: link.navigate,
class: {
[props.activeClass ?? options.linkActiveClass ?? "router-link-active"]: link.isActive,
[props.exactActiveClass ?? options.linkExactActiveClass ?? "router-link-exact-active"]: link.isExactActive
}
}, attrs]));
setInsertionState(a);
createSlot("default", { $: [() => link] });
return a;
}
});
/**
* Vapor version of `RouterView`. Displays the component of the current route.
*/
const RouterView = /* @__PURE__ */ defineVaporComponent({
name: "RouterView",
inheritAttrs: false,
props: routerViewProps,
setup(props, { attrs, slots }) {
const [routeToDisplay, matchedRouteRef, viewRef, settle, depth] = useRouterViewState(props);
if (__VUE_OPTIONS_API__) watch(() => [
viewRef.value,
matchedRouteRef.value,
props.name
], ([instance, record, name]) => {
if (record && instance && instance.$ && record.instances[name] !== instance) {
record.instances[name] = instance;
onUnmounted(() => {
if (record.instances[name] === instance) record.instances[name] = null;
}, instance.$);
}
}, { flush: "post" });
const ViewComponent = computed(() => {
const matchedRoute = matchedRouteRef.value;
return matchedRoute && matchedRoute.components[props.name];
});
const setRef = createTemplateRefSetter();
let view;
const settleView = () => {
const route = routeToDisplay.value;
nextTick(() => {
if (view && (!isVaporComponent(view) || view.isMounted && !view.isDeactivated)) settle(route);
});
};
watch(routeToDisplay, settleView, { flush: "post" });
const slotProps = {
get Component() {
const Component = ViewComponent.value;
if (!Component) return view = void 0;
if (!view || view.type !== Component || view.isMounted) {
view = createComponent(Component, { $: [() => {
const route = routeToDisplay.value;
const routePropsOption = matchedRouteRef.value.props[props.name];
return assign({}, routePropsOption ? routePropsOption === true ? route.params : typeof routePropsOption === "function" ? routePropsOption(route) : routePropsOption : null, attrs);
}] });
setRef(view, viewRef);
if (!isVaporComponent(view)) settleView();
else if (!view.isMounted) {
const instance = view;
onMounted(() => {
if ((process.env.NODE_ENV !== "production" || __VUE_PROD_DEVTOOLS__) && isBrowser) {
const record = matchedRouteRef.value;
instance.__vrv_devtools = {
depth: depth.value,
name: record.name,
path: record.path,
meta: record.meta
};
}
settleView();
}, instance);
onActivated(() => settleView(), instance);
}
}
return view;
},
get route() {
return routeToDisplay.value;
}
};
return slots.default ? slots.default(slotProps) : createDynamicComponent(() => slotProps.Component);
}
});
export { RouterLink, RouterView };