nitrowind 0.0.0-stage → 0.1.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/CHANGELOG.md +29 -0
- package/LICENSE +21 -0
- package/README.md +51 -2
- package/css.d.ts +6 -0
- package/dist/color.d.ts +24 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +158 -0
- package/dist/color.js.map +1 -0
- package/dist/core.d.ts +121 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +1026 -0
- package/dist/core.js.map +1 -0
- package/dist/css/compile.d.ts +18 -0
- package/dist/css/compile.d.ts.map +1 -0
- package/dist/css/compile.js +1018 -0
- package/dist/css/compile.js.map +1 -0
- package/dist/css/declarations.d.ts +18 -0
- package/dist/css/declarations.d.ts.map +1 -0
- package/dist/css/declarations.js +1173 -0
- package/dist/css/declarations.js.map +1 -0
- package/dist/css/index.d.ts +7 -0
- package/dist/css/index.d.ts.map +1 -0
- package/dist/css/index.js +7 -0
- package/dist/css/index.js.map +1 -0
- package/dist/css/parser.d.ts +9 -0
- package/dist/css/parser.d.ts.map +1 -0
- package/dist/css/parser.js +188 -0
- package/dist/css/parser.js.map +1 -0
- package/dist/css/tokenizer.d.ts +14 -0
- package/dist/css/tokenizer.d.ts.map +1 -0
- package/dist/css/tokenizer.js +200 -0
- package/dist/css/tokenizer.js.map +1 -0
- package/dist/css/types.d.ts +128 -0
- package/dist/css/types.d.ts.map +1 -0
- package/dist/css/types.js +19 -0
- package/dist/css/types.js.map +1 -0
- package/dist/css/values.d.ts +37 -0
- package/dist/css/values.d.ts.map +1 -0
- package/dist/css/values.js +343 -0
- package/dist/css/values.js.map +1 -0
- package/dist/environment.d.ts +5 -0
- package/dist/environment.d.ts.map +1 -0
- package/dist/environment.js +99 -0
- package/dist/environment.js.map +1 -0
- package/dist/hooks.d.ts +22 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +140 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-dev-runtime.d.ts +17 -0
- package/dist/jsx-dev-runtime.d.ts.map +1 -0
- package/dist/jsx-dev-runtime.js +12 -0
- package/dist/jsx-dev-runtime.js.map +1 -0
- package/dist/jsx-namespace.d.ts +29 -0
- package/dist/jsx-namespace.d.ts.map +1 -0
- package/dist/jsx-namespace.js +2 -0
- package/dist/jsx-namespace.js.map +1 -0
- package/dist/jsx-runtime.d.ts +22 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.js +21 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/math.d.ts +50 -0
- package/dist/math.d.ts.map +1 -0
- package/dist/math.js +442 -0
- package/dist/math.js.map +1 -0
- package/dist/plugin.d.ts +45 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +172 -0
- package/dist/plugin.js.map +1 -0
- package/dist/props.d.ts +29 -0
- package/dist/props.d.ts.map +1 -0
- package/dist/props.js +163 -0
- package/dist/props.js.map +1 -0
- package/dist/scope.d.ts +99 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +228 -0
- package/dist/scope.js.map +1 -0
- package/dist/store.d.ts +18 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +244 -0
- package/dist/store.js.map +1 -0
- package/dist/tokens/index.d.ts +12 -0
- package/dist/tokens/index.d.ts.map +1 -0
- package/dist/tokens/index.js +237 -0
- package/dist/tokens/index.js.map +1 -0
- package/dist/tokens/tailwind.generated.d.ts +3 -0
- package/dist/tokens/tailwind.generated.d.ts.map +1 -0
- package/dist/tokens/tailwind.generated.js +610 -0
- package/dist/tokens/tailwind.generated.js.map +1 -0
- package/dist/tokens/types.d.ts +95 -0
- package/dist/tokens/types.d.ts.map +1 -0
- package/dist/tokens/types.js +2 -0
- package/dist/tokens/types.js.map +1 -0
- package/dist/transform.d.ts +22 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/transform.js +79 -0
- package/dist/transform.js.map +1 -0
- package/dist/transition.d.ts +7 -0
- package/dist/transition.d.ts.map +1 -0
- package/dist/transition.js +355 -0
- package/dist/transition.js.map +1 -0
- package/dist/types.d.ts +214 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utilities.d.ts +109 -0
- package/dist/utilities.d.ts.map +1 -0
- package/dist/utilities.js +2000 -0
- package/dist/utilities.js.map +1 -0
- package/dist/variables.d.ts +65 -0
- package/dist/variables.d.ts.map +1 -0
- package/dist/variables.js +408 -0
- package/dist/variables.js.map +1 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/dist/withNitroWind.d.ts +11 -0
- package/dist/withNitroWind.d.ts.map +1 -0
- package/dist/withNitroWind.js +27 -0
- package/dist/withNitroWind.js.map +1 -0
- package/package.json +89 -4
- package/src/color.ts +164 -0
- package/src/core.ts +1248 -0
- package/src/css/compile.ts +1198 -0
- package/src/css/declarations.ts +1141 -0
- package/src/css/index.ts +22 -0
- package/src/css/parser.ts +196 -0
- package/src/css/tokenizer.ts +227 -0
- package/src/css/types.ts +178 -0
- package/src/css/values.ts +330 -0
- package/src/environment.ts +112 -0
- package/src/hooks.ts +190 -0
- package/src/index.ts +61 -0
- package/src/jsx-dev-runtime.ts +43 -0
- package/src/jsx-namespace.ts +23 -0
- package/src/jsx-runtime.ts +39 -0
- package/src/math.ts +488 -0
- package/src/plugin.ts +230 -0
- package/src/props.ts +173 -0
- package/src/scope.ts +331 -0
- package/src/store.ts +288 -0
- package/src/tokens/index.ts +252 -0
- package/src/tokens/tailwind.generated.ts +613 -0
- package/src/tokens/types.ts +99 -0
- package/src/transform.ts +115 -0
- package/src/transition.ts +414 -0
- package/src/types.ts +248 -0
- package/src/utilities.ts +1992 -0
- package/src/variables.ts +509 -0
- package/src/version.ts +2 -0
- package/src/withNitroWind.ts +36 -0
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { formatLocation, type NitroWindCSSConfig, parseCSS } from './css'
|
|
4
|
+
|
|
5
|
+
/** Context passed by NitroUI's Rolldown bundler to plugin factories (`margelo/nitro-ui#927`). */
|
|
6
|
+
export interface BundlerPluginContext {
|
|
7
|
+
root: string
|
|
8
|
+
/** Undefined when the build doesn't target one platform. */
|
|
9
|
+
platform: 'ios' | 'android' | undefined
|
|
10
|
+
mode: 'development' | 'production'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Options accepted by the `nitrowind/plugin` bundler plugin factory. */
|
|
14
|
+
export interface NitroWindPluginOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Output path for generated TypeScript declarations (relative to `context.root` or absolute).
|
|
17
|
+
* Defaults to `'nitrowind.d.ts'`. Pass `false` to disable `.d.ts` generation.
|
|
18
|
+
*/
|
|
19
|
+
dts?: string | false
|
|
20
|
+
/**
|
|
21
|
+
* When `true`, CSS parse warnings are escalated to build errors.
|
|
22
|
+
* Defaults to `false`.
|
|
23
|
+
*/
|
|
24
|
+
strict?: boolean
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Minimal Rollup / Vite / Rolldown-compatible plugin context surface used by `load`. */
|
|
28
|
+
export interface PluginContext {
|
|
29
|
+
addWatchFile(id: string): void
|
|
30
|
+
warn(message: string): void
|
|
31
|
+
error(message: string): never
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Return value of `NitroWindPlugin.load` for handled `.css` modules. */
|
|
35
|
+
export interface LoadResult {
|
|
36
|
+
code: string
|
|
37
|
+
moduleType: 'js'
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Rollup / Vite / Rolldown-compatible plugin object returned by `nitroWindPlugin`. */
|
|
41
|
+
export interface NitroWindPlugin {
|
|
42
|
+
name: 'nitrowind'
|
|
43
|
+
load(this: PluginContext, id: string): LoadResult | null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface FileDeclarations {
|
|
47
|
+
readonly themes: ReadonlySet<string>
|
|
48
|
+
readonly variables: ReadonlySet<string>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const VALID_IDENTIFIER_RE = /^[A-Za-z_$][A-Za-z0-9_$]*$/
|
|
52
|
+
const BUILTIN_THEMES = new Set(['light', 'dark'])
|
|
53
|
+
|
|
54
|
+
function stripQueryAndHash(id: string): string {
|
|
55
|
+
const queryIndex = id.indexOf('?')
|
|
56
|
+
const hashIndex = id.indexOf('#')
|
|
57
|
+
let end = id.length
|
|
58
|
+
if (queryIndex !== -1 && queryIndex < end) end = queryIndex
|
|
59
|
+
if (hashIndex !== -1 && hashIndex < end) end = hashIndex
|
|
60
|
+
return id.slice(0, end)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function isCSSModuleId(id: string): boolean {
|
|
64
|
+
if (id.startsWith('\0')) return false
|
|
65
|
+
const cleanPath = stripQueryAndHash(id)
|
|
66
|
+
return path.extname(cleanPath).toLowerCase() === '.css'
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function formatErrorMessage(err: unknown): string {
|
|
70
|
+
if (err instanceof Error) return err.message
|
|
71
|
+
return String(err)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function formatPropertyKey(key: string): string {
|
|
75
|
+
if (VALID_IDENTIFIER_RE.test(key)) return key
|
|
76
|
+
return `'${key.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function collectDeclarations(config: NitroWindCSSConfig): FileDeclarations {
|
|
80
|
+
const themes = new Set<string>()
|
|
81
|
+
const variables = new Set<string>()
|
|
82
|
+
|
|
83
|
+
for (const varName of Object.keys(config.variables)) {
|
|
84
|
+
if (varName.startsWith('--')) variables.add(varName)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
for (const [themeName, entry] of Object.entries(config.themes)) {
|
|
88
|
+
if (!BUILTIN_THEMES.has(themeName)) {
|
|
89
|
+
themes.add(themeName)
|
|
90
|
+
}
|
|
91
|
+
for (const varName of Object.keys(entry.variables)) {
|
|
92
|
+
if (varName.startsWith('--')) variables.add(varName)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return { themes, variables }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function generateDtsContent(entries: ReadonlyMap<string, FileDeclarations>): string {
|
|
100
|
+
const allThemes = new Set<string>()
|
|
101
|
+
const allVariables = new Set<string>()
|
|
102
|
+
|
|
103
|
+
for (const entry of entries.values()) {
|
|
104
|
+
for (const theme of entry.themes) allThemes.add(theme)
|
|
105
|
+
for (const variable of entry.variables) allVariables.add(variable)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const sortedThemes = [...allThemes].sort()
|
|
109
|
+
const sortedVariables = [...allVariables].sort()
|
|
110
|
+
|
|
111
|
+
const lines: string[] = [
|
|
112
|
+
'/// <reference types="nitrowind/css" />',
|
|
113
|
+
'// Auto-generated by nitrowind/plugin. Do not edit manually.',
|
|
114
|
+
'export {}',
|
|
115
|
+
]
|
|
116
|
+
|
|
117
|
+
if (sortedThemes.length > 0 || sortedVariables.length > 0) {
|
|
118
|
+
lines.push('', "declare module 'nitrowind' {")
|
|
119
|
+
if (sortedThemes.length > 0) {
|
|
120
|
+
lines.push(' interface NitroWindThemes {')
|
|
121
|
+
for (const theme of sortedThemes) {
|
|
122
|
+
lines.push(` ${formatPropertyKey(theme)}: true`)
|
|
123
|
+
}
|
|
124
|
+
lines.push(' }')
|
|
125
|
+
}
|
|
126
|
+
if (sortedVariables.length > 0) {
|
|
127
|
+
lines.push(' interface NitroWindVariables {')
|
|
128
|
+
for (const variable of sortedVariables) {
|
|
129
|
+
lines.push(` ${formatPropertyKey(variable)}: string`)
|
|
130
|
+
}
|
|
131
|
+
lines.push(' }')
|
|
132
|
+
}
|
|
133
|
+
lines.push('}')
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return `${lines.join('\n')}\n`
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function writeDtsIfChanged(dtsPath: string, content: string): void {
|
|
140
|
+
try {
|
|
141
|
+
const existing = readFileSync(dtsPath, 'utf8')
|
|
142
|
+
if (existing === content) return
|
|
143
|
+
} catch {
|
|
144
|
+
// File does not exist yet (or is unreadable); proceed to write it.
|
|
145
|
+
}
|
|
146
|
+
mkdirSync(path.dirname(dtsPath), { recursive: true })
|
|
147
|
+
writeFileSync(dtsPath, content, 'utf8')
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* NitroUI Rolldown bundler plugin factory (`nitrowind/plugin`).
|
|
152
|
+
* Compiles `.css` imports via `parseCSS` at build time, registers watch files for
|
|
153
|
+
* all resolved `@import`s, and generates TypeScript declarations for `.css` imports,
|
|
154
|
+
* custom themes, and CSS variables.
|
|
155
|
+
*/
|
|
156
|
+
export function nitroWindPlugin(context: BundlerPluginContext, options?: NitroWindPluginOptions): NitroWindPlugin {
|
|
157
|
+
const strict = options?.strict ?? false
|
|
158
|
+
const dtsOption = options?.dts ?? 'nitrowind.d.ts'
|
|
159
|
+
const dtsPath = dtsOption === false ? null : path.resolve(context.root, dtsOption)
|
|
160
|
+
const declarationsByFile = new Map<string, FileDeclarations>()
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
name: 'nitrowind',
|
|
164
|
+
load(this: PluginContext, id: string): LoadResult | null {
|
|
165
|
+
if (!isCSSModuleId(id)) return null
|
|
166
|
+
|
|
167
|
+
const cleanId = stripQueryAndHash(id)
|
|
168
|
+
const entryPath = path.resolve(context.root, cleanId)
|
|
169
|
+
const watched = new Set<string>()
|
|
170
|
+
const watch = (filePath: string): void => {
|
|
171
|
+
if (watched.has(filePath)) return
|
|
172
|
+
watched.add(filePath)
|
|
173
|
+
this.addWatchFile(filePath)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
watch(entryPath)
|
|
177
|
+
|
|
178
|
+
let css: string
|
|
179
|
+
try {
|
|
180
|
+
css = readFileSync(entryPath, 'utf8')
|
|
181
|
+
} catch (err) {
|
|
182
|
+
return this.error(`[nitrowind] Failed to read CSS file "${entryPath}": ${formatErrorMessage(err)}`)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
const config = parseCSS(css, {
|
|
187
|
+
filename: entryPath,
|
|
188
|
+
strict,
|
|
189
|
+
onWarning: strict
|
|
190
|
+
? undefined
|
|
191
|
+
: (warning) => {
|
|
192
|
+
this.warn(`[nitrowind] ${warning.message} (${formatLocation(warning)})`)
|
|
193
|
+
},
|
|
194
|
+
resolveImport: (specifier, from) => {
|
|
195
|
+
if (!specifier.startsWith('./') && !specifier.startsWith('../')) {
|
|
196
|
+
throw new Error(
|
|
197
|
+
`[nitrowind] Unsupported @import "${specifier}" in "${from ?? entryPath}": only preset imports ("tailwindcss", "nitrowind", "uniwind") and relative imports ("./...", "../...") are supported`,
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
const baseDir = path.dirname(from ?? entryPath)
|
|
201
|
+
const resolvedPath = path.resolve(baseDir, specifier)
|
|
202
|
+
watch(resolvedPath)
|
|
203
|
+
try {
|
|
204
|
+
const importedCss = readFileSync(resolvedPath, 'utf8')
|
|
205
|
+
return { css: importedCss, filename: resolvedPath }
|
|
206
|
+
} catch (err) {
|
|
207
|
+
throw new Error(
|
|
208
|
+
`[nitrowind] Failed to resolve @import "${specifier}" from "${from ?? entryPath}" (${resolvedPath}): ${formatErrorMessage(err)}`,
|
|
209
|
+
)
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
if (dtsPath !== null) {
|
|
215
|
+
declarationsByFile.set(entryPath, collectDeclarations(config))
|
|
216
|
+
writeDtsIfChanged(dtsPath, generateDtsContent(declarationsByFile))
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
code: `export default ${JSON.stringify(config)}\n`,
|
|
221
|
+
moduleType: 'js',
|
|
222
|
+
}
|
|
223
|
+
} catch (err) {
|
|
224
|
+
return this.error(formatErrorMessage(err))
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export default nitroWindPlugin
|
package/src/props.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import type { ClassNameTargetMap, EdgeInsets, PropMapping, PropMappingTarget, ResolvedClasses } from './types'
|
|
2
|
+
|
|
3
|
+
type Props = Record<string, unknown>
|
|
4
|
+
|
|
5
|
+
/** NitroUI host components whose typography props live on the element itself. */
|
|
6
|
+
const TEXT_HOSTS = new Set(['Text'])
|
|
7
|
+
|
|
8
|
+
/** Default mapping for `className` on NitroUI components. */
|
|
9
|
+
export const DEFAULT_CLASS_NAME_TARGET: ClassNameTargetMap = { layout: 'layout', style: 'style' }
|
|
10
|
+
|
|
11
|
+
export type MappingEntry = readonly [propName: string, target: PropMappingTarget]
|
|
12
|
+
|
|
13
|
+
function mergeGroup(fromClass: object | undefined, explicit: unknown): unknown {
|
|
14
|
+
if (fromClass === undefined) return explicit
|
|
15
|
+
if (explicit == null) return fromClass
|
|
16
|
+
return { ...fromClass, ...(explicit as object) }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
20
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isEdgeInsetValue(value: unknown): value is number {
|
|
24
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Extracts numeric padding utilities (`p-*`, `px-*`, `py-*`, `pt-*`, `pr-*`, `pb-*`, `pl-*`)
|
|
29
|
+
* from compiled classes into a `Partial<EdgeInsets>` (`{ top, right, bottom, left }` in points).
|
|
30
|
+
* Precedence follows CSS/Yoga: individual edge > axis (`px`/`py`) > shorthand (`p`).
|
|
31
|
+
*/
|
|
32
|
+
export function extractEdgeInsets(resolved: ResolvedClasses): Partial<EdgeInsets> | undefined {
|
|
33
|
+
const layout = resolved.layout
|
|
34
|
+
if (layout === undefined) return undefined
|
|
35
|
+
|
|
36
|
+
const top = layout.paddingTop ?? layout.paddingVertical ?? layout.padding
|
|
37
|
+
const right = layout.paddingRight ?? layout.paddingHorizontal ?? layout.padding
|
|
38
|
+
const bottom = layout.paddingBottom ?? layout.paddingVertical ?? layout.padding
|
|
39
|
+
const left = layout.paddingLeft ?? layout.paddingHorizontal ?? layout.padding
|
|
40
|
+
|
|
41
|
+
const insets: { top?: number; right?: number; bottom?: number; left?: number } = {}
|
|
42
|
+
let hasAny = false
|
|
43
|
+
|
|
44
|
+
if (isEdgeInsetValue(top)) {
|
|
45
|
+
insets.top = top
|
|
46
|
+
hasAny = true
|
|
47
|
+
}
|
|
48
|
+
if (isEdgeInsetValue(right)) {
|
|
49
|
+
insets.right = right
|
|
50
|
+
hasAny = true
|
|
51
|
+
}
|
|
52
|
+
if (isEdgeInsetValue(bottom)) {
|
|
53
|
+
insets.bottom = bottom
|
|
54
|
+
hasAny = true
|
|
55
|
+
}
|
|
56
|
+
if (isEdgeInsetValue(left)) {
|
|
57
|
+
insets.left = left
|
|
58
|
+
hasAny = true
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return hasAny ? insets : undefined
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Normalizes a `PropMapping` into a fixed-order list of `[propName, target]` entries.
|
|
66
|
+
* Always includes `className` with the default `{ layout: 'layout', style: 'style' }` mapping
|
|
67
|
+
* unless the caller explicitly overrides `className` in `propMapping`.
|
|
68
|
+
*/
|
|
69
|
+
export function normalizePropMapping(propMapping?: PropMapping): readonly MappingEntry[] {
|
|
70
|
+
if (propMapping === undefined) {
|
|
71
|
+
return [['className', DEFAULT_CLASS_NAME_TARGET]]
|
|
72
|
+
}
|
|
73
|
+
const entries: MappingEntry[] = []
|
|
74
|
+
if (!Object.prototype.hasOwnProperty.call(propMapping, 'className')) {
|
|
75
|
+
entries.push(['className', DEFAULT_CLASS_NAME_TARGET])
|
|
76
|
+
}
|
|
77
|
+
for (const [propName, target] of Object.entries(propMapping)) {
|
|
78
|
+
entries.push([propName, target])
|
|
79
|
+
}
|
|
80
|
+
return entries
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function applyPropMapping(next: Props, target: PropMappingTarget, resolved: ResolvedClasses): void {
|
|
84
|
+
if (typeof target === 'function') {
|
|
85
|
+
const mapped = target(resolved)
|
|
86
|
+
for (const [key, value] of Object.entries(mapped)) {
|
|
87
|
+
if (value === undefined) continue
|
|
88
|
+
const explicit = next[key]
|
|
89
|
+
if (isPlainObject(value) && isPlainObject(explicit)) {
|
|
90
|
+
next[key] = { ...value, ...explicit }
|
|
91
|
+
} else if (explicit === undefined) {
|
|
92
|
+
next[key] = value
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (typeof target === 'string') {
|
|
99
|
+
if (target === 'layout') {
|
|
100
|
+
const layout = mergeGroup(resolved.layout, next.layout)
|
|
101
|
+
if (layout !== undefined) next.layout = layout
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
if (target === 'style') {
|
|
105
|
+
const style = mergeGroup(resolved.style, next.style)
|
|
106
|
+
if (style !== undefined) next.style = style
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
const combined =
|
|
110
|
+
resolved.layout !== undefined && resolved.style !== undefined
|
|
111
|
+
? { ...resolved.layout, ...resolved.style }
|
|
112
|
+
: (resolved.layout ?? resolved.style)
|
|
113
|
+
const merged = mergeGroup(combined, next[target])
|
|
114
|
+
if (merged !== undefined) next[target] = merged
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (target.layout !== undefined) {
|
|
119
|
+
const layout = mergeGroup(resolved.layout, next[target.layout])
|
|
120
|
+
if (layout !== undefined) next[target.layout] = layout
|
|
121
|
+
}
|
|
122
|
+
if (target.style !== undefined) {
|
|
123
|
+
const style = mergeGroup(resolved.style, next[target.style])
|
|
124
|
+
if (style !== undefined) next[target.style] = style
|
|
125
|
+
}
|
|
126
|
+
if (target.text === true && resolved.text !== undefined) {
|
|
127
|
+
for (const [key, value] of Object.entries(resolved.text)) {
|
|
128
|
+
if (next[key] === undefined) next[key] = value
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (target.edgeInsets !== undefined) {
|
|
132
|
+
const insets = extractEdgeInsets(resolved)
|
|
133
|
+
const merged = mergeGroup(insets, next[target.edgeInsets])
|
|
134
|
+
if (merged !== undefined) next[target.edgeInsets] = merged
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Merges a list of mapped `*ClassName` resolutions into component props and strips
|
|
140
|
+
* every mapped `*ClassName` prop. Explicit props always win over values from classes.
|
|
141
|
+
*/
|
|
142
|
+
export function mergeMappedProps(
|
|
143
|
+
props: Props,
|
|
144
|
+
entries: readonly MappingEntry[],
|
|
145
|
+
resolvedList: readonly ResolvedClasses[],
|
|
146
|
+
): Props {
|
|
147
|
+
const next: Props = { ...props }
|
|
148
|
+
for (let i = 0; i < entries.length; i++) {
|
|
149
|
+
delete next[entries[i]![0]]
|
|
150
|
+
}
|
|
151
|
+
for (let i = 0; i < entries.length; i++) {
|
|
152
|
+
applyPropMapping(next, entries[i]![1], resolvedList[i]!)
|
|
153
|
+
}
|
|
154
|
+
return next
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Merges compiled classes into a NitroUI host element's props and drops `className`.
|
|
159
|
+
* Explicit `layout`, `style` and text props win over values from classes.
|
|
160
|
+
*/
|
|
161
|
+
export function mergeClassProps(type: string, props: Props, resolved: ResolvedClasses): Props {
|
|
162
|
+
const { className: _className, ...next } = props
|
|
163
|
+
const layout = mergeGroup(resolved.layout, props.layout)
|
|
164
|
+
if (layout !== undefined) next.layout = layout
|
|
165
|
+
const style = mergeGroup(resolved.style, props.style)
|
|
166
|
+
if (style !== undefined) next.style = style
|
|
167
|
+
if (resolved.text !== undefined && TEXT_HOSTS.has(type)) {
|
|
168
|
+
for (const [key, value] of Object.entries(resolved.text)) {
|
|
169
|
+
if (next[key] === undefined) next[key] = value
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return next
|
|
173
|
+
}
|