-
Notifications
You must be signed in to change notification settings - Fork 390
Feat rn atomic css #1788
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
base: master
Are you sure you want to change the base?
Feat rn atomic css #1788
Changes from 108 commits
68434b2
a88cdd9
f9ab9c1
e39ec8a
450f76b
777dcca
c414f17
ff99449
7e90afe
386c0ff
11ddc5b
2034a02
372ab91
99dd414
7c9f994
27c13ac
3da4fee
f898f6b
792cde2
cc0cbf8
13fd97d
f3789ca
d86b308
dc50729
57ce2f8
c933e3e
320ace5
abf1b6f
ea891f8
e238722
dbccab7
f5f4160
dc6373f
b5e03c4
c4c9825
8d21de4
a3dacd4
30e96f7
f0adcbb
eaaf945
c5cbd43
7e1327e
8161d1b
77e9d1a
b918849
dd43b07
aa2a250
9466418
db17b59
45e2202
dc6a12f
01b76bf
6d7a499
769f7c7
ce5fbd3
5be0325
e14884b
f9b4b40
c66de72
459165e
c6dbef1
8d38f4d
3f87e59
ff32e25
a00c069
43219d9
e3e4dac
1091497
3fc9efa
f4132e9
b79ef77
5123832
810a235
0b0b38b
4bea9bb
7abaa37
c9c48ab
3d9bb0c
35ac85b
962b83b
976fc02
d54aaed
a8cb2d3
f99d366
8c50fdc
08d75f0
d641c3d
bf3df99
3a00f88
6c8fbed
33458ef
c9ea2e3
7cceee9
1232ba5
0efa7f2
74f0b63
74eb854
d75716f
54a0264
c5d09d7
cc5e287
248e0f4
adefb09
a3fedee
774632f
a6e1c43
ca2b28d
017bd1f
9f2566b
4dffe76
524b883
6beeeda
72b709a
bab88e6
8163c1b
8b0b71b
e0a6cde
1b641ab
7c6ab73
4c4d509
83e6ca5
569a9d8
cc41bb1
68062a6
de03b53
a8c9a13
058564c
87d7df9
9f2fa78
ceb565a
f0c0959
4e29bcb
965f560
6218ac0
e735ca4
ed9988c
469bf12
a19dda9
89335c5
b76a20e
7155707
62d9b51
b48aae8
ca4cc89
8f9fb17
96eb314
d62ecc4
176c14a
c9ef019
9f8a56e
1eabd9e
43b694a
2d4274a
8faca4f
35ca41d
5a4c119
e810e66
60d8856
6a0a5ca
7b6cf24
45dc487
2c04c47
bb964dd
bf2c650
7a37ece
7e428c1
eeb9a71
5cd921b
5bd333c
9d5b21e
4727d1f
3a495c7
a98b92e
567ca5b
c3f66ec
90dae12
f84c2ba
ff37b47
aed75bf
96d2310
25cdabd
b786ad2
b529969
32e993f
3983355
db38762
13fda9b
613ec5c
8080f54
3cbae5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { isObject, isArray, dash2hump, cached, isEmptyObject } from '@mpxjs/utils' | ||
| import { Dimensions, StyleSheet } from 'react-native' | ||
| import { Dimensions, StyleSheet, Appearance } from 'react-native' | ||
|
|
||
| let { width, height } = Dimensions.get('screen') | ||
|
|
||
|
|
@@ -43,35 +43,6 @@ function formatValue (value) { | |
|
|
||
| global.__formatValue = formatValue | ||
|
|
||
| const escapeReg = /[()[\]{}#!.:,%'"+$]/g | ||
| const escapeMap = { | ||
| '(': '_pl_', | ||
| ')': '_pr_', | ||
| '[': '_bl_', | ||
| ']': '_br_', | ||
| '{': '_cl_', | ||
| '}': '_cr_', | ||
| '#': '_h_', | ||
| '!': '_i_', | ||
| '/': '_s_', | ||
| '.': '_d_', | ||
| ':': '_c_', | ||
| ',': '_2c_', | ||
| '%': '_p_', | ||
| '\'': '_q_', | ||
| '"': '_dq_', | ||
| '+': '_a_', | ||
| $: '_si_' | ||
| } | ||
|
|
||
| const mpEscape = cached((str) => { | ||
| return str.replace(escapeReg, function (match) { | ||
| if (escapeMap[match]) return escapeMap[match] | ||
| // unknown escaped | ||
| return '_u_' | ||
| }) | ||
| }) | ||
|
|
||
| function concat (a = '', b = '') { | ||
| return a ? b ? (a + ' ' + b) : a : b | ||
| } | ||
|
|
@@ -168,24 +139,39 @@ export default function styleHelperMixin () { | |
| return concat(staticClass, stringifyDynamicClass(dynamicClass)) | ||
| }, | ||
| __getStyle (staticClass, dynamicClass, staticStyle, dynamicStyle, hide) { | ||
| const result = {} | ||
| let result = {} | ||
| let unoResult = {} | ||
| const unoVarResult = {} | ||
| const classMap = this.__getClassMap?.() || {} | ||
| const { unoClassMap = {}, unoVarClassMap = {}, unoPreflightsClassMap = {} } = global.__getUnoClass?.() || {} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这三个classMap分别是啥?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unocssClassMap就是所有的unocss的样式类,例如
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unoPreflightsClassMap是unocss的全局var变量,会作为前置属性被unoClassMap覆盖例如 unoPreflightsClassMap为
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以基于一个支持css优先级的泛化方案来实现 |
||
| let hasUnoClass = false | ||
| const appClassMap = global.__getAppClassMap?.() || {} | ||
|
|
||
| if (staticClass || dynamicClass) { | ||
| // todo 当前为了复用小程序unocss产物,暂时进行mpEscape,等后续正式支持unocss后可不进行mpEscape | ||
| const classString = mpEscape(concat(staticClass, stringifyDynamicClass(dynamicClass))) | ||
| const classString = concat(staticClass, stringifyDynamicClass(dynamicClass)) | ||
| classString.split(/\s+/).forEach((className) => { | ||
| if (classMap[className]) { | ||
| Object.assign(result, classMap[className]) | ||
| } else if (appClassMap[className]) { | ||
| // todo 全局样式在每个页面和组件中生效,以支持全局原子类,后续支持样式模块复用后可考虑移除 | ||
| Object.assign(result, appClassMap[className]) | ||
| } else if (unoClassMap[className]) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unoClassMap整体需要参考local/appclassmap的按需执行设计,并且需要支持rpx,vw等样式刷新
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unoClassMap能合到AppClassMap中吗,包括preflightMap |
||
| hasUnoClass = true | ||
| Object.assign(unoResult, unoClassMap[className]) | ||
| } else if (unoVarClassMap[className]) { | ||
| Object.assign(unoVarResult, unoVarClassMap[className]) | ||
| } else if (isObject(this.__props[className])) { | ||
| // externalClasses必定以对象形式传递下来 | ||
| Object.assign(result, this.__props[className]) | ||
| } | ||
| }) | ||
| if (hasUnoClass) { | ||
| // 两个类需要前置默认css变量 | ||
| if (unoResult.transform || unoResult.filter) { | ||
| unoResult = Object.assign({}, unoPreflightsClassMap, unoResult) | ||
| } | ||
| // 合并uno工具变量 | ||
| result = Object.assign({}, unoResult, unoVarResult, result) | ||
| } | ||
| } | ||
|
|
||
| if (staticStyle || dynamicStyle) { | ||
|
|
@@ -198,8 +184,38 @@ export default function styleHelperMixin () { | |
| display: 'none' | ||
| }) | ||
| } | ||
|
|
||
| return isEmptyObject(result) ? empty : result | ||
| }, | ||
| __getDynamicClass (dynamicClass, mediaQueryClass) { | ||
| return [dynamicClass, this.__getMediaQueryClass(mediaQueryClass)] | ||
| }, | ||
| __getMediaQueryClass (mediaQueryClass = []) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 像media query这种应该做成一个通用能力,然后unocss借助这个能力来实现variant,而不是单独为了unocss实现 |
||
| if (!mediaQueryClass.length) return '' | ||
| const { width, height } = Dimensions.get('screen') | ||
| const colorScheme = Appearance.getColorScheme() | ||
| const { unoBreakpoints } = global.__getUnoClass?.() || {} | ||
| const { entries = [], entriesMap = {} } = unoBreakpoints | ||
| return mediaQueryClass.map(([className, querypoints = []]) => { | ||
| const res = querypoints.every(([prefix = '', point = 0]) => { | ||
| if (prefix === 'landscape') return width > height | ||
| if (prefix === 'portrait') return width <= height | ||
| if (prefix === 'dark') return colorScheme === 'dark' | ||
| if (prefix === 'light') return colorScheme === 'light' | ||
| const size = formatValue(entriesMap[point] || point) | ||
| const index = entries.findIndex(item => item[0] === point) | ||
| const isGtPrefix = prefix.startsWith('min-') | ||
| const isLtPrefix = prefix.startsWith('lt-') || prefix.startsWith('<') || prefix.startsWith('max-') | ||
| const isAtPrefix = prefix.startsWith('at-') || prefix.startsWith('~') | ||
| if (isGtPrefix) return width > size | ||
| if (isLtPrefix) return width < size | ||
| if (isAtPrefix && (index && index < entries.length - 1)) { | ||
| return width >= size && width < formatValue(entries[index + 1][1]) | ||
| } | ||
| return width > size | ||
| }) | ||
|
|
||
| return res ? className : '' | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,24 @@ | ||
| const { presetUno } = require('@unocss/preset-uno') | ||
| import { presetUno } from '@unocss/preset-uno' | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. v66.x + 版本已经剔除掉了 |
||
| import presetLegacyCompat from '@unocss/preset-legacy-compat' | ||
| import presetRn from '../preset-rn/index.js' | ||
|
|
||
| // eslint-disable-next-line | ||
| const remRE = /(-?[\.\d]+)rem/g | ||
|
|
||
| module.exports = function presetMpx (options = {}) { | ||
| export default function presetMpx (options = {}) { | ||
| const mpxCurrentTargetMode = process.env.MPX_CURRENT_TARGET_MODE | ||
| const isReact = mpxCurrentTargetMode === 'ios' || mpxCurrentTargetMode === 'android' | ||
| const extraPresets = [] | ||
| if (isReact) { | ||
| extraPresets.push(presetRn()) | ||
| extraPresets.push(presetLegacyCompat({ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个是干嘛的
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hiyuki unocss 目前生成的 rgb(a) 是用的 space style 的语法形式 rgb(255 255 255),在之前的 rn 版本上不支持这个语法格式,仅支持 rgb(a) 是 comma style 的语法 rgb(255, 255, 255),所以用这个 preset 单独将语法统一为 comma style。 (不过目前看 rn 文档是支持了 comma style,后续会再测试确认下,来看下是否还需要这个 preset ) |
||
| commaStyleColorFunction: true | ||
| })) | ||
| options.dark = 'media' | ||
| } | ||
| const uno = presetUno(options) | ||
| const { baseFontSize = 37.5 } = options | ||
|
|
||
| return { | ||
| ...uno, | ||
| name: '@mpxjs/unocss-base', | ||
|
|
@@ -17,11 +30,12 @@ module.exports = function presetMpx (options = {}) { | |
| util.entries.forEach((i) => { | ||
| const value = i[1] | ||
| if (typeof value === 'string' && remRE.test(value)) { | ||
| i[1] = value.replace(remRE, (_, p1) => process.env.MPX_CURRENT_TARGET_MODE === 'web' | ||
| i[1] = value.replace(remRE, (_, p1) => mpxCurrentTargetMode === 'web' | ||
| ? `${p1 * baseFontSize * (100 / 750).toFixed(8)}vw` | ||
| : `${p1 * baseFontSize}rpx`) | ||
| } | ||
| }) | ||
| } | ||
| }, | ||
| presets: extraPresets | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个和presetUno是个什么合并规则?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hiyuki merge 的规则。目前是将 presetUno 作为最基础的规则集合,rn 的规则会覆盖 presetUno 的规则。 |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| "directories": { | ||
| "lib": "lib" | ||
| }, | ||
| "type": "module", | ||
| "scripts": {}, | ||
| "author": "", | ||
| "license": "Apache-2.0", | ||
|
|
@@ -14,6 +15,7 @@ | |
| "access": "public" | ||
| }, | ||
| "dependencies": { | ||
| "@unocss/preset-uno": "^0.52.7" | ||
| "@unocss/preset-uno": "^0.65.1", | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete |
||
| "@unocss/preset-legacy-compat": "^0.65.1" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import rules, { blocklistRules } from './rules/index.js' | ||
| import { normalizeTransfromVar } from './rules/transforms.js' | ||
| import theme from './theme.js' | ||
| import blocklistVariants from './variants/index.js' | ||
| import { transformBase } from '@unocss/preset-mini/rules' | ||
| import { filterBase } from '@unocss/preset-wind/rules' | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @unocss/preset-wind -> @unocss/preset-wind3/4 |
||
|
|
||
| function normalizePreflightBase (preflightBase) { | ||
| normalizeTransfromVar(preflightBase) | ||
| return preflightBase | ||
| } | ||
|
|
||
| function preflights () { | ||
| return [ | ||
| { | ||
| layer: 'preflights', | ||
| getCSS ({ theme, generator }) { | ||
| generator._mpx2rnUnoPreflightBase = { | ||
| ...normalizePreflightBase(transformBase), | ||
| ...filterBase | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| function postprocess (utilsObject) { | ||
| const everyIsVar = utilsObject.entries.every(v => { | ||
| return v[0].startsWith('--un') | ||
| }) | ||
| if (everyIsVar) { | ||
| utilsObject.layer = 'varUtilities' | ||
| } | ||
| return utilsObject | ||
| } | ||
|
|
||
| export default function presetRnMpx () { | ||
| return { | ||
| name: '@mpxjs/unocss-preset-rn', | ||
| rules, | ||
| theme, | ||
| preflights: preflights(), | ||
| postprocess: [postprocess], | ||
| blocklist: [ | ||
| ...blocklistRules, | ||
| ...blocklistVariants | ||
| ] | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { verticalAligns } from '@unocss/preset-mini/rules' | ||
|
|
||
| const support = [ | ||
| 'mid', | ||
| 'start', | ||
| 'btm', | ||
| 'end', | ||
| 'auto', | ||
| 'top', | ||
| 'bottom', | ||
| 'middle' | ||
| ] | ||
|
|
||
| const verticalAlignsRules = verticalAligns.map(([rule]) => (raw) => { | ||
| const result = raw.match(rule) | ||
| if (result && !support.includes(result[1])) { | ||
| return true | ||
| } | ||
| }) | ||
|
|
||
| const textAligns = [ | ||
| 'text-start', | ||
| 'text-end', | ||
| 'text-align-start', | ||
| 'text-align-end' | ||
| ] | ||
|
|
||
| export { | ||
| verticalAlignsRules as verticalAligns, | ||
| textAligns | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { animations } from '@unocss/preset-wind/rules' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| const backgroundStyles = [ | ||
| // attachments | ||
| 'bg-fixed', | ||
| 'bg-locale', | ||
| 'bg-scroll', | ||
| // repeat | ||
| 'bg-repeat', | ||
| 'bg-repeat-x', | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这些应该都不支持
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hiyuki 这些单独罗列出来的规则都是不支持的。目前主要是利用 unocss blocklist 的规则特性,所以目前各个 rules 是将这些不支持的规则单独摘出来了。
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. block这块改清晰一点吧 |
||
| 'bg-repeat-y', | ||
| 'bg-repeat-round', | ||
| 'bg-repeat-space', | ||
| // origins | ||
| 'bg-origin-border', | ||
| 'bg-origin-padding', | ||
| 'bg-origin-content', | ||
| // clips | ||
| 'bg-clip-border', | ||
| 'bg-clip-content', | ||
| 'bg-clip-padding', | ||
| 'bg-clip-text' | ||
| ] | ||
|
|
||
| export { backgroundStyles } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { imageRenderings, overscrolls, listStyle, accents, carets } from '@unocss/preset-wind/rules' | ||
| import { appearance, outline, willChange } from '@unocss/preset-mini/rules' | ||
|
|
||
| export default [ | ||
| ...overscrolls, | ||
| ...imageRenderings, | ||
| ...listStyle, | ||
| ...outline, | ||
| ...willChange, | ||
| ...appearance, | ||
| ...accents, | ||
| ...carets | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { borders } from '@unocss/preset-mini/rules' | ||
|
|
||
| const unSupport = [ | ||
| 's', | ||
| 'e', | ||
| 'bs', | ||
| 'be', | ||
| 'is', | ||
| 'ie', | ||
| 'block', | ||
| 'inline' | ||
| ] | ||
|
|
||
| const bordersRules = borders.map(([rule]) => (raw) => { | ||
| const result = raw.match(rule) | ||
| if (raw === 'border-double') { | ||
| return true | ||
| } else if (result && unSupport.includes(result[1])) { | ||
| return true | ||
| } | ||
| }) | ||
|
|
||
| export default bordersRules |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { colorScheme } from '@unocss/preset-mini/rules' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { columns } from '@unocss/preset-wind/rules' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { containerParent } from '@unocss/preset-mini/rules' | ||
| import { container } from '@unocss/preset-wind/rules' | ||
|
|
||
| export { | ||
| containerParent, | ||
| container | ||
| } |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥改这个?