Skip to content

postcss-logical: add ignoreCustomProperties plugin option #1573

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

Merged
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
597 changes: 276 additions & 321 deletions package-lock.json

Large diffs are not rendered by default.

40 changes: 0 additions & 40 deletions plugin-packs/postcss-preset-env/test/logical-properties.css
Original file line number Diff line number Diff line change
Expand Up @@ -200,46 +200,6 @@
contain-intrinsic-width: inherit;
}

.corner-shape {
/* corner-bottom-left-shape */
corner-bottom-left-shape: inherit;
}

.corner-shape {
/* corner-bottom-right-shape */
corner-bottom-right-shape: inherit;
}

.corner-shape {
/* corner-end-end-shape */
corner-end-end-shape: inherit;
}

.corner-shape {
/* corner-end-start-shape */
corner-end-start-shape: inherit;
}

.corner-shape {
/* corner-start-end-shape */
corner-start-end-shape: inherit;
}

.corner-shape {
/* corner-start-start-shape */
corner-start-start-shape: inherit;
}

.corner-shape {
/* corner-top-left-shape */
corner-top-left-shape: inherit;
}

.corner-shape {
/* corner-top-right-shape */
corner-top-right-shape: inherit;
}

.inset {
/* bottom */
bottom: inherit;
Expand Down
40 changes: 0 additions & 40 deletions plugin-packs/postcss-preset-env/test/logical-properties.expect.css
Original file line number Diff line number Diff line change
Expand Up @@ -200,46 +200,6 @@
contain-intrinsic-width: inherit;
}

.corner-shape {
/* corner-bottom-left-shape */
corner-bottom-left-shape: inherit;
}

.corner-shape {
/* corner-bottom-right-shape */
corner-bottom-right-shape: inherit;
}

.corner-shape {
/* corner-end-end-shape */
corner-end-end-shape: inherit;
}

.corner-shape {
/* corner-end-start-shape */
corner-end-start-shape: inherit;
}

.corner-shape {
/* corner-start-end-shape */
corner-start-end-shape: inherit;
}

.corner-shape {
/* corner-start-start-shape */
corner-start-start-shape: inherit;
}

.corner-shape {
/* corner-top-left-shape */
corner-top-left-shape: inherit;
}

.corner-shape {
/* corner-top-right-shape */
corner-top-right-shape: inherit;
}

.inset {
/* bottom */
bottom: inherit;
Expand Down
4 changes: 4 additions & 0 deletions plugins/postcss-logical/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes to PostCSS Logical Properties

### Unreleased (minor)

- Add: `ignoreCustomProperties` plugin option.

### 8.0.0

_August 3, 2024_
Expand Down
89 changes: 88 additions & 1 deletion plugins/postcss-logical/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ instructions for:

## Options

### blockDirection & inlineDirection
### `blockDirection` and `inlineDirection`

The `blockDirection` and `inlineDirection` options allow you to specify the direction of the block and inline axes. The default values are `top-to-bottom` and `left-to-right` respectively, which would match any latin language.

Expand Down Expand Up @@ -120,6 +120,93 @@ You can't mix two vertical directions or two horizontal directions so for exampl

Please do note that `text-align` won't be transformed if `inlineDirection` becomes vertical.

### `ignoreCustomProperties`

The `ignoreCustomProperties` option allows you to ignore any properties containing `var()`.
`postcss-logical` assumes that all custom properties are single value (e.g. `--foo: 10px;`) and will assign these to physical properties as fallbacks for logical properties.

This will produce broken declarations when your custom properties contain multiple values instead (e.g. `--foo: 1px 2px;`).

```css
:root {
--inset-a: 10px;
}

.foo {
inset: var(--inset-a);
}

:root {
--inset-b: 1px 2px 3px 4px;
}

.bar {
inset: var(--inset-b);
}

/* becomes */

:root {
--inset-a: 10px;
}

.foo {
top: var(--inset-a);
right: var(--inset-a);
bottom: var(--inset-a);
left: var(--inset-a);
}

:root {
--inset-b: 1px 2px 3px 4px;
}

.bar {
top: var(--inset-b);
right: var(--inset-b);
bottom: var(--inset-b);
left: var(--inset-b);
}
```

With `ignoreCustomProperties` set to `true`:

```css
:root {
--inset-a: 10px;
}

.foo {
inset: var(--inset-a);
}

:root {
--inset-b: 1px 2px 3px 4px;
}

.bar {
inset: var(--inset-b);
}

/* becomes */

:root {
--inset-a: 10px;
}

.foo {
inset: var(--inset-a);
}

:root {
--inset-b: 1px 2px 3px 4px;
}

.bar {
inset: var(--inset-b);
}
```

[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
[css-url]: https://cssdb.org/#logical-properties-and-values
[discord]: https://discord.gg/bUadyRwkJS
Expand Down
2 changes: 1 addition & 1 deletion plugins/postcss-logical/dist/index.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions plugins/postcss-logical/dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export declare enum DirectionFlow {

/** postcss-logical plugin options */
export declare type pluginOptions = {
/** Ignore logical properties with values containing `var()` */
ignoreCustomProperties?: true;
/** Sets the direction for block. default: top-to-bottom */
blockDirection?: DirectionFlow;
/** Sets the direction for inline. default: left-to-right */
Expand Down
2 changes: 1 addition & 1 deletion plugins/postcss-logical/dist/index.mjs

Large diffs are not rendered by default.

27 changes: 26 additions & 1 deletion plugins/postcss-logical/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

## Options

### blockDirection & inlineDirection
### `blockDirection` and `inlineDirection`

The `blockDirection` and `inlineDirection` options allow you to specify the direction of the block and inline axes. The default values are `top-to-bottom` and `left-to-right` respectively, which would match any latin language.

Expand Down Expand Up @@ -65,5 +65,30 @@ You can't mix two vertical directions or two horizontal directions so for exampl

Please do note that `text-align` won't be transformed if `inlineDirection` becomes vertical.

### `ignoreCustomProperties`

The `ignoreCustomProperties` option allows you to ignore any properties containing `var()`.
`postcss-logical` assumes that all custom properties are single value (e.g. `--foo: 10px;`) and will assign these to physical properties as fallbacks for logical properties.

This will produce broken declarations when your custom properties contain multiple values instead (e.g. `--foo: 1px 2px;`).

```css
<inset.css>

/* becomes */

<inset.expect.css>
```

With `ignoreCustomProperties` set to `true`:

```css
<inset.css>

/* becomes */

<inset.ignore-custom-properties.expect.css>
```

<linkList>
[CSS Logical Properties and Values]: <specUrl>
11 changes: 11 additions & 0 deletions plugins/postcss-logical/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { Axes, DirectionFlow } from './lib/types';
import { directionFlowToAxes } from './utils/direction-flow-to-axes';
import { transformTransition } from './lib/transform-transition';
import { prepareTransforms } from './lib/transforms';
import { HAS_VARIABLE_FUNCTION_REGEX } from './utils/has-variable-function';

export type { DirectionFlow } from './lib/types';

/** postcss-logical plugin options */
export type pluginOptions = {
/** Ignore logical properties with values containing `var()` */
ignoreCustomProperties?: true,
/** Sets the direction for block. default: top-to-bottom */
blockDirection?: DirectionFlow,
/** Sets the direction for inline. default: left-to-right */
Expand Down Expand Up @@ -59,6 +62,10 @@ const creator: PluginCreator<pluginOptions> = (opts?: pluginOptions) => {
return;
}

if (options.ignoreCustomProperties && HAS_VARIABLE_FUNCTION_REGEX.test(decl.value)) {
return;
}

let transformed: Array<Declaration> = [];

try {
Expand Down Expand Up @@ -153,6 +160,10 @@ const creator: PluginCreator<pluginOptions> = (opts?: pluginOptions) => {
'border-end-start-radius': makeTransform(transforms['border-end-start-radius']),
'border-end-end-radius': makeTransform(transforms['border-end-end-radius']),
'transition': (decl, { result, postcss }): void => {
if (options.ignoreCustomProperties && HAS_VARIABLE_FUNCTION_REGEX.test(decl.value)) {
return;
}

let transformed: Array<Declaration> = [];

try {
Expand Down
1 change: 1 addition & 0 deletions plugins/postcss-logical/src/utils/has-variable-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const HAS_VARIABLE_FUNCTION_REGEX = /var\(/i;
16 changes: 16 additions & 0 deletions plugins/postcss-logical/test/_tape.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ postcssTape(plugin)({
message: 'supports logical "offsets" properties',
warnings: 3,
},
'offsets:ignore-custom-properties': {
message: 'supports logical "offsets" properties { ignoreCustomProperties: true }',
warnings: 3,
options: {
ignoreCustomProperties: true,
},
},
'offsets:chinese': {
message: 'supports logical "offsets" properties { blockDirection: "right-to-left", inlineDirection: "top-to-bottom" }',
warnings: 3,
Expand Down Expand Up @@ -144,4 +151,13 @@ postcssTape(plugin)({
inlineDirection: 'top-to-bottom',
},
},
'examples/inset': {
message: 'inset example',
},
'examples/inset:ignore-custom-properties': {
message: 'inset example { ignoreCustomProperties: true }',
options: {
ignoreCustomProperties: true,
},
},
});
15 changes: 15 additions & 0 deletions plugins/postcss-logical/test/examples/inset.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
:root {
--inset-a: 10px;
}

.foo {
inset: var(--inset-a);
}

:root {
--inset-b: 1px 2px 3px 4px;
}

.bar {
inset: var(--inset-b);
}
21 changes: 21 additions & 0 deletions plugins/postcss-logical/test/examples/inset.expect.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
:root {
--inset-a: 10px;
}

.foo {
top: var(--inset-a);
right: var(--inset-a);
bottom: var(--inset-a);
left: var(--inset-a);
}

:root {
--inset-b: 1px 2px 3px 4px;
}

.bar {
top: var(--inset-b);
right: var(--inset-b);
bottom: var(--inset-b);
left: var(--inset-b);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
:root {
--inset-a: 10px;
}

.foo {
inset: var(--inset-a);
}

:root {
--inset-b: 1px 2px 3px 4px;
}

.bar {
inset: var(--inset-b);
}
Loading