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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 22x 37x 37x 37x 37x 37x 37x 37x 41x 37x 37x 37x 37x 37x 23x 1x | import type { RouterLinkProps } from '@esmx/router';
import { defineComponent, h, type PropType } from 'vue';
import { useLink } from './use';
import { isVue2 } 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<RouterLinkProps['to']>,
required: true
},
/**
* Navigation type for the link.
* @default 'push'
* @example 'push' | 'replace' | 'pushWindow' | 'replaceWindow' | 'pushLayer'
*/
type: {
type: String as PropType<RouterLinkProps['type']>,
default: 'push'
},
/**
* @deprecated Use 'type="replace"' instead
* @example :replace={true} → type="replace"
*/
replace: {
type: Boolean as PropType<RouterLinkProps['replace']>,
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<RouterLinkProps['exact']>,
default: 'include'
},
/**
* CSS class to apply when link is active (route matches).
* @example 'nav-active' | 'selected'
*/
activeClass: {
type: String as PropType<RouterLinkProps['activeClass']>
},
/**
* Event(s) that trigger navigation. Can be string or array of strings.
* @default 'click'
* @example 'click' | ['click', 'mouseenter']
*/
event: {
type: [String, Array] as PropType<RouterLinkProps['event']>,
default: 'click'
},
/**
* Custom tag to render instead of 'a'.
* @default 'a'
* @example 'button' | 'div' | 'span'
*/
tag: { type: String as PropType<RouterLinkProps['tag']>, 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<RouterLinkProps['layerOptions']>
},
/**
* Custom navigation handler called before navigation.
* Receives the event object and the event name that triggered navigation.
*
* @Note you need to call `e.preventDefault()` to prevent default browser navigation.
*/
beforeNavigate: {
type: Function as PropType<RouterLinkProps['beforeNavigate']>
}
},
setup(props, context) {
const link = useLink(props);
if (isVue2) {
return () => {
const { class: className, ...attributes } =
link.value.attributes;
return h(
link.value.tag,
{
attrs: {
...attributes,
...context.attrs
},
class: className,
on: link.value.createEventHandlers()
},
context.slots.default?.()
);
};
}
return () => {
return h(
link.value.tag,
{
...link.value.attributes,
...context.attrs,
...link.value.createEventHandlers(
(name) =>
`on${name.charAt(0).toUpperCase()}${name.slice(1)}`
)
},
context.slots.default?.()
);
};
}
});
|