Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,10 @@ describe('@stylexjs/babel-plugin', () => {
{
"stylex": [
[
"xodj72a",
"x1wx8h15",
{
"ltr": ".xodj72a{clear:right}",
"rtl": ".xodj72a{clear:left}",
"ltr": ".x1wx8h15{clear:var(--stylex-logical-end)}",
"rtl": null,
},
3000,
],
Expand All @@ -652,10 +652,10 @@ describe('@stylexjs/babel-plugin', () => {
{
"stylex": [
[
"x390i0x",
"x1fhb4fj",
{
"ltr": ".x390i0x{clear:left}",
"rtl": ".x390i0x{clear:right}",
"ltr": ".x1fhb4fj{clear:var(--stylex-logical-start)}",
"rtl": null,
},
3000,
],
Expand All @@ -676,10 +676,10 @@ describe('@stylexjs/babel-plugin', () => {
{
"stylex": [
[
"x1guec7k",
"xtrg13t",
{
"ltr": ".x1guec7k{float:right}",
"rtl": ".x1guec7k{float:left}",
"ltr": ".xtrg13t{float:var(--stylex-logical-end)}",
"rtl": null,
},
3000,
],
Expand All @@ -700,10 +700,10 @@ describe('@stylexjs/babel-plugin', () => {
{
"stylex": [
[
"xrbpyxo",
"xj87blo",
{
"ltr": ".xrbpyxo{float:left}",
"rtl": ".xrbpyxo{float:right}",
"ltr": ".xj87blo{float:var(--stylex-logical-start)}",
"rtl": null,
},
3000,
],
Expand Down Expand Up @@ -1010,10 +1010,10 @@ describe('@stylexjs/babel-plugin', () => {
{
"stylex": [
[
"xodj72a",
"x1wx8h15",
{
"ltr": ".xodj72a{clear:right}",
"rtl": ".xodj72a{clear:left}",
"ltr": ".x1wx8h15{clear:var(--stylex-logical-end)}",
"rtl": ".x1wx8h15{clear:var(--stylex-logical-end)}",
},
3000,
],
Expand All @@ -1034,10 +1034,10 @@ describe('@stylexjs/babel-plugin', () => {
{
"stylex": [
[
"x390i0x",
"x1fhb4fj",
{
"ltr": ".x390i0x{clear:left}",
"rtl": ".x390i0x{clear:right}",
"ltr": ".x1fhb4fj{clear:var(--stylex-logical-start)}",
"rtl": ".x1fhb4fj{clear:var(--stylex-logical-start)}",
},
3000,
],
Expand All @@ -1058,10 +1058,10 @@ describe('@stylexjs/babel-plugin', () => {
{
"stylex": [
[
"x1guec7k",
"xtrg13t",
{
"ltr": ".x1guec7k{float:right}",
"rtl": ".x1guec7k{float:left}",
"ltr": ".xtrg13t{float:var(--stylex-logical-end)}",
"rtl": ".xtrg13t{float:var(--stylex-logical-end)}",
},
3000,
],
Expand All @@ -1082,10 +1082,10 @@ describe('@stylexjs/babel-plugin', () => {
{
"stylex": [
[
"xrbpyxo",
"xj87blo",
{
"ltr": ".xrbpyxo{float:left}",
"rtl": ".xrbpyxo{float:right}",
"ltr": ".xj87blo{float:var(--stylex-logical-start)}",
"rtl": ".xj87blo{float:var(--stylex-logical-start)}",
},
3000,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ describe('@stylexjs/babel-plugin', () => {
"paddingInlineEnd-kwRFfy": "paddingInlineEnd-x1q3ajuy",
"paddingBottom-kGO01o": "paddingBottom-xs9asl8",
"paddingInlineStart-kZCmMZ": "paddingInlineStart-x1gx403c",
"float-kyUFMd": "float-x1kmio9f",
"float-kyUFMd": "float-xj87blo",
$$css: "app/main.js:25"
}
};"
Expand All @@ -510,13 +510,20 @@ describe('@stylexjs/babel-plugin', () => {
enableLTRRTLComments: true,
}),
).toMatchInlineSnapshot(`
":root, .xsg933n{--blue-xpqh4lw:blue;--marginTokens-x8nt2k2:10px;--colorTokens-xkxfyv:red;}
":root, [dir="ltr"] {
--stylex-logical-start: left;
--stylex-logical-end: right;
}
[dir="rtl"] {
--stylex-logical-start: right;
--stylex-logical-end: left;
}
:root, .xsg933n{--blue-xpqh4lw:blue;--marginTokens-x8nt2k2:10px;--colorTokens-xkxfyv:red;}
:root, .xbiwvf9{--small-x19twipt:2px;--medium-xypjos2:4px;--large-x1ec7iuc:8px;}
@media (prefers-color-scheme: dark){:root, .xsg933n{--colorTokens-xkxfyv:lightblue;}}
@media (min-width: 600px){:root, .xsg933n{--marginTokens-x8nt2k2:20px;}}
@supports (color: oklab(0 0 0)){@media (prefers-color-scheme: dark){:root, .xsg933n{--colorTokens-xkxfyv:oklab(0.7 -0.3 -0.4);}}}
/* @ltr begin */.float-x1kmio9f:not(#\\#){float:left}/* @ltr end */
/* @rtl begin */.float-x1kmio9f:not(#\\#){float:right}/* @rtl end */
.float-xj87blo:not(#\\#){float:var(--stylex-logical-start)}
/* @ltr begin */.marginInlineStart-xqsn43r:not(#\\#){margin-left:20px}/* @ltr end */
/* @rtl begin */.marginInlineStart-xqsn43r:not(#\\#){margin-right:20px}/* @rtl end */
/* @ltr begin */.marginInlineEnd-x3aesyq:not(#\\#){margin-right:20px}/* @ltr end */
Expand Down
34 changes: 33 additions & 1 deletion packages/@stylexjs/babel-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import transformStylexProps from './visitors/stylex-props';
import { skipStylexPropsChildren } from './visitors/stylex-props';
import transformStyleXViewTransitionClass from './visitors/stylex-view-transition-class';
import transformStyleXDefaultMarker from './visitors/stylex-default-marker';
import {
LOGICAL_FLOAT_START_VAR,
LOGICAL_FLOAT_END_VAR,
} from './shared/preprocess-rules/legacy-expand-shorthands';

const NAME = 'stylex';

Expand Down Expand Up @@ -357,6 +361,32 @@ export type Rule = [
},
number,
];

function getLogicalFloatVars(rules: Array<Rule>): string {
const hasLogicalFloat = rules.some(([, { ltr, rtl }]) => {
const ltrStr = String(ltr);
const rtlStr = rtl ? String(rtl) : '';
return (
ltrStr.includes(LOGICAL_FLOAT_START_VAR) ||
ltrStr.includes(LOGICAL_FLOAT_END_VAR) ||
rtlStr.includes(LOGICAL_FLOAT_START_VAR) ||
rtlStr.includes(LOGICAL_FLOAT_END_VAR)
);
});

return hasLogicalFloat
? `:root, [dir="ltr"] {
${LOGICAL_FLOAT_START_VAR}: left;
${LOGICAL_FLOAT_END_VAR}: right;
}
[dir="rtl"] {
${LOGICAL_FLOAT_START_VAR}: right;
${LOGICAL_FLOAT_END_VAR}: left;
}
`
: '';
}

function processStylexRules(
rules: Array<Rule>,
config?:
Expand Down Expand Up @@ -491,6 +521,8 @@ function processStylexRules(
return acc;
}, []);

const logicalFloatVars = getLogicalFloatVars(nonConstantRules);

const header = useLayers
? '\n@layer ' +
grouped.map((_, index) => `priority${index + 1}`).join(', ') +
Expand Down Expand Up @@ -549,7 +581,7 @@ function processStylexRules(
})
.join('\n');

return header + collectedCSS;
return logicalFloatVars + header + collectedCSS;
}

styleXTransform.processStylexRules = processStylexRules;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import type { TStyleValue } from '../common-types';

import splitValue from '../utils/split-css-value';

export const LOGICAL_FLOAT_START_VAR = '--stylex-logical-start';
export const LOGICAL_FLOAT_END_VAR = '--stylex-logical-end';

// TODO: to be added later.
// const aliases = {
// marginInlineStart: (rawValue) => [['marginStart', rawValue]],
Expand Down Expand Up @@ -515,6 +518,23 @@ const aliases = {
scrollMarginBlockEnd: (value: TStyleValue): TReturn => [
['scrollMarginBottom', value],
],
float: (value: TStyleValue): TReturn => {
if (value === 'inline-start' || value === 'start') {
return [['float', `var(${LOGICAL_FLOAT_START_VAR})`]];
} else if (value === 'inline-end' || value === 'end') {
return [['float', `var(${LOGICAL_FLOAT_END_VAR})`]];
}
return [['float', value]];
},

clear: (value: TStyleValue): TReturn => {
if (value === 'inline-start' || value === 'start') {
return [['clear', `var(${LOGICAL_FLOAT_START_VAR})`]];
} else if (value === 'inline-end' || value === 'end') {
return [['clear', `var(${LOGICAL_FLOAT_END_VAR})`]];
}
return [['clear', value]];
},
};

const expansions = {
Expand Down