silarhi / ux-icons-js-bundle
Render Symfony UX Icons in JavaScript, React, Preact and Vue, with the same attributes and configuration as ux_icon(), the PHP companion to @silarhi/ux-icons-js
Package info
github.com/silarhi/ux-icons-js-bundle
Type:symfony-bundle
pkg:composer/silarhi/ux-icons-js-bundle
Requires
- php: >=8.2
- ext-ctype: *
- ext-dom: *
- ext-libxml: *
- symfony/console: ^6.4 || ^7.0 || ^8.0
- symfony/event-dispatcher: ^6.4 || ^7.0 || ^8.0
- symfony/filesystem: ^6.4 || ^7.0 || ^8.0
- symfony/finder: ^6.4 || ^7.0 || ^8.0
- symfony/http-kernel: ^6.4 || ^7.0 || ^8.0
- symfony/ux-icons: ^2.36.1 || ^3.5
Requires (Dev)
- ergebnis/composer-normalize: ^2.58.0
- friendsofphp/php-cs-fixer: ^3.95.32
- phpstan/extension-installer: ^1.4.3
- phpstan/phpstan: ^2.3.7
- phpstan/phpstan-strict-rules: ^2.1.2
- phpstan/phpstan-symfony: ^2.1.1
- phpunit/phpunit: ^11.5.57 || ^12.5.38 || ^13.4.1
- rector/rector: ^2.9.2
- symfony/asset-mapper: ^6.4 || ^7.0 || ^8.0
- symfony/config: ^6.4 || ^7.0 || ^8.0
- symfony/dependency-injection: ^6.4 || ^7.0 || ^8.0
- symfony/error-handler: ^6.4 || ^7.0 || ^8.0
- symfony/framework-bundle: ^6.4 || ^7.0 || ^8.0
- symfony/http-client: ^6.4 || ^7.0 || ^8.0
- symfony/twig-bundle: ^6.4 || ^7.0 || ^8.0
Suggests
- symfony/asset-mapper: Maps the dumped icons as "var/icons" in the importmap
- symfony/framework-bundle: Register Silarhi\UxIconsJsBundle\SilarhiUxIconsJsBundle for zero-config wiring: the ux_icons configuration, the cache warmer, the ux-icons-js:dump command (optional: without it, build IconsDumper and DumpIconsCommand by hand, without symfony/dependency-injection nor symfony/config)
Provides
None
Conflicts
- symfony/config: <6.4
- symfony/dependency-injection: <6.4
- symfony/framework-bundle: <6.4
Replaces
None
README
UX Icons JS Bundle
Render Symfony UX Icons in JavaScript, React, Preact and Vue.
Same attributes, same configuration, same HTML as ux_icon(), without innerHTML.
📖 Documentation
A backport of symfony/ux#4072 on top of an unmodified symfony/ux-icons,
in two packages released from the same git tag:
silarhi/ux-icons-js-bundle(PHP): dumps your local icons, with the attributesux_icon()gives them, as a JavaScript module with one named export per icon.@silarhi/ux-icons-js(JS/TS): renders them in vanilla JavaScript, React, Preact or Vue.
Features
- Same output as
ux_icon():default_icon_attributes, icon seticon_attributes,suffixesandaliasescome from yourux_iconsconfiguration, and the attributes you give follow theux_icon()rules. - Tree-shaken:
lucide:trash-2is exported asLucideTrash2Icon, your bundler drops the icons you never import. - Dynamic names: an icon name from an API response loads that icon alone,
await loadIcon(loaders, data.icon),useIcon()in React, Preact and Vue, typed with the dumped names. <ux-icon name="lucide:trash-2">: a custom element for Twig templates, Turbo Streams and Stimulus, replaced by the<svg>ofux_icon().- No
innerHTML:createIconElement()builds SVG elements, the React, Preact and Vue components render elements. - Works with
ux:icons:lock: write the import first, runux:icons:lock, the icon is downloaded and dumped.
Requirements
PHP 8.2+, Symfony 6.4 / 7.x / 8.x, symfony/ux-icons 2.36.1+ or 3.5+ (3.x needs PHP 8.4; see the
compatibility notes for 2.x). React 18/19,
Preact 10/11 and Vue 3 are optional peer dependencies.
The package does not require symfony/framework-bundle, symfony/config nor symfony/dependency-injection: only the
bundle class uses them, see Without the bundle. symfony/ux-icons still requires the framework
until symfony/ux#4378 is released; the CI job testing without it comes then.
Installation
composer require silarhi/ux-icons-js-bundle pnpm add @silarhi/ux-icons-js
// config/bundles.php — after UXIconsBundle, whose configuration it reuses Silarhi\UxIconsJsBundle\SilarhiUxIconsJsBundle::class => ['all' => true],
Without the bundle
An application without a kernel builds the dumper and the ux-icons-js:dump command by hand, with the values of its
ux_icons configuration:
use Silarhi\UxIconsJsBundle\Command\DumpIconsCommand; use Silarhi\UxIconsJsBundle\IconResolver; use Silarhi\UxIconsJsBundle\IconsDumper; use Symfony\UX\Icons\IconFactory; use Symfony\UX\Icons\Registry\LocalSvgIconRegistry; $iconDir = __DIR__ . '/assets/icons'; // icon_dir $iconSetPaths = ['flags' => __DIR__ . '/assets/flags']; // icon_sets.*.path $aliases = ['delete' => 'lucide:trash-2']; // aliases $resolver = new IconResolver( new LocalSvgIconRegistry(new IconFactory(), $iconDir, $iconSetPaths), ['fill' => 'currentColor'], // default_icon_attributes $aliases, ['flags' => ['class' => 'flag']], // icon_sets.*.icon_attributes ); $dumper = new IconsDumper($resolver, $iconDir, $iconSetPaths, $aliases, __DIR__ . '/var/icons'); $application->addCommands([new DumpIconsCommand($dumper)]); // or $dumper->dump()
LocalSvgIconRegistry and IconFactory are internal to symfony/ux-icons, with the same constructor from 2.36.1 to
3.8. Without the bundle, nothing dumps the icons on cache warmup nor after ux:icons:import: run the command.
Quick start
The icons are dumped into var/icons/ on cache warmup, or with php bin/console ux-icons-js:dump:
import { createIconElement } from '@silarhi/ux-icons-js' import { UXIcon } from '@silarhi/ux-icons-js/react' // or '@silarhi/ux-icons-js/preact', '@silarhi/ux-icons-js/vue' import { LucideTrash2Icon } from '../var/icons/index.js' button.append(createIconElement(LucideTrash2Icon, { class: 'text-danger', 'aria-label': 'Delete' })) const icon = <UXIcon icon={LucideTrash2Icon} className="text-danger" aria-label="Delete" />
Documentation
Everything else is on ux-icons-js-bundle.silarhi.dev:
- Installation, quick start and live examples
- JavaScript: vanilla, dynamic names,
<ux-icon>, attributes, React, Preact, Vue - PHP: dumping icons, remote icons, Webpack Encore and AssetMapper
- Reference: configuration, compatibility, differences with symfony/ux#4072
Contributing
See CONTRIBUTING.md. Release notes are in CHANGELOG.md.
License
Released under the MIT License.