Skip to content

Commit

Permalink
feat: add is external logic from NuxtLink component (#2273)
Browse files Browse the repository at this point in the history
* Add is external logic from NuxtLink component

* Add test

* Linting

* Use NuxtLinkLocale for external links
  • Loading branch information
TitusKirch committed Aug 1, 2023
1 parent 03b020b commit d8889ce
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
3 changes: 3 additions & 0 deletions specs/basic_usage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ test('basic usage', async () => {
expect(await page.locator('#nuxt-link-locale-usages .object-with-named a').getAttribute('href')).toEqual(
'/category/nintendo'
)
expect(await page.locator('#nuxt-link-locale-usages .external-url a').getAttribute('href')).toEqual(
'https://nuxt.com/'
)

// Language switching path localizing with `useSwitchLocalePath`
expect(await page.locator('#switch-locale-path-usages .switch-to-en a').getAttribute('href')).toEqual('/')
Expand Down
3 changes: 3 additions & 0 deletions specs/fixtures/basic_usage/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ function onClick() {
{{ category.title }}
</NuxtLinkLocale>
</li>
<li class="external-url">
<NuxtLinkLocale :to="'https://nuxt.com/'">Nuxt.com</NuxtLinkLocale>
</li>
</ul>
</section>
<section id="switch-locale-path-usages">
Expand Down
26 changes: 25 additions & 1 deletion src/runtime/components/NuxtLinkLocale.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useLocalePath } from '#i18n'
import { defineComponent, computed, defineNuxtLink, h } from '#imports'
import { hasProtocol } from 'ufo'

import type { PropType } from 'vue'
import type { RawLocation, RouteLocation } from '@intlify/vue-router-bridge'
Expand All @@ -25,6 +26,29 @@ export default defineComponent({
const localePath = useLocalePath()
const resolvedPath = computed(() => (props.to != null ? localePath(props.to, props.locale) : props.to))

return () => h(NuxtLinkLocale, { ...props, to: resolvedPath }, slots.default)
// Resolving link type
const isExternal = computed<boolean>(() => {
// External prop is explicitly set
if (props.external) {
return true
}

// When `target` prop is set, link is external
if (props.target && props.target !== '_self') {
return true
}

// When `to` is a route object then it's an internal link
if (typeof props.to === 'object') {
return false
}

return props.to === '' || hasProtocol(props.to, { acceptRelative: true })
})

return () =>
isExternal.value
? h(NuxtLinkLocale, props, slots.default)
: h(NuxtLinkLocale, { ...props, to: resolvedPath }, slots.default)
}
})
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"paths": {
"#app": ["./node_modules/nuxt/dist/app"],
"#app/*": ["./node_modules/nuxt/dist/app/*"],
"#components": ["./.nuxt/components"],
"#imports": ["./.nuxt/imports.d.ts"],
"#pages": ["./node_modules/nuxt/dist/pages/runtime/index.d.ts"],
"#head": ["./node_modules/nuxt/dist/index.d.ts"],
Expand Down

0 comments on commit d8889ce

Please sign in to comment.