@@ -13,12 +13,72 @@ Installation
13
13
14
14
$ composer require symfony/ux-icons
15
15
16
+
16
17
Usage
17
18
-----
18
19
20
+ The ``symfony/ux-icons `` package aspires to offer simple and easy-to-use ways to
21
+ handle SVG icons in your Symfony application.
22
+
19
23
This package provides a ``ux_icon() `` Twig function to define the icons that you
20
24
want to include in the templates:
21
25
26
+
27
+ Icons
28
+ ------
29
+
30
+ The ``symfony/ux-icons `` package aspires to offer simple and easy-to-use ways to
31
+ handle SVG icons in your Symfony application.
32
+
33
+ SVG Icons
34
+ ~~~~~~~~~
35
+
36
+ [SVG](https://en.wikipedia.org/wiki/SVG) (Scalable Vector Graphics) is an XML-based
37
+ vector image format. SVGs are a great way to add scalable, resolution-independent
38
+ graphics to your website. They can be styled with CSS, combined, animated, reused..
39
+ for a fraction of the file size of bitmap images.
40
+
41
+ Making them the perfect choice for icons.
42
+
43
+
44
+ Icon Sets
45
+ ~~~~~~~~~
46
+
47
+ There are many icon sets available, each with their own unique style and set of icons.
48
+
49
+
50
+ Icon identifiers
51
+ ~~~~~~~~~~~~~~~~
52
+
53
+ In the UX Icons component, icons are referenced using an unique identifier that
54
+ follows one of the following syntaxes:
55
+
56
+ * ``prefix ``:``name `` (e.g. ``mdi:check ``, ``bi:check ``, ``editor:align-left ``)
57
+ * ``name `` only (e.g. ``check ``, ``close ``, ``menu ``)
58
+
59
+ Icon Name
60
+ ^^^^^^^^^
61
+
62
+ The ``name `` is the... name of the icon, without the file extension (e.g. ``check ``).
63
+
64
+ .. caution ::
65
+
66
+ The name must match a standard ``slug `` format: ``[a-z0-9-]+(-[0-9a-z])+ ``.
67
+
68
+ Icon Prefix
69
+ ^^^^^^^^^^^
70
+
71
+ Depending on your configuration, the ``prefix `` can be the name of an icon set, a directory
72
+ where the icon is located, or a combination of both.
73
+
74
+
75
+ For example, the `bi ` prefix refers to the Bootstrap Icons set, while the `header ` prefix
76
+ refers to the icons located in the `header ` directory.
77
+
78
+
79
+ Loading Icons
80
+ -------------
81
+
22
82
.. code-block :: twig
23
83
24
84
{# includes the contents of the 'assets/icons/user-profile.svg' file in the template #}
@@ -83,10 +143,40 @@ a subdirectory, the *name* will be ``subdirectory:icon_name``.
83
143
84
144
On-Demand Open Source Icons
85
145
~~~~~~~~~~~~~~~~~~~~~~~~~~~
146
+ Add your svg icons to the ``assets/icons/ `` directory and commit them.
147
+ The name of the file is used as the _name_ of the icon (``name.svg `` will be named ``name ``).
148
+ If located in a subdirectory, the _name_ will be ``sub-dir:name ``.
149
+
150
+ .. code-block :: text
151
+
152
+ your-project/
153
+ ├─ assets/
154
+ │ └─ icons/
155
+ │ ├─ bi/
156
+ │ │ └─ pause-circle.svg
157
+ │ │ └─ play-circle.svg
158
+ │ ├─ header/
159
+ │ │ ├─ logo.svg
160
+ │ │ └─ menu.svg
161
+ │ ├─ close.svg
162
+ │ ├─ menu.svg
163
+ │ └─ ...
164
+ └─ ...
165
+
166
+
167
+ Icons On-Demand
168
+ ~~~~~~~~~~~~~~~
169
+
170
+ `ux.symfony.com/icons `_ has a huge searchable repository of icons
171
+ from many different sets. This package provides a way to include any icon found on this site _on-demand _.
86
172
87
173
`ux.symfony.com/icons `_ has a huge searchable repository of icons from many
88
174
different sets. This package provides a way to include any icon found on this
89
175
site *on-demand *:
176
+ 1. Visit `ux.symfony.com/icons `_ and search for an icon
177
+ you'd like to use. Once you find one you like, copy one of the code snippets provided.
178
+ 2. Paste the snippet into your Twig template and the icon will be automatically fetched (and cached).
179
+ 3. That's it!
90
180
91
181
1. Visit `ux.symfony.com/icons `_ and search for an icon you'd like to use. Once
92
182
you find one you like, copy one of the code snippets provided.
@@ -128,6 +218,95 @@ the ``assets/icons/`` directory. You can think of importing an icon as *locking
128
218
129
219
Caching
130
220
-------
221
+ On-Demand VS Import
222
+ ^^^^^^^^^^^^^^^^^^^
223
+
224
+ While *on-demand * icons are great during development, they require http requests to fetch the icon
225
+ and always use the *latest version * of the icon. It's possible the icon could change or be removed
226
+ in the future. Additionally, the cache warming process will take significantly longer if using
227
+ many _on-demand _ icons. You can think of importing the icon as *locking it * (similar to how
228
+ ``composer.lock `` _locks_ your dependencies).
229
+
230
+
231
+ Rendering Icons
232
+ ---------------
233
+
234
+ .. code-block :: html+twig
235
+
236
+ {{ ux_icon('user-profile', {class: 'w-4 h-4'}) }} <!-- renders "user-profile.svg" -->
237
+
238
+ {{ ux_icon('sub-dir:user-profile', {class: 'w-4 h-4'}) }} <!-- renders "sub-dir/user-profile.svg" (sub-directory) -->
239
+
240
+ {{ ux_icon('flowbite:user-solid') }} <!-- renders "flowbite:user-solid" from ux.symfony.com -->
241
+
242
+ HTML Attributes
243
+ ~~~~~~~~~~~~~~~
244
+
245
+ The second argument of the ``ux_icon `` function is an array of attributes to add to the icon.
246
+
247
+ .. code-block :: twig
248
+
249
+ {# renders "user-profile.svg" with class="w-4 h-4" #}
250
+ {{ ux_icon('user-profile', {class: 'w-4 h-4'}) }}
251
+
252
+ {# renders "user-profile.svg" with class="w-4 h-4" and aria-hidden="true" #}
253
+ {{ ux_icon('user-profile', {class: 'w-4 h-4', 'aria-hidden': true}) }}
254
+
255
+ Default Attributes
256
+ ~~~~~~~~~~~~~~~~~~
257
+
258
+ You can set default attributes for all icons in your configuration. These attributes will be
259
+ added to all icons unless overridden by the second argument of the ``ux_icon `` function.
260
+
261
+ .. code-block :: yaml
262
+
263
+ # config/packages/ux_icons.yaml
264
+ ux_icons :
265
+ default_icon_attributes :
266
+ fill : currentColor
267
+
268
+ Now, all icons will have the ``fill `` attribute set to ``currentColor `` by default.
269
+
270
+ .. code-block :: twig
271
+
272
+ # renders "user-profile.svg" with fill="currentColor"
273
+ {{ ux_icon('user-profile') }}
274
+
275
+ # renders "user-profile.svg" with fill="red"
276
+ {{ ux_icon('user-profile', {fill: 'red'}) }}
277
+
278
+
279
+ HTML Syntax
280
+ ~~~~~~~~~~~
281
+
282
+ .. code-block :: html+twig
283
+
284
+ <twig:UX: Icon name="user-profile" />
285
+
286
+ {# Renders "user-profile.svg" #}
287
+ <twig:UX: Icon name="user-profile" class="w-4 h-4" />
288
+
289
+ {# Renders "sub-dir/user-profile.svg" (sub-directory) #}
290
+ <twig:UX: Icon name="sub-dir:user-profile" class="w-4 h-4" />
291
+
292
+ {# Renders "flowbite:user-solid" from ux.symfony.com #}
293
+ <twig:UX: Icon name="flowbite:user-solid" />
294
+
295
+
296
+ .. note ::
297
+
298
+ ``symfony/ux-twig-component `` is required to use the HTML syntax.
299
+
300
+
301
+ Performances
302
+ ------------
303
+
304
+ The UXIcon component is designed to be as fast as possible, while offering a
305
+ great deal of flexibility. The following are some of the optimizations made to
306
+ ensure the best performance possible.
307
+
308
+ Icon Caching
309
+ ~~~~~~~~~~~~
131
310
132
311
To avoid having to parse icon files on every request, icons are cached.
133
312
In production, you can pre-warm the cache by running the following command:
@@ -165,20 +344,55 @@ This command looks in all your Twig templates for ``ux_icon()`` calls and
165
344
166
345
If using `symfony/asset-mapper `_, the cache is warmed automatically when running ``asset-map:compile ``.
167
346
168
- Full Configuration Reference (UxIconsBundle)
169
- --------------------------------------------
347
+ TwigComponent
348
+ ~~~~~~~~~~~~~
349
+
350
+ The ``ux_icon `` function is optimized to be as fast as possible. To deliver the same level
351
+ of performance with the TwigComponent (``<twig:UX:Icon name="..." /> ``), the TwigComponent
352
+ usual overhead is reduced to the bare minimum, immediately calling the IconRenderer and
353
+ returning the HTML output.
354
+
355
+ .. warning ::
356
+
357
+ The <twig:UX: Icon> component does not support embedded content.
358
+
359
+ .. code-block :: twig+html
360
+
361
+ {# The 🧸 will be ignore in the HTML output #}
362
+ <twig:UX: Icon name="user-profile" class="w-4 h-4">🧸</twig:UX: Icon>
170
363
171
- The UXIconsBundle integrates seamlessly in Symfony applications. All these
172
- options are configured under the ``ux_icons `` key in your application configuration.
364
+ {# Renders "user-profile.svg" #}
365
+ <svg viewBox="0 0 24 24" class="w-4 h-4">
366
+ <path fill="currentColor" d="M21 7L9 19l-5.5-5.5l1.41-1.41L9 16.17L19.59 5.59z"/>
367
+ </svg>
368
+
369
+
370
+ Configuration
371
+ -------------
372
+
373
+ The UXIcon integrates seamlessly in Symfony applications. All these options are configured under
374
+ the ``ux_icons `` key in your application configuration.
375
+
376
+ .. code-block :: yaml
377
+
378
+ # config/packages/ux_icons.yaml
379
+ ux_icons :
380
+ {# ... # }
381
+
382
+
383
+ Debugging Configuration
384
+ ~~~~~~~~~~~~~~~~~~~~~~~
173
385
174
386
.. code-block :: terminal
175
387
176
- # displays the default config values defined by Symfony
388
+ # Displays the default config values
177
389
$ php bin/console config:dump-reference ux_icons
178
390
179
- # displays the actual config values used by your application
391
+ # Displays the actual config values used by your application
180
392
$ php bin/console debug:config ux_icons
181
393
394
+ Full Configuration
395
+ ~~~~~~~~~~~~~~~~~~
182
396
183
397
.. code-block :: yaml
184
398
@@ -202,6 +416,12 @@ options are configured under the ``ux_icons`` key in your application configurat
202
416
# The endpoint for the Iconify API.
203
417
endpoint : ' https://api.iconify.design'
204
418
419
+ Learn more
420
+ ----------
421
+
422
+ * :doc: `Creating and Using Templates </templates >`
423
+ * :doc: `How to manage CSS and JavaScript assets in Symfony applications </frontend >`
424
+
205
425
.. _`ux.symfony.com/icons` : https://ux.symfony.com/icons
206
426
.. _`Iconify` : https://iconify.design
207
427
.. _`symfony/asset-mapper` : https://symfony.com/doc/current/frontend/asset_mapper.html
0 commit comments