-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
213 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ | |
"unocss", | ||
"Weapp", | ||
"whitespaces" | ||
] | ||
], | ||
"testing.automaticallyOpenPeekView": "never" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,79 @@ | ||
import type { Variant } from '@unocss/core' | ||
import type { VariantObject } from '@unocss/core' | ||
import { resolveBreakpoints } from '../utils' | ||
import type { Theme } from '../theme' | ||
|
||
const regexCache: Record<string, RegExp> = {} | ||
|
||
function calcMaxWidthBySize(size: string) { | ||
export function calcMaxWidthBySize(size: string) { | ||
const value = size.match(/^-?[0-9]+\.?[0-9]*/)?.[0] || '' | ||
const unit = size.slice(value.length) | ||
const maxWidth = (Number.parseFloat(value) - 0.1) | ||
return Number.isNaN(maxWidth) ? size : `${maxWidth}${unit}` | ||
} | ||
|
||
export const variantBreakpoints: Variant<Theme> = { | ||
name: 'breakpoints', | ||
match(matcher, context) { | ||
const variantEntries: Array<[string, string, number]> | ||
= Object.entries(resolveBreakpoints(context) ?? {}).map(([point, size], idx) => [point, size, idx]) | ||
for (const [point, size, idx] of variantEntries) { | ||
if (!regexCache[point]) | ||
regexCache[point] = new RegExp(`^((?:([al]t-|[<~]))?${point}[:-])`) | ||
export function variantBreakpoints(): VariantObject { | ||
const regexCache: Record<string, RegExp> = {} | ||
return { | ||
name: 'breakpoints', | ||
match(matcher, context) { | ||
const variantEntries: Array<[string, string, number]> | ||
= Object.entries(resolveBreakpoints(context) ?? {}).map(([point, size], idx) => [point, size, idx]) | ||
for (const [point, size, idx] of variantEntries) { | ||
if (!regexCache[point]) | ||
regexCache[point] = new RegExp(`^((?:([al]t-|[<~]))?${point}(?:${context.generator.config.separators.join('|')}))`) | ||
|
||
const match = matcher.match(regexCache[point]) | ||
if (!match) | ||
continue | ||
const match = matcher.match(regexCache[point]) | ||
if (!match) | ||
continue | ||
|
||
const [, pre] = match | ||
const [, pre] = match | ||
|
||
const m = matcher.slice(pre.length) | ||
// container rule is responsive, but also is breakpoint aware | ||
// it is handled on its own module (container.ts) and so we | ||
// exclude it from here | ||
if (m === 'container') | ||
continue | ||
const m = matcher.slice(pre.length) | ||
// container rule is responsive, but also is breakpoint aware | ||
// it is handled on its own module (container.ts) and so we | ||
// exclude it from here | ||
if (m === 'container') | ||
continue | ||
|
||
const isLtPrefix = pre.startsWith('lt-') || pre.startsWith('<') | ||
const isAtPrefix = pre.startsWith('at-') || pre.startsWith('~') | ||
const isLtPrefix = pre.startsWith('lt-') || pre.startsWith('<') | ||
const isAtPrefix = pre.startsWith('at-') || pre.startsWith('~') | ||
|
||
let order = 1000 // parseInt(size) | ||
let order = 1000 // parseInt(size) | ||
|
||
if (isLtPrefix) { | ||
order -= (idx + 1) | ||
return { | ||
matcher: m, | ||
handle: (input, next) => next({ | ||
...input, | ||
parent: `${input.parent ? `${input.parent} $$ ` : ''}@media (max-width: ${calcMaxWidthBySize(size)})`, | ||
parentOrder: order, | ||
}), | ||
if (isLtPrefix) { | ||
order -= (idx + 1) | ||
return { | ||
matcher: m, | ||
handle: (input, next) => next({ | ||
...input, | ||
parent: `${input.parent ? `${input.parent} $$ ` : ''}@media (max-width: ${calcMaxWidthBySize(size)})`, | ||
parentOrder: order, | ||
}), | ||
} | ||
} | ||
} | ||
|
||
order += (idx + 1) | ||
order += (idx + 1) | ||
|
||
// support for windicss @<breakpoint> => last breakpoint will not have the upper bound | ||
if (isAtPrefix && idx < variantEntries.length - 1) { | ||
return { | ||
matcher: m, | ||
handle: (input, next) => next({ | ||
...input, | ||
parent: `${input.parent ? `${input.parent} $$ ` : ''}@media (min-width: ${size}) and (max-width: ${calcMaxWidthBySize(variantEntries[idx + 1][1])})`, | ||
parentOrder: order, | ||
}), | ||
} | ||
} | ||
|
||
// support for windicss @<breakpoint> => last breakpoint will not have the upper bound | ||
if (isAtPrefix && idx < variantEntries.length - 1) { | ||
return { | ||
matcher: m, | ||
handle: (input, next) => next({ | ||
...input, | ||
parent: `${input.parent ? `${input.parent} $$ ` : ''}@media (min-width: ${size}) and (max-width: ${calcMaxWidthBySize(variantEntries[idx + 1][1])})`, | ||
parent: `${input.parent ? `${input.parent} $$ ` : ''}@media (min-width: ${size})`, | ||
parentOrder: order, | ||
}), | ||
} | ||
} | ||
|
||
return { | ||
matcher: m, | ||
handle: (input, next) => next({ | ||
...input, | ||
parent: `${input.parent ? `${input.parent} $$ ` : ''}@media (min-width: ${size})`, | ||
parentOrder: order, | ||
}), | ||
} | ||
} | ||
}, | ||
multiPass: true, | ||
autocomplete: '(at-|lt-|)$breakpoints:', | ||
}, | ||
multiPass: true, | ||
autocomplete: '(at-|lt-|)$breakpoints:', | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
import type { Variant, VariantContext } from '@unocss/core' | ||
import type { VariantObject } from '@unocss/core' | ||
import { restoreSelector } from 'unplugin-transform-class/utils' | ||
import type { Theme } from '../theme' | ||
|
||
export const variantImportant: Variant = { | ||
name: 'important', | ||
match(matcher, { theme }: VariantContext<Theme>) { | ||
let base: string | undefined | ||
export function variantImportant(): VariantObject<Theme> { | ||
let re: RegExp | ||
|
||
matcher = restoreSelector(matcher, theme?.transformRules) | ||
const match = matcher.match(/^(important[:-]|!)/) | ||
return { | ||
name: 'important', | ||
match(matcher, ctx) { | ||
if (!re) | ||
re = new RegExp(`^(important(?:${ctx.generator.config.separators.join('|')})|!)`) | ||
|
||
if (match) | ||
base = matcher.slice(match[0].length) | ||
let base: string | undefined | ||
matcher = restoreSelector(matcher, ctx.theme?.transformRules) | ||
const match = matcher.match(re) | ||
if (match) | ||
base = matcher.slice(match[0].length) | ||
else if (matcher.endsWith('!')) | ||
base = matcher.slice(0, -1) | ||
|
||
else if (matcher.endsWith('!')) | ||
base = matcher.slice(0, -1) | ||
|
||
if (base) { | ||
return { | ||
matcher: base, | ||
body: (body) => { | ||
body.forEach((v) => { | ||
if (v[1]) | ||
v[1] += ' !important' | ||
}) | ||
return body | ||
}, | ||
if (base) { | ||
return { | ||
matcher: base, | ||
body: (body) => { | ||
body.forEach((v) => { | ||
if (v[1]) | ||
v[1] += ' !important' | ||
}) | ||
return body | ||
}, | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.