Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve typing of CSS styles using csstype package #216

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading