Skip to content

Commit 5948a9b

Browse files
authored
fix: expand explanation of the .getPropVal() method (#1099)
1 parent 66dd7e6 commit 5948a9b

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed

src/content/docs/curriculum-help.mdx

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,25 +189,39 @@ If you call the `getStyle` method with `body` as an argument, you would get an o
189189
}
190190
```
191191

192-
This method allows you to test that specific properties have been set:
192+
There are three ways to retrieve CSS values from the returned object:
193+
194+
| Method | Example | Returns |
195+
| ---------------------------- | --------------------------------------------- | -------- |
196+
| Direct property access | `getStyle('body')?.width` | `'100%'` |
197+
| Native `.getPropertyValue()` | `getStyle('body')?.getPropertyValue('width')` | `'100%'` |
198+
| Helper `.getPropVal()` | `getStyle('body')?.getPropVal('width')` | `'100%'` |
199+
200+
`.getPropertyValue()` is a native CSS style declaration object method for reading CSS properties by their kebab-case name (for example, 'background-color'). See [MDN: CSSStyleDeclaration.getPropertyValue()](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/getPropertyValue).
201+
202+
The `.getStyle()` helper attaches the `.getPropVal()` method to the style declaration object. It wraps the native `getPropertyValue()` method and optionally strips whitespace. It accepts an optional second parameter that, when `true`, removes all whitespace from the returned value.
203+
204+
`getPropVal()` default behavior (preserves whitespace):
193205

194206
```js
195-
assert.strictEqual(helper.getStyle('body')?.width, '100%');
207+
assert.strictEqual(
208+
helper.getStyle('body').getPropVal('background-color'),
209+
'rgb(118, 201, 255)'
210+
);
196211
```
197212

198-
The helper attaches a `.getPropVal()` method to the style declaration object that allows you to get the value of a specific property:
213+
`getPropVal()` with whitespace stripping (true removes all whitespace):
199214

200215
```js
201-
assert.strictEqual(helper.getStyle('body').getPropVal('width'), '100%');
216+
assert.strictEqual(
217+
helper.getStyle('body').getPropVal('background-color', true),
218+
'rgb(118,201,255)'
219+
);
202220
```
203221

204-
On the style object returned, there is a native method called `getPropertyValue` that can retrieve
205-
CSS properties. More info at [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/getPropertyValue).
206-
207222
#### `.getStyleAny()`
208223

209-
The `.getStyleAny()` method takes multiple CSS possible selectors and returns
210-
the first matching CSS style declaration object.
224+
The `.getStyleAny()` method takes multiple CSS possible selectors and returns the first matching CSS style declaration object.
211225

212226
For example, if the camper has written the following CSS:
213227

@@ -257,18 +271,31 @@ If you call the `getStyleAny()` method with `.earth` and `.sky` as arguments, yo
257271
}
258272
```
259273

260-
This method allows you to test that specific properties have been set:
274+
There are three ways to retrieve CSS values from the returned object:
275+
276+
| Method | Example | Returns |
277+
| ---------------------------- | ------------------------------------------------------------ | -------- |
278+
| Direct property access | `getStyleAny(['.earth', '.sky'])?.width` | `'100%'` |
279+
| Native `.getPropertyValue()` | `getStyleAny(['.earth', '.sky'])?.getPropertyValue('width')` | `'100%'` |
280+
| Helper `.getPropVal()` | `getStyleAny(['.earth', '.sky'])?.getPropVal('width')` | `'100%'` |
281+
282+
Like the CSS style declaration objects returned by `.getStyle()`, the object returned by `.getStyleAny()` also includes the `.getPropVal()` method (see the `.getStyle()` section). It provides the same property access and whitespace stripping feature.
283+
284+
`getPropVal()` default behavior (preserves whitespace):
261285

262286
```js
263-
assert.strictEqual(helper.getStyleAny(['.earth', '.sky'])?.width, '100%');
287+
assert.strictEqual(
288+
helper.getStyleAny(['.earth', '.sky']).getPropVal('background-color'),
289+
'rgb(255, 207, 51)'
290+
);
264291
```
265292

266-
The helper attaches a `.getPropVal()` method to the style declaration object that allows you to get the value of a specific property:
293+
`getPropVal()` with whitespace stripping (true removes all whitespace):
267294

268295
```js
269296
assert.strictEqual(
270-
helper.getStyleAny(['.earth', '.sky']).getPropVal('width'),
271-
'100%'
297+
helper.getStyleAny(['.earth', '.sky']).getPropVal('background-color', true),
298+
'rgb(255,207,51)'
272299
);
273300
```
274301

0 commit comments

Comments
 (0)