Skip to content

Commit a12a6b9

Browse files
committed
fix(card): support color via @deprecation
1 parent d0a9bf3 commit a12a6b9

File tree

5 files changed

+81
-16
lines changed

5 files changed

+81
-16
lines changed

elements/pfe-card/demo/pfe-card.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,21 @@ <h2 slot="header">Background Image on card </h2>
105105
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Soluta rerum tempore natus alias! Cumque illum
106106
provident corrupti voluptates.</p>
107107
</pfe-card>
108+
109+
<pfe-card>
110+
<h2 slot="header">Deprecated <code>color</code> attribute</h2>
111+
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Soluta rerum tempore natus alias! Cumque illum
112+
provident corrupti voluptates.</p>
113+
<pfe-select id="context-selector" slot="footer">
114+
<select>
115+
<option disabled>Select a palette</option>
116+
<option value="lightest">Lightest</option>
117+
<option value="lighter">Lighter</option>
118+
<option value="base" selected>Default</option>
119+
<option value="darker">Darker</option>
120+
<option value="darkest">Darkest</option>
121+
<option value="accent">Accent</option>
122+
<option value="complement">Complement</option>
123+
</select>
124+
</pfe-select>
125+
</pfe-card>

elements/pfe-card/demo/pfe-card.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
import '@patternfly/pfe-band';
22
import '@patternfly/pfe-card';
3+
import '@patternfly/pfe-select';
4+
5+
const root = document.querySelector('[data-demo="pfe-card"]')?.shadowRoot ?? document;
6+
7+
root.querySelector('#context-selector').addEventListener('select', event => {
8+
event.target.closest('pfe-card').setAttribute('color', event.value);
9+
});

elements/pfe-card/pfe-card.scss

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ $size-small: (
5353
:host {
5454
--context: #{pfe-local(context)};
5555

56+
--pfe-theme--color--surface--lightest: #ffffff;
57+
--pfe-theme--color--surface--lighter: #ececec;
58+
--pfe-theme--color--surface--base: #f0f0f0;
5659
--pfe-theme--color--surface--darker: #3c3f42;
5760
--pfe-theme--color--surface--darkest: #151515;
58-
--pfe-theme--color--surface--base: #f0f0f0;
59-
--pfe-theme--color--surface--lighter: #ececec;
60-
--pfe-theme--color--surface--lightest: #ffffff;
6161
--pfe-theme--color--surface--accent: #004080;
6262
--pfe-theme--color--surface--complement: #002952;
6363

@@ -91,7 +91,10 @@ $size-small: (
9191
}
9292
}
9393

94-
:host([color-palette]), :host([on]) {
94+
/* @deprecated */
95+
:host([color]),
96+
:host([color-palette]),
97+
:host([on]) {
9598
background-color: var(--pfe-card--BackgroundColor, var(--pfe-context-background-color));
9699
}
97100

elements/pfe-card/pfe-card.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { LitElement, html } from 'lit';
44
import { customElement, property } from 'lit/decorators.js';
55
import { classMap } from 'lit/directives/class-map.js';
66

7-
import { colorContextConsumer, colorContextProvider, observed, pfelement } from '@patternfly/pfe-core/decorators.js';
7+
import {
8+
colorContextConsumer,
9+
colorContextProvider,
10+
deprecation,
11+
observed,
12+
pfelement
13+
} from '@patternfly/pfe-core/decorators.js';
814
import { SlotController } from '@patternfly/pfe-core/controllers/slot-controller.js';
915

1016
import style from './pfe-card.scss';
@@ -30,6 +36,15 @@ import style from './pfe-card.scss';
3036
* @csspart body - The container for *body* content
3137
* @csspart footer - The container for *footer* content
3238
*
39+
*
40+
* @cssproperty {<color>} --pfe-theme--color--surface--lightest {@default `#ffffff`}
41+
* @cssproperty {<color>} --pfe-theme--color--surface--lighter {@default `#ececec`}
42+
* @cssproperty {<color>} --pfe-theme--color--surface--base {@default `#f0f0f0`}
43+
* @cssproperty {<color>} --pfe-theme--color--surface--darker {@default `#3c3f42`}
44+
* @cssproperty {<color>} --pfe-theme--color--surface--darkest {@default `#151515`}
45+
* @cssproperty {<color>} --pfe-theme--color--surface--accent {@default `#004080`}
46+
* @cssproperty {<color>} --pfe-theme--color--surface--complement {@default `#002952`}
47+
*
3348
* @cssproperty --pfe-band--BackgroundColor
3449
* Though using the `color` attribute is strongly recommended when setting the background color for the band, you can also use completely custom colors by updating the `--pfe-band--BackgroundColor` variable. If you update this value manually, you should also update the `--theme` context variable to invoke the right theme on it and it's child elements. Supported themes include: `light`, `dark`, and `saturated`.
3550
* @cssproperty --pfe-card--BackgroundPosition
@@ -64,21 +79,22 @@ export class PfeCard extends LitElement {
6479
@property({ attribute: 'img-src', reflect: true }) imgSrc?: string;
6580

6681
/**
82+
* Sets color palette, which affects the element's styles as well as descendants' color theme.
83+
* Overrides parent color context.
6784
* Your theme will influence these colors so check there first if you are seeing inconsistencies.
85+
* See [CSS Custom Properties](#css-custom-properties) for default values
6886
*
69-
* | Color | Hex Value |
70-
* | ---------- | ---------------------------------------------------------------- |
71-
* | lightest | <span class="color-preview" style="--bg:#ffffff"></span> #ffffff |
72-
* | lighter | <span class="color-preview" style="--bg:#ececec"></span> #ececec |
73-
* | default | <span class="color-preview" style="--bg:#dfdfdf"></span> #dfdfdf |
74-
* | darker | <span class="color-preview" style="--bg:#464646"></span> #464646 |
75-
* | darkest | <span class="color-preview" style="--bg:#131313"></span> #131313 |
76-
* | accent | <span class="color-preview" style="--bg:#ee0000"></span> #ee0000 |
77-
* | complement | <span class="color-preview" style="--bg:#0477a4"></span> #0477a4 |
87+
* Card always resets its context to `base`, unless explicitly provided with a `color-palette`.
7888
*/
7989
@colorContextProvider()
80-
@property({ reflect: true, attribute: 'color-palette' }) colorPalette: ColorPalette = 'base';
90+
@property({ reflect: true, attribute: 'color-palette' }) colorPalette?: ColorPalette;
91+
92+
/** @deprecated use `color-palette` */
93+
@deprecation({ alias: 'colorPalette', attribute: 'color' }) color: ColorPalette = 'base';
8194

95+
/**
96+
* Sets color theme based on parent context
97+
*/
8298
@colorContextConsumer()
8399
@property({ reflect: true }) on?: ColorTheme;
84100

elements/pfe-card/test/pfe-card.spec.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('<pfe-card>', function() {
106106
});
107107

108108
// Iterate over the colors object to test expected background color results
109-
describe('color values', function() {
109+
describe('color palettes', function() {
110110
// Themes and their expected hex values
111111
Object.entries({
112112
default: '#f0f0f0',
@@ -134,6 +134,27 @@ describe('<pfe-card>', function() {
134134
expect(luminance(r, g, b)).to.be.greaterThan(0.5);
135135
}
136136
});
137+
138+
it(`it should have a background color of ${colorValue} when deprecated color attribute is ${colorName}`, async function() {
139+
const element = await createFixture<PfeCard>(TEMPLATES.card1);
140+
// If this is not the default color, update the color attribute
141+
if (colorName !== 'default') {
142+
element.setAttribute('color', colorName);
143+
}
144+
145+
await element.updateComplete;
146+
147+
// Get the background color value
148+
const [r, g, b] = getColor(element, 'background-color');
149+
// Test that the color is rendering as expected
150+
expect([r, g, b]).to.deep.equal(hexToRgb(colorValue));
151+
// Test that the color is working
152+
if (['dark', 'darker', 'darkest', 'complement', 'accent'].includes(colorName)) {
153+
expect(luminance(r, g, b)).to.be.lessThan(0.5);
154+
} else {
155+
expect(luminance(r, g, b)).to.be.greaterThan(0.5);
156+
}
157+
});
137158
});
138159
});
139160

0 commit comments

Comments
 (0)