@anchor

Create linkable anchors that appear in the Elements Link Panel

The @anchor directive creates a linkable anchor point within your component. Anchors defined with this directive automatically appear in the Elements Link Panel, making it easy for users to link to specific sections of your component from anywhere in their project.

Syntax

Using a literal string:

<div id="@anchor("anchorName")"></div>

Using a property value:

<div id="@anchor(anchorProperty)"></div>

How It Works

When you use @anchor, Elements:

  1. Outputs the anchor name/value as the element's id attribute

  2. Registers the anchor in the Link Panel so users can select it when creating links

  3. Enables smooth scrolling to the anchor when the link is clicked

Examples

Basic Anchor

Create a static anchor point:

This creates an anchor called "features" that users can link to via the Link Panel.

Dynamic Anchor from Property

Allow users to customize the anchor name through a component property:

Where sectionId is defined in your properties.json as a text input.

Multiple Anchors in One Component

Components can define multiple anchor points:

Accordion Sections

Create anchors for each accordion section:

Create an anchor for a navigation section:

Anchors in Hooks File

For more complex anchor scenarios or when anchors need to be generated programmatically, you can use rw.addAnchor() in your hooks file instead of the template directive.

This is useful when:

  • Anchor names need to be computed from multiple properties

  • You need to conditionally add anchors based on complex logic

  • Anchors need to be added in a loop with dynamic IDs

See rw.addAnchor for more details on the hooks approach.

Best Practices

  1. Use descriptive anchor names - Choose names that clearly describe the section: "pricing", "contact-form", "testimonials".

  2. Keep anchor names URL-friendly - Use lowercase letters, numbers, and hyphens. Avoid spaces and special characters.

  3. Consider user control - For reusable components, expose the anchor name as a property so users can customize it.

  4. Document your anchors - If your component creates anchors, document them so users know what links are available.

Last updated

Was this helpful?