Skip to content

Commit 2fd3b23

Browse files
authored
refactor: rename icon.font to icon.iconClass (#6465)
1 parent 043dbb1 commit 2fd3b23

6 files changed

Lines changed: 67 additions & 63 deletions

File tree

‎packages/icon/src/vaadin-icon-font-size-mixin.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const IconFontSizeMixin = dedupingMixin((superclass) =>
3434
? superclass
3535
: class extends ResizeMixin(superclass) {
3636
static get observers() {
37-
return ['__iconFontSizeMixinfontChanged(font, char, ligature)'];
37+
return ['__iconFontSizeMixinfontChanged(iconClass, char, ligature)'];
3838
}
3939

4040
/** @protected */
@@ -46,8 +46,8 @@ export const IconFontSizeMixin = dedupingMixin((superclass) =>
4646
}
4747

4848
/** @private */
49-
__iconFontSizeMixinfontChanged(_font, _char, _ligature) {
50-
// Update when font or char changes
49+
__iconFontSizeMixinfontChanged(_iconClass, _char, _ligature) {
50+
// Update when iconClass, char or ligature changes
5151
this.__updateFontIconSize();
5252
}
5353

@@ -61,12 +61,12 @@ export const IconFontSizeMixin = dedupingMixin((superclass) =>
6161
}
6262

6363
/**
64-
* Updates the --_vaadin-font-icon-size CSS variable value if char or font is set (font icons in use).
64+
* Updates the --_vaadin-font-icon-size CSS variable value if font icons are used.
6565
*
6666
* @private
6767
*/
6868
__updateFontIconSize() {
69-
if (this.char || this.font || this.ligature) {
69+
if (this.char || this.iconClass || this.ligature) {
7070
const { paddingTop, paddingBottom, height } = getComputedStyle(this);
7171
const fontIconSize = parseFloat(height) - parseFloat(paddingTop) - parseFloat(paddingBottom);
7272
this.style.setProperty('--_vaadin-font-icon-size', `${fontIconSize}px`);

‎packages/icon/src/vaadin-icon.d.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ declare class Icon extends ThemableMixin(
8484
/**
8585
* Class names defining an icon font and/or a specific glyph inside an icon font.
8686
*
87-
* @attr {string} font
87+
* Example: "fa-solid fa-user"
88+
*
89+
* @attr {string} icon-class
8890
*/
89-
font: string | null;
91+
iconClass: string | null;
9092

9193
/**
9294
* A hexadecimal code point that specifies a glyph from an icon font.

‎packages/icon/src/vaadin-icon.js‎

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Icon extends ThemableMixin(ElementMixin(ControllerMixin(SlotStylesMixin(Ic
9292
height: 100%;
9393
}
9494
95-
:host(:is([font], [font-icon-content])) svg {
95+
:host(:is([icon-class], [font-icon-content])) svg {
9696
display: none;
9797
}
9898
@@ -160,10 +160,12 @@ class Icon extends ThemableMixin(ElementMixin(ControllerMixin(SlotStylesMixin(Ic
160160
/**
161161
* Class names defining an icon font and/or a specific glyph inside an icon font.
162162
*
163-
* @attr {string} font
163+
* Example: "fa-solid fa-user"
164+
*
165+
* @attr {string} icon-class
164166
* @type {string}
165167
*/
166-
font: {
168+
iconClass: {
167169
type: String,
168170
reflectToAttribute: true,
169171
},
@@ -226,7 +228,7 @@ class Icon extends ThemableMixin(ElementMixin(ControllerMixin(SlotStylesMixin(Ic
226228
}
227229

228230
static get observers() {
229-
return ['__svgChanged(svg, __svgElement)', '__fontChanged(font, char, ligature)', '__srcChanged(src)'];
231+
return ['__svgChanged(svg, __svgElement)', '__fontChanged(iconClass, char, ligature)', '__srcChanged(src)'];
230232
}
231233

232234
static get observedAttributes() {
@@ -244,7 +246,7 @@ class Icon extends ThemableMixin(ElementMixin(ControllerMixin(SlotStylesMixin(Ic
244246
const tag = this.localName;
245247
return [
246248
`
247-
${tag}[font] {
249+
${tag}[icon-class] {
248250
display: inline-flex;
249251
vertical-align: middle;
250252
font-size: inherit;
@@ -254,8 +256,8 @@ class Icon extends ThemableMixin(ElementMixin(ControllerMixin(SlotStylesMixin(Ic
254256
}
255257

256258
/** @private */
257-
get __fontClasses() {
258-
return this.font ? this.font.split(' ') : [];
259+
get __iconClasses() {
260+
return this.iconClass ? this.iconClass.split(' ') : [];
259261
}
260262

261263
/** @protected */
@@ -370,11 +372,11 @@ class Icon extends ThemableMixin(ElementMixin(ControllerMixin(SlotStylesMixin(Ic
370372
}
371373

372374
/** @private */
373-
__fontChanged(font, char, ligature) {
374-
this.classList.remove(...(this.__addedFontClasses || []));
375-
if (font) {
376-
this.__addedFontClasses = [...this.__fontClasses];
377-
this.classList.add(...this.__addedFontClasses);
375+
__fontChanged(iconClass, char, ligature) {
376+
this.classList.remove(...(this.__addedIconClasses || []));
377+
if (iconClass) {
378+
this.__addedIconClasses = [...this.__iconClasses];
379+
this.classList.add(...this.__addedIconClasses);
378380
}
379381

380382
if (char) {
@@ -385,7 +387,7 @@ class Icon extends ThemableMixin(ElementMixin(ControllerMixin(SlotStylesMixin(Ic
385387
this.removeAttribute('font-icon-content');
386388
}
387389

388-
if ((font || char || ligature) && !this.icon) {
390+
if ((iconClass || char || ligature) && !this.icon) {
389391
// The "icon" attribute needs to be set on the host also when using font icons
390392
// to avoid issues such as https://github.com/vaadin/web-components/issues/6301
391393
this.icon = '';
@@ -397,8 +399,8 @@ class Icon extends ThemableMixin(ElementMixin(ControllerMixin(SlotStylesMixin(Ic
397399
super.attributeChangedCallback(name, oldValue, newValue);
398400

399401
// Make sure class list always contains all the font class names
400-
if (name === 'class' && this.__fontClasses.some((className) => !this.classList.contains(className))) {
401-
this.classList.add(...this.__fontClasses);
402+
if (name === 'class' && this.__iconClasses.some((className) => !this.classList.contains(className))) {
403+
this.classList.add(...this.__iconClasses);
402404
}
403405
}
404406

‎packages/icon/test/icon-font.test.js‎

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('vaadin-icon - icon fonts', () => {
4040
let icon;
4141

4242
beforeEach(async () => {
43-
icon = fixtureSync('<vaadin-icon font="my-icon-font icon-before"></vaadin-icon>');
43+
icon = fixtureSync('<vaadin-icon icon-class="my-icon-font icon-before"></vaadin-icon>');
4444
await onceResized(icon);
4545
});
4646

@@ -81,79 +81,79 @@ describe('vaadin-icon - icon fonts', () => {
8181
});
8282
});
8383

84-
describe('font', () => {
84+
describe('iconClass', () => {
8585
let icon;
8686

87-
it('should add the font to element class list', () => {
88-
icon = fixtureSync('<vaadin-icon font="my-icon-font icon-before"></vaadin-icon>');
87+
it('should add the icon class to element class list', () => {
88+
icon = fixtureSync('<vaadin-icon icon-class="my-icon-font icon-before"></vaadin-icon>');
8989
expect(icon.classList.contains('my-icon-font')).to.be.true;
9090
expect(icon.classList.contains('icon-before')).to.be.true;
9191
});
9292

9393
it('should not overwrite existing classes', () => {
9494
icon = fixtureSync('<vaadin-icon class="foo"></vaadin-icon>');
95-
icon.font = 'my-icon-font icon-before';
95+
icon.iconClass = 'my-icon-font icon-before';
9696
expect(icon.classList.contains('foo')).to.be.true;
9797
});
9898

99-
it('should change font classes', () => {
100-
icon = fixtureSync('<vaadin-icon class="foo" font="my-icon-font icon-before"></vaadin-icon>');
101-
icon.font = 'my-icon-font icon-after';
99+
it('should change icon classes', () => {
100+
icon = fixtureSync('<vaadin-icon class="foo" icon-class="my-icon-font icon-before"></vaadin-icon>');
101+
icon.iconClass = 'my-icon-font icon-after';
102102
expect(icon.classList.contains('icon-before')).to.be.false;
103103
expect(icon.classList.contains('icon-after')).to.be.true;
104104
});
105105

106-
it('should remove all font classes', () => {
107-
icon = fixtureSync('<vaadin-icon class="foo" font="my-icon-font icon-before"></vaadin-icon>');
108-
icon.font = '';
106+
it('should remove all icon classes', () => {
107+
icon = fixtureSync('<vaadin-icon class="foo" icon-class="my-icon-font icon-before"></vaadin-icon>');
108+
icon.iconClass = '';
109109
expect(icon.classList.contains('my-icon-font')).to.be.false;
110110
expect(icon.classList.contains('icon-before')).to.be.false;
111111
expect(icon.classList.contains('foo')).to.be.true;
112112
});
113113

114-
it('should restore the font to element class list', () => {
115-
icon = fixtureSync('<vaadin-icon font="my-icon-font icon-before"></vaadin-icon>');
114+
it('should restore the icon class to element class list', () => {
115+
icon = fixtureSync('<vaadin-icon icon-class="my-icon-font icon-before"></vaadin-icon>');
116116
icon.className = 'foo';
117117
expect(icon.classList.contains('my-icon-font')).to.be.true;
118118
expect(icon.classList.contains('icon-before')).to.be.true;
119119
expect(icon.classList.contains('foo')).to.be.true;
120120
});
121121

122-
it('should restore the missing font to element class list', () => {
123-
icon = fixtureSync('<vaadin-icon font="my-icon-font icon-before"></vaadin-icon>');
122+
it('should restore the missing icon class to element class list', () => {
123+
icon = fixtureSync('<vaadin-icon icon-class="my-icon-font icon-before"></vaadin-icon>');
124124
icon.className = 'my-icon-font';
125125
expect(icon.classList.contains('my-icon-font')).to.be.true;
126126
expect(icon.classList.contains('icon-before')).to.be.true;
127127
});
128128

129-
it('should restore the removed font to element class list', () => {
130-
icon = fixtureSync('<vaadin-icon font="my-icon-font icon-before"></vaadin-icon>');
129+
it('should restore the removed icon class to element class list', () => {
130+
icon = fixtureSync('<vaadin-icon icon-class="my-icon-font icon-before"></vaadin-icon>');
131131
icon.classList.remove('my-icon-font');
132132
expect(icon.classList.contains('my-icon-font')).to.be.true;
133133
expect(icon.classList.contains('icon-before')).to.be.true;
134134
});
135135

136-
it('should reflect font as an attribute', () => {
136+
it('should reflect icon class as an attribute', () => {
137137
icon = fixtureSync('<vaadin-icon></vaadin-icon>');
138-
icon.font = 'my-icon-font icon-before';
139-
expect(icon.getAttribute('font')).to.equal('my-icon-font icon-before');
138+
icon.iconClass = 'my-icon-font icon-before';
139+
expect(icon.getAttribute('icon-class')).to.equal('my-icon-font icon-before');
140140
});
141141

142-
it('should add icon attribute if font is set', () => {
142+
it('should add icon attribute if icon class is set', () => {
143143
icon = fixtureSync('<vaadin-icon></vaadin-icon>');
144-
icon.font = 'my-icon-font icon-before';
144+
icon.iconClass = 'my-icon-font icon-before';
145145
expect(icon.hasAttribute('icon')).to.be.true;
146146
});
147147

148-
it('should not add icon attribute if font is not set', () => {
148+
it('should not add icon attribute if icon class is not set', () => {
149149
icon = fixtureSync('<vaadin-icon></vaadin-icon>');
150-
icon.font = null;
150+
icon.iconClass = null;
151151
expect(icon.hasAttribute('icon')).to.be.false;
152152
});
153153

154154
it('should not override existing icon', () => {
155155
icon = fixtureSync('<vaadin-icon icon="foo:bar"></vaadin-icon>');
156-
icon.font = 'my-icon-font icon-before';
156+
icon.iconClass = 'my-icon-font icon-before';
157157
expect(icon.icon).to.equal('foo:bar');
158158
});
159159
});
@@ -162,19 +162,19 @@ describe('vaadin-icon - icon fonts', () => {
162162
let icon;
163163

164164
it('should reflect unprefixed char as font-icon-content attribute', () => {
165-
icon = fixtureSync('<vaadin-icon font="my-icon-font"></vaadin-icon>');
165+
icon = fixtureSync('<vaadin-icon icon-class="my-icon-font"></vaadin-icon>');
166166
icon.char = 'e900';
167167
expect(icon.getAttribute('font-icon-content')).to.equal('\ue900');
168168
});
169169

170170
it('should reflect prefixed char as font-icon-content attribute', () => {
171-
icon = fixtureSync('<vaadin-icon font="my-icon-font"></vaadin-icon>');
171+
icon = fixtureSync('<vaadin-icon icon-class="my-icon-font"></vaadin-icon>');
172172
icon.char = '\ue900';
173173
expect(icon.getAttribute('font-icon-content')).to.equal('\ue900');
174174
});
175175

176176
it('should remove font-icon-content attribute', () => {
177-
icon = fixtureSync('<vaadin-icon font="my-icon-font"></vaadin-icon>');
177+
icon = fixtureSync('<vaadin-icon icon-class="my-icon-font"></vaadin-icon>');
178178
icon.char = 'e900';
179179
icon.char = null;
180180
expect(icon.hasAttribute('font-icon-content')).to.be.false;
@@ -206,13 +206,13 @@ describe('vaadin-icon - icon fonts', () => {
206206
let icon;
207207

208208
it('should reflect ligature as font-icon-content attribute', () => {
209-
icon = fixtureSync('<vaadin-icon font="my-icon-font"></vaadin-icon>');
209+
icon = fixtureSync('<vaadin-icon icon-class="my-icon-font"></vaadin-icon>');
210210
icon.ligature = 'foo';
211211
expect(icon.getAttribute('font-icon-content')).to.equal('foo');
212212
});
213213

214214
it('should remove font-icon-content attribute', () => {
215-
icon = fixtureSync('<vaadin-icon font="my-icon-font"></vaadin-icon>');
215+
icon = fixtureSync('<vaadin-icon icon-class="my-icon-font"></vaadin-icon>');
216216
icon.ligature = 'foo';
217217
icon.ligature = null;
218218
expect(icon.hasAttribute('font-icon-content')).to.be.false;
@@ -280,8 +280,8 @@ describe('vaadin-icon - icon fonts', () => {
280280
expect(supportsCQUnitsForPseudoElements()).to.be.false;
281281
});
282282

283-
fallBackIt('should have the custom property (font)', async () => {
284-
icon = fixtureSync('<vaadin-icon font="foo"></vaadin-icon>');
283+
fallBackIt('should have the custom property (iconClass)', async () => {
284+
icon = fixtureSync('<vaadin-icon icon-class="foo"></vaadin-icon>');
285285
await nextFrame();
286286
expect(icon.style.getPropertyValue('--_vaadin-font-icon-size')).to.equal('24px');
287287
});
@@ -301,12 +301,12 @@ describe('vaadin-icon - icon fonts', () => {
301301
fallBackIt('should set the custom property', async () => {
302302
icon = fixtureSync('<vaadin-icon></vaadin-icon>');
303303
await nextFrame();
304-
icon.font = 'foo';
304+
icon.iconClass = 'foo';
305305
expect(icon.style.getPropertyValue('--_vaadin-font-icon-size')).to.equal('24px');
306306
});
307307

308308
fallBackIt('should update the custom property', async () => {
309-
icon = fixtureSync('<vaadin-icon font="foo"></vaadin-icon>');
309+
icon = fixtureSync('<vaadin-icon icon-class="foo"></vaadin-icon>');
310310
await nextFrame();
311311
icon.style.width = '100px';
312312
icon.style.height = '100px';

‎packages/icon/test/typings/icon.types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ assertType<ControllerMixinClass>(icon);
99

1010
assertType<IconSvgLiteral | null>(icon.svg);
1111

12-
assertType<string | null>(icon.font);
12+
assertType<string | null>(icon.iconClass);
1313
assertType<string | null>(icon.char);
1414
assertType<string | null>(icon.ligature);
1515
assertType<string | null>(icon.fontFamily);

‎packages/icon/test/visual/icon.common.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ describe('icon', () => {
2727

2828
fixtureSync(
2929
`
30-
<!-- Font icons using class names -->
31-
<vaadin-icon font="my-icon-font icon-before"></vaadin-icon>
32-
<vaadin-icon font="my-icon-font icon-after"></vaadin-icon>
33-
<vaadin-icon font="my-icon-font icon-ligature"></vaadin-icon>
30+
<!-- Font icons using icon class -->
31+
<vaadin-icon icon-class="my-icon-font icon-before"></vaadin-icon>
32+
<vaadin-icon icon-class="my-icon-font icon-after"></vaadin-icon>
33+
<vaadin-icon icon-class="my-icon-font icon-ligature"></vaadin-icon>
3434
3535
<!-- Font icons using char and ligature -->
36-
<vaadin-icon font="my-icon-font" char="e900"></vaadin-icon>
37-
<vaadin-icon font="my-icon-font" ligature="figma"></vaadin-icon>
36+
<vaadin-icon icon-class="my-icon-font" char="e900"></vaadin-icon>
37+
<vaadin-icon icon-class="my-icon-font" ligature="figma"></vaadin-icon>
3838
<vaadin-icon style="font-family: 'My icons'" char="e900"></vaadin-icon>
3939
4040
<!-- Font icons using fontFamily -->

0 commit comments

Comments
 (0)