react-native-unistyles 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +243 -0
- package/lib/commonjs/UnistylesTheme.js +21 -0
- package/lib/commonjs/UnistylesTheme.js.map +1 -0
- package/lib/commonjs/createUnistyles.js +52 -0
- package/lib/commonjs/createUnistyles.js.map +1 -0
- package/lib/commonjs/index.js +20 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/types.js +6 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/utils/breakpoints.js +139 -0
- package/lib/commonjs/utils/breakpoints.js.map +1 -0
- package/lib/commonjs/utils/breakpoints.spec.js +149 -0
- package/lib/commonjs/utils/breakpoints.spec.js.map +1 -0
- package/lib/commonjs/utils/common.js +11 -0
- package/lib/commonjs/utils/common.js.map +1 -0
- package/lib/commonjs/utils/index.js +32 -0
- package/lib/commonjs/utils/index.js.map +1 -0
- package/lib/commonjs/utils/mediaQueries.js +189 -0
- package/lib/commonjs/utils/mediaQueries.js.map +1 -0
- package/lib/commonjs/utils/mediaQueries.spec.js +220 -0
- package/lib/commonjs/utils/mediaQueries.spec.js.map +1 -0
- package/lib/commonjs/utils/styles.js +82 -0
- package/lib/commonjs/utils/styles.js.map +1 -0
- package/lib/commonjs/utils/styles.spec.js +98 -0
- package/lib/commonjs/utils/styles.spec.js.map +1 -0
- package/lib/module/UnistylesTheme.js +12 -0
- package/lib/module/UnistylesTheme.js.map +1 -0
- package/lib/module/createUnistyles.js +45 -0
- package/lib/module/createUnistyles.js.map +1 -0
- package/lib/module/index.js +3 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/utils/breakpoints.js +131 -0
- package/lib/module/utils/breakpoints.js.map +1 -0
- package/lib/module/utils/breakpoints.spec.js +147 -0
- package/lib/module/utils/breakpoints.spec.js.map +1 -0
- package/lib/module/utils/common.js +4 -0
- package/lib/module/utils/common.js.map +1 -0
- package/lib/module/utils/index.js +3 -0
- package/lib/module/utils/index.js.map +1 -0
- package/lib/module/utils/mediaQueries.js +176 -0
- package/lib/module/utils/mediaQueries.js.map +1 -0
- package/lib/module/utils/mediaQueries.spec.js +218 -0
- package/lib/module/utils/mediaQueries.spec.js.map +1 -0
- package/lib/module/utils/styles.js +75 -0
- package/lib/module/utils/styles.js.map +1 -0
- package/lib/module/utils/styles.spec.js +96 -0
- package/lib/module/utils/styles.spec.js.map +1 -0
- package/lib/typescript/src/UnistylesTheme.d.ts +9 -0
- package/lib/typescript/src/UnistylesTheme.d.ts.map +1 -0
- package/lib/typescript/src/createUnistyles.d.ts +9 -0
- package/lib/typescript/src/createUnistyles.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +3 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +27 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/lib/typescript/src/utils/breakpoints.d.ts +63 -0
- package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -0
- package/lib/typescript/src/utils/breakpoints.spec.d.ts +2 -0
- package/lib/typescript/src/utils/breakpoints.spec.d.ts.map +1 -0
- package/lib/typescript/src/utils/common.d.ts +2 -0
- package/lib/typescript/src/utils/common.d.ts.map +1 -0
- package/lib/typescript/src/utils/index.d.ts +3 -0
- package/lib/typescript/src/utils/index.d.ts.map +1 -0
- package/lib/typescript/src/utils/mediaQueries.d.ts +130 -0
- package/lib/typescript/src/utils/mediaQueries.d.ts.map +1 -0
- package/lib/typescript/src/utils/mediaQueries.spec.d.ts +2 -0
- package/lib/typescript/src/utils/mediaQueries.spec.d.ts.map +1 -0
- package/lib/typescript/src/utils/styles.d.ts +56 -0
- package/lib/typescript/src/utils/styles.d.ts.map +1 -0
- package/lib/typescript/src/utils/styles.spec.d.ts +2 -0
- package/lib/typescript/src/utils/styles.spec.d.ts.map +1 -0
- package/package.json +132 -0
- package/src/UnistylesTheme.tsx +17 -0
- package/src/createUnistyles.ts +56 -0
- package/src/index.ts +2 -0
- package/src/types.ts +52 -0
- package/src/utils/breakpoints.ts +140 -0
- package/src/utils/common.ts +3 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/mediaQueries.ts +201 -0
- package/src/utils/styles.ts +95 -0
@@ -0,0 +1,82 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.proxifyFunction = exports.parseStyle = void 0;
|
7
|
+
var _breakpoints = require("./breakpoints");
|
8
|
+
/**
|
9
|
+
* Proxies a function to parse its return value for custom media queries or breakpoints.
|
10
|
+
*
|
11
|
+
* If the function's string representation contains a custom media query or a defined breakpoint,
|
12
|
+
* the returned function will be proxied to parse its return value based on the provided screen size and breakpoints.
|
13
|
+
* If neither is found, the original function is returned.
|
14
|
+
*
|
15
|
+
* @template B - An object type where keys represent breakpoint names and values represent breakpoint values.
|
16
|
+
*
|
17
|
+
* @param {Function} fn - The function to be proxified.
|
18
|
+
* @param {keyof B & string} breakpoint - The breakpoint name to check against.
|
19
|
+
* @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
|
20
|
+
* @param {B} breakpoints - An object representing the defined breakpoints.
|
21
|
+
*
|
22
|
+
* @returns {Function} Returns the proxified function or the original function if no custom media query or breakpoint is found in its string representation.
|
23
|
+
*
|
24
|
+
* @example
|
25
|
+
*
|
26
|
+
* const myFunction = () => ({ ':w[200]': 'value1', sm: 'value2' })
|
27
|
+
* const screenSize = { width: 250, height: 400 }
|
28
|
+
* const breakpoints = { sm: 300, md: 600 }
|
29
|
+
*
|
30
|
+
* const proxifiedFunction = proxifyFunction(myFunction, 'sm', screenSize, breakpoints)
|
31
|
+
* proxifiedFunction() // parsed style based on screenSize and breakpoints
|
32
|
+
*/
|
33
|
+
const proxifyFunction = (fn, breakpoint, screenSize, breakpoints) => {
|
34
|
+
const stringifiedFunction = fn.toString();
|
35
|
+
const hasCustomMediaQuery = stringifiedFunction.includes(':w[') || stringifiedFunction.includes(':h[');
|
36
|
+
const hasBreakpoint = Object.keys(breakpoints).some(bp => stringifiedFunction.includes(bp));
|
37
|
+
if (!hasCustomMediaQuery && !hasBreakpoint) {
|
38
|
+
return fn;
|
39
|
+
}
|
40
|
+
return new Proxy(fn, {
|
41
|
+
apply: (target, thisArg, argumentsList) => parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize, breakpoints)
|
42
|
+
});
|
43
|
+
};
|
44
|
+
|
45
|
+
/**
|
46
|
+
* Parses a style object to resolve custom media queries or breakpoints based on the provided screen size and breakpoints.
|
47
|
+
*
|
48
|
+
* The function processes each key-value pair in the style object. If the value is a function or a valid style (not an object or a 'transform' key),
|
49
|
+
* it is returned as-is. Otherwise, the function attempts to resolve the value based on the provided breakpoint, screen size, and defined breakpoints.
|
50
|
+
*
|
51
|
+
* @template T - The type of the style object.
|
52
|
+
* @template B - An object type where keys represent breakpoint names and values represent breakpoint values.
|
53
|
+
*
|
54
|
+
* @param {CustomNamedStyles<T, B>} style - The style object to be parsed.
|
55
|
+
* @param {keyof B & string} breakpoint - The breakpoint name to check against.
|
56
|
+
* @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
|
57
|
+
* @param {B} breakpoints - An object representing the defined breakpoints.
|
58
|
+
*
|
59
|
+
* @returns {Record<string, string | number | Function>} Returns the parsed style object with resolved custom media queries or breakpoints.
|
60
|
+
*
|
61
|
+
* @example
|
62
|
+
*
|
63
|
+
* const style = { fontSize: { sm: '12px', md: '16px' } }
|
64
|
+
* const screenSize = { width: 300, height: 400 }
|
65
|
+
* const breakpoints = { xs: 0, sm: 300, md: 600 }
|
66
|
+
*
|
67
|
+
* const parsedStyle = parseStyle(style, 'sm', screenSize, breakpoints)
|
68
|
+
* // { fontSize: '12px' }
|
69
|
+
*/
|
70
|
+
exports.proxifyFunction = proxifyFunction;
|
71
|
+
const parseStyle = (style, breakpoint, screenSize, breakpoints) => Object.fromEntries(Object.entries(style).map(_ref => {
|
72
|
+
let [key, value] = _ref;
|
73
|
+
const isDynamicFunction = typeof value === 'function';
|
74
|
+
const isValidStyle = typeof value !== 'object' || key === 'transform';
|
75
|
+
if (isDynamicFunction || isValidStyle) {
|
76
|
+
return [key, value];
|
77
|
+
}
|
78
|
+
const valueWithBreakpoint = value;
|
79
|
+
return [key, (0, _breakpoints.getValueForBreakpoint)(valueWithBreakpoint, breakpoint, screenSize, breakpoints)];
|
80
|
+
}));
|
81
|
+
exports.parseStyle = parseStyle;
|
82
|
+
//# sourceMappingURL=styles.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["_breakpoints","require","proxifyFunction","fn","breakpoint","screenSize","breakpoints","stringifiedFunction","toString","hasCustomMediaQuery","includes","hasBreakpoint","Object","keys","some","bp","Proxy","apply","target","thisArg","argumentsList","parseStyle","exports","style","fromEntries","entries","map","_ref","key","value","isDynamicFunction","isValidStyle","valueWithBreakpoint","getValueForBreakpoint"],"sourceRoot":"../../../src","sources":["utils/styles.ts"],"mappings":";;;;;;AACA,IAAAA,YAAA,GAAAC,OAAA;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMC,eAAe,GAAGA,CAC3BC,EAAY,EAAEC,UAA4B,EAC1CC,UAAsB,EACtBC,WAAc,KACH;EACX,MAAMC,mBAAmB,GAAGJ,EAAE,CAACK,QAAQ,CAAC,CAAC;EACzC,MAAMC,mBAAmB,GAAGF,mBAAmB,CAACG,QAAQ,CAAC,KAAK,CAAC,IAAIH,mBAAmB,CAACG,QAAQ,CAAC,KAAK,CAAC;EACtG,MAAMC,aAAa,GAAGC,MAAM,CACvBC,IAAI,CAACP,WAAW,CAAC,CACjBQ,IAAI,CAACC,EAAE,IAAIR,mBAAmB,CAACG,QAAQ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
+
|
3
|
+
var _styles = require("./styles");
|
4
|
+
describe('styles', () => {
|
5
|
+
describe('proxifyFunction', () => {
|
6
|
+
it('should parse style for dynamic function', () => {
|
7
|
+
const screenSize = {
|
8
|
+
width: 400,
|
9
|
+
height: 800
|
10
|
+
};
|
11
|
+
const breakpoint = 'sm';
|
12
|
+
const breakpoints = {
|
13
|
+
xs: 0,
|
14
|
+
sm: 400,
|
15
|
+
md: 800
|
16
|
+
};
|
17
|
+
const dynamicFunction = isEven => ({
|
18
|
+
backgroundColor: {
|
19
|
+
sm: isEven ? 'green' : 'red',
|
20
|
+
md: isEven ? 'orange' : 'pink'
|
21
|
+
}
|
22
|
+
});
|
23
|
+
expect((0, _styles.proxifyFunction)(dynamicFunction, breakpoint, screenSize, breakpoints)(true)).toEqual({
|
24
|
+
backgroundColor: 'green'
|
25
|
+
});
|
26
|
+
});
|
27
|
+
it('should return proxified function for custom media query', () => {
|
28
|
+
const screenSize = {
|
29
|
+
width: 400,
|
30
|
+
height: 800
|
31
|
+
};
|
32
|
+
const breakpoint = 'sm';
|
33
|
+
const breakpoints = {
|
34
|
+
xs: 0,
|
35
|
+
sm: 400,
|
36
|
+
md: 800
|
37
|
+
};
|
38
|
+
const dynamicFunction = isEven => ({
|
39
|
+
backgroundColor: {
|
40
|
+
':w[,399]': isEven ? 'green' : 'red',
|
41
|
+
':w[400]': isEven ? 'orange' : 'pink'
|
42
|
+
}
|
43
|
+
});
|
44
|
+
expect((0, _styles.proxifyFunction)(dynamicFunction, breakpoint, screenSize, breakpoints)(false)).toEqual({
|
45
|
+
backgroundColor: 'pink'
|
46
|
+
});
|
47
|
+
});
|
48
|
+
it('should return same function for no breakpoints nor media queries', () => {
|
49
|
+
const screenSize = {
|
50
|
+
width: 400,
|
51
|
+
height: 800
|
52
|
+
};
|
53
|
+
const breakpoint = 'sm';
|
54
|
+
const breakpoints = {
|
55
|
+
xs: 0,
|
56
|
+
sm: 400,
|
57
|
+
md: 800
|
58
|
+
};
|
59
|
+
const dynamicFunction = isEven => ({
|
60
|
+
backgroundColor: isEven ? 'pink' : 'purple'
|
61
|
+
});
|
62
|
+
expect((0, _styles.proxifyFunction)(dynamicFunction, breakpoint, screenSize, breakpoints)(false)).toEqual({
|
63
|
+
backgroundColor: 'purple'
|
64
|
+
});
|
65
|
+
});
|
66
|
+
});
|
67
|
+
describe('parseStyle', () => {
|
68
|
+
it('should correctly parse styles', () => {
|
69
|
+
const screenSize = {
|
70
|
+
width: 400,
|
71
|
+
height: 800
|
72
|
+
};
|
73
|
+
const breakpoint = 'sm';
|
74
|
+
const breakpoints = {
|
75
|
+
xs: 0,
|
76
|
+
sm: 400,
|
77
|
+
md: 800
|
78
|
+
};
|
79
|
+
const style = {
|
80
|
+
fontSize: {
|
81
|
+
sm: 12,
|
82
|
+
md: 20
|
83
|
+
},
|
84
|
+
backgroundColor: {
|
85
|
+
xs: 'pink',
|
86
|
+
md: 'orange'
|
87
|
+
},
|
88
|
+
fontWeight: 'bold'
|
89
|
+
};
|
90
|
+
expect((0, _styles.parseStyle)(style, breakpoint, screenSize, breakpoints)).toEqual({
|
91
|
+
fontSize: 12,
|
92
|
+
backgroundColor: 'pink',
|
93
|
+
fontWeight: 'bold'
|
94
|
+
});
|
95
|
+
});
|
96
|
+
});
|
97
|
+
});
|
98
|
+
//# sourceMappingURL=styles.spec.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["_styles","require","describe","it","screenSize","width","height","breakpoint","breakpoints","xs","sm","md","dynamicFunction","isEven","backgroundColor","expect","proxifyFunction","toEqual","style","fontSize","fontWeight","parseStyle"],"sourceRoot":"../../../src","sources":["utils/styles.spec.ts"],"mappings":";;AAAA,IAAAA,OAAA,GAAAC,OAAA;AAGAC,QAAQ,CAAC,QAAQ,EAAE,MAAM;EACrBA,QAAQ,CAAC,iBAAiB,EAAE,MAAM;IAC9BC,EAAE,CAAC,yCAAyC,EAAE,MAAM;MAChD,MAAMC,UAAsB,GAAG;QAC3BC,KAAK,E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@ -0,0 +1,12 @@
|
|
1
|
+
import React, { createContext } from 'react';
|
2
|
+
export const UnistylesContext = /*#__PURE__*/createContext({});
|
3
|
+
export const UnistylesTheme = _ref => {
|
4
|
+
let {
|
5
|
+
theme,
|
6
|
+
children
|
7
|
+
} = _ref;
|
8
|
+
return /*#__PURE__*/React.createElement(UnistylesContext.Provider, {
|
9
|
+
value: theme
|
10
|
+
}, children);
|
11
|
+
};
|
12
|
+
//# sourceMappingURL=UnistylesTheme.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["React","createContext","UnistylesContext","UnistylesTheme","_ref","theme","children","createElement","Provider","value"],"sourceRoot":"../../src","sources":["UnistylesTheme.tsx"],"mappings":"AAAA,OAAOA,KAAK,IAAIC,aAAa,QAAQ,OAAO;AAO5C,OAAO,MAAMC,gBAAgB,gBAAGD,aAAa,CAAC,CAAC,CAAC,CAAC;AAEjD,OAAO,MAAME,cAA4D,GAAGC,IAAA;EAAA,IAAC;IACzEC,KAAK;IACLC;EACJ,CAAC,GAAAF,IAAA;EAAA,oBACGJ,KAAA,CAAAO,aAAA,CAACL,gBAAgB,CAACM,QAAQ;IAACC,KAAK,EAAEJ;EAAM,GACnCC,QACsB,CAAC;AAAA,CAC/B"}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
import { useContext } from 'react';
|
2
|
+
import { useWindowDimensions } from 'react-native';
|
3
|
+
import { UnistylesContext } from './UnistylesTheme';
|
4
|
+
import { getBreakpointFromScreenWidth, proxifyFunction, parseStyle, sortAndValidateBreakpoints } from './utils';
|
5
|
+
export const createUnistyles = breakpoints => {
|
6
|
+
const sortedBreakpoints = sortAndValidateBreakpoints(breakpoints);
|
7
|
+
return {
|
8
|
+
createStyles: styles => styles,
|
9
|
+
useStyles: stylesheet => {
|
10
|
+
const theme = useContext(UnistylesContext);
|
11
|
+
const dimensions = useWindowDimensions();
|
12
|
+
const breakpoint = getBreakpointFromScreenWidth(dimensions.width, sortedBreakpoints);
|
13
|
+
const screenSize = {
|
14
|
+
width: dimensions.width,
|
15
|
+
height: dimensions.height
|
16
|
+
};
|
17
|
+
if (!stylesheet) {
|
18
|
+
return {
|
19
|
+
theme,
|
20
|
+
styles: {}
|
21
|
+
};
|
22
|
+
}
|
23
|
+
const parsedStyles = typeof stylesheet === 'function' ? stylesheet(theme) : stylesheet;
|
24
|
+
const dynamicStyleSheet = Object.entries(parsedStyles).reduce((acc, _ref) => {
|
25
|
+
let [key, value] = _ref;
|
26
|
+
const x = value;
|
27
|
+
if (typeof value === 'function') {
|
28
|
+
return {
|
29
|
+
...acc,
|
30
|
+
[key]: proxifyFunction(value, breakpoint, screenSize, sortedBreakpoints)
|
31
|
+
};
|
32
|
+
}
|
33
|
+
return {
|
34
|
+
...acc,
|
35
|
+
[key]: parseStyle(x, breakpoint, screenSize, sortedBreakpoints)
|
36
|
+
};
|
37
|
+
}, {});
|
38
|
+
return {
|
39
|
+
theme,
|
40
|
+
styles: dynamicStyleSheet
|
41
|
+
};
|
42
|
+
}
|
43
|
+
};
|
44
|
+
};
|
45
|
+
//# sourceMappingURL=createUnistyles.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["useContext","useWindowDimensions","UnistylesContext","getBreakpointFromScreenWidth","proxifyFunction","parseStyle","sortAndValidateBreakpoints","createUnistyles","breakpoints","sortedBreakpoints","createStyles","styles","useStyles","stylesheet","theme","dimensions","breakpoint","width","screenSize","height","parsedStyles","dynamicStyleSheet","Object","entries","reduce","acc","_ref","key","value","x"],"sourceRoot":"../../src","sources":["createUnistyles.ts"],"mappings":"AAAA,SAASA,UAAU,QAAQ,OAAO;AAClC,SAASC,mBAAmB,QAAQ,cAAc;AAElD,SAASC,gBAAgB,QAAQ,kBAAkB;AACnD,SAASC,4BAA4B,EAAEC,eAAe,EAAEC,UAAU,EAAEC,0BAA0B,QAAQ,SAAS;AAE/G,OAAO,MAAMC,eAAe,GAA8CC,WAAc,IAAK;EACzF,MAAMC,iBAAiB,GAAGH,0BA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CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["UnistylesTheme","createUnistyles"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":"AAAA,SAASA,cAAc,QAAQ,kBAAkB;AACjD,SAASC,eAAe,QAAQ,mBAAmB"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["types.ts"],"mappings":""}
|
@@ -0,0 +1,131 @@
|
|
1
|
+
import { throwError } from './common';
|
2
|
+
import { getKeyForCustomMediaQuery, isMediaQuery } from './mediaQueries';
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Sorts the breakpoints object based on its numeric values in ascending order and validates them.
|
6
|
+
*
|
7
|
+
* This function takes an object where keys represent breakpoint names and values are numeric.
|
8
|
+
* It returns a new object with the same keys but sorted based on their corresponding numeric values.
|
9
|
+
* Additionally, it validates that:
|
10
|
+
* 1. The first breakpoint starts with a value of 0.
|
11
|
+
* 2. No duplicate breakpoint values exist.
|
12
|
+
*
|
13
|
+
* If the validation fails, appropriate error messages are logged to the console.
|
14
|
+
*
|
15
|
+
* @template B - An object type where keys are strings and values are numbers.
|
16
|
+
* @param {B} breakpoints - The breakpoints object to be sorted and validated.
|
17
|
+
* @returns {B} A new object with sorted and validated breakpoints.
|
18
|
+
*
|
19
|
+
* @example
|
20
|
+
* const input = { md: 768, lg: 1024, sm: 0 }
|
21
|
+
* sortAndValidateBreakpoints(input) // returns { sm: 0, md: 768, lg: 1024 }
|
22
|
+
*/
|
23
|
+
export const sortAndValidateBreakpoints = breakpoints => {
|
24
|
+
const sortedPairs = Object.entries(breakpoints).sort((breakpoint1, breakpoint2) => {
|
25
|
+
const [, value1] = breakpoint1;
|
26
|
+
const [, value2] = breakpoint2;
|
27
|
+
return value1 - value2;
|
28
|
+
});
|
29
|
+
const sortedBreakpoints = Object.freeze(Object.fromEntries(sortedPairs));
|
30
|
+
const breakpointValues = Object.values(sortedBreakpoints);
|
31
|
+
const [firstBreakpoint] = breakpointValues;
|
32
|
+
if (firstBreakpoint !== 0) {
|
33
|
+
throwError('first breakpoint must start with 0');
|
34
|
+
}
|
35
|
+
if (breakpointValues.length !== new Set(breakpointValues).size) {
|
36
|
+
throwError('breakpoint values are duplicated');
|
37
|
+
}
|
38
|
+
return sortedBreakpoints;
|
39
|
+
};
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Determines the appropriate breakpoint key for a given screen width based on provided breakpoints.
|
43
|
+
*
|
44
|
+
* This function takes a screen width and an object of breakpoints. It returns the key of the breakpoint
|
45
|
+
* that the screen width falls into. The breakpoints are assumed to be sorted in ascending order.
|
46
|
+
*
|
47
|
+
* @template B - An object type where keys are strings and values are numbers representing screen widths.
|
48
|
+
* @param {number} width - The screen width to determine the breakpoint for.
|
49
|
+
* @param {B} breakpoints - The breakpoints object to use for determination.
|
50
|
+
* @returns {keyof B & string} The key of the breakpoint that the screen width falls into.
|
51
|
+
*
|
52
|
+
* @example
|
53
|
+
* const breakpoints = { sm: 0, md: 768, lg: 1024 }
|
54
|
+
* getBreakpointFromScreenWidth(800, breakpoints) // returns 'md'
|
55
|
+
*/
|
56
|
+
export const getBreakpointFromScreenWidth = (width, breakpoints) => {
|
57
|
+
const [key] = Object.entries(breakpoints).find((_ref, index, otherBreakpoints) => {
|
58
|
+
let [, value] = _ref;
|
59
|
+
const minVal = value;
|
60
|
+
const maxVal = otherBreakpoints[index + 1]?.[1];
|
61
|
+
if (!maxVal) {
|
62
|
+
return true;
|
63
|
+
}
|
64
|
+
return width >= minVal && width < maxVal;
|
65
|
+
});
|
66
|
+
return key;
|
67
|
+
};
|
68
|
+
|
69
|
+
/**
|
70
|
+
* Retrieves the value associated with a given breakpoint or custom media query based on the provided screen size.
|
71
|
+
*
|
72
|
+
* The function first checks for custom media queries. If a matching custom media query is found, its associated value is returned.
|
73
|
+
* If no custom media query matches, the function then checks for a direct breakpoint match.
|
74
|
+
* If there's no direct breakpoint match, the function simulates CSS cascading to find the closest matching breakpoint.
|
75
|
+
*
|
76
|
+
* @template B - An object type where keys represent breakpoint names and values represent breakpoint values.
|
77
|
+
*
|
78
|
+
* @param {Record<keyof B & string, string | number>} value - An object containing values associated with breakpoints or custom media queries.
|
79
|
+
* @param {keyof B & string} breakpoint - The breakpoint name to check against.
|
80
|
+
* @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
|
81
|
+
* @param {B} breakpoints - An object representing the defined breakpoints.
|
82
|
+
*
|
83
|
+
* @returns {string | number | undefined} Returns the value associated with the matching breakpoint or custom media query, or `undefined` if no match is found.
|
84
|
+
*
|
85
|
+
* @example
|
86
|
+
*
|
87
|
+
* const values = { ':w[200]': 'value1', sm: 'value2', md: 'value3' }
|
88
|
+
* const screenSize = { width: 250, height: 400 }
|
89
|
+
* const breakpoints = { sm: 300, md: 600, lg: 900 }
|
90
|
+
*
|
91
|
+
* getValueForBreakpoint(values, 'sm', screenSize, breakpoints); // 'value1'
|
92
|
+
*/
|
93
|
+
export const getValueForBreakpoint = (value, breakpoint, screenSize, breakpoints) => {
|
94
|
+
// the highest priority is for custom media queries
|
95
|
+
const customMediaQueries = Object.entries(value).filter(_ref2 => {
|
96
|
+
let [key] = _ref2;
|
97
|
+
return isMediaQuery(key);
|
98
|
+
});
|
99
|
+
const customMediaQueryKey = getKeyForCustomMediaQuery(customMediaQueries, screenSize);
|
100
|
+
if (customMediaQueryKey && customMediaQueryKey in value) {
|
101
|
+
return value[customMediaQueryKey];
|
102
|
+
}
|
103
|
+
|
104
|
+
// if no custom media query, or didn't match, proceed with defined breakpoints
|
105
|
+
const unifiedKey = breakpoint.toLowerCase();
|
106
|
+
const directBreakpoint = value[unifiedKey];
|
107
|
+
|
108
|
+
// if there is a direct key like 'sm' or 'md', or value for this key exists but its undefined
|
109
|
+
if (directBreakpoint || unifiedKey in value) {
|
110
|
+
return directBreakpoint;
|
111
|
+
}
|
112
|
+
|
113
|
+
// there is no direct hit for breakpoint nor media-query, so let's simulate CSS cascading
|
114
|
+
const allBreakpoints = Object.entries(breakpoints).map(_ref3 => {
|
115
|
+
let [key, bpValue] = _ref3;
|
116
|
+
return [key.toLowerCase(), bpValue];
|
117
|
+
});
|
118
|
+
const currentBreakpoint = allBreakpoints.findIndex(_ref4 => {
|
119
|
+
let [key] = _ref4;
|
120
|
+
return key === unifiedKey;
|
121
|
+
});
|
122
|
+
const availableBreakpoints = allBreakpoints.filter((_ref5, index) => {
|
123
|
+
let [key] = _ref5;
|
124
|
+
return index < currentBreakpoint && key && key in value;
|
125
|
+
}).map(_ref6 => {
|
126
|
+
let [key] = _ref6;
|
127
|
+
return key;
|
128
|
+
});
|
129
|
+
return allBreakpoints.length > 0 ? value[availableBreakpoints[availableBreakpoints.length - 1]] : undefined;
|
130
|
+
};
|
131
|
+
//# sourceMappingURL=breakpoints.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["throwError","getKeyForCustomMediaQuery","isMediaQuery","sortAndValidateBreakpoints","breakpoints","sortedPairs","Object","entries","sort","breakpoint1","breakpoint2","value1","value2","sortedBreakpoints","freeze","fromEntries","breakpointValues","values","firstBreakpoint","length","Set","size","getBreakpointFromScreenWidth","width","key","find","_ref","index","otherBreakpoints","value","minVal","maxVal","getValueForBreakpoint","breakpoint","screenSize","customMediaQueries","filter","_ref2","customMediaQueryKey","unifiedKey","toLowerCase","directBreakpoint","allBreakpoints","map","_ref3","bpValue","currentBreakpoint","findIndex","_ref4","availableBreakpoints","_ref5","_ref6","undefined"],"sourceRoot":"../../../src","sources":["utils/breakpoints.ts"],"mappings":"AAAA,SAASA,UAAU,QAAQ,UAAU;AAErC,SAASC,yBAAyB,EAAEC,YAAY,QAAQ,gBAAgB;;AAExE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,0BAA0B,GAAsCC,WAAc,IAAQ;EAC/F,MAAMC,WAAW,GAAGC,MAAM,CACrBC,OAAO,CAACH,WAAW,C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E7C,MAAMU,oBAAoB,GAAGP,cAAc,CACtCN,MAAM,CAAC,CAAAc,KAAA,EAAQvB,KAAK;IAAA,IAAZ,CAACH,GAAG,CAAC,GAAA0B,KAAA;IAAA,OAAYvB,KAAK,GAAGmB,iBAAiB,IAAItB,GAAG,IAAIA,GAAG,IAAIK,KAAK;EAAA,EAAC,CAC1Ec,GAAG,CAACQ,KAAA;IAAA,IAAC,CAAC3B,GAAG,CAAC,GAAA2B,KAAA;IAAA,OAAK3B,GAAG;EAAA,EAAC;EAExB,OAAOkB,cAAc,CAACvB,MAAM,GAAG,CAAC,GAC1BU,KAAK,CAACoB,oBAAoB,CAACA,oBAAoB,CAAC9B,MAAM,GAAG,CAAC,CAAC,CAAqB,GAChFiC,SAAS;AACnB,CAAC"}
|
@@ -0,0 +1,147 @@
|
|
1
|
+
import { getBreakpointFromScreenWidth, getValueForBreakpoint, sortAndValidateBreakpoints } from './breakpoints';
|
2
|
+
describe('breakpoints', () => {
|
3
|
+
describe('sortAndValidateBreakpoints', () => {
|
4
|
+
it('should throw error for empty breakpoints', () => {
|
5
|
+
const breakpoints = {};
|
6
|
+
expect(() => sortAndValidateBreakpoints(breakpoints)).toThrow();
|
7
|
+
});
|
8
|
+
it('should throw error for breakpoints that don\'t start with 0', () => {
|
9
|
+
const breakpoints = {
|
10
|
+
sm: -1
|
11
|
+
};
|
12
|
+
expect(() => sortAndValidateBreakpoints(breakpoints)).toThrow();
|
13
|
+
});
|
14
|
+
it('should return same order for sorted elements', () => {
|
15
|
+
const breakpoints = {
|
16
|
+
sm: 0,
|
17
|
+
md: 200,
|
18
|
+
lg: 300,
|
19
|
+
xl: 500
|
20
|
+
};
|
21
|
+
expect(sortAndValidateBreakpoints(breakpoints)).toEqual(breakpoints);
|
22
|
+
});
|
23
|
+
it('should sort the order of breakpoints', () => {
|
24
|
+
const breakpoints = {
|
25
|
+
md: 200,
|
26
|
+
sm: 0,
|
27
|
+
lg: 300,
|
28
|
+
xl: 500
|
29
|
+
};
|
30
|
+
expect(sortAndValidateBreakpoints(breakpoints)).toEqual({
|
31
|
+
sm: 0,
|
32
|
+
md: 200,
|
33
|
+
lg: 300,
|
34
|
+
xl: 500
|
35
|
+
});
|
36
|
+
});
|
37
|
+
it('should throw errors for duplicated values', () => {
|
38
|
+
const breakpoints = {
|
39
|
+
md: 200,
|
40
|
+
sm: 0,
|
41
|
+
lg: 300,
|
42
|
+
xl: 300
|
43
|
+
};
|
44
|
+
expect(() => sortAndValidateBreakpoints(breakpoints)).toThrow();
|
45
|
+
});
|
46
|
+
});
|
47
|
+
describe('getBreakpointFromScreenWidth', () => {
|
48
|
+
it('should return correct breakpoints based on screen width', () => {
|
49
|
+
const breakpoints = {
|
50
|
+
xs: 0,
|
51
|
+
sm: 300,
|
52
|
+
md: 500,
|
53
|
+
xl: 1200
|
54
|
+
};
|
55
|
+
expect(getBreakpointFromScreenWidth(0, breakpoints)).toEqual('xs');
|
56
|
+
expect(getBreakpointFromScreenWidth(20, breakpoints)).toEqual('xs');
|
57
|
+
expect(getBreakpointFromScreenWidth(100, breakpoints)).toEqual('xs');
|
58
|
+
expect(getBreakpointFromScreenWidth(101, breakpoints)).toEqual('xs');
|
59
|
+
expect(getBreakpointFromScreenWidth(250, breakpoints)).toEqual('xs');
|
60
|
+
expect(getBreakpointFromScreenWidth(300, breakpoints)).toEqual('sm');
|
61
|
+
expect(getBreakpointFromScreenWidth(499, breakpoints)).toEqual('sm');
|
62
|
+
expect(getBreakpointFromScreenWidth(500, breakpoints)).toEqual('md');
|
63
|
+
expect(getBreakpointFromScreenWidth(799, breakpoints)).toEqual('md');
|
64
|
+
expect(getBreakpointFromScreenWidth(1200, breakpoints)).toEqual('xl');
|
65
|
+
expect(getBreakpointFromScreenWidth(2000, breakpoints)).toEqual('xl');
|
66
|
+
});
|
67
|
+
});
|
68
|
+
describe('getValueForBreakpoint', () => {
|
69
|
+
it('should prioritize custom media query', () => {
|
70
|
+
const breakpoint = 'sm';
|
71
|
+
const breakpoints = {
|
72
|
+
xs: 0,
|
73
|
+
sm: 200,
|
74
|
+
md: 500
|
75
|
+
};
|
76
|
+
const style = {
|
77
|
+
':w[, 200]': 'green',
|
78
|
+
':w[201]': 'orange',
|
79
|
+
sm: 'pink'
|
80
|
+
};
|
81
|
+
const screenSize = {
|
82
|
+
width: 200,
|
83
|
+
height: 800
|
84
|
+
};
|
85
|
+
expect(getValueForBreakpoint(style, breakpoint, screenSize, breakpoints)).toEqual('green');
|
86
|
+
});
|
87
|
+
});
|
88
|
+
it('should match breakpoint if media query doesnt exist', () => {
|
89
|
+
const breakpoint = 'md';
|
90
|
+
const breakpoints = {
|
91
|
+
xs: 0,
|
92
|
+
sm: 200,
|
93
|
+
md: 500
|
94
|
+
};
|
95
|
+
const style = {
|
96
|
+
':w[, 200]': 'green',
|
97
|
+
':w[201, 499]': 'orange',
|
98
|
+
sm: 'pink',
|
99
|
+
md: 'red'
|
100
|
+
};
|
101
|
+
const screenSize = {
|
102
|
+
width: 500,
|
103
|
+
height: 1200
|
104
|
+
};
|
105
|
+
expect(getValueForBreakpoint(style, breakpoint, screenSize, breakpoints)).toEqual('red');
|
106
|
+
});
|
107
|
+
it('should match breakpoint even if value is undefined', () => {
|
108
|
+
const breakpoint = 'md';
|
109
|
+
const breakpoints = {
|
110
|
+
xs: 0,
|
111
|
+
sm: 200,
|
112
|
+
md: 500
|
113
|
+
};
|
114
|
+
const style = {
|
115
|
+
':w[, 200]': 'green',
|
116
|
+
':w[201, 499]': 'orange',
|
117
|
+
sm: 'pink',
|
118
|
+
md: undefined
|
119
|
+
};
|
120
|
+
const screenSize = {
|
121
|
+
width: 500,
|
122
|
+
height: 1200
|
123
|
+
};
|
124
|
+
expect(getValueForBreakpoint(style, breakpoint, screenSize, breakpoints)).toEqual(undefined);
|
125
|
+
});
|
126
|
+
it('should match lower breakpoint to match css cascading', () => {
|
127
|
+
const breakpoint = 'xl';
|
128
|
+
const breakpoints = {
|
129
|
+
xs: 0,
|
130
|
+
sm: 200,
|
131
|
+
md: 500,
|
132
|
+
xl: 600
|
133
|
+
};
|
134
|
+
const style = {
|
135
|
+
':w[, 200]': 'green',
|
136
|
+
':w[201, 499]': 'orange',
|
137
|
+
sm: 'pink',
|
138
|
+
md: 'red'
|
139
|
+
};
|
140
|
+
const screenSize = {
|
141
|
+
width: 500,
|
142
|
+
height: 1200
|
143
|
+
};
|
144
|
+
expect(getValueForBreakpoint(style, breakpoint, screenSize, breakpoints)).toEqual('red');
|
145
|
+
});
|
146
|
+
});
|
147
|
+
//# sourceMappingURL=breakpoints.spec.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["getBreakpointFromScreenWidth","getValueForBreakpoint","sortAndValidateBreakpoints","describe","it","breakpoints","expect","toThrow","sm","md","lg","xl","toEqual","xs","breakpoint","style","screenSize","width","height","undefined"],"sourceRoot":"../../../src","sources":["utils/breakpoints.spec.ts"],"mappings":"AAAA,SAASA,4BAA4B,EAAEC,qBAAqB,EAAEC,0BAA0B,QAAQ,eAAe;AAG/GC,QAAQ,CAAC,aAAa,EAAE,MAAM;EAC1BA,QAAQ,CAAC,4BAA4B,EAAE,MAAM;IACzCC,EAAE,CAAC,0CAA0C,EAAE,MAAM;MACjD,MAAMC,WAAW,GAAG,CAAC,CAAC;MAEtBC,MAAM,CAAC,MAAMJ,0BAA0B,CAACG,WAAW,CAAC,CAAC,CAACE,OAAO,CAAC,CAAC;IACnE,CAAC,CAAC;IAEFH,EAAE,CAAC,6DAA6D,EAAE,MAAM;MACpE,MAAMC,WAAW,GAAG;QAChBG,EAAE,EAAE,CAAC;MACT,CAAC;MAEDF,MAAM,CAAC,MAAMJ,0BAA0B,CAACG,WAAW,CAAC,CAAC,CAACE,OAAO,CAAC,CAAC;IACnE,CAAC,CAAC;IAEFH,EAAE,CAAC,8CAA8C,EAAE,MAAM;MACrD,MAAMC,WAAW,GAAG;QAChBG,EAAE,EAAE,CAAC;QACLC,EAAE,EAAE,GAAG;QACPC,EAAE,EAAE,GAAG;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AEFf,EAAE,CAAC,sDAAsD,EAAE,MAAM;IAC7D,MAAMU,UAAU,GAAG,IAAI;IACvB,MAAMT,WAAW,GAAG;MAChBQ,EAAE,EAAE,CAAC;MACLL,EAAE,EAAE,GAAG;MACPC,EAAE,EAAE,GAAG;MACPE,EAAE,EAAE;IACR,CAAC;IACD,MAAMI,KAAyC,GAAG;MAC9C,WAAW,EAAE,OAAO;MACpB,cAAc,EAAE,QAAQ;MACxBP,EAAE,EAAE,MAAM;MACVC,EAAE,EAAE;IACR,CAAC;IACD,MAAMO,UAAsB,GAAG;MAC3BC,KAAK,EAAE,GAAG;MACVC,MAAM,EAAE;IACZ,CAAC;IAEDZ,MAAM,CAACL,qBAAqB,CAACc,KAAK,EAAED,UAAU,EAAEE,UAAU,EAAEX,WAAW,CAAC,CAAC,CAACO,OAAO,CAAC,KAAK,CAAC;EAC5F,CAAC,CAAC;AACN,CAAC,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["throwError","message","Error"],"sourceRoot":"../../../src","sources":["utils/common.ts"],"mappings":"AAAA,OAAO,MAAMA,UAAU,GAAIC,OAAe,IAAK;EAC3C,MAAM,IAAIC,KAAK,CAAE,gCAA+BD,OAAQ,EAAC,CAAC;AAC9D,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["getBreakpointFromScreenWidth","sortAndValidateBreakpoints","proxifyFunction","parseStyle"],"sourceRoot":"../../../src","sources":["utils/index.ts"],"mappings":"AAAA,SAASA,4BAA4B,EAAEC,0BAA0B,QAAQ,eAAe;AACxF,SAASC,eAAe,EAAEC,UAAU,QAAQ,UAAU"}
|