@@ -309,7 +309,6 @@ class View extends \yii\web\View
309309 */
310310 private array $ _assetBundleBuffers = [];
311311
312-
313312 /**
314313 * @var array
315314 * @see startJsImportBuffer()
@@ -335,6 +334,12 @@ class View extends \yii\web\View
335334 */
336335 private array $ _jsImports = [];
337336
337+ /**
338+ * @var string[] The icons that should be registered to the page.
339+ * @see registerIcons()
340+ */
341+ private array $ _icons = [];
342+
338343 /**
339344 * @var callable[][]
340345 */
@@ -1609,6 +1614,20 @@ public function registerTranslations(string $category, array $messages): void
16091614 $ this ->registerJs ($ js , self ::POS_BEGIN );
16101615 }
16111616
1617+ /**
1618+ * Registers icons for `Craft.ui.icon()`.
1619+ *
1620+ * @param string[] $icons The icons to be registered
1621+ * @since 5.7.0
1622+ */
1623+ public function registerIcons (array $ icons ): void
1624+ {
1625+ $ this ->_icons = [
1626+ ...$ this ->_icons ,
1627+ ...array_flip ($ icons ),
1628+ ];
1629+ }
1630+
16121631 /**
16131632 * Returns the active namespace.
16141633 *
@@ -2256,6 +2275,18 @@ protected function renderBodyEndHtml($ajaxMode): string
22562275 $ lines [] = implode ("\n" , $ this ->_html [self ::POS_END ]);
22572276 }
22582277
2278+ if (!empty ($ this ->_icons )) {
2279+ $ icons = [];
2280+ foreach (array_keys ($ this ->_icons ) as $ icon ) {
2281+ $ icons [$ icon ] = Cp::iconSvg ($ icon );
2282+ }
2283+ $ iconsJs = Json::encode ($ icons );
2284+ $ this ->js [self ::POS_END ][] = <<<JS
2285+ Craft.icons = $ iconsJs;
2286+ JS ;
2287+ $ this ->_icons = [];
2288+ }
2289+
22592290 $ html = parent ::renderBodyEndHtml ($ ajaxMode );
22602291
22612292 return empty ($ lines ) ? $ html : implode ("\n" , $ lines ) . $ html ;
0 commit comments