Custom Block System - Streats22/LaraGrapePackage GitHub Wiki
The Custom Blocks system allows you to create reusable, visual components using a built-in block builder with live preview functionality.
Custom Blocks are user-created components that can be:
- ✅ Built Visually: Use the block builder interface
- ✅ Reused: Add to multiple pages
- ✅ Customized: HTML, CSS, and JavaScript support
- ✅ Organized: Categorized for easy management
- ✅ Previewed: Live preview during creation
-
Login to Admin Panel
- Go to
/admin
- Use your admin credentials
- Go to
-
Navigate to Custom Blocks
- Click Custom Blocks in the navigation
- Click Create Custom Block
Fill in the basic information:
- Name: Descriptive name for your block (e.g., "Feature Card")
- Description: Brief description of what the block does
- Category: Choose from available categories (components, layouts, etc.)
- Sort Order: Control display order in the block manager
The block builder has four main tabs:
Write the structure of your block:
<div class="feature-card">
<div class="icon">
<i class="fas fa-star"></i>
</div>
<h3 data-gjs-type="text" data-gjs-name="title">Feature Title</h3>
<p data-gjs-type="text" data-gjs-name="description">Feature description goes here</p>
<button data-gjs-type="text" data-gjs-name="button-text">Learn More</button>
</div>
Key Points:
- Use
data-gjs-type="text"
for editable text elements - Use
data-gjs-name="unique-name"
to identify editable areas - Structure your HTML semantically
Add styling to your block:
.feature-card {
background: white;
border-radius: 12px;
padding: 2rem;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
text-align: center;
transition: transform 0.3s ease;
}
.feature-card:hover {
transform: translateY(-4px);
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}
.feature-card .icon {
width: 64px;
height: 64px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 1rem;
}
.feature-card .icon i {
color: white;
font-size: 1.5rem;
}
.feature-card h3 {
color: #2d3748;
margin-bottom: 0.5rem;
font-size: 1.25rem;
font-weight: 600;
}
.feature-card p {
color: #718096;
margin-bottom: 1.5rem;
line-height: 1.6;
}
.feature-card button {
background: #4299e1;
color: white;
border: none;
padding: 0.75rem 1.5rem;
border-radius: 6px;
font-weight: 500;
cursor: pointer;
transition: background 0.3s ease;
}
.feature-card button:hover {
background: #3182ce;
}
CSS Best Practices:
- Use descriptive class names
- Include hover states and transitions
- Make blocks responsive with media queries
- Use CSS variables for consistent theming
Add interactivity to your block:
// Feature card interactions
document.addEventListener('DOMContentLoaded', function() {
const featureCards = document.querySelectorAll('.feature-card');
featureCards.forEach(card => {
// Add click handler for the button
const button = card.querySelector('button');
if (button) {
button.addEventListener('click', function() {
// Get the title for analytics or navigation
const title = card.querySelector('h3').textContent;
console.log('Feature clicked:', title);
// You can add navigation, analytics, or other functionality
// window.location.href = '/features/' + title.toLowerCase().replace(/\s+/g, '-');
});
}
// Add animation on scroll
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
}
});
});
card.style.opacity = '0';
card.style.transform = 'translateY(20px)';
card.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
observer.observe(card);
});
});
JavaScript Best Practices:
- Use event delegation for dynamic content
- Include error handling
- Keep code modular and reusable
- Consider performance implications
See your block in action:
- Live Preview: See how your block looks
- Responsive Testing: Test on different screen sizes
- Interaction Testing: Test JavaScript functionality
UI components like buttons, cards, alerts, etc.
Structural blocks like containers, grids, sections
Content-focused blocks like text, headings, lists
Media blocks like galleries, videos, sliders
Form-related blocks like contact forms, newsletters
User-defined categories for specific needs
Custom blocks automatically integrate with GrapesJS:
<!-- Make elements editable in GrapesJS -->
<h3 data-gjs-type="text" data-gjs-name="title">Editable Title</h3>
<p data-gjs-type="text" data-gjs-name="content">Editable content</p>
<img data-gjs-type="image" data-gjs-name="image" src="placeholder.jpg" alt="Editable image">
Make your blocks responsive:
.feature-card {
/* Mobile first */
padding: 1rem;
margin-bottom: 1rem;
}
/* Tablet */
@media (min-width: 768px) {
.feature-card {
padding: 1.5rem;
margin-bottom: 1.5rem;
}
}
/* Desktop */
@media (min-width: 1024px) {
.feature-card {
padding: 2rem;
margin-bottom: 2rem;
}
}
Use JavaScript to make content dynamic:
// Example: Dynamic pricing display
function updatePricing() {
const priceElements = document.querySelectorAll('.price');
const currency = 'USD'; // Could be dynamic
priceElements.forEach(element => {
const basePrice = element.dataset.price;
const formattedPrice = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: currency
}).format(basePrice);
element.textContent = formattedPrice;
});
}
// Call on page load
document.addEventListener('DOMContentLoaded', updatePricing);
/* Ensure touch targets are large enough */
.feature-card button {
min-height: 44px; /* iOS minimum touch target */
min-width: 44px;
padding: 12px 20px;
}
/* Add touch feedback */
.feature-card button:active {
transform: scale(0.98);
}
// Lazy load images
const images = document.querySelectorAll('img[data-src]');
const imageObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.classList.remove('lazy');
imageObserver.unobserve(img);
}
});
});
images.forEach(img => imageObserver.observe(img));
- Consistency: Use consistent spacing, colors, and typography
- Accessibility: Include proper ARIA labels and keyboard navigation
- Performance: Keep blocks lightweight and optimized
- Reusability: Design blocks to work in different contexts
- Semantic HTML: Use proper HTML structure
- Clean CSS: Organize styles logically
- Modular JavaScript: Keep functions focused and reusable
- Error Handling: Include fallbacks and error states
- Loading States: Show loading indicators for dynamic content
- Error States: Handle errors gracefully
- Feedback: Provide visual feedback for interactions
- Progressive Enhancement: Work without JavaScript
- Check Active Status: Ensure block is marked as active
-
Clear Cache:
php artisan cache:clear
- Check Category: Verify block is in correct category
- Preview Block: Test in preview mode
- CSS Conflicts: Check for conflicting styles
- Specificity: Use more specific selectors if needed
- Responsive Issues: Test on different screen sizes
- Browser Compatibility: Test in different browsers
- Console Errors: Check browser console for errors
- Event Listeners: Ensure proper event handling
- DOM Ready: Wait for DOM to be ready
- Scope Issues: Check variable scope and naming
Enable debug mode to see more information:
// Add debug logging
const DEBUG = true;
if (DEBUG) {
console.log('Custom block loaded:', blockName);
console.log('Block elements:', document.querySelectorAll('.feature-card'));
}
<!-- HTML -->
<div class="simple-card">
<img data-gjs-type="image" data-gjs-name="image" src="placeholder.jpg" alt="Card image">
<div class="content">
<h3 data-gjs-type="text" data-gjs-name="title">Card Title</h3>
<p data-gjs-type="text" data-gjs-name="description">Card description</p>
</div>
</div>
/* CSS */
.simple-card {
border: 1px solid #e2e8f0;
border-radius: 8px;
overflow: hidden;
transition: box-shadow 0.3s ease;
}
.simple-card:hover {
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.simple-card img {
width: 100%;
height: 200px;
object-fit: cover;
}
.simple-card .content {
padding: 1rem;
}
.simple-card h3 {
margin: 0 0 0.5rem 0;
color: #2d3748;
}
.simple-card p {
margin: 0;
color: #718096;
line-height: 1.5;
}
<!-- HTML -->
<button class="interactive-button" data-gjs-type="text" data-gjs-name="button-text">
Click Me
</button>
/* CSS */
.interactive-button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 12px 24px;
border-radius: 6px;
font-weight: 500;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
.interactive-button:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
}
.interactive-button:active {
transform: translateY(0);
}
.interactive-button::before {
content: '';
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
transition: left 0.5s;
}
.interactive-button:hover::before {
left: 100%;
}
// JavaScript
document.addEventListener('DOMContentLoaded', function() {
const buttons = document.querySelectorAll('.interactive-button');
buttons.forEach(button => {
button.addEventListener('click', function() {
// Add click animation
this.style.transform = 'scale(0.95)';
setTimeout(() => {
this.style.transform = '';
}, 150);
// Add your custom functionality here
console.log('Button clicked:', this.textContent);
});
});
});
- [Block System](Block System.md) - Understanding the block system
- [Admin Panel](Admin Panel.md) - Admin interface guide
- [GrapesJS Integration](GrapeJs Intergration.md) - Visual editor integration
- [API Reference](Api Reference.md) - Service classes and methods
Custom Blocks give you the power to create reusable, interactive components that enhance your LaraGrape website! 🧩
With the visual block builder, you can create professional components without writing complex code, while still having full control over HTML, CSS, and JavaScript when needed.