-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(editor): Use web native <a> element in nav menus (#8385)
- Loading branch information
Showing
22 changed files
with
343 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/design-system/src/components/ConditionalRouterLink/CondtionalRouterLink.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<script setup lang="ts"> | ||
/** | ||
* Component that renders either a RouterLink or a normal anchor tag or | ||
* just the slot content based on whether the `to` or `href` prop is | ||
* passed or not. | ||
*/ | ||
import { useAttrs } from 'vue'; | ||
import type { RouterLinkProps } from 'vue-router'; | ||
import { RouterLink } from 'vue-router'; | ||
defineOptions({ | ||
name: 'ConditionalRouterLink', | ||
inheritAttrs: false, | ||
}); | ||
const props = defineProps({ | ||
// @ts-expect-error TS doesn't understand this but it works | ||
...RouterLink.props, | ||
// Make to optional | ||
to: { | ||
type: [String, Object] as unknown as () => string | RouterLinkProps['to'] | undefined, | ||
default: undefined, | ||
}, | ||
// <a> element "props" are passed as attributes | ||
}) as Partial<RouterLinkProps>; | ||
const attrs = useAttrs(); | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<RouterLink v-if="props.to" v-bind="props" :to="props.to"> | ||
<slot /> | ||
</RouterLink> | ||
<a v-else-if="attrs.href" v-bind="attrs"> | ||
<slot /> | ||
</a> | ||
<slot v-else /> | ||
</div> | ||
</template> |
67 changes: 67 additions & 0 deletions
67
...esign-system/src/components/ConditionalRouterLink/__tests__/ConditionalRouterLink.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { render } from '@testing-library/vue'; | ||
import { beforeAll, describe } from 'vitest'; | ||
import { createRouter, createWebHistory } from 'vue-router'; | ||
import CondtionalRouterLink from '../CondtionalRouterLink.vue'; | ||
|
||
const slots = { | ||
default: 'Button', | ||
}; | ||
|
||
const router = createRouter({ | ||
history: createWebHistory(), | ||
routes: [ | ||
{ | ||
path: '/', | ||
name: 'home', | ||
redirect: '/home', | ||
}, | ||
], | ||
}); | ||
|
||
describe('CondtionalRouterLink', () => { | ||
beforeAll(async () => { | ||
await router.push('/'); | ||
|
||
await router.isReady(); | ||
}); | ||
|
||
it("renders router-link when 'to' prop is passed", () => { | ||
const wrapper = render(CondtionalRouterLink, { | ||
props: { | ||
to: { name: 'home' }, | ||
}, | ||
slots, | ||
global: { | ||
plugins: [router], | ||
}, | ||
}); | ||
|
||
expect(wrapper.html()).toMatchSnapshot(); | ||
}); | ||
|
||
it("renders <a> when 'href' attr is passed", () => { | ||
const wrapper = render(CondtionalRouterLink, { | ||
attrs: { | ||
href: 'https://n8n.io', | ||
target: '_blank', | ||
}, | ||
slots, | ||
global: { | ||
plugins: [router], | ||
}, | ||
}); | ||
|
||
expect(wrapper.html()).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders only the slot when neither to nor href is given', () => { | ||
const wrapper = render(CondtionalRouterLink, { | ||
slots, | ||
global: { | ||
plugins: [router], | ||
}, | ||
}); | ||
|
||
expect(wrapper.html()).toMatchSnapshot(); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
...mponents/ConditionalRouterLink/__tests__/__snapshots__/ConditionalRouterLink.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`CondtionalRouterLink > renders <a> when 'href' attr is passed 1`] = `"<div><a href="https://n8n.io" target="_blank">Button</a></div>"`; | ||
exports[`CondtionalRouterLink > renders only the slot when neither to nor href is given 1`] = `"<div>Button</div>"`; | ||
exports[`CondtionalRouterLink > renders router-link when 'to' prop is passed 1`] = `"<div><a href="/" class="">Button</a></div>"`; |
3 changes: 3 additions & 0 deletions
3
packages/design-system/src/components/ConditionalRouterLink/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import CondtionalRouterLink from './CondtionalRouterLink.vue'; | ||
|
||
export default CondtionalRouterLink; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/design-system/src/components/N8nMenuItem/routerUtil.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { IMenuItem, RouteObject } from '@/types'; | ||
import type { RouteLocationRaw } from 'vue-router'; | ||
|
||
/** | ||
* Checks if the given menu item matches the current route. | ||
*/ | ||
export function doesMenuItemMatchCurrentRoute(item: IMenuItem, currentRoute: RouteObject) { | ||
let activateOnRouteNames: string[] = []; | ||
if (Array.isArray(item.activateOnRouteNames)) { | ||
activateOnRouteNames = item.activateOnRouteNames; | ||
} else if (item.route && isNamedRouteLocation(item.route.to)) { | ||
activateOnRouteNames = [item.route.to.name]; | ||
} | ||
|
||
let activateOnRoutePaths: string[] = []; | ||
if (Array.isArray(item.activateOnRoutePaths)) { | ||
activateOnRoutePaths = item.activateOnRoutePaths; | ||
} else if (item.route && isPathRouteLocation(item.route.to)) { | ||
activateOnRoutePaths = [item.route.to.path]; | ||
} | ||
|
||
return ( | ||
activateOnRouteNames.includes(currentRoute.name ?? '') || | ||
activateOnRoutePaths.includes(currentRoute.path) | ||
); | ||
} | ||
|
||
function isPathRouteLocation(routeLocation?: RouteLocationRaw): routeLocation is { path: string } { | ||
return ( | ||
typeof routeLocation === 'object' && | ||
'path' in routeLocation && | ||
typeof routeLocation.path === 'string' | ||
); | ||
} | ||
|
||
function isNamedRouteLocation(routeLocation?: RouteLocationRaw): routeLocation is { name: string } { | ||
return ( | ||
typeof routeLocation === 'object' && | ||
'name' in routeLocation && | ||
typeof routeLocation.name === 'string' | ||
); | ||
} |
Oops, something went wrong.