Custom components based on Link block with a default href
@csb346 you can define a new custom component type: and then use it inside a block: just remember you must define your custom component type inside a plugin Cheers!
Read full answer below ↓Question
Hi! I have a GrapesJS integration on a laravel project with custom components directly on the page (each block is supposed to be block link to a content). So far I replicated the block link component like that
@foreach($contents as $content)
editor.BlockManager.add('content_{{ $content->id }}', {
category: 'Contents',
label: '<b>{{ $content->title }}</b>',
attributes: { class:'fa fa-graduation-cap' },
content: {
type: 'link',
editable: false,
droppable: true,
style:{
display: 'inline-block',
padding: '5px',
'min-height': '50px',
'min-width': '50px'
},
content: '{{ $content->title }}',
},
});
@endforeach
So far so good. But I need that each of these components has his own href defined by default. No matter if it's editable or not. Something like this:
href: 'http://url.com/content/{{ $content->id }}',
Can't find the solution. Any idea?
Thanks
Answers (1)
@csb346 you can define a new custom component type:
editor.DomComponents.addType('content-link', {
extend: 'link',
model: {
defaults: {
editable: false,
droppable: true,
style: {
display: 'inline-block',
padding: '5px',
'min-height': '50px',
'min-width': '50px'
},
attributes:{href:'http://google.com'}
content: '{{ $content->title }}',
}
}
});
and then use it inside a block:
editor.BlockManager.add('content-link', {
label:'Content Link',
category:'Contents',
attributes: { class:'fa fa-graduation-cap' },
content:{
type:'content-link'
}
})
just remember you must define your custom component type inside a plugin Cheers!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2961
BUG: Components in custom modal isn't editable
Hello, I'm trying to add custom modal block to my project and I want to change content of this modal also with drag&drop. Here's code of my...
Issue #6685
Custom component styles are not applied after deletion and re-addition
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v143.0.7499.193 Reproducible demo l...
Issue #3207
Form is not submitting
I am trying to create as custom block. But the form is not submitting when i use components inside content. I am able to get all the form d...
Issue #3309
TextNodes interfere when changing content on element
Version: 0.16.44 Hi @artf I have a custom block with a custom button component, which its content is similar to this one. <a data-gjs-type=...
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 →Related tutorials
In-depth guides on the same topic.
Tutorial
Building an AI-Assisted Document Engine with Next.js, GrapesJS, and the AI SDK
Transform your static template editor into an intelligent, generative design workspace with Vercel AI SDK, AI Elements and Next.js.
Tutorial
Ship to Production Faster: What’s New in GrapesJS Shadcn
Supercharge your page builder! GrapesJS Shadcn adds live drag previews, rich text / commands, dynamic data, and canvas presets to ship to prod faster.
Tutorial
How to Build a Production GrapesJS Editor: The Complete Walkthrough of Brief, Preset, Plugins, and Services
A complete walkthrough of building a production GrapesJS editor: how to choose a preset, pick plugins, and scope setup services without burning a sprint.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.