storyblok / storyblok-nuxt

Storyblok Nuxt module
https://www.storyblok.com/tp/nuxt-js-multilanguage-website-tutorial
262 stars 40 forks source link

Add component to easily handle Storyblok's link fields (<StoryblokLink />) #864

Open ZackPlauche opened 6 days ago

ZackPlauche commented 6 days ago

Description

As a developer using Storyblok SDK I want a link component that easily handles all potential scenarios for the Storyblok link field so I don't re-write the same logic and have a faster experience developing with Storyblok and converting my components to work properly :)

Suggested solution or improvement

I'd like to add a built-in Storyblok component called StoryblokLink that handles all of the different situations from the Storyblok link field.

Something like:

<!-- components/StoryblokLink.vue -->
<template>
  <NuxtLink :to="linkMap[linkField.linktype]" :target="linkField.target" :download="linkField.linktype === 'asset' ? '' : null">
    <slot />
  </NuxtLink>
</template>

<script setup>
const { linkField } = defineProps({ linkField: Object })

const linkMap = {
  url: linkField.url,
  asset: linkField.url,
  email: `mailto:${linkField.email}`,
  story: linkField.story?.url,
}
</script>

Usage:

<template>
  <div>
    <StoryblokLink  link-field="blok.my_link" class="btn">Click me!</StoryblokLink>
  </div>
</template>

<script setup>
defineProps({ blok: Object })
</script>

<style>
.btn {
  @apply whitespace-nowrap inline-flex justify-center items-center px-5 py-2 rounded bg-cyan-500 hover:bg-cyan-400 transition-colors;
}
</style>

Additional context

No response

Validations

markus-gx commented 4 days ago

Interesting. We do work with such a component. In our codebase it is called StoryblokLink too.

Some suggestions to make this work which definitely should be considered: