How to control if link opens in new tab or same tab?

9 replies
Last updated: Jun 26, 2024
How can I choose whether a link opens in a new tab or in the same one?
AI Update

When working with Portable Text in Sanity, you can add a field to your link annotations that lets content editors choose whether a link opens in a new tab or the same one. Here's how:

Adding the field to your schema

In your Portable Text schema definition, add a boolean field to your link annotation:

{
  name: 'link',
  type: 'object',
  title: 'External link',
  fields: [
    {
      name: 'href',
      type: 'url',
      title: 'URL'
    },
    {
      title: 'Open in new tab',
      name: 'blank',
      description: 'Read https://css-tricks.com/use-target_blank/',
      type: 'boolean'
    }
  ]
}

This adds a checkbox in Sanity Studio that editors can toggle to control the link behavior.

When rendering with React using @portabletext/react:

import {PortableText} from '@portabletext/react'

const components = {
  marks: {
    link: ({value, children}) => {
      const { blank, href } = value
      return blank ?
        <a href={href} target="_blank" rel="noopener">{children}</a>
        : <a href={href}>{children}</a>
    }
  }
}

export const Body = ({blocks}) => {
  return <PortableText value={blocks} components={components} />
}

The rel="noopener" attribute is important for security when using target="_blank" - it prevents the new page from accessing the window.opener property.

For Vue.js with sanity-blocks-vue-component:

<template>
  <PortableText
    :blocks="blocks"
    :serializers="serializers"
  />
</template>

<script>
import PortableText from 'sanity-blocks-vue-component'

export default {
  components: { PortableText },
  data() {
    return {
      serializers: {
        marks: {
          link: ({mark, children}) => {
            const { blank, href } = mark
            return blank ?
              <a href={href} target="_blank" rel="noopener">{children}</a>
              : <a href={href}>{children}</a>
          }
        }
      }
    }
  }
}
</script>

For other frameworks or when rendering to Markdown, you'd follow a similar pattern in your custom serializers, checking the blank value and conditionally adding the target="_blank" attribute.

You can find complete examples for React, Vue, and Markdown rendering in the Internal and external links with Portable Text guide.

Show original thread
9 replies
By adding the
target
attribute to your
a
tags and setting its value to
_blank
will make it so when a user clicks a link it opens in a new tab.
And is there something in the Sanity UI that allows the admin to choose it when editing the content?
Do you already have link object? If so add a boolean field to allow admins to set whether the link should open in a new window or not
Your going to have to modify your link schema to include probably a boolean, then update your serializer to make that rendering decision for you. Its not an "out of the box" Sanity feature
thanx guys
Let’s use greetings like “Hey Everyone,” “Hi, Y’all," or “Hello, Folks” to make sure everyone in the community is included. Please read the Sanity Community Code of Conduct to stay updated on expected communication &amp; behavior in our spaces: https://www.sanity.io/docs/community-code-of-conduct
mean this
Yep, here's a guide from Sanity showing what needs adding to the backend side of your link/description schema - https://www.sanity.io/guides/portable-text-internal-and-external-links#71764b37d9ea
i view this plugin for its useful for other people https://www.sanity.io/plugins/sanity-plugin-link-field

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?