Anchor links with hash navigation don't work in preview mode
Question
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Chrome 143.0.7499.41
Reproducible demo link
https://jsfiddle.net/mghzw41k/2/
Describe the bug
On preview mode, anchor links with hash navigation (e.g., href="#section") don't work. The default browser navigation behavior is prevented, even though preview mode should (I think) allow normal page interactions.
How to reproduce the bug?
- Initialize GrapesJS editor
- Add content with an anchor link pointing to a section:
<a href="#about">Go to About</a> - Add a target section:
<section id="about">About Section</section> - Enter preview mode
- Click the anchor link
Expected Behavior: The preview page should scroll to the target section when clicking the anchor link, as it would in a normal browser.
Actual Behavior: Nothing happens when clicking the anchor link. The default navigation behavior is prevented.
JSFiddle Code
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/grapes.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/grapes.min.css">
<div id="gjs"></div>
const editor = grapesjs.init({
container: '#gjs',
});
editor.on('load', () => {
editor.setComponents(`
<div>
<a href="#about" style="display: inline-block; padding: 10px; background: #007bff; color: white; text-decoration: none; border-radius: 4px;">Click to nav to About</a>
</div>
<div style="height:1000px;background:red;">
<span>... Spacing ...</span>
</div>
<section id="about" style="padding: 20px; background: #f0f0f0;">
<h2>About Me</h2>
<p>This is the about section that the button should navigate to.</p>
</section>
`);
});
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (2)
I encountered the same problem.
Thanks for reporting this, @OfirD1.
Great suggestion about Anchor links with hash navigation don't work in preview mode! While this specific feature isn't yet in the core API, there are several ways to achieve similar behavior.
Using the event system:
editor.on('component:update', (component) => {
// your logic here
});
Alternative approaches:
- Listen to
selector:addfor CSS selector changes - Use
selector:customfor custom rules - Tap into the
change:*events for fine-grained tracking - Build a plugin that extends the editor with this capability
Making it official: If this feature would benefit many users, consider opening a formal Feature Request on the GrapesJS repo with:
- A detailed use case
- Code example showing the desired behavior
- Why this matters for your workflow
The core team is receptive to well-motivated feature requests backed by real use cases.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #5718
Possible upgrade path issue where 'textnode' is now called 'wrapper' from 0.18 -> latest
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Version 122.0.6261.95 (Official Bui...
Issue #4904
getHtml() producing invalid structure when there is js inside it
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? chrome latestReproducible demo link https://...
Issue #5156
Cannot drag drop in mobile screen even add grapejs-touch
GrapesJS version[X] I confirm to use the latest version of GrapesJSWhat browser are you using? Chrome, IOSReproducible demo link https://ww...
Issue #4503
Missing inline styles used by CKEditor when adding raw html components
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chromium v96 Reproducible demo link https:...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins βPremium plugins ship with support, regular updates, and production-ready features β save days of integration work.
Browse premium plugins βBrowse Plugin Categories
Jump directly to plugin category pages on the marketplace.