Skip to content

Commit 4bab7d9

Browse files
authored
feat(icon): support custom naming and selector
This PR allows for custom selector strategies for icons. Especially for Fontawesome Pro there are icons which share the exact same words but in a different order ("foo-bar" and "bar-foo" are 2 different icons). This wouldn't work using the default fui logic i.icon.foo.bar::before {} Now, it's possible to adjust 3 new variables which would allow different icon class selectors 1. Ordered selector @iconClassSeparator: " "; // a space separator automatically forces order // or @iconForcedOrder: true; results in i.icon[class*="foo bar"]::before {} 2. Dashed one word selector @iconClassSeparator: "-"; results in i.icon.foo-bar::before {} 3. Use data attribute instead of classname (this was originally planned as default for 3.0) and optionall use a custom class separator @iconClassSeparator: "_"; @iconForcedOrder: true; @iconForcedAttribute: data-icon results in i.icon[data-icon*="foo_bar"]::before {} I also simplified the generateIcon function so it can be reused for duotone icons (because the only difference is the pseudo selector)
1 parent 2ec6af4 commit 4bab7d9

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

src/definitions/elements/icon.less

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,14 @@ and ((@variationIconBordered) or (@variationIconCircular)) {
513513
}
514514
}
515515

516-
.generateIcons(@map, @fontFamily) {
516+
.generateIcons(@map, @fontFamily: false, @pseudo: before) {
517517
each(@map, {
518518
@escapedKey: replace(@key, "^([0-9])", "\3$1 ");
519-
@normalizedKey: replace(@escapedKey, "_", ".", "g");
520-
i.icon.@{normalizedKey}::before {
519+
@normalizedKey: replace(@escapedKey, "_", @iconClassSeparator, "g");
520+
@unorderedKey: e(%(".%s::%s",@normalizedKey, @pseudo));
521+
@orderedKey: e(%('[%s*="%s"]::%s', @iconForcedAttribute, @normalizedKey, @pseudo));
522+
@selectorKey: if(@iconForcedOrder or @iconClassSeparator = " ", @orderedKey, @unorderedKey);
523+
i.icon@{selectorKey} {
521524
content: "@{value}";
522525
& when not (@fontFamily = false) {
523526
font-family: @fontFamily;
@@ -526,19 +529,9 @@ and ((@variationIconBordered) or (@variationIconCircular)) {
526529
});
527530
};
528531

529-
.generateSecondaryIcons(@map) {
530-
each(@map, {
531-
@escapedKey: replace(@key, "^([0-9])", "\3$1 ");
532-
@normalizedKey: replace(@escapedKey, "_", ".", "g");
533-
i.icon.@{normalizedKey}::after {
534-
content: "@{value}";
535-
}
536-
});
537-
};
538-
539532
& when (@variationIconDeprecated) {
540533
/* Deprecated *In/Out Naming Conflict) */
541-
.generateIcons(@icon-deprecated-map, false);
534+
.generateIcons(@icon-deprecated-map);
542535
}
543536

544537
& when (@variationIconSolid) {
@@ -547,10 +540,10 @@ and ((@variationIconBordered) or (@variationIconCircular)) {
547540
*******************************/
548541

549542
/* Icons */
550-
.generateIcons(@icon-map, false);
543+
.generateIcons(@icon-map);
551544
& when (@variationIconAliases) {
552545
/* Aliases */
553-
.generateIcons(@icon-aliases-map, false);
546+
.generateIcons(@icon-aliases-map);
554547
}
555548
}
556549

@@ -564,10 +557,10 @@ and ((@variationIconBordered) or (@variationIconCircular)) {
564557
}
565558

566559
/* Icons */
567-
.generateIcons(@icon-outline-map, false);
560+
.generateIcons(@icon-outline-map);
568561
& when (@variationIconAliases) {
569562
/* Aliases */
570-
.generateIcons(@icon-outline-aliases-map, false);
563+
.generateIcons(@icon-outline-aliases-map);
571564
}
572565
}
573566

@@ -580,10 +573,10 @@ and ((@variationIconBordered) or (@variationIconCircular)) {
580573
i.icon.thin {
581574
font-family: @thinFontName;
582575
}
583-
.generateIcons(@icon-thin-map, false);
576+
.generateIcons(@icon-thin-map);
584577
& when (@variationIconAliases) {
585578
/* Aliases */
586-
.generateIcons(@icon-thin-aliases-map, false);
579+
.generateIcons(@icon-thin-aliases-map);
587580
}
588581
}
589582

@@ -630,13 +623,13 @@ and ((@variationIconBordered) or (@variationIconCircular)) {
630623
opacity: @duotoneSecondaryOpacity;
631624
}
632625

633-
.generateIcons(@icon-duotone-map, false);
634-
.generateSecondaryIcons(@icon-duotone-secondary-map);
626+
.generateIcons(@icon-duotone-map);
627+
.generateIcons(@icon-duotone-secondary-map, false, after);
635628

636629
& when (@variationIconAliases) {
637630
/* Aliases */
638-
.generateIcons(@icon-duotone-aliases-map, false);
639-
.generateSecondaryIcons(@icon-duotone-secondary-aliases-map);
631+
.generateIcons(@icon-duotone-aliases-map);
632+
.generateIcons(@icon-duotone-secondary-aliases-map, false, after);
640633
}
641634

642635
/*

src/themes/default/elements/icon.variables

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ Icons are order A-Z in their group, Solid, Outline, Thin (Pro only) and Brand
7878
};
7979
};
8080

81-
// Underscores in map keys will be replaced by dots to separate classnames at compile time
81+
// Underscores in map keys will be replaced by @iconClassSeparator to separate classnames at compile time
82+
@iconClassSeparator: ".";
83+
@iconForcedOrder: false;
84+
@iconForcedAttribute: class;
8285

8386
/* Deprecated (In/Out Naming Conflict) */
8487
@icon-deprecated-map: {

0 commit comments

Comments
 (0)