-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
Currently all navigation in the menus is done programmatically. This is not accessible way to do navigation, as it prevents browser default behaviour, like cmd/ctrl+click to open into new tab. Also screen readers don't give any indication that the active element is an anchor. This PR refactors component library's Menu and MenuItem to use either vue-router RouterLink or <a> when the menu item is a navigation element.
- Loading branch information
Showing
15 changed files
with
269 additions
and
287 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
44 changes: 44 additions & 0 deletions
44
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,44 @@ | ||
<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 { h, useAttrs, useSlots } from 'vue'; | ||
import type { RouterLinkProps } from 'vue-router'; | ||
import { RouterLink } from 'vue-router'; | ||
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 slots = useSlots(); | ||
const attrs = useAttrs(); | ||
const renderContent = () => { | ||
const { to } = props; | ||
const { href } = attrs; | ||
if (!to && !href) { | ||
return slots.default?.(); | ||
} | ||
return to | ||
? // to is required to be passed explicitly for TS to be happy | ||
h(RouterLink, { ...props, to }, () => slots.default?.()) | ||
: h('a', { ...attrs }, slots.default?.()); | ||
}; | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<!-- This will render either RouterLink or just the slot content --> | ||
<component :is="renderContent" /> | ||
</div> | ||
</template> |
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' | ||
); | ||
} |
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
Oops, something went wrong.