Skip to content
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
5 changes: 2 additions & 3 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ export default function (api) {
// files on disk, which Jest's transform cache does not track.
[]
: // Output is published as "type": "module", which requires fully specified
// import paths (e.g. './View/index.js', 'styleq/transform-localize-style.js')
// Only list packages without an "exports" field in `includePackages`
[['babel-plugin-fully-specified', { includePackages: ['styleq/'] }]])
// import paths (e.g. './View/index.js')
['babel-plugin-fully-specified'])
],
presets: [
['@babel/preset-react', { useSpread: true }],
Expand Down
10 changes: 2 additions & 8 deletions flow-typed/npm/styleq.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type CompiledStyle = {
$$css: boolean,
$$css: boolean | string,
[key: string]: string,
};

Expand All @@ -19,7 +19,7 @@ type StyleqOptions = {
transform?: (EitherStyle) => EitherStyle,
};

type StyleqResult = [string, InlineStyle | null];
type StyleqResult = [string, InlineStyle | null, string];
type Styleq = (styles: Styles) => StyleqResult;

type IStyleq = {
Expand All @@ -32,9 +32,3 @@ declare module "styleq" {
styleq: IStyleq
};
}

declare module "styleq/transform-localize-style" {
declare module.exports: {
localizeStyle: (style: EitherStyle, isRTL: boolean) => EitherStyle
};
}
11 changes: 6 additions & 5 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion packages/benchmarks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"d3-scale-chromatic": "^3.1.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-native-web": "0.21.4"
"react-native-web": "0.21.4",
"styleq": "^0.2.1"
},
"devDependencies": {
"babel-loader": "^10.1.1",
Expand Down
6 changes: 5 additions & 1 deletion packages/benchmarks/src/impl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import packageJson from '../package.json';

const context = require.context('./implementations/', true, /index\.js$/);
const { dependencies } = packageJson;

const context = import.meta.webpackContext('./implementations/', {
recursive: true,
regExp: /index\.js$/
});

/**
* @typedef {Object} ComponentsType
* @property {React.ComponentType<any>} Box
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"memoize-one": "^6.0.0",
"nullthrows": "^1.1.1",
"postcss-value-parser": "^4.2.0",
"styleq": "^0.1.3"
"styleq": "^0.2.1"
},
"devDependencies": {
"del-cli": "^8.0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('StyleSheet', () => {
[
"r-position-u8s1d",
null,
"",
]
`);
});
Expand All @@ -76,6 +77,7 @@ describe('StyleSheet', () => {
[
"r-boxShadow-o3ayyy r-textShadow-1x2q051",
null,
"",
]
`);
});
Expand Down Expand Up @@ -147,18 +149,21 @@ describe('StyleSheet', () => {
[
"",
null,
"",
]
`);
expect(StyleSheet({})).toMatchInlineSnapshot(`
[
"",
null,
"",
]
`);
expect(StyleSheet([])).toMatchInlineSnapshot(`
[
"",
null,
"",
]
`);
});
Expand All @@ -177,6 +182,7 @@ describe('StyleSheet', () => {
[
"position-absolute opacity-05 width-200",
null,
"",
]
`);
});
Expand Down Expand Up @@ -207,6 +213,7 @@ describe('StyleSheet', () => {
[
"borderWidth-0 borderColor-red display-flex position-absolute opacity-05 width-200",
null,
"",
]
`);
});
Expand Down Expand Up @@ -260,6 +267,7 @@ describe('StyleSheet', () => {
"paddingRight": "40px",
"paddingTop": "8px",
},
"",
]
`);

Expand All @@ -274,6 +282,7 @@ describe('StyleSheet', () => {
"marginRight": "8px",
"marginTop": "40px",
},
"",
]
`);
});
Expand All @@ -298,6 +307,7 @@ describe('StyleSheet', () => {
"marginRight": "10px",
"textAlign": "left",
},
"",
]
`);
expect(StyleSheet(inlineStyle, { writingDirection }))
Expand All @@ -309,6 +319,7 @@ describe('StyleSheet', () => {
"right": "12.34%",
"textAlign": "right",
},
"",
]
`);
expect(
Expand All @@ -317,6 +328,7 @@ describe('StyleSheet', () => {
inlineStyle,
{ marginLeft: 1, marginEnd: 0, marginStart: 0, marginRight: 11 }
],

{ writingDirection }
)
).toMatchInlineSnapshot(`
Expand All @@ -328,6 +340,7 @@ describe('StyleSheet', () => {
"right": "12.34%",
"textAlign": "right",
},
"",
]
`);
expect(
Expand All @@ -342,6 +355,7 @@ describe('StyleSheet', () => {
"right": "12.34%",
"textAlign": "right",
},
"",
]
`);

Expand All @@ -351,20 +365,23 @@ describe('StyleSheet', () => {
[
"r-insetInlineStart-1xn1m1p r-textAlign-fdjqy7 r-marginInlineEnd-1l8l4mf",
null,
"",
]
`);
expect(StyleSheet(staticStyle, { writingDirection }))
.toMatchInlineSnapshot(`
[
"r-insetInlineStart-1y2vi53 r-textAlign-1ff274t r-marginInlineEnd-t1sew1",
null,
"",
]
`);
const z = StyleSheet.create({ x: { marginRight: 33 } }).x;
expect(StyleSheet([staticStyle, z])).toMatchInlineSnapshot(`
[
"r-insetInlineStart-1xn1m1p r-textAlign-fdjqy7 r-marginInlineEnd-1l8l4mf r-marginRight-j4vy6k",
null,
"",
]
`);
expect(
Expand All @@ -385,6 +402,7 @@ describe('StyleSheet', () => {
"marginLeft": "1px",
"marginRight": "11px",
},
"",
]
`);
// logical can be nulled
Expand All @@ -396,6 +414,7 @@ describe('StyleSheet', () => {
[
"r-insetInlineStart-1y2vi53 r-textAlign-1ff274t",
null,
"",
]
`);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-web/src/exports/StyleSheet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { atomic, classic, inline } from './compiler';
import { createSheet } from './dom';
import { localizeStyle } from 'styleq/transform-localize-style';
import { localizeStyle } from './localizeStyle';
import { preprocess } from './preprocess';
import { styleq } from 'styleq';
import { validate } from './validate';
Expand Down Expand Up @@ -141,7 +141,7 @@ function getSheet(): { id: string, textContent: string } {
/**
* resolve
*/
type StyleProps = [string, { [key: string]: unknown } | null];
type StyleProps = [string, { [key: string]: unknown } | null, string];
type Options = {
shadow?: boolean,
textShadow?: boolean,
Expand Down
62 changes: 62 additions & 0 deletions packages/react-native-web/src/exports/StyleSheet/localizeStyle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) Nicolas Gallagher.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

const cache = new WeakMap<Object, Array<Object>>();
const markerProp = '$$css$localize';

/**
* The compiler polyfills logical properties and values, generating a class
* name for both writing directions. The style objects are annotated by
* the compiler as needing this runtime transform. The results are memoized.
*
* { '$$css$localize': true, float: [ 'float-left', 'float-right' ] }
* => { float: 'float-left' }
*/

function compileStyle(style: Object, isRTL: boolean) {
// Create a new compiled style for styleq
const compiledStyle: Object = {};
for (const prop in style) {
if (prop !== markerProp) {
const value = style[prop];
if (Array.isArray(value)) {
compiledStyle[prop] = isRTL ? value[1] : value[0];
} else {
compiledStyle[prop] = value;
}
}
}
return compiledStyle;
}

export function localizeStyle(style: Object, isRTL: boolean): Object {
if (style[markerProp] != null) {
const compiledStyleIndex = isRTL ? 1 : 0;
// Check the cache in case we've already seen this object
let cachedStyles = cache.get(style);
if (cachedStyles != null) {
let compiledStyle = cachedStyles[compiledStyleIndex];
if (compiledStyle == null) {
// Update the missing cache entry
compiledStyle = compileStyle(style, isRTL);
cachedStyles[compiledStyleIndex] = compiledStyle;
cache.set(style, cachedStyles);
}
return compiledStyle;
}

// Create a new compiled style for styleq
const compiledStyle = compileStyle(style, isRTL);
cachedStyles = new Array<Object>(2);
cachedStyles[compiledStyleIndex] = compiledStyle;
cache.set(style, cachedStyles);
return compiledStyle;
}
return style;
}
Loading