8000 feat(preset-typography): compatible with theme colors with alpha placeholder by zyyv · Pull Request #4420 · unocss/unocss · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(preset-typography): compatible with theme colors with alpha placeholder #4420

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

Merged
merged 2 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages-presets/preset-typography/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"dependencies": {
"@unocss/core": "workspace:*",
"@unocss/preset-mini": "workspace:*"
"@unocss/preset-mini": "workspace:*",
"@unocss/rule-utils": "workspace:*"
}
}
62 changes: 42 additions & 20 deletions packages-presets/preset-typography/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { CSSObject, Preset } from '@unocss/core'
import type { Theme } from '@unocss/preset-mini'
import type { TypographyCompatibilityOptions } from './types/compatibilityOptions'
import { definePreset, toEscapedSelector } from '@unocss/core'
import { alphaPlaceholders } from '@unocss/rule-utils'
import { getElements, getPreflights } from './preflights'

/**
Expand Down Expand Up @@ -100,28 +101,49 @@ export const presetTypography = definePreset((options?: TypographyOptions): Pres
return

const colorObject = typeof baseColor === 'object' ? baseColor : {}
return {
'--un-prose-body': colorObject[700] ?? baseColor,
'--un-prose-headings': colorObject[900] ?? baseColor,
'--un-prose-links': colorObject[900] ?? baseColor,
'--un-prose-lists': colorObject[400] ?? baseColor,
'--un-prose-hr': colorObject[200] ?? baseColor,
'--un-prose-captions': colorObject[500] ?? baseColor,
'--un-prose-code': colorObject[900] ?? baseColor,
'--un-prose-borders': colorObject[200] ?? baseColor,
'--un-prose-bg-soft': colorObject[100] ?? baseColor,

const TagColorMap = {
'body': 700,
'headings': 900,
'links': 900,
'lists': 400,
'hr': 200,
'captions': 500,
'code': 900,
'borders': 200,
'bg-soft': 100,
// invert colors (dark mode)
'--un-prose-invert-body': colorObject[200] ?? baseColor,
'--un-prose-invert-headings': colorObject[100] ?? baseColor,
'--un-prose-invert-links': colorObject[100] ?? baseColor,
'--un-prose-invert-lists': colorObject[500] ?? baseColor,
'--un-prose-invert-hr': colorObject[700] ?? baseColor,
'--un-prose-invert-captions': colorObject[400] ?? baseColor,
'--un-prose-invert-code': colorObject[100] ?? baseColor,
'--un-prose-invert-borders': colorObject[700] ?? baseColor,
'--un-prose-invert-bg-soft': colorObject[800] ?? baseColor,
'invert-body': 200,
'invert-headings': 100,
'invert-links': 100,
'invert-lists': 500,
'invert-hr': 700,
'invert-captions': 400,
'invert-code': 100,
'invert-borders': 700,
'invert-bg-soft': 800,
}

const result: any = {}

for (const key in TagColorMap) {
const value = TagColorMap[key as keyof typeof TagColorMap]
const color = colorObject[value] ?? baseColor
let hasAlpha = false

for (const placeholder of alphaPlaceholders) {
if (color.includes(placeholder)) {
hasAlpha = true
result[`--un-prose-${key}-opacity`] = 1
result[`--un-prose-${key}`] = color.replace(placeholder, `var(--un-prose-${key}-opacity)`)
break
}
}

if (!hasAlpha)
result[`--un-prose-${key}`] = color
}

return result
},
{ layer: 'typography' },
],
Expand Down
2 changes: 1 addition & 1 deletion packages-presets/rule-utils/src/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface ParsedColorValue {

/* eslint-disable no-case-declarations */
const cssColorFunctions = ['hsl', 'hsla', 'hwb', 'lab', 'lch', 'oklab', 'oklch', 'rgb', 'rgba']
const alphaPlaceholders = ['%alpha', '<alpha-value>']
export const alphaPlaceholders = ['%alpha', '<alpha-value>']
const alphaPlaceholdersRE = new RegExp(alphaPlaceholders.map(v => escapeRegExp(v)).join('|'))

export function hex2rgba(hex = ''): RGBAColorValue | undefined {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

34 changes: 34 additions & 0 deletions test/preset-typography.test.ts
7204
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,37 @@ describe('typography elements modify', () => {
`)
})
})

describe('typography with custom theme colors', () => {
it('with alpha', async () => {
const uno = await createGenerator({
presets: [
presetTypography(),
],
theme: {
colors: {
myColor: {
50: 'oklch(0.95 0.018 0 / <alpha-value>)',
100: 'oklch(0.93 0.05 0 / <alpha-value>)',
200: 'oklch(0.88 0.09 0 / <alpha-value>)',
300: 'oklch(0.82 0.14 0 / <alpha-value>)',
400: 'oklch(0.73 0.195 0 / <alpha-value>)',
500: 'oklch(0.64 0.24 0 / <alpha-value>)',
600: 'oklch(0.56 0.235 0 / <alpha-value>)',
700: 'oklch(0.49 0.22 0)', // prose-body will be no alpha.
800: 'oklch(0.41 0.185 0 / <alpha-value>)',
900: 'oklch(0.36 0.15 0 / <alpha-value>)',
950: 'oklch(0.26 0.11 0 / <alpha-value>)',
},
},
},
})

const { css } = await uno.generate('prose-myColor', { preflights: false })

expect(css).toMatchInlineSnapshot(`
"/* layer: typography */
.prose-myColor{--un-prose-body:oklch(0.49 0.22 0);--un-prose-headings-opacity:1;--un-prose-headings:oklch(0.36 0.15 0 / var(--un-prose-headings-opacity));--un-prose-links-opacity:1;--un-prose-links:oklch(0.36 0.15 0 / var(--un-prose-links-opacity));--un-prose-lists-opacity:1;--un-prose-lists:oklch(0.73 0.195 0 / var(--un-prose-lists-opacity));--un-prose-hr-opacity:1;--un-prose-hr:oklch(0.88 0.09 0 / var(--un-prose-hr-opacity));--un-prose-captions-opacity:1;--un-prose-captions:oklch(0.64 0.24 0 / var(--un-prose-captions-opacity));--un-prose-code-opacity:1;--un-prose-code:oklch(0.36 0.15 0 / var(--un-prose-code-opacity));--un-prose-borders-opacity:1;--un-prose-borders:oklch(0.88 0.09 0 / var(--un-prose-borders-opacity));--un-prose-bg-soft-opacity:1;--un-prose-bg-soft:oklch(0.93 0.05 0 / var(--un-prose-bg-soft-opacity));--un-prose-invert-body-opacity:1;--un-prose-invert-body:oklch(0.88 0.09 0 / var(--un-prose-invert-body-opacity));--un-prose-invert-headings-opacity:1;--un-prose-invert-headings:oklch(0.93 0.05 0 / var(--un-prose-invert-headings-opacity));--un-prose-invert-links-opacity:1;--un-prose-invert-links:oklch(0.93 0.05 0 / var(--un-prose-invert-links-opacity));--un-prose-invert-lists-opacity:1;--un-prose-invert-lists:oklch(0.64 0.24 0 / var(--un-prose-invert-lists-opacity));--un-prose-invert-hr:oklch(0.49 0.22 0);--un-prose-invert-captions-opacity:1;--un-prose-invert-captions:oklch(0.73 0.195 0 / var(--un-prose-invert-captions-opacity));--un-prose-invert-code-opacity:1;--un-prose-invert-code:oklch(0.93 0.05 0 / var(--un-prose-invert-code-opacity));--un-prose-invert-borders:oklch(0.49 0.22 0);--un-prose-invert-bg-soft-opacity:1;--un-prose-invert-bg-soft:oklch(0.41 0.185 0 / var(--un-prose-invert-bg-soft-opacity));}"
`)
})
})
Loading
0