Skip to content

Commit 36fa214

Browse files
authored
improved typing of CSS styles using csstype package (#216)
1 parent 9cc94c4 commit 36fa214

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

addon/modifiers/style.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import Modifier from 'ember-modifier';
22
import { dasherize } from '@ember/string';
33
import { assert } from '@ember/debug';
44
import { typeOf } from '@ember/utils';
5+
import type * as CSS from 'csstype';
56

67
// Cannot be typed as `Partial<CSSStyleDeclaration>` because `CSSStyleDeclaration`
78
// interface does _not_ included dashed CSS property names. It only includes the
89
// camelCase version of a CSS property.
910
// https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1672
10-
type CSSStyles = { [key: string]: string | undefined };
11+
type CSSStyles = Partial<CSS.Properties> | Partial<CSS.PropertiesHyphen>;
1112

1213
function isObject(o: unknown): boolean {
1314
return typeof o === 'object' && Boolean(o);

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"dependencies": {
5050
"@babel/core": "^7.23.6",
51+
"csstype": "^3.1.3",
5152
"ember-auto-import": "^2.7.0",
5253
"ember-cli-babel": "^8.2.0",
5354
"ember-modifier": "^3.2.7 || ^4.0.0"

pnpm-lock.yaml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/modifiers/style-test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ module('Integration | Modifiers | style', function (hooks) {
1313
assert.dom('p').hasStyle({ display: 'none' });
1414
});
1515

16+
test('it supports multiple styles', async function (assert) {
17+
await render(hbs`<p {{style display="none" float="left"}}></p>`);
18+
19+
assert.dom('p').hasStyle({ display: 'none', float: 'left' });
20+
});
21+
1622
test('it allows to set priority', async function (assert) {
1723
await render(hbs`<p {{style display="none !important"}}></p>`);
1824

@@ -37,6 +43,15 @@ module('Integration | Modifiers | style', function (hooks) {
3743
assert.dom('p').hasStyle({ fontSize: '6px' });
3844
});
3945

46+
test('it supports dasherized and camelCase property names in same declaration', async function (assert) {
47+
await render(hbs`<p {{style font-size="6px" fontStyle="italic"}}></p>`);
48+
49+
assert
50+
.dom('p')
51+
.hasStyle({ fontSize: '6px' })
52+
.hasStyle({ fontStyle: 'italic' });
53+
});
54+
4055
{
4156
interface Context extends TestContext {
4257
// eslint-disable-next-line @typescript-eslint/ban-types

0 commit comments

Comments
 (0)