components/font-awesome
Shim repository for Font Awesome, providing the icon font, CSS/LESS/SASS, and webfonts via common package managers (Composer, npm, Bower, Component). Use it to include Font Awesome assets in your build pipeline and projects.
Install via Composer:
composer require components/font-awesome
This installs the package in vendor/components/font-awesome.
Set Up Asset Pipeline:
For Laravel Mix (Webpack):
Ensure your webpack.mix.js includes Font Awesome’s SASS files:
mix.sass('resources/scss/app.scss', 'public/css')
.options({
processCssUrls: false,
postCss: [require('postcss-import')()]
});
Then import Font Awesome in resources/scss/app.scss:
@import '~@fortawesome/fontawesome-free/scss/fontawesome';
@import '~@fortawesome/fontawesome-free/scss/solid';
@import '~@fortawesome/fontawesome-free/scss/regular';
@import '~@fortawesome/fontawesome-free/scss/brands';
For Vite:
Update vite.config.js:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: ['resources/scss/app.scss'],
refresh: true,
}),
],
});
Import Font Awesome in resources/scss/app.scss (same as above).
Move Font Files:
Copy the font files to your public directory for direct access:
mkdir -p public/fonts/fontawesome
cp -r vendor/components/font-awesome/webfonts/* public/fonts/fontawesome/
Update your CSS to reference the local fonts:
@font-face {
font-family: 'Font Awesome 6 Free';
src: url('../fonts/fontawesome/fa-solid-900.woff2') format('woff2'),
url('../fonts/fontawesome/fa-solid-900.woff') format('woff');
font-weight: 900;
font-style: normal;
}
First Usage: Use icons in Blade templates:
<i class="fas fa-user"></i> <!-- Solid icon -->
<i class="far fa-envelope"></i> <!-- Regular icon -->
<i class="fab fa-twitter"></i> <!-- Brand icon -->
Blade Templates:
<button class="p-2">
<i class="fas fa-search"></i>
</button>
// app/Helpers/IconHelper.php
namespace App\Helpers;
class IconHelper {
public static function render(string $icon, string $style = 'solid', string $prefix = 'fa'): string {
return "<i class=\"{$prefix}{$style} fa-{$icon}\"></i>";
}
}
Usage in Blade:
{{ App\Helpers\IconHelper::render('user', 'solid') }}
Tailwind CSS Integration: Combine Font Awesome with Tailwind utility classes for responsive and interactive icons:
<i class="fas fa-spinner fa-spin text-blue-500"></i>
<i class="fas fa-check-circle text-green-500 hover:text-green-700"></i>
Livewire/Alpine.js: Use icons for dynamic states:
<!-- Livewire example -->
<button wire:click="toggle" class="p-2">
<i class="fas fa-{{ $isActive ? 'check' : 'times' }}"></i>
</button>
<!-- Alpine.js example -->
<div x-data="{ open: false }">
<button @click="open = !open" class="p-2">
<i :class="open ? 'fas fa-times' : 'fas fa-bars'"></i>
</button>
</div>
SVG Icons (Advanced):
For better performance or customization, use SVG icons via a plugin like vite-plugin-svg-icons:
// vite.config.js
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import path from 'path';
export default defineConfig({
plugins: [
createSvgIconsPlugin({
iconDirs: [path.resolve(process.cwd(), 'resources/icons')],
symbolId: 'icon-[dir]-[name]',
}),
],
});
Then reference SVG icons in your templates:
<svg class="w-6 h-6 text-blue-500">
<use href="#icon-user" />
</svg>
Icon Selection:
Theming: Customize icon colors and styles via CSS:
.text-primary {
color: #3b82f6; // Tailwind blue-500
}
.fa-icon {
@apply text-primary;
}
Lazy Loading: Load icons dynamically to improve performance:
<div class="icon-container">
<i class="fas fa-user lazy-icon" data-icon="user"></i>
</div>
<script>
document.querySelectorAll('.lazy-icon').forEach(icon => {
const style = document.createElement('style');
style.textContent = `
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css');
`;
document.head.appendChild(style);
});
</script>
Accessibility: Ensure icons are accessible by adding ARIA labels or screen reader-only text:
<i class="fas fa-search" aria-hidden="true"></i>
<span class="sr-only">Search</span>
Laravel Mix/Vite:
purgecss or unplugin-icons to remove unused icon styles from your CSS bundle.unplugin-icons:
// vite.config.js
import Icons from 'unplugin-icons/vite';
import { FileSystemIconLoader } from 'unplugin-icons/loaders';
export default defineConfig({
plugins: [
Icons({
compiler: 'vue',
customLoader: FileSystemIconLoader('./resources/icons'),
}),
],
});
CDN Fallback: For faster loading, use a CDN and cache it:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="..." crossorigin="anonymous">
Versioning:
composer.json to avoid unexpected updates:
"components/font-awesome": "^6.4.0"
Testing:
Font Loading Issues:
public directory and the CSS @font-face rules point to the correct paths. Test in incognito mode to rule out caching issues.Breaking Changes in v6.x:
fa- to fa-solid, fa-regular, etc., which can break existing code.Asset Pipeline Conflicts:
<!-- Font Awesome -->
<i class="fas fa-user"></i>
<!-- Heroicons -->
<svg class="h-6 w-6 text-gray-800" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h1
How can I help you explore Laravel packages today?