Skip to content

Commit 466df5c

Browse files
jouniweb-padawan
andauthored
feat: add icon visual size and stroke width custom CSS properties (#10160)
Co-authored-by: web-padawan <iamkulykov@gmail.com>
1 parent 89bdb78 commit 466df5c

14 files changed

+70
-3
lines changed

packages/icon/src/styles/vaadin-icon-base-styles.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const iconStyles = css`
2222
:host::after,
2323
:host::before {
2424
line-height: 1;
25-
font-size: 100cqh;
25+
font-size: var(--vaadin-icon-visual-size, 100cqh);
2626
-webkit-font-smoothing: antialiased;
2727
text-rendering: optimizeLegibility;
2828
-moz-osx-font-smoothing: grayscale;
@@ -34,10 +34,14 @@ export const iconStyles = css`
3434
3535
svg {
3636
display: block;
37-
width: 100%;
38-
height: 100%;
37+
width: var(--vaadin-icon-visual-size, 100%);
38+
height: var(--vaadin-icon-visual-size, 100%);
3939
/* prevent overflowing icon from clipping, see https://github.com/vaadin/flow-components/issues/5872 */
4040
overflow: visible;
41+
42+
@container style(--vaadin-icon-stroke-width) {
43+
stroke-width: var(--vaadin-icon-stroke-width);
44+
}
4145
}
4246
4347
:host(:is([icon-class], [font-icon-content])) svg {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ import { IconMixin } from './vaadin-icon-mixin.js';
4848
*
4949
* ### Styling
5050
*
51+
* The following custom CSS properties are available for styling:
52+
*
53+
* Custom CSS property | Description
54+
* -----------------------------|-------------
55+
* `--vaadin-icon-size` | Size (width and height) of the icon
56+
* `--vaadin-icon-stroke-width` | Stroke width of the SVG icon
57+
* `--vaadin-icon-visual-size` | Visual size of the icon
58+
*
5159
* The following state attributes are available for styling:
5260
*
5361
* Attribute | Description

packages/icon/src/vaadin-icon.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ import { ensureSvgLiteral } from './vaadin-icon-svg.js';
5656
*
5757
* ### Styling
5858
*
59+
* The following custom CSS properties are available for styling:
60+
*
61+
* Custom CSS property | Description
62+
* -----------------------------|-------------
63+
* `--vaadin-icon-size` | Size (width and height) of the icon
64+
* `--vaadin-icon-stroke-width` | Stroke width of the SVG icon
65+
* `--vaadin-icon-visual-size` | Visual size of the icon
66+
*
5967
* The following state attributes are available for styling:
6068
*
6169
* Attribute | Description
1.83 KB
Loading
1.05 KB
Loading
691 Bytes
Loading
1021 Bytes
Loading
647 Bytes
Loading

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { fixtureSync, nextFrame } from '@vaadin/testing-helpers';
22
import { visualDiff } from '@web/test-runner-visual-regression';
3+
import { Iconset } from '../../vaadin-iconset.js';
34
import { iconFontCss } from '../test-icon-font.js';
45

56
describe('icon', () => {
@@ -49,5 +50,51 @@ describe('icon', () => {
4950
it('font icons', async () => {
5051
await visualDiff(div, 'font-icons');
5152
});
53+
54+
it('font icons visual size', async () => {
55+
div.style.setProperty('--vaadin-icon-visual-size', '24px');
56+
await visualDiff(div, 'font-icons-visual-size');
57+
});
58+
});
59+
60+
describe('svg icon', () => {
61+
const template = document.createElement('template');
62+
template.innerHTML = `
63+
<svg viewBox="0 0 24 24">
64+
<defs>
65+
<g id="lucide:layers" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round">
66+
<path d="M12.83 2.18a2 2 0 0 0-1.66 0L2.6 6.08a1 1 0 0 0 0 1.83l8.58 3.91a2 2 0 0 0 1.66 0l8.58-3.9a1 1 0 0 0 0-1.83z" />
67+
<path d="M2 12a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 12" />
68+
<path d="M2 17a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 17" />
69+
</g>
70+
</defs>
71+
</svg>
72+
`;
73+
74+
Iconset.register('lucide', 24, template);
75+
76+
beforeEach(async () => {
77+
fixtureSync(`<vaadin-icon icon="lucide:layers"></vaadin-icon>`, div);
78+
await nextFrame();
79+
});
80+
81+
it('svg icon', async () => {
82+
await visualDiff(div, 'svg-icon');
83+
});
84+
85+
it('svg icon size', async () => {
86+
div.style.setProperty('--vaadin-icon-size', '32px');
87+
await visualDiff(div, 'svg-icon-size');
88+
});
89+
90+
it('svg icon visual size', async () => {
91+
div.style.setProperty('--vaadin-icon-visual-size', '32px');
92+
await visualDiff(div, 'svg-icon-visual-size');
93+
});
94+
95+
it('svg icon stroke width', async () => {
96+
div.style.setProperty('--vaadin-icon-stroke-width', '2px');
97+
await visualDiff(div, 'svg-icon-stroke-width');
98+
});
5299
});
53100
});
1.81 KB
Loading

0 commit comments

Comments
 (0)