Sometimes you want editors to be able to create links within a RichText
.
To enable the link rich-text feature, you just need to add the types.RichTextFeatures.Link
to the allowedFeatures
.
// Thumbnail.tsximport { Link } from 'react-bricks/rsc'...<RichTextpropName="description"value={description}renderBlock={({ children }) => (<p className="text-lg text-gray-500">{children}</p>)}placeholder="Type a description"allowedFeatures={[types.RichTextFeatures.Bold,types.RichTextFeatures.Highlight,types.RichTextFeatures.Link,]}renderHighlight={({ children }) => (<span className="px-1 rounded bg-blue-200 text-blue-900">{children}</span>)}/>
Underneath, React Bricks will use the Link
component from the library.
The Link component has many advantages over a normal anchor tag (or even a link component from the chosen React framework).
/about-us`
), it will render the local link (for example the Next.js link for a Next.js starter).https://...
), it will render a normal <a>
tag.target="_blank"
, but also the rel="noopener"
automatically, for security reasons (see the "noopener" HTML reference).Since we are using Tailwind CSS for this tutorial, and Tailwind doesn't apply any default style to a link, we need to provide a custom render function to make the link visible.
In the renderLink
function we'll use the <Link>
component:
// Thumbnail.tsximport { Link } from 'react-bricks/rsc'...<RichTextpropName="description"value={description}renderBlock={({ children }) => (<p className="text-lg text-gray-500">{children}</p>)}placeholder="Type a description"allowedFeatures={[types.RichTextFeatures.Bold,types.RichTextFeatures.Highlight,types.RichTextFeatures.Link,]}renderHighlight={({ children }) => (<span className="px-1 rounded bg-blue-200 text-blue-900">{children}</span>)}renderLink={({ children, href, target, rel }) => (<Linkhref={href}target={target}rel={rel}className="text-sky-500 hover:text-sky-600 transition-colors">{children}</Link>)}/>
You will use the Link component also when you you want to render a visually editable link outside of a RichText, for example a "button-like" brick, like in the code below.
// Button.tsximport { Text, Link, types } from 'react-bricks/rsc'interface ButtonProps {buttonText: types.TextValuebuttonPath: string}const Button: types.Brick<ButtonProps> = ({buttonText,buttonPath,}) => {return (<Linkhref={buttonPath}className="py-2 px-6 text-white text-center bg-sky-500"><TextpropName="buttonText"value={buttonText}placeholder="Action"renderBlock={({ children }) => <span>{children}</span>}/></Link>)}Button.schema = {name: 'button',label: 'Button',getDefaultProps: () => ({buttonText: 'Submit',}),sideEditProps: [{name: 'buttonPath',label: 'Path or URL',type: types.SideEditPropType.Text,validate: (value) =>value?.startsWith('/') ||value?.startsWith('https://') ||'Please, enter a valid URL',},],}export default Button
sideEditProp
property. Don't consider that part of the code now: we'll discover sidebar controls soon, and then you could come back to fully understand the code.