Skip to content

Commit

Permalink
feat: add line-height unit block
Browse files Browse the repository at this point in the history
  • Loading branch information
chizukicn committed Nov 24, 2023
1 parent b516b54 commit 6ba6c80
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
52 changes: 13 additions & 39 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const presetBlock = definePreset((_options: PresetBlockOptions = {}) => {
}

const regexes = [
/^((p|m|rounded|rd|space|inset)(-?[xyrltb])?|pa|ma|top|bottom|left|right|w|h|min-w|min-h)-?((\.\d+)|(\d+)(\.\d+)?)(rem|px)$/,
/^((p|m|rounded|rd|space|inset)(-?[xyrltb])?|pa|ma|top|bottom|left|right|w|h|min-w|min-h|lh|leading)-?((\.\d+)|(\d+)(\.\d+)?)(rem|px)$/,
/^text-(size-)?[0-9]$/,
/\w+--((\.\d+)|(\d+)(\.\d+)?)/,
]
Expand Down
45 changes: 39 additions & 6 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createGenerator } from 'unocss'
import { createGenerator } from '@unocss/core'
import { describe, expect, it } from 'vitest'
import { presetUno } from 'unocss'
import { presetBlock } from '../src'

function cartesian<T>(arr: T[][]): T[][] {
Expand All @@ -17,13 +18,39 @@ function cartesian<T>(arr: T[][]): T[][] {
)
}

function createBlockMatcher(uno: ReturnType<typeof createGenerator>) {
const blocked = new Set<string>()
const matchBlocked = async (raw: string) => {
if (blocked.has(raw))
return
if (uno.isBlocked(raw)) {
blocked.add(raw)
return
}
let current = raw
for (const p of uno.config.preprocess)
current = p(raw)!
const applied = await uno.matchVariants(raw, current)
if (applied && uno.isBlocked(applied[1]))
blocked.add(raw)
}
return async (raw: string, matches: string[]) => {
const extracts = Array.from(await uno.applyExtractors(raw))
await Promise.all(extracts.map(matchBlocked))
return matches.every(m => blocked.has(m))
}
}

describe('presetBlock', () => {
const uno = createGenerator({
presets: [presetBlock({
ignores: [
'w-4px',
],
})],
presets: [
presetUno(),
presetBlock({
ignores: [
'w-4px',
],
}),
],
blocklist: [
/block-\d+/,
],
Expand Down Expand Up @@ -60,4 +87,10 @@ describe('presetBlock', () => {
it('base blocklist', () => {
expect(uno.isBlocked('block-1')).toBe(true)
})

it('match variants', async () => {
const matchBlocked = createBlockMatcher(uno)
const rs = await matchBlocked('!w-100px w-30 p-10 lh-40px', ['!w-100px', 'lh-40px'])
expect(rs).toBe(true)
})
})

0 comments on commit 6ba6c80

Please sign in to comment.