Installation Add the package via Composer:
composer require xlabs/emojisbundle
Publish the assets (if needed):
php artisan vendor:publish --provider="Xlabs\EmojiBundle\EmojiServiceProvider" --tag=public
Basic Integration
Include the CSS/JS in your resources/views/layouts/app.blade.php:
<link href="{{ asset('vendor/emojisbundle/css/emojis.css') }}" rel="stylesheet">
<script src="{{ asset('vendor/emojisbundle/js/emojis.js') }}"></script>
First Use Case
Initialize the plugin on a <textarea>:
<textarea id="comment" name="comment"></textarea>
$(document).ready(function() {
$('#comment').emojis();
});
Dynamic Form Integration Use Laravel Blade directives to conditionally load the plugin:
@if($showEmojis)
<textarea name="message" class="emoji-enabled"></textarea>
<script>
$(document).ready(function() {
$('.emoji-enabled').emojis();
});
</script>
@endif
AJAX Form Handling Reinitialize the plugin after AJAX updates:
$('.emoji-trigger').on('click', function() {
$.ajax({
url: '/update-comment',
success: function(response) {
$('#comment').html(response.comment);
$('#comment').emojis(); // Reinitialize
}
});
});
Custom Emoji Sets Override the default emoji set via config:
// config/emojis.php
'emoji_set' => 'custom_path/to/emojis.json',
Publish the config first:
php artisan vendor:publish --provider="Xlabs\EmojiBundle\EmojiServiceProvider" --tag=config
Laravel Validation Sanitize emoji input before storage:
$validated = $request->validate([
'message' => 'required|string|max:1000',
]);
// Strip HTML/JS from emoji markup if needed
$cleanMessage = strip_tags($validated['message']);
Asset Paths
webpack.mix.js or vite.config.js.mix.copy('vendor/xlabs/emojisbundle/public/js/emojis.js', 'public/js/');
jQuery Dependency
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="{{ asset('vendor/emojisbundle/js/emojis.js') }}"></script>
Conflict with Other Plugins
ClassicEditor.create(document.querySelector('#editor'))
.then(editor => {
$('#editor').emojis(); // Initialize emojis on top of TinyMCE
})
.catch(error => {
console.error(error);
});
Emoji Shortcode Parsing
:smile:. If storing raw input, escape shortcodes in the database:
$message = str_replace([':(', ':)', ':|'], ['(sad)', '(happy)', '(neutral)'], $request->message);
Check Console for Errors
Uncaught ReferenceError: $ is not defined (missing jQuery) or emojis is not a function (incorrect path).Verify Emoji JSON
emojis.json) is valid JSON and accessible at the configured path.Disable Cache
php artisan cache:clear
php artisan view:clear
Custom Shortcodes
Extend the emoji set by modifying the JSON file or overriding the emojis.js file:
// Override the default emoji set
$.fn.emojis.defaults.emoji_set = '/custom/emojis.json';
Laravel Blade Directives Create a custom Blade directive for reusable emoji fields:
// AppServiceProvider.php
Blade::directive('emojiField', function ($expression) {
return "<textarea name=\"{$expression}\" class=\"emoji-enabled\"></textarea>
<script>$(document).ready(function(){$('.emoji-enabled').emojis();});</script>";
});
Usage:
@emojiField('comment')
Server-Side Emoji Rendering For non-JS environments, render emojis server-side using a helper:
// app/Helpers/EmojiHelper.php
function renderEmojis($text) {
$emojiMap = [
':smile:' => '😊',
':heart:' => '❤️',
];
return str_replace(array_keys($emojiMap), array_values($emojiMap), $text);
}
How can I help you explore Laravel packages today?