Skip to content

Commit

Permalink
improved typing of CSS styles using csstype package
Browse files Browse the repository at this point in the history
  • Loading branch information
jelhan committed Jan 11, 2024
1 parent 9cc94c4 commit 9497d15
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion addon/modifiers/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import Modifier from 'ember-modifier';
import { dasherize } from '@ember/string';
import { assert } from '@ember/debug';
import { typeOf } from '@ember/utils';
import type * as CSS from 'csstype';

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

function isObject(o: unknown): boolean {
return typeof o === 'object' && Boolean(o);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
},
"dependencies": {
"@babel/core": "^7.23.6",
"csstype": "^3.1.3",
"ember-auto-import": "^2.7.0",
"ember-cli-babel": "^8.2.0",
"ember-modifier": "^3.2.7 || ^4.0.0"
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions tests/integration/modifiers/style-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ module('Integration | Modifiers | style', function (hooks) {
assert.dom('p').hasStyle({ display: 'none' });
});

test('it supports multiple styles', async function (assert) {
await render(hbs`<p {{style display="none" float="left"}}></p>`);

assert.dom('p').hasStyle({ display: 'none', float: 'left' });
});

test('it allows to set priority', async function (assert) {
await render(hbs`<p {{style display="none !important"}}></p>`);

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

test('it supports dasherized and camelCase property names in same declaration', async function (assert) {
await render(hbs`<p {{style font-size="6px" fontStyle="italic"}}></p>`);

assert
.dom('p')
.hasStyle({ fontSize: '6px' })
.hasStyle({ fontStyle: 'italic' });
});

{
interface Context extends TestContext {
// eslint-disable-next-line @typescript-eslint/ban-types
Expand Down

0 comments on commit 9497d15

Please sign in to comment.