Skip to content

Commit 4f20d2c

Browse files
authored
refactor: allow Aura badges to inherit the accent color from the container (#11282)
1 parent 06aeb82 commit 4f20d2c

File tree

3 files changed

+62
-58
lines changed

3 files changed

+62
-58
lines changed

dev/aura/colors.html

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import '@vaadin/button';
2323
import '@vaadin/checkbox';
2424
import '@vaadin/checkbox-group';
25+
import '@vaadin/icon';
26+
import '@vaadin/icons';
2527
import '@vaadin/side-nav';
2628
import '@vaadin/tabs';
2729

@@ -67,9 +69,12 @@
6769
accentColors.forEach((color, i) => {
6870
section.querySelectorAll(`:scope > :nth-child(${i + 1}) vaadin-badge`).forEach((badge) => {
6971
badge.classList.toggle('aura-accent-' + color.toLowerCase(), e.target.checked);
70-
badge.textContent = e.target.checked ? color : 'Accent';
72+
badge.lastChild.textContent = e.target.checked ? color : 'Accent';
7173
});
7274
});
75+
section.querySelectorAll(`:scope > :nth-child(${accentColors.length + 1}) vaadin-badge`).forEach((badge) => {
76+
badge.classList.toggle('aura-accent-color', e.target.checked);
77+
});
7378
});
7479
});
7580
</script>
@@ -246,19 +251,28 @@ <h3 class="heading">Accent Colors</h3>
246251
<section class="section generate-variations">
247252
<vaadin-button>
248253
Button
249-
<vaadin-badge class="aura-accent-color" slot="suffix">Accent</vaadin-badge>
254+
<vaadin-badge slot="suffix">
255+
<vaadin-icon icon="vaadin:check" slot="icon"></vaadin-icon>
256+
Accent
257+
</vaadin-badge>
250258
</vaadin-button>
251259
</section>
252260

253261
<section class="section generate-variations">
254262
<vaadin-tabs>
255263
<vaadin-tab>
256264
Tab
257-
<vaadin-badge class="aura-accent-color">Accent</vaadin-badge>
265+
<vaadin-badge>
266+
<vaadin-icon icon="vaadin:check" slot="icon"></vaadin-icon>
267+
Accent
268+
</vaadin-badge>
258269
</vaadin-tab>
259270
<vaadin-tab>
260271
Tab
261-
<vaadin-badge class="aura-accent-color">Accent</vaadin-badge>
272+
<vaadin-badge>
273+
<vaadin-icon icon="vaadin:check" slot="icon"></vaadin-icon>
274+
Accent
275+
</vaadin-badge>
262276
</vaadin-tab>
263277
</vaadin-tabs>
264278
</section>
@@ -267,11 +281,17 @@ <h3 class="heading">Accent Colors</h3>
267281
<vaadin-side-nav>
268282
<vaadin-side-nav-item path="">
269283
Nav Item
270-
<vaadin-badge class="aura-accent-color" slot="suffix">Accent</vaadin-badge>
284+
<vaadin-badge slot="suffix">
285+
<vaadin-icon icon="vaadin:check" slot="icon"></vaadin-icon>
286+
Accent
287+
</vaadin-badge>
271288
</vaadin-side-nav-item>
272289
<vaadin-side-nav-item path="foo">
273290
Nav Item
274-
<vaadin-badge class="aura-accent-color" slot="suffix">Accent</vaadin-badge>
291+
<vaadin-badge slot="suffix">
292+
<vaadin-icon icon="vaadin:check" slot="icon"></vaadin-icon>
293+
Accent
294+
</vaadin-badge>
275295
</vaadin-side-nav-item>
276296
</vaadin-side-nav>
277297
</section>

packages/aura/src/color.css

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -249,34 +249,3 @@ vaadin-upload-file,
249249
--aura-accent-color-dark: var(--aura-blue);
250250
--aura-accent-color: light-dark(var(--aura-accent-color-light), var(--aura-accent-color-dark));
251251
}
252-
253-
/* Restore accent color for child elements like badges */
254-
:where(vaadin-button, vaadin-upload-button, vaadin-menu-bar-button, vaadin-drawer-toggle) > *,
255-
:where(vaadin-tab) > *,
256-
:where(vaadin-side-nav-item) > * {
257-
--aura-accent-color-light: var(--aura-accent-color-light-initial);
258-
--aura-accent-color-dark: var(--aura-accent-color-dark-initial);
259-
--aura-accent-color: light-dark(var(--aura-accent-color-light-initial), var(--aura-accent-color-dark-initial));
260-
}
261-
262-
/* Badges and other content that uses contrast color inside filled variants */
263-
:is(vaadin-button, vaadin-upload-button, vaadin-menu-bar-button, vaadin-drawer-toggle)[theme~='primary'] > *,
264-
vaadin-tabs[theme~='filled'] > vaadin-tab[selected] > :not([slot='tooltip']),
265-
vaadin-side-nav[theme~='filled'] > vaadin-side-nav-item[current] > :not([slot='children']):not([slot='tooltip']),
266-
:is(
267-
vaadin-combo-box-item,
268-
vaadin-context-menu-item,
269-
vaadin-item,
270-
vaadin-menu-bar-item,
271-
vaadin-multi-select-combo-box-item,
272-
vaadin-select-item[role]
273-
)[theme~='filled']:not([disabled], [aria-disabled='true']):hover
274-
> * {
275-
--vaadin-icon-color: currentColor;
276-
--vaadin-text-color: currentColor;
277-
--vaadin-text-color-secondary: color-mix(in srgb, currentColor 70%, transparent);
278-
--vaadin-text-color-disabled: color-mix(in srgb, currentColor 50%, transparent);
279-
--aura-accent-text-color: currentColor;
280-
--aura-accent-border-color: color-mix(in srgb, currentColor 30%, transparent);
281-
--aura-accent-surface: color-mix(in srgb, currentColor 10%, transparent);
282-
}

packages/aura/src/components/badge.css

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,48 @@ vaadin-badge:is([theme~='filled'], [theme~='dot']) {
1111
color: var(--aura-accent-contrast-color);
1212
}
1313

14-
/* Special case for filled badges within filled variants */
15-
/* TODO is there a way to solve this without explicitly listing the special cases? */
16-
:is(vaadin-button, vaadin-menu-bar-button, vaadin-drawer-toggle)[theme~='primary'],
17-
vaadin-tabs[theme~='filled'] > vaadin-tab[selected],
18-
vaadin-side-nav[theme~='filled'] > vaadin-side-nav-item[current],
14+
/* Inside filled component variants, increase the opacity of default badge variants so that they retain their color */
15+
:is(vaadin-button, vaadin-upload-button, vaadin-menu-bar-button, vaadin-drawer-toggle)[theme~='primary'] > *,
16+
vaadin-tabs[theme~='filled'] > vaadin-tab[selected] > :not([slot='tooltip']),
17+
vaadin-side-nav[theme~='filled'] > vaadin-side-nav-item[current] > :not([slot='children']):not([slot='tooltip']),
1918
:is(
2019
vaadin-combo-box-item,
2120
vaadin-context-menu-item,
2221
vaadin-item,
2322
vaadin-menu-bar-item,
2423
vaadin-multi-select-combo-box-item,
2524
vaadin-select-item[role]
26-
)[theme~='filled']:not([disabled], [aria-disabled='true']):hover {
27-
> vaadin-badge[theme~='filled']:not(
28-
.aura-accent-red,
29-
.aura-accent-orange,
30-
.aura-accent-yellow,
31-
.aura-accent-green,
32-
.aura-accent-blue,
33-
.aura-accent-purple,
34-
[theme~='info'],
35-
[theme~='success'],
36-
[theme~='warning'],
37-
[theme~='error']
38-
) {
39-
border-color: transparent;
40-
background: var(--aura-accent-contrast-color);
41-
color: var(--aura-accent-color);
25+
)[theme~='filled']:not([disabled], [aria-disabled='true']):hover
26+
> vaadin-badge {
27+
--vaadin-icon-color: currentColor;
28+
--aura-surface-opacity: 1;
29+
--aura-surface-level: 3;
30+
31+
/* Badges that are not explicitly colored (i.e. default/inherited accent color and neutral accent color) inherit their color from the parent container */
32+
&:not(
33+
.aura-accent-color,
34+
.aura-accent-red,
35+
.aura-accent-orange,
36+
.aura-accent-yellow,
37+
.aura-accent-green,
38+
.aura-accent-blue,
39+
.aura-accent-purple,
40+
[theme~='info'],
41+
[theme~='success'],
42+
[theme~='warning'],
43+
[theme~='error']
44+
) {
45+
--aura-surface-opacity: 0.5;
46+
--aura-surface-level: 1;
47+
--aura-accent-text-color: currentColor;
48+
--aura-accent-border-color: color-mix(in srgb, currentColor 30%, transparent);
49+
--aura-accent-surface: color-mix(in srgb, currentColor 10%, transparent);
50+
51+
/* Invert the colors of filled, non-explicitly colored badges */
52+
&[theme~='filled'] {
53+
background: var(--aura-accent-contrast-color);
54+
color: var(--aura-accent-color);
55+
border-color: transparent;
56+
}
4257
}
4358
}

0 commit comments

Comments
 (0)