Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 16x 16x 16x 16x 17x 16x 16x 16x 16x 16x 16x 16x 17x 1x | import type { RouteLayerOptions, RouteLocationInput, RouteMatchType, RouterLinkType } from '@esmx/router'; import { type PropType, defineComponent, h } from 'vue'; import { useLink } from './use'; import { isVue3 } from './util'; /** * RouterLink component for navigation. * Renders an anchor tag with proper navigation behavior and active state management. * * @param props - Component properties * @param props.to - Target route location to navigate to * @param props.type - Navigation type ('push' | 'replace' | 'pushWindow' | 'replaceWindow' | 'pushLayer') * @param props.replace - Use type='replace' instead * @param props.exact - How to match the active state ('include' | 'exact' | 'route') * @param props.activeClass - CSS class to apply when link is active * @param props.event - Event(s) that trigger navigation * @param props.tag - Custom tag to render instead of 'a' * @param props.layerOptions - Layer options for layer-based navigation * @param slots - Component slots * @param slots.default - Default slot content * @returns Vue component instance * * @example * ```vue * <template> * <nav> * <!-- Basic navigation --> * <RouterLink to="/home">Home</RouterLink> * <RouterLink to="/about">About</RouterLink> * * <!-- With custom styling --> * <RouterLink * to="/dashboard" * active-class="nav-active" * > * Dashboard * </RouterLink> * * <!-- Replace navigation --> * <RouterLink to="/login" type="replace">Login</RouterLink> * * <!-- Custom tag and exact matching --> * <RouterLink * to="/contact" * exact="exact" * tag="button" * class="btn" * > * Contact * </RouterLink> * </nav> * </template> * ``` */ export const RouterLink = defineComponent({ name: 'RouterLink', props: { /** * Target route location to navigate to. * Can be a string path or route location object. * @example '/home' | { path: '/user', query: { id: '123' } } */ to: { type: [String, Object] as PropType<RouteLocationInput>, required: true }, /** * Navigation type for the link. * @default 'push' * @example 'push' | 'replace' | 'pushWindow' | 'replaceWindow' | 'pushLayer' */ type: { type: String as PropType<RouterLinkType>, default: 'push' }, /** * @deprecated Use 'type="replace"' instead * @example replace={true} → type="replace" */ replace: { type: Boolean, default: false }, /** * How to match the active state. * - 'include': Match if current route includes this path * - 'exact': Match only if routes are exactly the same * - 'route': Match based on route configuration * @default 'include' */ exact: { type: String as PropType<RouteMatchType>, default: 'include' }, /** * CSS class to apply when link is active (route matches). * @example 'nav-active' | 'selected' */ activeClass: { type: String }, /** * Event(s) that trigger navigation. Can be string or array of strings. * @default 'click' * @example 'click' | ['click', 'mouseenter'] */ event: { type: [String, Array] as PropType<string | string[]>, default: 'click' }, /** * Custom tag to render instead of 'a'. * @default 'a' * @example 'button' | 'div' | 'span' */ tag: { type: String, default: 'a' }, /** * Layer options for layer-based navigation. * Only used when type='pushLayer'. * @example { zIndex: 1000, autoPush: false, routerOptions: { mode: 'memory' } } */ layerOptions: { type: Object as PropType<RouteLayerOptions> } }, setup(props, { slots }) { const link = useLink(props); return () => { const data = link.value; // Generate event handlers with proper type transformation for Vue 2/3 compatibility const eventHandlers = data.getEventHandlers( isVue3 ? (name: string): string => `on${name.charAt(0).toUpperCase()}${name.slice(1)}` : undefined ); const props = {}; if (isVue3) { Object.assign(props, data.attributes, eventHandlers); } else { const { class: className, ...attrs } = data.attributes; Object.assign(props, { attrs, class: className, on: eventHandlers }); } return h(data.tag, props, slots.default?.()); }; } }); |